Beruflich Dokumente
Kultur Dokumente
Learn
03/11/14
Talk
Build
Grow
Store
http://www.duino-robotics.com/obstacle-avoidance.html
1/5
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.
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
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();
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
03/11/14
int fDistance;
int lDistance;
int rDistance;
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;
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){
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.
delay(1000);
1)
moveRight();
delay(500);
2)
3)
moveStop();
//Stop
scan();
//Take a reading
rDistance = distance;
moveLeft();
4)
5)
delay(1000);
moveStop();
//Stop
scan();
//Take a reading
lDistance = distance;
moveRight();
6)
delay(1000);
moveForward();
}
else{
moveForward();
7)
}
}
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
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();
}
}
That' s it! We have now learned how to add obstacle avoidance capabilities to our robots!
http://www.duino-robotics.com/obstacle-avoidance.html
5/5