This is another simple robot since light following. 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.
We'll use an IR ranging sensor for following the wall. We're avoiding a sonar sensor, due to their wide beam width. Though you can find sonar sensor as well as IR ranging sensor in techshopbd.com.
The IR range sensor that we'll be working with has only three pins on it:
Vcc - This is for the +5v source.
Gnd - Connect this pin to ground.
Vo - The output analog voltage that tells us distance.
We only have to worry ourselves with connecting the 1 pin from the sensor to the µC and the other two to Vcc and GND. The Vo pin from the sensor will connect to the PA0/ADC0 pin (Pin 20) on the AVR. This is an analog input pin which has the capability to convert analog-to-digital. This conversion will give us a digital value & tell us how far away any object seen by the sensor is.
The way that infrared range sensors work is by using infrared light. Two infrared leds are used, one as a transmitter and one as a receiver. The transmitter simply outputs like any normal led, since it is in the infrared spectrum we cannot see it with our eyes. If any infrared light bounces off an object the infrared receiver outputs a voltage seemingly proportional to the intensity of that light. The follow picture makes sense of that:
This shows us that there exists a certain range of voltage that we're interested in, namely: about 3v - 0.4v. These voltages tell us how far away an object is. In order to get a better understanding of what this really means check out the datasheet.
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 robots had the IR sensors directly over the axle line and pointed straight at the side wall. This produced a problem, seen 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:
There is also a pair of IR LED and phototransistor for detecting corner. This attached with the external interrupt 0 of the µC. Whenever there is a wall in front, interrupt occur, robot will move reverse, then move left and continue to follow wall.