Shop OBEX P1 Docs P2 Docs Learn Events
WAM - Standard Servo Control Understanding — Parallax Forums

WAM - Standard Servo Control Understanding

hacktorioushacktorious Posts: 72
edited 2007-02-13 22:06 in Learn with BlocklyProp
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-13 01:53
    You're basically correct. For standard servos, the center position is 750 (about 1.5ms pulse). Any value less than this causes the servo to position proportionally to one side of center. Any value greater than this causes the servo to position to the other side of center. Usually the extremes of position of the servo correspond to pulses ranging from about 0.5ms to about 2.5ms. These pulses need to recur about every 20ms. For the BS2, the width of the pulse corresponds to the number in the PULSOUT statement (1 count is 2us). The program decreases the pulse width on every loop if one switch is pushed, increases the pulse width on every loop if the other switch is pushed. The pulse width is limited to remain between 1.0ms and 2.0ms by the IF statements
  • hacktorioushacktorious Posts: 72
    edited 2007-02-13 02:44
    OK, that makes some sense, but I am still a little confused. If the duration is 950, which is to the right of 750, and IN3 = 1, it will subtract 25 and rotate CW; the value used will be 925. On the same hand if the duration is 950, and IN4 = 1 the value of 25 is added to the duration giving a value of 975; except it rotates CCW. Both 975 and 925 are to the right of 750, so how does the servo know which way it has to go? This would be 1.85ms and 1.90ms, which are both greater than 1.5ms.

    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 HebelMartin Hebel Posts: 1,239
    edited 2007-02-13 03:04
    The servo has an internal feedback network.· It rotates to get the setpoint (pulse width sent) to the actual (feedback from a potentiometer that rotates with servo horn).· Based upon the error it determines which way to turn.· Pretty cool for technology that's been around a long time for flying model airplanes and cars, eh?

    -Martin


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    StampPlot - GUI and Plotting, and XBee Wireless Adapters
    Southern Illinois University Carbondale, Electronic Systems Technologies
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-02-13 17:42
    Where'd you get the value of "Duration" being "950"? "Duration" starts out at 750, or 'center point', or 'still'.

    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?
  • hacktorioushacktorious Posts: 72
    edited 2007-02-13 18:05
    The duration becomes 950 when I push the button plugged into p4. Then I push button p3 and it starts spinning in the opposite direction. You can see the duration increase/decrease by looking at the debug screen because it is written with the DEBUG command.

    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-02-13 20:57
    Well, it can go from 950 to 700 in 10 steps. At 10 mSec per step, that's 100 mSec, or 1/10 second. That's fast enough to seem instantaneous.

    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.
  • edited 2007-02-13 21:33
    Folks, remember that we've got extra information published to this forum that will be added to that chapter in What's a Microconntroller.· Here is a link: More about Standard Servo Control with the BASIC Stamp 2.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • hacktorioushacktorious Posts: 72
    edited 2007-02-13 22:06
    Excellent. That clears everything up. Having a diagram and thinking in terms of location makes all the difference. Thanks.

    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
Sign In or Register to comment.