Shop OBEX P1 Docs P2 Docs Learn Events
Roaming with ping on servo — Parallax Forums

Roaming with ping on servo

Hey guys,

I'm trying to get my ActivityBot to roam with ping on a servo with the code below but I can't get it to work. What am I doing wrong.


#include "simpletools.h" // Library includes
#include "abdrive.h"
#include "ping.h"
#include "servo.h"

int main() // Main function
{
drive_setRampStep(10); // Set step size
drive_speed(128, 128); // Initial speed

int angle = 0, distance = 0, sweep = 1; // Variables

while(1) // Main loop
{
if(angle >= 1800) // Sweep multiplier
sweep = -1;
else if(angle <= 0)
sweep = 1;

servo_angle(16, angle += (sweep *180)); // Set angle
pause(50); // Let servo get there

distance = ping_cm(17); // Check distance

if(distance < 15) // Oops, too close
{
while(distance < 25) // While too close
{
drive_rampStep(-128, -128); // Back up
distance = ping_cm(17); // ...and re-check
}
}
else if(distance < 35) // If proximate (< 35 cm)
{
if(angle > 900) // If kinda in front of on right
{
while(distance < 35) // Freeze turret, turn until
{ // opening
drive_rampStep(128, 0); // ...turn right
distance = ping_cm(17); // Check distance
}
}
else // If kinda in front of on left
{
while(distance < 35) // Freeze turret, turn until
{ // opening
drive_rampStep(0, 128); // ...turn left
distance = ping_cm(17); // Check distance
}
}
}
else // If not too close
{
drive_rampStep(128, 128); // ... go forward
}
}
}

Comments

  • WhitWhit Posts: 4,191
    edited 2016-05-25 12:44
    Welcome to the forums dbd16!

    Have you had a look here? http://learn.parallax.com/tutorials/robot/activitybot/activitybot/navigate-ultrasound
    I know it isn't exactly what you are looking for, but it might help you figure it out. learn.parallax.com has many resources. Have a look and come back if you have questions - lots of folks here are willing to help.

    Also - you might want to post in the Robotics section of the forum and use the "C" button when you post code (it is above on the Leave a Comment or Post window and will keep code in format.
  • Thanks!
Sign In or Register to comment.