Shop OBEX P1 Docs P2 Docs Learn Events
Help with servo vibration! — Parallax Forums

Help with servo vibration!

PlusOn3PlusOn3 Posts: 15
edited 2012-01-20 15:19 in Robotics
Hey guys.
So I am starting to work on a project, and right now I am working on getting my servo to be controlled by a 2 axis joystick. I will post the code below and a picture of the setup. The problem I am having is sometimes in the middle of its rotation the servo will start to oscillate rather fast. I'm not sure if this is due to some argument in the code, or if I programmed something incorrectly. These vibrations would be quite devastating to the project and I need to get rid of them. I don't know if I need to post more information or not. I could probably get a video of what it is doing if I haven't made it clear enough. Any help would be great.


' {$STAMP BS2}
'{$PBASIC 2.5}

UDPin CON 8
LRPIn CON 9
LR VAR Word
UD VAR Word
position VAR Word

DO
HIGH LRPin
PAUSE 2
RCTIME LRPin, 1, UD
HIGH UDPin
PAUSE 2
RCTIME UDPin, 1, LR
DEBUG HOME, "UD = ", DEC UD, ", LR = ", DEC LR, CLREOL
IF UD < 20 AND UD > 10 THEN
position = position +5
ELSEIF UD < 10 THEN
position = position +10
ELSEIF UD > 36 AND UD < 45 THEN
position = position -5
ELSEIF UD > 45 THEN
position = position -10
END IF
IF position < 200 THEN
position = 200
ELSEIF position > 1210 THEN
position = 1210
ENDIF
PULSOUT 14, position
LOOP

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2012-01-19 16:57
    There is too much time between successive pulses to the servo. A servo should receive a pulse about every 20 milliseconds.

    Try commenting out the DEBUG portion of your code.

    Look up MIN/MAX, you could use that rather than the last two IF statements.

    See if you can change the PAUSE 2 to PAUSE 1.
  • PlusOn3PlusOn3 Posts: 15
    edited 2012-01-20 11:58
    What do you mean by "Commenting out the DEBUG portion of your code"
    I don't understand what that means or how to do it.
    Sorry, I am still pretty new to this stuff.
  • W9GFOW9GFO Posts: 4,010
    edited 2012-01-20 12:24
    Any line of code that begins with an apostrophe will not be executed.

    When you write a program it is a good idea to include some notes in plain english so that when you or someone else looks at it, they can understand what is going on.

    Below I have added some comments to your code. The comments I added will not change the way your code executes in any way.

    To "comment out" the DEBUG line of your code all you need to do it put an apostrophe before it - and it will not execute, the program will skip right over it as if it weren't there.
    [COLOR=#232323]'{$STAMP BS2}[/COLOR]
    [COLOR=#232323]'{$PBASIC 2.5}[/COLOR]
    [COLOR=#232323]
    [/COLOR]
    [COLOR=#232323]UDPin    CON   8[/COLOR]
    [COLOR=#232323]LRPIn    CON   9[/COLOR]
    [COLOR=#232323]LR       VAR   Word[/COLOR]
    [COLOR=#232323]UD       VAR   Word[/COLOR]
    [COLOR=#232323]position VAR   Word[/COLOR]
    [COLOR=#232323]
    [/COLOR]
    [COLOR=#232323]DO                      ' Begin loop[/COLOR]
    [COLOR=#232323]
    [/COLOR]
    [COLOR=#232323]HIGH LRPin   [/COLOR]
    [COLOR=#232323]PAUSE 2                 ' pause 2 milliseconds[/COLOR]
    [COLOR=#232323]RCTIME LRPin, 1, UD [/COLOR]
    [COLOR=#232323]
    [/COLOR]
    [COLOR=#232323]HIGH    UDPin [/COLOR]
    [COLOR=#232323]PAUSE 2 [/COLOR]
    [COLOR=#232323]RCTIME UDPin, 1, LR [/COLOR]
    [COLOR=#232323]
    [/COLOR]
    [COLOR=#232323]DEBUG HOME, "UD = ", DEC UD, ", LR = ", DEC LR, CLREOL    ' Display values[/COLOR]
    [COLOR=#232323]
    [/COLOR]
    [COLOR=#232323]IF UD < 20 AND UD > 10 THEN [/COLOR]
    [COLOR=#232323]   position = position +5 [/COLOR]
    [COLOR=#232323] ELSEIF UD < 10 THEN [/COLOR]
    [COLOR=#232323]   position = position +10 [/COLOR]
    [COLOR=#232323] ELSEIF UD > 36 AND UD < 45 THEN [/COLOR]
    [COLOR=#232323]   position = position -5 [/COLOR]
    [COLOR=#232323] ELSEIF UD > 45 THEN [/COLOR]
    [COLOR=#232323]   position = position -10 [/COLOR]
    [COLOR=#232323]END IF [/COLOR]
    [COLOR=#232323]
    [/COLOR]
    [COLOR=#232323]IF position < 200 THEN [/COLOR]
    [COLOR=#232323]  position = 200 [/COLOR]
    [COLOR=#232323] ELSEIF position > 1210 THEN [/COLOR]
    [COLOR=#232323]  position = 1210 [/COLOR]
    [COLOR=#232323]ENDIF[/COLOR]
    [COLOR=#232323]
    [/COLOR]
    [COLOR=#232323]PULSOUT 14, position    ' Send pulse to servo
    [/COLOR]
    LOOP                    ' Go back to beginning
    
  • PlusOn3PlusOn3 Posts: 15
    edited 2012-01-20 15:05
    WOW!!!!!!!
    I commented out the DEBUG line and changed the Pause 2's to Pause 1's and it works so much better!! It's completely smooth!!!!! You rock!!!
    Plus, how did you get the code to be entered into your reply so nicely? With a scroll bar and everything.Thats sweet!
  • W9GFOW9GFO Posts: 4,010
    edited 2012-01-20 15:11
    Do this...
    [noparse]
    Enter 
    many 
    lines
    of
    code
    here
    
    [/noparse]
    ... to make it look like this;
    Enter 
    many 
    lines
    of
    code
    here
    
  • PlusOn3PlusOn3 Posts: 15
    edited 2012-01-20 15:13
    AWESOME!!!!
    Once again, 
    you rock!
    
  • W9GFOW9GFO Posts: 4,010
    edited 2012-01-20 15:19
    You're welcome!
Sign In or Register to comment.