Sie sind auf Seite 1von 5

Obstacle Avoidance - Duino-Robotics

Learn

03/11/14

Talk

Build

Grow

Store

Last updated 16/09/13

Obstacle Avoidance Tutorial


Obstacle avoidance is a means of a robot being able to move around in an unknown environment whilst not
colliding with surrounding objects. Obstacle avoidance can be found on many different types of machines, big and
industrial robots, or even on newer cars. Knowing how to program obstacle avoidance into your robots can be a
very useful skill to have, whether for a little robot project, or a larger project where obstacle avoidance is just a
small piece of the larger picture.
First of all check out the below video which runs you through the basics of obstacle avoidance, it runs for nearly
twenty minutes so be prepared to learn a lot. After you have watched this video you can scroll down to check out
further explanations of the examples shown and even some Arduino code.

http://www.duino-robotics.com/obstacle-avoidance.html

1/5

Obstacle Avoidance - Duino-Robotics

03/11/14

Alright, so first of all let' s look at the flowchart. This flowchart is a little different (not so Arduino specific) but the
idea behind it is still the same.
First of all we start o with the diamond shape, the
diamond shape represents a check. For this check we
are seeing if there is an object closer than 30 cm to our
robot. The check has two possible outcomes, yes or no.
Yes, meaning that there is indeed some object closer
than 30 cm. No, meaning that there is no objects
detected within 30 cm.

If there is nothing within 30 cm the robot can simply


move forward as the path is clear. If there is something
closer than 30 cm the robot must perform obstacle
avoidance maneuvers.

The rst stage of obstacle avoidance is to stop the


robot! If you don' t stop the robot immediately it will
crash!

Phew! That was dramatic.

After the robot has stopped it needs to see what way it


should go. It does this by looking both directions, much
like you should when you cross the road. First the robot
turns left, takes a reading, turns right, takes a reading.

Another check occurs to see what direction is the best way to go. If left is the way to go it has to turn back to the
left and then go forward. If right is the way to go the robot simply moves forward as it is already facing in the right
direction.
So that was pretty easy wasn' t it? All we have to do now is convert that diagram into Arduino code and that' s it,
obstacle avoidance code done!

For this example I will be assuming that you already know how continuous rotation servos work and how to hook
them up to your Arduino. If you don' t know how to do what I just mentioned make sure to go to the Arduino Tutorials
and watch the video on Servos and DC motors.
The first thing that I always do when making robots is to make the robot move forward. Getting your robot to move
forward a) means that you have connected everything together properly and b) means that you have the orientation
and the signals you are sending to the servos correct. One I have done this I move those commands into a separate
void with the name something along the lines of moveForward.
void moveForward(){
leftServo.write(0);
http://www.duino-robotics.com/obstacle-avoidance.html

2/5

Obstacle Avoidance - Duino-Robotics

03/11/14

RightServo.write(180);
}

Once you have got your robot to move forward it is easy to add all the other movement commands. To get it to move
backwards, just reverse the values for the left and right motor. To get the robot to turn right you can make the left
motor go forward and the right motor to move backward. Left, right motor forward, left motor backward.
void moveBackward(){
rightServo.write(180);
leftServo.write(0);
}

void moveRight(){
rightServo.write(0);
leftServo.write(0);
}

void moveLeft(){
rightServo.write(180);
leftServo.write(180);
}

void moveStop(){
rightServo.write(90);
leftServo.write(90);
}

I put all these voids below the void loop so that they aren' t in the way.
I also like to put my scan routine in a void of its own as well. Depending on what sensor you are using and how you
are coding it this will change, so I won' t put in code for this part. I will simply say that I have a void routine for
scanning the distance named void scan().
Now that all of that is done the rest of the coding is as easy as making two minute noodles. We will simply follow
the flowchart from before but write it in Arduino language. First comes the initial check to see if there is something
in front of the robot. Because I have a void named scan(); I can simply call to that void to take care of the distance
scanning
scan();

//Get the distance retrieved

Now because this void stores the distance retrieved from the sensor into a variable named ' distance' I have to
create variables for each direction; front, left and right. Remember these go above the void setup.
http://www.duino-robotics.com/obstacle-avoidance.html

3/5

Obstacle Avoidance - Duino-Robotics

03/11/14

int fDistance;

//Variable to store the distance in front of the robot

int lDistance;

//Variable to store the distance on the left side of the robot

int rDistance;

//Variable to store the distance on the right side of the robot

Now because the scan function was called the distance in front of the robot is stored in the ' distance' variable.
Because the robot if facing forward I need to give the value the variable ' distance' has into the variable
' fDistance' .
fDistance = distance;

//Set that distance to the front distance

Now we can perform the check to see if there is something in front of the robot. What I like to do is to create a
variable that is purely used as a trigger distance. The trigger distance should be set to the minimum distance you
want your robot to perform obstacle avoidance maneuvers. In this case my trigger distance is 30 cm.
if(fDistance < triggerDistance){

//If there is something closer than 30cm in front of us

Remember that the curly bracket signifies the beginning of the block, this means everything inside this block will
happen when this if statement is true, or yes in flowchart terms. Everything that is in this block is everything that
stems from the ' yes' stem from the first check in the flowchart.

Now to begin the obstacle avoidance routine!


moveBackward();

//Move Backward for a second

delay(1000);

1)

moveRight();

//Turn Right for half a second

delay(500);

2)

3)

moveStop();

//Stop

scan();

//Take a reading

rDistance = distance;

//Store that to the distance on the right side

moveLeft();
4)

5)

delay(1000);

//Turn left for a second

moveStop();

//Stop

scan();

//Take a reading

lDistance = distance;

//Store that to the distance on the left side

if(lDistance < rDistance){

//Left distance smaller than right?

moveRight();

6)

//Move right for a second

delay(1000);
moveForward();

//Then move forward

}
else{
moveForward();

7)

//This happens if left is larger than right

}
}

This may look like a jumble of code, but it really isn' t, OK it is, but we can understand it by looking at it bit by bit.
http://www.duino-robotics.com/obstacle-avoidance.html

4/5

Obstacle Avoidance - Duino-Robotics

03/11/14

1) Once an object has been detected the robot then begins to move backward, it does so for one second.
2) The robot then begins to turn right and does so for half a second, then coming to a hault.
3) The robot takes a reading and stores that to the ' rDistance' variable.
4) The robot begins to move left and does so for one second (to get half a second left relative to it' s original
orientation).
5) The robot scans and stores the distance value to the variable ' lDistance' .
6) Another check happens seeing which direction is the better way to go. If right is the better way to go the robot
turns right for one second and then continues on to move forward.
7) If however left is the better way to go the robot will end up moving forward because it is already facing the left
direction.
The last section of the code goes back to the first if statement at the top of the code checking to see if there is an
object in front. Remember how we looked out how the curly brackets held all the statements in for that check, well
this is at the end of that check. The else represents the ' no side' of the original flowchart at the first check.
else{
moveForward();

//If there is nothing infront of the robot move forward

}
}

That' s it! We have now learned how to add obstacle avoidance capabilities to our robots!

Download full Source Code (Same as Yogy)

Note: Code uses newping library which can be dowloaded here

http://www.duino-robotics.com/obstacle-avoidance.html

5/5

Das könnte Ihnen auch gefallen