I have a task that involves implementing robot behaviour that will follow wall and avoid obstacles along it's path. The robot must stay at desired distance from the wall but also stick to it so it should not loose sight of it. Robot is sensing it's surrounding with ultrasonic sensor that is oscillating from left to right and filling an array of small length (10 values) with detected distances (every 10 degrees). From this reading I would like to calculate heading vector that will result in robot path similar to one shown in bottom picture:

Black(walls), red(obstacles), blue(robot), green(desired path)

• I recommend looking at using artificial potential fields. A colleague and I published some work on this topic, see his thesis here. – Brian Lynch Nov 30 '15 at 20:18

It sounds like you want some sort of bug algorithm.

But the pseudo-code for what you want is simple:

while not at goal:
if there is a wall in front:
turn right a little
else if there is a wall on the left:
drive straight a little
else:
turn left a little
stop


Of course tuning this thing can be tricky. For example, you probably don't want to turn in place when you see no wall. You should drive forward and left in an arc. The specifics of which depend on your robot and environment.

Also, because you have a spinning ultrasonic sensor, you should not drive the robot too fast.

Note that the pseudo code really only needs a single sensor on the left of the robot and in front of the robot. But since you have an array of range values, you can potentially do something more sophisticated, although more complicated. For example, use the few range readings on the left side of the robot to determine the distance and angle of the wall relative to the robot, and act accordingly.

• Thanks Ben, I tried to implement your solution but my readings were not enough consistent and accurate because of spinning sensor, so I made my sensor static. The robot followed the wall but movement is not smooth so I need to make some more improvements. I tried to implement PID for wall following, but it is much harder to tune it in comparison to PID for line following. – Denis Vitez Dec 7 '15 at 11:02
• Welcome to the world of robotics! :) – Ben Dec 7 '15 at 14:30