Obstacle Avoidance - Duino-Robotics

Embed Size (px)

Citation preview

  • 8/10/2019 Obstacle Avoidance - Duino-Robotics

    1/5

    Obstacle Avoidance Tutorial

    Obstacle avoidance is a means of a robot being able to move around in an unknown environment whil st not

    coll iding 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 skil l to have, whether for a l it tle robot project, or a larger project where obstacle avoidance is just a

    small pieceof 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.

    Learn Talk Build Grow Store

    Last updated 16/09/13

    Obstacle Avoidance - Duino-Robotics 03/11/14

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

    http://www.duino-robotics.com/learn.htmlhttp://www.duino-robotics.com/talk.htmlhttp://www.duino-robotics.com/build.htmlhttp://www.duino-robotics.com/http://www.duino-robotics.com/about.htmlhttp://www.duino-robotics.com/store.htmlhttp://www.duino-robotics.com/grow.htmlhttp://www.duino-robotics.com/build.htmlhttp://www.duino-robotics.com/talk.htmlhttp://www.duino-robotics.com/learn.htmlhttp://www.duino-robotics.com/
  • 8/10/2019 Obstacle Avoidance - Duino-Robotics

    2/5

    Alright, so fi rst of all let' s look at the flowchart. This flowchart is a li ttle different (not so Arduino specific) but the

    idea behind it i s sti ll the same.

    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 thi s example I wil l be assuming that you already know how continuous rotat ion 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 fi rst thing that I always do when making robots is to make the robot move forward. Getti ng 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 l ines of moveForward.

    voidmoveForward(){

    leftServo.write(0);

    First of all we start off 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 first 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

    li ke you should when you cross the road. First the robot

    turns left, takes a reading, turns right, takes a reading.

    Obstacle Avoidance - Duino-Robotics 03/11/14

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

    http://www.duino-robotics.com/arduino-tutorials.html
  • 8/10/2019 Obstacle Avoidance - Duino-Robotics

    3/5

    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, j ust 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 r ight motor to move backward. Left, right motor forward, left motor backward.

    voidmoveBackward(){

    rightServo.write(180);

    leftServo.write(0);

    }

    voidmoveRight(){

    rightServo.write(0);

    leftServo.write(0);

    }

    voidmoveLeft(){

    rightServo.write(180);

    leftServo.write(180);

    }

    voidmoveStop(){

    rightServo.write(90);

    leftServo.write(90);

    }

    I put all these voids below the void loop so that they aren't in the way.

    I al so l ike 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 wi ll change, so I won't put in code for this part. I wil l simply say that I have a void routine for

    scanning the di stance 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.

    Obstacle Avoidance - Duino-Robotics 03/11/14

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

  • 8/10/2019 Obstacle Avoidance - Duino-Robotics

    4/5

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

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

    intrDistance; //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 i s 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!

    This may look like a jumble of code, but i t really isn't, OK i t i s, but we can understand i t by looking at i t bi t by bit .

    1)

    2)

    3)

    4)

    5)

    6)

    7)

    moveBackward(); //Move Backward for a second

    delay(1000);

    moveRight(); //Turn Right for half a second

    delay(500);

    moveStop(); //Stop

    scan(); //Take a reading

    rDistance = distance; //Store that to the distance on the right side

    moveLeft();

    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(); //Move right for a second

    delay(1000);

    moveForward(); //Then move forward

    }

    else{

    moveForward(); //This happens if left is larger than right

    }

    }

    Obstacle Avoidance - Duino-Robotics 03/11/14

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

  • 8/10/2019 Obstacle Avoidance - Duino-Robotics

    5/5

    1) Once an object has been detected the robot then begins to move backward, i t 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. I f 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 i n front. Remember how we looked out how the curly brackets held al l the statements in for that check, well

    this is at the end of that check. The else represents the 'no side' of the original fl owchart at the first check.

    else{

    moveForward(); //If there is nothing infront of the robot move forward

    }

    }

    That' s i t! We have now learned how to add obstacle avoidance capabiliti es to our robots!

    Download full Source Code (Same as Yogy)

    Note: Code uses newping library which can be dowloaded here

    Obstacle Avoidance - Duino-Robotics 03/11/14

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

    http://www.weebly.com/?utm_source=internal&utm_medium=footer&utm_campaign=3http://www.weebly.com/?utm_source=internal&utm_medium=footer&utm_campaign=3https://code.google.com/p/arduino-new-ping/http://www.duino-robotics.com/uploads/2/2/9/8/22988902/yoghurt_tub_bot_basic_obstable_avoidance_code.ino