Shop OBEX P1 Docs P2 Docs Learn Events
Programming ping with servos — Parallax Forums

Programming ping with servos

fahadfahad Posts: 14
edited 2011-03-13 16:32 in Accessories
Hi i am making a boe bot for a simple navigation such as avoiding obstacles(I am only using ping sensor for this). I am using the following code. But the thing is that when I use this code in the main loop the boe bot with shaky movement and not that fast when the servos turn in left_turn: . How can I avoid this shaky and slow movement in the main loop. I want the boebot to move(fast) as well as detect changes in distance in real time. I think this is the reason why the robot is becoming slow and shaky because the robot is just reading one command at a time, the delay due to the reading changes in sensor affects the rate at which the servos turn...I wanna know how do i avoid this...I am new to programming and am making a boe bot foe navigation in a maze.
main_loop:

DO
PULSOUT leftservo, 850 'move forward
PULSOUT rightservo, 650
PAUSE 20

PULSOUT 0, 5
PULSIN 0, 1, distance3 'ping right activate
PAUSE 20

distance3 = distance3 ** 900
DEBUG HOME, DEC3 Distance3, " cm"

IF (distance3 < 5) THEN
GOSUB left_turn
ENDIF

LOOP

left_turn:

FOR pulscount= 0 TO 100
PULSOUT rightservo, 850
PULSOUT leftservo, 850
PAUSE 20
NEXT
RETURN

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-13 11:19
    Servos have to see a control pulse about 50 times a second (once roughly every 20ms). In your program, you send a control pulse to both servos, then wait 20ms. That's find if you do little else, but you are then triggering the PING and waiting for the sound pulse to return, then waiting another 20ms. That's too long. You need to remove the "PAUSE 20" after the servo pulses and remove the "PAUSE 20" after doing the PING measurement. You then have to add one PAUSE after the PING reading that delays for maybe 18ms minus the time needed for the PULSIN. distance3 is a count of the number of 2us periods it takes for the ultrasonic pulse to return. Try something like "PAUSE 20 - (distance3 / 500)"
  • PublisonPublison Posts: 12,366
    edited 2011-03-13 16:32
    You also may want to remove the "DEBUG" statement from your program. That takes time to send to the Debug Terminal and can cause delays.
Sign In or Register to comment.