WAM - Standard Servo Control Understanding
hacktorious
Posts: 72
Hello,
I am new to microcontrollers/PBasic and am having difficulty understanding the code below. This code is in the WAM document on page 132-133 (p.143, as far as finding it with acrobat reader find function). The part I am not comprehending is exactly what is causing the motor to rotate CCW, and/or CW. Maybe I missed something in the reading, I don't know, but whatever the reason I was hoping someone in this forum could help.
At first I thought it had something to do with the value being above, or below the center value for the servo (750), but that doesn't seem to be the case. To me it appears the direction of the motor is dependent on whether a value (25) is "subtracted", or "added" to the duration of the PULSOUT command. Is this correct, or am I missing something here? Thanks. [noparse]:)[/noparse]
' What's a Microcontroller - ServoControlWithPushbuttons.bs2
' Press and hold P4 pushbutton to rotate the servo counterclockwise,
' or press the pushbutton connected to P3 to rotate the servo clockwise.
' {$STAMP BS2}
' {$PBASIC 2.5}
duration VAR Word
duration = 750
DO
IF IN3 = 1 THEN
IF duration > 500 THEN
duration = duration - 25
ENDIF
ENDIF
IF IN4 = 1 THEN
IF duration < 1000 THEN
duration = duration + 25
ENDIF
ENDIF
PULSOUT 14, duration
PAUSE 10
DEBUG HOME, DEC4 duration, " = duration"
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Scott
I am new to microcontrollers/PBasic and am having difficulty understanding the code below. This code is in the WAM document on page 132-133 (p.143, as far as finding it with acrobat reader find function). The part I am not comprehending is exactly what is causing the motor to rotate CCW, and/or CW. Maybe I missed something in the reading, I don't know, but whatever the reason I was hoping someone in this forum could help.
At first I thought it had something to do with the value being above, or below the center value for the servo (750), but that doesn't seem to be the case. To me it appears the direction of the motor is dependent on whether a value (25) is "subtracted", or "added" to the duration of the PULSOUT command. Is this correct, or am I missing something here? Thanks. [noparse]:)[/noparse]
' What's a Microcontroller - ServoControlWithPushbuttons.bs2
' Press and hold P4 pushbutton to rotate the servo counterclockwise,
' or press the pushbutton connected to P3 to rotate the servo clockwise.
' {$STAMP BS2}
' {$PBASIC 2.5}
duration VAR Word
duration = 750
DO
IF IN3 = 1 THEN
IF duration > 500 THEN
duration = duration - 25
ENDIF
ENDIF
IF IN4 = 1 THEN
IF duration < 1000 THEN
duration = duration + 25
ENDIF
ENDIF
PULSOUT 14, duration
PAUSE 10
DEBUG HOME, DEC4 duration, " = duration"
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Scott
Comments
It would make total sense if one value was 700, and the other was 800, since 700 is to the left of 750, and 800 is to the right.
700
750
800
750
925
975
Thanks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Scott
-Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
StampPlot - GUI and Plotting, and XBee Wireless Adapters
Southern Illinois University Carbondale, Electronic Systems Technologies
It sounds like you're confusing yourself over nothing -- Duration is never 950.
Edit:· Oh, wait, it looks like as long as one input is high, 'Duration' decrements by 25 each time through the loop, until it reaches 500 (1 mSec, "Full Left").· So the decrement is actually repeated.
If the other input is high, 'Duration' is incremented by 25 each time through the loop, until it passes 750, and keeps going to 1000 (2 mSec, "Full Right").
Maybe it's the "repeat" that's confusing you?
The thing that is confusing is that it can turn CW or CCW if the duration is above 750, and it can turn CW or CCW if the duration is below 750 depending on the button you push. I am almost certain it has nothing to do with the way the buttons are connected because they are connected in an identical manner.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Scott
Unless your servo is defective, it simply can't turn CW AND turn CCW with the same pulse-width going to it. That would be non-deterministic behavior (also called "Random") and a random servo would not be very useful.
It's actually more likely that the debug screen is 'saving up' messages, so that while it's scrolling through 900, 875, 850 the servo has already started recieving its 700, 675, 650 pulses and reversed.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Andy Lindsay
Education Department
Parallax, Inc.
Getting back to my previous post of:
700<
>750<
>800
750<
>925<
>975
It now makes total sense if I push p4 and goto 975 (location/coordinate) the only way to get back to 950 (location/coordinate) (by subtracting 25) is by spinning in the opposite direction. For some reason I was not thinking of the duration in terms of location.
Thanks for your help everyone.
I guess what I should say is that 2:00 is always 2:00 and is in the same location on the dial.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Scott
Post Edited (hacktorious) : 2/13/2007 10:11:01 PM GMT