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

Servo question

InteractivesInteractives Posts: 83
edited 2005-08-10 19:15 in BASIC Stamp
·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

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2005-08-10 17:59
    What you probably want to do is have a main like this:

    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.
  • InteractivesInteractives Posts: 83
    edited 2005-08-10 18:02
    does that just store the position of the servo in the variable named servovar?
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-08-10 18:17
    It seems that a servo's behaviour when no pulses are received on the signal line varies according to manufacturer. According to a student at Pitt:
    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-08-10 18:21
    The purpose of "ServoVar" is to de-couple your decision code from your Servo refresh code.

    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.
  • InteractivesInteractives Posts: 83
    edited 2005-08-10 18:22
    Thanks Paul- it seems to be Ok with no signal, but I think I'm going to try to rewrite the code without all the IF- THENS, just so if some one five years down the road changes servos, the thing will still work!
  • InteractivesInteractives Posts: 83
    edited 2005-08-10 18:43
    UUmmm, Ive been trying to write the code here, and I cant seem to figure out what sets the value of servoVAr to a number between 350 and 850? I think I got the rest though.
    '{$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
  • InteractivesInteractives Posts: 83
    edited 2005-08-10 18:47
    ok, clearly i dont get it, because that program is riddled with errors, and it doesnt really do anything.
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-08-10 19:06
    Oh, for heaven's sake.

    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:
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-08-10 19:10
    And, it looks like this...

    '{$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
  • InteractivesInteractives Posts: 83
    edited 2005-08-10 19:11
    Thank you so much! I'm slowly learning, believe it or not. Thank you. Thank you , thank you.
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-08-10 19:15
    You're welcome. Sorry about my own frustration, I know this stuff isn't obvious at first.
Sign In or Register to comment.