How to Build a Wall-Following Robot - LEKULE BLOG

## Sunday, 31 January 2016

### Overview

This is part 6 of a series of articles on my experiences building a robot that can do various things. I thought it would be neat to create a robot that was easy to put together with a single soldering iron and was also affordable. I made up the following requirements for my robot:
• Many kits are expensive, so it must be relatively inexpensive.
• It must be easily put together without special equipment.
• It must be easily programmable without a complicated IDE or programmer.
• It must be powerful enough for expandability.
• It should run off a simple power source.
• It should be able to follow a line or a wall, and avoid obstacles.

### Following Walls

In order to follow walls, you need at least two sensors (2 bits of information) to handle the four potential situations the robot could be in. One sensor has to be in the front, and the second could be on the left or right of the robot. The more sensors you use, the more information you have, so you can make better judgements about what is going on. For this example, I just used two. The robot cannot find the wall, so you have to place the robot next to the wall. If you placed it in the middle of the room, it would just drive in circles.

#### Truth Table

Front Sensor Right Sensor Situation Action
Off Off Robot is driving away from wall. Come back to wall, turn right.
On Off Robot is away from wall but headed towards a wall or obstacle. Turn hard left to get back parallel with the wall.
Off On Robot is following the wall. Drive forward.
On On The robot is at a corner. Turn hard left.
In order to work, I had to add code to turn hard left. Hard left just means I only turn on the right wheel so the robot basically turns in place rather than continue to move forward while turning. I couldn't just turn slowly like the line follower because you have no idea how close the robot is to the wall. This is a limitation of the sensor I chose, because the sensor reflects differently based on the surface. Additionally, the logic is set up to only be binary because there is no way to tell the distance based on the sensor. If you knew distance, you could add additional logic to vary the speed based on the distance to make the robot travel around the room faster. I actually couldn't get the sensor to reflect at all off a black surface, so in the video you'll see I had to put a white surface in front of the dishwasher. An audio based sensor would not have this problem.

### Programming

The following sketch performs the logic:
• Start the robot driver and wait 5 seconds. This gives you time to put the robot next to the wall before it starts moving.
• Implement the truth table above using if statements.
• Each statement executes the action associated with the sensor configuration.
• I had the robot turn right faster than normal so that the robot can turn around corners more sharply. Most wall corners are 90 degrees so turning faster makes sense.
##### robot_wall_follower

```                    ```#include "robot.h"

void setup()
{
Serial.begin(38400);
Serial.println("Boot");
rbt_init();
delay(5000);
rbt_move(FWD,100);
}

uint16_t lleft,lmid,lright;
boolean wleft,wmid,wright;
uint16_t avoid_count=0;
void loop()
{
rbt_sns(&lleft,&lmid,&lright,&wleft,&wmid,&wright);
/*if the wall is sensed, go forward
* the wall is sensed if the right sensor is on but the mid
* sensor is off.
*/
if(wright && !wmid)
{
rbt_move(FWD,100);
}
/*likely going towards the wall
* not sure how close so turn as fast
* as we can
*/
if(wright && wmid)
{
rbt_move(HARD_LEFT,100);
}
/*going away from the wall
* slowly turn back towards the wall
*/
if(!wright && !wmid)
{
rbt_move(RIGHT,130);
}
/*likely at a corner or coming in at an angle to the wall*/
if(!wright && wmid)
{
rbt_move(HARD_LEFT,100);
}
}
```
```

Changes required for hard left to robot.h and robot.
##### robot.h
Add HARD_LEFT to the enum for direction.

```                    ```/*robot interface*/
typedef enum{
LEFT,
RIGHT,
FWD,
REV,
BRAKE,
HARD_LEFT,
}direction_t;
```
```

##### robot.ino
Add a case for HARD_LEFT in rbt_move().

```                    ```case HARD_LEFT:
digitalWrite(BPHASE,MOTOR_FWD);
digitalWrite(APHASE,MOTOR_FWD);
analogWrite(AEN,speed);
analogWrite(BEN,0);
break;```
```

### Conclusion

In this article, I showed how you might use the proximity sensors to follow walls in order to navigate around a room. This concludes the series of articles on making a robot! The robot is able to autonomously follow a line, a wall and avoid obstacles. Can you combine them into a single robot that does it all? You can also take control by adding a BLE module and control the robot from your phone!