Introduction: Arduino Object Avoidance Robot

Picture of Arduino Object Avoidance Robot

In this instructable i will describe to you how i built my Object Avoidance Robot. I hope this information will help you to create your own OAR or (maybe) better your own designs.


The materials i used were:
----------------------------------------------------------------------------------------------------------------------------------------------
1 x - Arduino
1 x - Arduino Motor Shield (http://www.adafruit.com/products/81)
    male header pins (for breakout's in the motor shield)
    female header wires (so i can recycle my parts if i decide to break this project apart)

1 x - Parallax Servo Motor (http://www.radioshack.com/product/index.jsp?productId=12296088)
1 x - Ping))) sensor or HC-SR04 Ultrasonic sensor

1 x - Parallax 5-Position Switch (http://www.radioshack.com/product/index.jsp?productId=12296089)

1 x - RC car with separate back-wheel dc motors
6 x - rechargeable AA batteries (conveniently stored in the RC cars battery housing)

1 x - plastic project box from RadioShack

super glue
double sided tape

and of course

soldering iron
solder
wires
----------------------------------------------------------------------------------------------------------------------------------------------

Step 1: Testing

First section/step is getting all of the parts together and getting them working.

I am not going to write out how to set up the motor shield, the ultrasonic sensor, or the servo. Others have already done so, and did a much nicer job than I could do. So here are a few links I found very useful when unsure of what I was doing.

The Ultrasonic Sensor:
Lets Make Robots

The Motor Shield
Ladyada


At this point I was only concerned with the bare minimum. I wanted to know the ultrasonic sensor was working, the motors in the RC car would spin when i told them to, and the servo would go to the position I set it to go.

Step 2: The Build

Picture of The Build

I started with the entire frame of the RC car, but when a friend told me it looked like a tractor I decided to scrap the front two wheels, and add a scratch plate to keep its balance.

The design was pretty easy for me to decide on since my options were limited. Using the plastic project box from radioshack, I attached the servo to the lid of the box, chucked my Arduino inside, and screwed on some bracket I had laying around to the servo for the ultrasonic sensor. I put a power switch on the side of the box and ran the wires from the battery block of the RC car to the switch and then to the motor shield of my arduino.

The scratch plate is from an old USB postal scale (I'm done selling stuff on ebay anyway), I sanded down the sides of the circled plastic to fit underneath the motors, and with some superglue, its held in place really well.

Soon after it was all put together and running I decided I wanted a parallax 5-position switch. With this I can move the OAR manually and improve on the movement functions.

Step 3: The Code

I am not big on sharing my code, just because it is personalized for my setup and most likely has no use to you and your unique setup. If you want my code, email me at cgmalantonio@gmail.com and I will gladly send you a copy. I will however describe my work in pseudo-code so you get the idea.

Lets get to it!


First I wanted a function that would use the ultrasonic sensor to get readings while the servo motor turned to 5 different points:
(0 = Left // 45 = Front left // 90 = Front // 135 = Front right // 180 = Right)

Scan{
I used a simple for loop to move the servo in increments of 45, from 0 to 180 (and another to bring it back to 0). Every time it reaches its next point it calls my "LowestNumber" function and adds the value received to an array for the 5 values received (left / left-front / front / right-front / right).

something like this:
for (int i = 0; i < 180; i = i+45){
myservo.write(i);
LowestNumber();
}
for (int i = 180; i > 0; i = i - 45){
myservo.write(i);
LowestNumber();
}


The LowestNumber function takes in 3 readings, spaced 50 milliseconds apart, from the ultrasonic sensor. The 3 readings are then compared against each other, and the lowest value (closest reading) is accepted and is stored into a variable (LowestNumberResult). This number is then stored into an array based on where the servo is positioned. The ultrasonic sensor is a little unpredictable sometimes so I like to check 3 times. And considering I am only worried about it hitting something, I keep the lowest number.

//grab your lowest number
a = ultrasonic.Ranging(INC)
delay(50)
b = ultrasonic.Ranging(INC)
delay(50)
c = ultrasonic.Ranging(INC)

delay(50)
if (a > b)
a = b
if (a > c)
a = c


Once all 5 positions of the servo motor have gathered a distance from the ultrasonic sensor, we then have to handle the positions appropriately.
}




Now we know the distances in front of the OAR's location. We start by finding out which direction is the greatest distance and if some conditions are met, we'll go in that direction.

Handle{

Some of the restrictions were as such:

- are all directions greater than 20 inches         
                    // make sure all paths are clear, can be any number,
                    // doesn't have to be 20 inches

- if furthest direction is less than the front position + 10 inches (some number), go forward  
                    // favoring the front direction means it wont just turn
                    // around in circles in the center of your room

                    // i eventually added a second statement, just like this,
                    // saying if the front was clear enough, go forward twice

- if furthest direction is Left, make sure left front is clear
                     -if not clear check right

- if furthest direction is Right, make sure right front is clear
                     -if not clear check left

- if furthest direction is Left front, make sure left is clear
                     -if not clear check right

- if furthest direction is Right front, make sure right is clear
                     -if not clear check left

- if all directions are less than 20 we want to turn the OAR around and try new measurements
}

Extra{

I added some extra features for my 5 position switch.

Power on
   Waiting for input
             - if you approach the OAR from the front too close, it'll back up and shake its head back and fourth
             - if you press a direction, it'll move in that direction
   Center click
             - starts a query, input 8 directions and it will drive in those directions
   Center click again
             - drives autonomously and avoids walls/objects
   Center click a third time returns menu to start

as well as some head turns of the servo when a direction is picked (as though the OAR is looking where its going to turn)

}


Step 4: Final Project (?)

I seem to have a problem with finishing my projects. I always want to continue updating and bettering my projects as I come up with new ideas or new answers to old problems. However for the sake of this instructable I put together a video of the latest version of my code to show you how my OAR is running. It still has some limitations and bugs to work out but it is a nice collection of successful runs.


Latest Video:



I'll leave you with a compilation of my earlier codes in action. The video goes from oldest to first working code. I just enjoy watching my OAR fall over and run into things, especially since it doesn't anymore.

Oldest Video:

Comments

GrantR2 (author)2015-09-12

Can someone please help me with coding on Arduino

Im new to coding and robotics and I built a biped robot with 4 servos and a HC-SRO4 servo for object avoidance. Im using Arduino uno and I only know how to make the servos move with write.microseconds. I want to make it avoid objects and be autonomous.

KusalW (author)2014-12-13

Hi, I made the chassi but i don`t know how to code.so can you send me the codes?

wijesingha.kusal@gmail.com

please send to this email.i`m gladly request you.i`m a junior with the Arduino.please send the codes.

Thank you

M3RCIL3SS (author)2014-01-05

Hey, what was the code you used, my robot messes up, I will be using arduino (UNO)
If you could give the code, that would be awesome
Thanks

cgmalantonio (author)2012-09-10

All updates regarding this OAR will just be updates to the code. I ran out of available pins on my Arduino and I don't want to scrap the tail. I do plan on creating another OAR in the near future with a few more sensors, I'll be sure to post it on instructables first.

amandaghassaei (author)2012-09-10

wow this looks great! I love the way it turns its "head" to scan its environment. are the updates you referred to in your last step just code updates, or are you thinking of adding additional hardware?

About This Instructable

21,022views

32favorites

License:

Bio: I am a recent graduate from the University of Pittsburgh. While i was there i got a Bachelors degree in Computer Science and explored a ... More »
More by cgmalantonio:Personal Black Box - Arduino Mega + Ultimate GPS Shield + LSM303Ambient Computer Lights - using an Arduino with NeoPixel stripArduino Motorized Window Blinds
Add instructable to: