Servo question
Interactives
Posts: 83
·Ok, so right now I'm working on making a switch of sorts, that is actually an arrow. The shaft of the arrow has a magnet on it, and positioned around the shaft are ten hall effect sensors. I've written some code to move a servo into position, based on what direction the arrow is pointing. But the code I've written is based on If.. then directives soo... it works the way I have it now, but I thought that servos had to have constant pulses sent to them in order to hold position. If my code is saying "the arrow is pointing left", then is the signal only being sent to the servo for the duration of the parameters of the "IF- THEN" condition? I'll attach my code, I'm just worried that I'm gonna kill another servo. Thankx!!!··
'{$STAMP BS2}
'{$PBASIC 2.5}
' servo control
DEBUG "ready",CR
one VAR Bit············ 'state of the hall effect sensor no. 1
two VAR Bit············ 'state of the hall effect sensor no. 1
three VAR Bit·········· 'state of the hall effect sensor no. 1
'=============================
counter VAR Word········ ' counts the passes the servo has taken
time CON 30············· ' how long the servo holds position
Main:
one=IN0
two=IN1
three=IN2
IF one=0 THEN first
IF two=0 THEN second
IF three=0 THEN third
GOTO Main
'==========================================================================================================================
first:
DEBUG "0 to 100 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 350················ 'outputs from pin 14
PAUSE 20······················· '20ms pause
NEXT
GOTO main
second:
DEBUG "101 to 200 lbs", CR
FOR counter = 1 TO time
PULSOUT 14, 400
PAUSE 20
NEXT
GOTO main
third:
DEBUG "201 to 300 lbs", CR
FOR counter = 1 TO time········ ' for counter = how long to hold
PULSOUT 14, 450················ 'outputs from pin 14
PAUSE 20
NEXT························ '20ms pause
GOTO main
·· DEBUG "301 to 400 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 500·············· 'outputs from pin 14
PAUSE 20
NEXT······················· '20ms pause
·DEBUG "401 to 500 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 550················ 'outputs from pin 14
PAUSE 20
NEXT····················· '20ms pause
DEBUG "501 to 600 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 600·············· 'outputs from pin 14
PAUSE 20
NEXT··················· '20ms pause
DEBUG "601 to 700 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 650·············· 'outputs from pin 14
PAUSE 20
NEXT················· '20ms pause
DEBUG "701 to 800 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14,700··············· 'outputs from pin 14
PAUSE 20······················· '20ms pause
NEXT
DEBUG "801 to 900 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 750··············· 'outputs from pin 14
PAUSE 20······················· '20ms pause
NEXT
DEBUG "901 to 1000 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 800·············· 'outputs from pin 14
PAUSE 20······················· '20ms pause
NEXT
GOTO main
'{$STAMP BS2}
'{$PBASIC 2.5}
' servo control
DEBUG "ready",CR
one VAR Bit············ 'state of the hall effect sensor no. 1
two VAR Bit············ 'state of the hall effect sensor no. 1
three VAR Bit·········· 'state of the hall effect sensor no. 1
'=============================
counter VAR Word········ ' counts the passes the servo has taken
time CON 30············· ' how long the servo holds position
Main:
one=IN0
two=IN1
three=IN2
IF one=0 THEN first
IF two=0 THEN second
IF three=0 THEN third
GOTO Main
'==========================================================================================================================
first:
DEBUG "0 to 100 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 350················ 'outputs from pin 14
PAUSE 20······················· '20ms pause
NEXT
GOTO main
second:
DEBUG "101 to 200 lbs", CR
FOR counter = 1 TO time
PULSOUT 14, 400
PAUSE 20
NEXT
GOTO main
third:
DEBUG "201 to 300 lbs", CR
FOR counter = 1 TO time········ ' for counter = how long to hold
PULSOUT 14, 450················ 'outputs from pin 14
PAUSE 20
NEXT························ '20ms pause
GOTO main
·· DEBUG "301 to 400 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 500·············· 'outputs from pin 14
PAUSE 20
NEXT······················· '20ms pause
·DEBUG "401 to 500 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 550················ 'outputs from pin 14
PAUSE 20
NEXT····················· '20ms pause
DEBUG "501 to 600 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 600·············· 'outputs from pin 14
PAUSE 20
NEXT··················· '20ms pause
DEBUG "601 to 700 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 650·············· 'outputs from pin 14
PAUSE 20
NEXT················· '20ms pause
DEBUG "701 to 800 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14,700··············· 'outputs from pin 14
PAUSE 20······················· '20ms pause
NEXT
DEBUG "801 to 900 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 750··············· 'outputs from pin 14
PAUSE 20······················· '20ms pause
NEXT
DEBUG "901 to 1000 lbs", CR
FOR counter = 1 TO time·········· ' for counter = how long to hold
PULSOUT 14, 800·············· 'outputs from pin 14
PAUSE 20······················· '20ms pause
NEXT
GOTO main
Comments
ServoVar VAR WORD
MAIN:
Check_Sensor
Update_Servo_Var
GOSUB Refresh_Servo
PAUSE 20
GOTO MAIN
Refresh_Servo:
PULSOUT ServoPin, ServoVar
RETURN
Then, all your 'if-then' statements should merely update the value
of 'ServoVar'. This value then gets sent to the servo every 20 mSecs or so.
You should not decide on ServoVar, AND send it to the Servo, inside the 'IF' tree.
Be careful of that “nearly zero” pulse length.· Some servos have a failsafe feature that powers down the motor if the servo stops receiving pulses.· Other motors interpret “no pulse” as “zero length pulse”.· When the control signal is switched off, these motors will turn as far counterclockwise as they possibly can, until they collide with and press against their internal hardstop.· High torque servos can self destruct this way.· Even if the servo isn’t strong enough to damage itself, the DC motor inside will draw a very large current while it is stalled out.
So if you want to test your servo's behavior, drive the servo to its full clockwise position, simulate no signal for a period of time less than it takes for the servo to move completely counter clockwise, then start driving it again to the clockwise position. If you notice it move counterclockize during the no signal time, you know it doesn't have a failsafe and therefore should never be run without a signal.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Thus, your decision code stores values in ServoVar, which you KNOW is going to get sent to the Servo every 20 mSecs.
This lets you remove all those "PULSOUT" and "PAUSE 20" statements from your decision code.
And the whole thing works so much better.
'{$STAMP BS2}
'{$PBASIC 2.5}
DEBUG "ready",CR
servoVar VAR Word ' stores pulse length for servo (position)
'=======================hall effect state=================================
one VAR Bit 'state of the hall effect sensor no. 1
two VAR Bit 'state of the hall effect sensor no. 1
three VAR Bit 'state of the hall effect sensor no. 1
'========================servo state======================================
counter VAR Word ' counts the passes the servo has taken
time CON 30 ' how long the servo holds position
one CON IN0
two CON IN1
three CON IN2
Main:
IF one=0 THEN Refresh_servo
IF two=0 THEN Refresh_servo
IF three=0 THEN Refresh_servo
GOTO Main
'==========================================================================================================================
Refresh_servo:
DEBUG ? DEC servoVar, CR
FOR counter = 1 TO time ' for counter = how long to hold
PULSOUT 14, servoVar 'outputs from pin 14
GOTO main
That was not a complete program, merely an indication.
HERE'S the program -- I've only done one, two, and three, but I think the
pattern's obvious:
'{$STAMP BS2}
'{$PBASIC 2.5}
' servo control
DEBUG "ready",CR
one VAR Bit 'state of the hall effect sensor no. 1
two VAR Bit 'state of the hall effect sensor no. 1
three VAR Bit 'state of the hall effect sensor no. 1
'=============================
counter VAR Word ' counts the passes the servo has taken
time CON 30 ' how long the servo holds position
ServoVal VAR Word
Main:
one=IN0
two=IN1
three=IN2
IF one=0 THEN
GOSUB FIRST
ELSE
IF two=0 THEN
GOSUB second
ELSE
IF three=0 THEN
GOSUB third
ENDIF
ENDIF
ENDIF
FOR Counter = 1 TO Time
GOSUB RefreshServo '****** HERE *****
PAUSE 20
NEXT
GOTO Main
'==========================================================================================================================
RefreshServo:
PULSOUT 14, ServoVal
RETURN
first:
DEBUG "0 to 100 lbs", CR
ServoVal = 350
RETURN
second:
DEBUG "101 to 200 lbs", CR
ServoVal = 400
RETURN
third:
DEBUG "201 to 300 lbs", CR
ServoVal = 450
RETURN