It's been said that an infallible way to solve a walk-through maze is to touch the wall to your right with your hand and then walk through the maze without ever losing touch with that wall to your right. I'm not really sure that that rule works, but let's try to make our robot do that anyway, in other words advance while always staying close to (but without touching) a wall.
It's not very difficult to do this using two ultra-sonic sensors. You would use one sensor to measure the distance to the wall at the robot's right, adjusting the steering to try to keep this distance constant. The other would be looking forwards in case there is a corner in the wall - if this sensor detects an imminent collision then the robot will have to veer left. Check out this video:
But most people don't have two ultrasound sensors (actually most people don't even have one since none is included in the retail version of the EV3 - I don't know whether the IR sensor can be used instead). So let's make it more challenging by trying to follow the wall using a single ultrasound sensor.
I did manage to get this working but was amazed and frustrated at how long it took. There's nothing wrong with the program below (also downloadable using the link at the bottom of the page) - is just tells the robot to turn left if the it gets too close to the wall (which is assumed to be to the right of the robot) and turn right if the robot wanders too far away from the wall. More exactly, it tells the robot to turn left if it is true that the distance reported by the ultrasonic sensor is less than 40cm and turn right otherwise.
By pointing the sensor forwards as well as sideways, at an angle of 45°, I was able to make the robot capable of turning left as it approached a corner in the wall. For that, it's helpful to keep a decent distance from the wall (40cm) and have a small turn radius for the left turn (that's why my program makes the robot turn more sharply left than right). This program also allows the robot to turn correctly around an 'obtuse' corner in the wall.
So how could I have had so much trouble making this program work? I think it is because for a long time I tried to keep the robot much closer to the wall (about 10cm) and the sensor actually seems to work better when it's further away. I also got the impression that the smooth plastered wall I was using did not work as well as a transparent plastic wall in my house - a nice reminder that just because a surface is transparent to light doesn't mean it is transparent to sound. Problem: having the robot keep so far away from the wall means my program wouldn't work well inside a typical maze where the walls are close together. Solutions, anyone?
Can you make a wall-follower that works with the IR sensor?
I'm glad I had so much trouble with this project because it made me understand better why programming robots is different to normal programming - it's the unpredictable behavior of real-world objects and especially the sensors that make it more challenging and interesting.
See also this page.