Trossen Dynamixel High Thread Banner

Tutorial: Following a Wall

  1. lnxfergy's Avatar
    lnxfergy lnxfergy is offline Mech Warfare Organizer Alumni
    Category
    How To, DIY
    Views
    14,962
    Replies
    0
     

    Following a Wall

    Difficulty
    Beginner
    Following a Wall
    Lots of people want to build a robot that navigates a house. I always suggest starting a little lower. Let's follow a wall. This tutorial will show you how to follow a wall with a single IR sensor, and two motors on the robot.

    Our Sensor

    We'll use an IR ranging sensor for following the wall. We're avoiding a sonar sensor, due to their wide beam width. See my tutorial on sensors for more about the comparison between IR and sonar sensors.

    Placement of the sensor is important. If the sensor is too far back on the robot, we might not actually be able to follow the wall. One of my early fire fighting bots had the IR sensors directly over the axle line and pointed straight at the side wall. This produced a problem, seen in (A) below:


    In diagram (A) the sensor is in-line with the wheel axles, or close to it. If we turn towards the wall, our sensor actually shows us a longer distance, and we turn more, until we run into the wall. In diagram (B), our sensor is angled out. This configuration, or one in which the sensor is quite far in front of the drive axles, is an important necessity to wall following.

    A Bang-Bang Controller
    While speed control of a motor typically requires fancy PID controllers, our wall following can use something simpler, called a Bang-Bang controller. The controller is called "bang-bang," because it is either on or off, it has no values in between. Our controller will steer away from the wall when too close to it, and steer towards the wall at all other times. If we do this fast enough, the robot will look like it is going straight along the wall. Since our IR sensor updates at about 30hz, we will try to update our motor speeds at the same rate. The image below, shows approximately what we are doing, and when our controller switches:

    An important point, is to make sure you adjust for any difference in your motors, try some experiments to find a set of values that actually steer to the left a bit, and a second set of values that steer to the right a bit.

    Our code will run on an Arduino, using my minibot and my motors and sharpIR libraries for the Arduino. I've attached a version of these libraries to this tutorial, but the latest libraries are always available on my SVN server.
    Code:
    // Wall Follow Demo - M. Ferguson
    // This sketch will follow a right side wall using a single IR sensor.
    
    // My motors/sharpIR library is available at svn.blunderingbotics.com
    #include <Motors.h>
    #include <SharpIR.h>
    Motors drive = Motors();
    SharpIR sensor = SharpIR(GP2D12,1);
    
    void setup(){    
        // drive is our motors, set takes left speed, right speed as params
        // we'll drive forward, and a little to the right (toward the wall)
        drive.set(160,140);
    }
    
    void loop(){
        // IR sensor returns distance in centimeters
        if(sensor.getData() < 17){
            // if we are too close, turn away
            drive.set(140,160);
        }else{
            drive.set(160,140);
        }
    }

    A Bang-Bang-Bang-Bang Controller
    Our controller above is nice, but it might not be enough, what if we get way too close, or way too far away? A typical extension is to add two extra states, one for each of the conditions. If our turn values are high enough, this can even let us turn around inside or outside corners.
    Code:
    // Wall Follow Demo - M. Ferguson
    // This sketch will follow a right side wall using a single IR sensor.
    
    // My motors/sharpIR library is available at svn.blunderingbotics.com
    #include <Motors.h>
    #include <SharpIR.h>
    Motors drive = Motors();
    SharpIR sensor = SharpIR(GP2D12,1);
    
    void setup(){    
        // drive is our motors, set takes left speed, right speed as params
        // we'll drive forward, and a little to the right (toward the wall)
        drive.set(160,140);
    }
    
    void loop(){
        // IR sensor returns distance in centimeters
        int val = sensor.getData();
        if(val < 14){
            // if we are way too close, turn away fast
            drive.set(20,200);
        }else if(val < 19){
            // if we are too close, drift away
            drive.set(140,160);
        }else if(val > 25){
            // too far away, turn towards wall more
            drive.set(130,80);
        }else{
            // default, drift towards wall
            drive.set(160,140);
        }
    }
    This video shows the bang-bang-bang-bang algorithm running:
    [youtube]-syZjFakOoQ[/youtube]

    Coastal Navigation

    Typically, even high-end bots that are using sophisticated mapping and sensory may follow walls. They of course need a fancy name for it: coastal navigation. Sticking to walls is important for robots, as it's easy to get lost in the center of rooms, since most sensors are short-range.

    Resources
    Arduino motors, sharpIR libraries
    Attached Files
    • sensorPlaceA
    • sensorPlaceB
    • wiggle