Shop OBEX P1 Docs P2 Docs Learn Events
I want to use an ultrasonic sensor as a trigger to a servo motor. — Parallax Forums

I want to use an ultrasonic sensor as a trigger to a servo motor.

ez2consumeez2consume Posts: 3
edited 2007-10-10 17:01 in BASIC Stamp
i'm really new to this whole physical computing thing and self teaching. would probably be easier to learn Japanese from a deaf and dumb guy, but still i love using this stuff. here's my problem. i'm working on a project where I want to use an ultrasonic sensor as a trigger to a servo motor. i basically just want the servo motor arm to swing back and forth is the unltrasonic sees objects > 10inches away. unfortunately, the code i have cobbled together doesn't to seem to be doing the trick. i'll copy it here and maybe someone can tell me some pointers (please spare me the open laughter - i'm well away this is ugly!)

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

Ping PIN 14

#SELECT $STAMP
#CASE BS2
Trigger CON 5
Scale CON $200
#ENDSELECT



RawToIn CON 889


IsHigh CON 1
IsLow CON 0

'
VARIABLES
RawDist VAR WORD
inches VAR WORD


'
Initialization
Reset:
DEBUG CLS,
"Parallax PING))) Sonar", CR,
"======================", CR,
CR,
"Time (uS)...... ", CR,
"Inches......... ",


'
Program Code
Main:
DO
GOSUB Get_Sonar
inches = RawDist ** RawToIn
cm = RawDist ** RawToCm

GOSUB motor_servo
GOSUB Display1

DEBUG CRSRXY, 10, 3,
DEC RawDist, CLREOL,
CRSRXY, 10, 4,
DEC inches, CLREOL,
CRSRXY, 10, 5,


PAUSE 100
LOOP
GOTO main

'
SubRoutines
'Triggers the Ping Sonar and measures the
'echo pulse. The values are converted
'and the result is the distance from the sensor
'to the target in microseconds.

Get_Sonar:
Ping = IsLow
PULSOUT Ping, Trigger
PULSIN Ping, IsHigh, rawDist
RawDist = RawDist */ Scale
RawDist = RawDist / 2
RETURN


'
motor conrol sub routines

motor_servo: ' if then to control servo motor
IF RawDist >= 10 THEN motorleft
IF RawDist <= 10 THEN motor_center



motorleft:
PULSOUT 15, 1000
PAUSE 65
RETURN

motor_center:
PULSOUT 15, 750
PAUSE 65
RETURN




'
[noparse][[/noparse] display / debug space ]
Display1: 'opening and running debug window
DEBUG HOME
DEBUG CLS,
"Parallax PING))) Sonar", CR,
"======================", CR,
CR,
"Time (uS)...... ", CR,
"Inches......... ", CR,
"Centimeters.... "

RETURN

Post Edited By Moderator (Chris Savage (Parallax)) : 10/9/2007 3:17:17 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-06 20:56
    1) Please delete your other message. It's a rule here that the same message should not be posted to multiple forums.

    2) Servos require a control pulse roughly every 20ms or they shut down until they get another pulse. The Robotics with the BoeBot tutorial has a lot of information and examples of controlling servos while your program does other things in the 20ms between servo pulses. So:
    a) Set up your main loop so that the PULSOUT occurs every 15-25ms. If you're only going to use the PING, you could get one PING reading, then pulse the servo every time through the loop. I would suggest that you either PAUSE after that or send your debug information to the display.

    b) The DEBUG statement outputs at 9600 Baud or about 1ms per character. You've got several times 20ms just in display delay. You could keep a counter that increments each time through the main loop and display only one line of information on each time through the loop using IF/THEN or one of the fancier "select the nth statement to execute" statements.
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-10-06 21:10
    My first reaction was - wow he's sure pausing alot. With out the over head added by the debug commands and math you have at least 165 ms between servo commands. try reducing the overall time down to around 20 ms. Your servos will love you for it.

    Ok Mike beat me to it. I gotta learn to type faster.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • ez2consumeez2consume Posts: 3
    edited 2007-10-09 03:48
    i know i'm really close now. thanks for the help so far. how do I bring this baby home?

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

    Ping PIN 14

    #SELECT $STAMP
    #CASE BS2
    Trigger CON 5
    Scale CON $200
    #ENDSELECT

    RawToIn CON 889

    IsHigh CON 1
    IsLow CON 0

    '
    VARIABLES
    RawDist VAR WORD
    inches VAR WORD
    myCount VAR BYTE
    '
    Program Code
    Main:
    DO
    GOSUB Get_Sonar
    ' inches = RawDist ** RawToIn
    GOSUB motor_servo
    GOSUB Display1

    PAUSE 100
    LOOP
    ' GOTO Main

    '
    SubRoutines
    'Triggers the Ping Sonar and measures the
    'echo pulse. The values are converted
    'and the result is the distance from the sensor
    'to the target in microseconds.

    Get_Sonar:
    Ping = IsLow
    PULSOUT Ping, Trigger
    PULSIN Ping, IsHigh, rawDist
    RawDist = RawDist */ Scale
    'RawDist = RawDist / 2
    RETURN

    '
    motor conrol sub routines
    motor_servo: ' if then to control servo motor
    IF RawDist >= 600 AND myCount = even THEN motorleft
    IF RawDist >= 600 AND myCount = odd THEN motorright
    IF RawDist < 600 THEN motor_center

    ' Hey, how do I make myCount be evaluated as either even or odd? So that when it goes to motor_center, it will then go to either motorleft or motorright that it wasn't on before

    RETURN

    motorleft:
    PULSOUT 15, 1000
    PAUSE 65
    RETURN

    motorright:
    PULSOUT 15, 500
    PAUSE 65
    RETURN

    motor_center:
    PULSOUT 15, 750
    PAUSE 65
    myCount = myCount + 1

    RETURN

    '
    [noparse][[/noparse] display / debug space ]
    Display1: 'opening and running debug window
    DEBUG HOME
    DEBUG DEC RawDist, CR

    RETURN
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-09 04:45
    1) You're not paying attention to anyone's comments about the time delays. For a servo to work properly, your overall loop time must be less than approximately 20ms. The servo must see a pulse about every 20ms. Don't bother asking for help if you're not going to listen to it. If you don't understand what people suggest, ask what they mean. If they can't restate what they're suggesting so you can understand it, perhaps someone else will chime in with another explanation that works for you.

    2) To test for an odd value do: IF (value & 1) <> 0 THEN ...

    3) To test for an even value do: IF (value & 1) == 0 THEN ...
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-10-09 15:18
    Your duplicate message in the Stamps In Class Forum was removed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ez2consumeez2consume Posts: 3
    edited 2007-10-10 01:49
    thanks for the help. i think i have started to understand. like i said, i'm a little slow, have pity. i cut down the the ms to under 20 i think. i included the code to test for odd and even by writing:

    '
    motor conrol sub routines
    motor_servo: ' if then to control servo motor
    IF RawDist >= 600 AND (myCount & 1) <> 0 THEN motorleft
    IF RawDist >= 600 AND (myCount & 1) == 0 THEN motorright
    IF RawDist < 600 THEN motor_center
    RETURN

    however, the MacBS2 editor says that the syntax right after == is incorrect. this, i do not understand. what should that number be if not '0'
  • Shawn LoweShawn Lowe Posts: 635
    edited 2007-10-10 17:01
    I think you only need 1 =

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    Maybe I should have waited to do that......
Sign In or Register to comment.