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. |
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.
- Read the sensor
- 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;
No comments:
Post a Comment