转载自:http://forums.trossenrobotics.com/tutorials/how-to-diy-128/following-a-wall-3283/
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.
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.
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.