Obstacle Avoiding Robot


hi all,

2 friends , making robot using our arduino leonardo move forwards , avoid obstacles in path. using components vex robotics, may or may not familiar with. using 3 vex motors, 3 wheels attached said motors, , 2 vex ultrasonic sensors. have setup bot although running problems while trying code it. wondering if arduino vets supply sample code project, or perhaps supply code have used on similar project before. our ultrasonic sensors returning values of 1 , 2 randomly , returns no other values. attach our code can give advice on or possibly supply new code. far enjoy arduino , community can shed light on our problem. thank you,

chris

code: [select]
#include "servo.h"
#include <softwareserial.h>

const int trig_pinright =  12;   // triggers pulse right ultrasonic sensor
const int echo_pinright = 13;     // recevies echo right ultrasonic sensor
const int trig_pinleft = 4;   // triggers pulse left ultrasonic sensor
const int echo_pinleft = 5;     // receives echo left ultrasonic sensor
long durationright;            // time takes pulse bounce right ultrasonic sensor
long durationleft;             // time takes pulse bounce left ultrasonic sensor

servo motorleft;               // left vex motor
servo motorright;              // right vex motor

void setup() {

  serial.begin(9600);           // set serial library @ 9600 bps

  serial.println("initializing...");  // print initializing... confirm code working serial library

  motorleft.attach(9);         // left motor in pin 9
  motorright.attach(11);        // right motor in pin 11


  serial.println ("starting");
  // initialize pulse pin output:
  pinmode(trig_pinright, output);     
  // initialize pulse pin output:
  pinmode(trig_pinleft, output);     
  // initialize echo_pin pin input:
  pinmode(echo_pinright, input);
  // initialize echo_pin pin input:
  pinmode(echo_pinleft, input);     

}


void loop()
  {                                                         
   
    // pulse , echo right ultrasonic sensor ; process determining , printing durations right ultrasonic sensor
   
    digitalwrite(trig_pinright, low);   
    delaymicroseconds(2);
    digitalwrite(trig_pinright, high);
    delaymicroseconds(5);
    digitalwrite(trig_pinright, low);
    durationright = pulsein(echo_pinright,high);
    serial.println("durationright: ");
    serial.println(durationright, dec);
   
    // pulse , echo left ultrasonic sensor ; process determining , printing durations left ultrasonic sensor
   
    digitalwrite(trig_pinleft, low);
    delaymicroseconds(2);
    digitalwrite(trig_pinleft, high);
    delaymicroseconds(5);
    digitalwrite(trig_pinleft, low);
    durationleft = pulsein(echo_pinleft,high);
    serial.println("durationleft: ");
    serial.println(durationleft, dec);
   
    if(durationright > 0 && durationright < 4000 && durationright > 0 && durationleft < 4000) // stop when human close both sensors
        {
            motorleft.write(90);
            motorright.write(90);
        }
    if(durationright > 4000 && durationright < 10000 && durationleft > 4000 && durationleft < 10000) // move forward when human equadistant both sensors
        {
            motorleft.write(113);
            motorright.write(68);
        }
    if(durationleft - 1000 > durationright && durationright > 4000) // turn right when human close right sensor
        {
            motorleft.write(120);
            motorright.write(100);
            delay(500);
           
            motorleft.write(120);
            motorright.write(75);
            delay(2000);
        }
    if(durationright - 1000 > durationleft && durationleft > 4000) // turn left when human closer left sensor
        {
            motorleft.write(100);
            motorright.write(75);
            delay(500);
           
            motorleft.write(120);
            motorright.write(75);
            delay(5000);
        }
    if(durationright > 10000 && durationleft > 10000) // move forward fast when human far away still equadistant
        {
            motorleft.write(135);
            motorright.write(45);
        }
  }

code looks right, adding softwareserial library might overkill, writing return data out standard serial i/o. (pins d0 & d1, connected usb-to-serial.)   you can wire bluetooth or xbee radio same pins serial i/o, ignore usb serial.

i'm trying make tank rover using vex drive platform, wiring through arduino, too. (occupying d11 2, leftmotor, rightmotor, sonarservo, pingin, pingout, leftline, midline, rightline, softin, softout, using 2 vex motor drives,
a single vex servo front-only 180-degree sense, vex sonic range finder, 3 line sensors (spaced apart 1.5") , rx & tx 9600 xbee series-1.  see you're using raw distances, might give odd readings. code i've written mine, (still work in progress) uses conversion raw cm distance (actually stripped out inches conversion example code.)

i'm trying mine run in 1 of 3 modes.. mode 1, follow key presses laptop (straight parallax usb xbee explorer), adafruit xbee adapter. (pins connect ftdi usb cable.).. mode 2, random explore, stopping 10cm pops in front of it, searching 0-179 in 5 steps, looking farthest object, turning , going direction, or mode 3, watching line follower sensors 3" wide white line on running track, , staying centered. (if either of outer sensors see change, turn towards correct path. if sees change on center, stop.)

i'm trying see if can change line sensors analog pins, , re-assign 2 digital pins pair of step counters (the original version, not offset. ).. i'll need re-work frame accommodate rotary encoders.  still have code @ home, on linux laptop..


Arduino Forum > Topics > Robotics (Moderator: fabioc84) > Obstacle Avoiding Robot


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support