Shop OBEX P1 Docs P2 Docs Learn Events
Servo troubles — Parallax Forums

Servo troubles

ElieElie Posts: 6
edited 2004-11-12 13:40 in BASIC Stamp
Hi
I wrote this program to turn a servo in one direction if a relay switch is turned on, and in the other direction if the relay switch is off.· Everything in the code seems to work the way I want it to, but the servo will not turn.· It just jerks left and right every once in a while and ends up staying in the same position.· Does anybody see anything wrong with the servo code?
Thanks for the help,
-Elie


Here is the program, the servo code is in the RotateNeedle subroutine:


' {$STAMP BS2sx}
' {$PBASIC 2.5}
'variable and constant declarations
DEBUG "initializing", CR
INPUT 0
relaySwitch VAR IN0
SwitchOn CON 0
SwitchOff CON 1
ConsumerLED CON 2
HippieLED CON 3
ServoPin CON 15
LOW ServoPin
MinRot CON 475
MaxRot CON 2750
MaxIncr CON 5
MaxDecr CON 2
increment VAR Word
MaxIndex CON 50
index VAR Word
currentRot VAR Word
currentRot = MinRot

'reset the gauge to its·start position
DEBUG "resetting", CR
FOR index = 0 TO MaxIndex
· 'DEBUG ? index
· PULSOUT· ServoPin, currentRot
· PAUSE 20
NEXT

'Main function
' checks if user is consuming and calls the appropriate subroutines
DEBUG "ready", CR
Main:
· DEBUG ? relaySwitch
· IF relaySwitch = SwitchOn THEN
··· LOW consumerLED
··· HIGH hippieLED
··· GOSUB Consume
· ELSE
··· HIGH consumerLED
··· LOW hippieLED
··· GOSUB Relax
· ENDIF
· PAUSE 50
· GOTO Main

'Consume subroutine
' gradually moves the needle towards the·maximum position until the user stops consuming
Consume:
· DEBUG "consuming", CR
· increment = 0
· DO
··· 'DEBUG ? "going up", CR
··· GOSUB IncrementRotation
··· GOSUB RotateNeedle
· LOOP WHILE (relaySwitch = SwitchOn)
· RETURN

'Relax subroutine
' gradually moves the needle towards the·minimum position until the user starts consuming
Relax:
· DEBUG "relaxing", CR
· increment = 0
· DO
··· 'DEBUG ? "going down", CR
··· GOSUB DecrementRotation
··· GOSUB RotateNeedle
· LOOP WHILE (relaySwitch = SwitchOff)
· RETURN

'IncrementRotation subroutine
'· increments the current rotation value of the servo taking into account another variable to slow down the movement
IncrementRotation:
· increment = increment + 1
· IF (increment >= MaxIncr) THEN
··· IF (currentRot <= MaxRot) THEN
····· DEBUG "incrementing rotation angle", CR
····· currentRot = currentRot + 10
··· ENDIF
··· increment = 0
··· DEBUG ? currentRot
· ENDIF
· DEBUG ? increment
· RETURN

'DecrementRotation subroutine
'· decrements the current rotation value of the servo taking into account another variable to slow down movement
DecrementRotation:
· increment = increment + 1
· IF (increment >= MaxDecr) THEN
··· IF (currentRot >= MinRot) THEN
····· DEBUG "decrementing rotation angle", CR
····· currentRot = currentRot - 10
··· ENDIF
··· increment = 0
··· DEBUG ? currentRot
· ENDIF
· DEBUG ? increment
· RETURN

'RotateNeedle subroutine
' rotates the servo to its current rotation angle
RotateNeedle:
· FOR index = 0 TO MaxIndex
··· 'DEBUG ? index
··· PULSOUT ServoPin, currentRot
··· PAUSE 20
· NEXT
· RETURN

Comments

  • NewzedNewzed Posts: 2,503
    edited 2004-11-11 19:37
    Is your servo a standard servo or has it been modified for continuous rotation?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    New Combo LCD Backpack

    http://hometown.aol.com/newzed/index.html
    ·
  • ElieElie Posts: 6
    edited 2004-11-11 22:06
    it's not modified, and it's·a futaba s3003 (similar to an s148)
  • NewzedNewzed Posts: 2,503
    edited 2004-11-11 22:21
    If your servo is not modified for continuous rotation, when you pulse it, it will go to a certain position and stay there.· If you want your servo to rotate 360 degrees, go to the Parallax site and search for "Servo Modification".· That will tell you how to modify your servo.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    New Combo LCD Backpack

    http://hometown.aol.com/newzed/index.html
    ·
  • ElieElie Posts: 6
    edited 2004-11-12 02:53
    I only need it to rotate from 0 to 180 degrees.
    Are you saying that I don't need that loop, I just send a PULSOUT command once?

    Every other servo example I saw used a FOR loop and sent the PULSOUT at least 20 times...
  • NewzedNewzed Posts: 2,503
    edited 2004-11-12 13:40
    If you write

    pulsout servo, 850

    The servo will go to 850 but it will have no holding power.· If you want it to maintin its position under load then you must write

    for x = 1 to whatever
    pulsout servo, 850
    pause 20
    next

    If you want the servo to move slowly from 750 to 850 then you write:

    pout = 750
    pstop = 850

    runit:
    pulsout servo, pout
    pause 25
    pout = pout + 1
    if pout = pstop then do something else
    goto runit











    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    New Combo LCD Backpack

    http://hometown.aol.com/newzed/index.html
    ·
Sign In or Register to comment.