Skip to content

Hold distance to wall

Diphthong edited this page Jul 26, 2015 · 3 revisions

One of the first steps to do with a new robot are the elementary distance-holding functions. If you want to navigate through a maze, you have to cut the task down to the simplest tasks. One of these are some functions to move the robot. In Rescue Maze, the maze consists of tiles with a length of 30cm. It is also rectangular. So, you simply have to make the robot move 30cm and make it turn 90° left or right (of course you can simply let it drive through, but this is way more complicated, escpecially if a map should be created easily). One thing to consider is the kind of driving mechanism. We used a simple differential drive (we had four motors (two on each side) but made only one site each controllable, it was the same like using only two motors, but so we had a deep point of gravity and way more power (nessesary to climp the ramp)). The alternative would have been mecanum wheels. With these awesome wheels the robot is able to move left or right without rotating, but they are way more expensive (as they are mechanically more complex and each motor has to be controlled seperately). Also, they only work properly on flat ground. If the ground is not even, in our opinion you may better use normal wheels (2015, they put tissues into the maze, a huge disatvantage to mecanum wheels).

So, to come to the point: How to simply hold the distance to the wall? As we started with a wall follower in 2012, we had a few experiences with this. If you want to follow a wall reliably, you need two distance sensors, located behind each other with a distance of at least 10cm. You can try to follow a wall with only one sensor, but this is more expensive to program as you have to consider the angle of the robot to the wall. So, with two sensors, you have two informations:

  • the distance of the robot to the wall
  • the angle of the robot to the wall

With these information, you can simply programm a nice regulator to calculate the speed for the two motors on each side. To get to the regulator: There are many types of regulators, like a simple P (proportional), PI (proportional and integral) or PID (proportional, integral and differential), but for our task with a not too fast robot and not too slow sensors, we only need a P regulator. We have to calculate the relative angle to the wall (like 0 means even to the wall, negative value or positive value not even in one or the other direction) and a relative distance (like 400 is the distance we want to keep from the wall). They may be unitless, but should be linear (if you use the old generation Sharp distance sensors like GP2D120 or so, you have to linearize them, this may take some time until you get good results). Now, we somehow have to calculate the distance and the angle to a speed of the left and right motor. Let's first think about the result we want to get from our regulating algorithm for the motors.Let's define that 0 means we can drive straight with full power. The lower or higher the value gets, the more the robot has to move left or right. As pseudo code:

SPEED_LEFT = MAXIMUM_SPEED - STEERINGVAL;
SPEED_RIGHT = MAXIMUM_SPEED + STEERINGVAL;

where

SPEED_LEFT = speed of left motor(s)
SPEED_RIGHT = speed of right motor(s)
MAXIMUM_SPEED = maximum possible speed if the robot does not have to corrigate left or right
STEERINGVAL = result of algorithm above

Now to the part where we calculate the STEERINGVAL. First of all, how do regulators work? We have always an input value (is) and one regulator value (this is the value we want to regulate to). Together with a constant regulator factor, the difference of these two is the whole regulator:

STEERINGVAL = (DIST_TO - DIST_IS)*FACTOR;

where

DIST_TO = distance we want to go to
DIST_IS = current distance

The factor is not given and depends on the whole robot. You just have to try out. But we have two regulators - how to bring them together? We solved this problem by simply adding the two regulator values.

The whole pseudo-code then looks like this:

STEERINGVAL = (0 - (DISTANCE_BACKSENSOR - DISTANCE_FRONTSENSOR)) * FACTOR_A; //For the angle
STEERINGVAL = STEERINGVAL + (DIST_TO - DIST_IS) * FACTOR_B;
SPEED_LEFT = MAXIMUM_SPEED - STEERINGVAL;
SPEED_RIGHT = MAXIMUM_SPEED + STEERINGVAL;

As said, you need to figure out the factors by yourself as this depends on your robot. One thing we can say is that on our configuration, the factor for the distance is 0.6 * factor for the angle. This means the robot has a higher priority on aligning to the angle than to the distance, but the distance is still important.

You can find this in drive.c line 131

Here is a diagram showing the connection between our driving functions:

Clone this wiki locally