I want to use an ultrasonic sensor as a trigger to a servo motor.
ez2consume
Posts: 3
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
' {$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
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.
Ok Mike beat me to it. I gotta learn to type faster.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
' {$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
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 Savage
Parallax Tech Support
'
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 Lowe
Maybe I should have waited to do that......