# Making a tunnel follower robot with 2 IR side sensors and 1 ultrasonic sensor

We have a tunnel and the distance from the walls to the robot is about 30cm when the robot is centered, the tunnel curves to the right. The robot has an infrared sensor on its left and one on its right and has an ultrasonic sensor at its front (so basically its eyes). How can I make the robot turn appropriately while its moving so that it's centered in the tunnel. I just need an idea for this algorithm because I can't find any. All what the infrared sensors can do is measure the distance to the walls.

• Walk slowly down a hallway at your house, or at your school. ... Think about what the robot does. ... Measure distance to each wall. ... What do you do with your motors if the distance is too small? ... Write it down in a flowchart form. Feb 12, 2018 at 16:42

One possible method is to find the width of your robot, subtract that from 30 cm, then half the remaining distance and if one of the IR sensors reads less than that number, turn in the other direction.

I agree with @NikolaPi in principle. But the controlling/driving mechanism is important here. If you are using PID controlling, then steering the robot can be very smooth. All you have to do is to tune the variables so that when robot starts getting closer to lets say, left wall, left motor's pwm increases w.r.t. the right one and vice versa. This way the robot will remain in an imaginary boundary/lane that is slightly wider than the robot but significantly smaller than the Tunnel's width. But the tuning will take a little effort to find the sweet spot where the correction neither occurs too often nor too late (Hint: You robot should move almost straight without compromising on speed. If its is moving in the form of the letter "S", your limits are too generous).

On the other hand, if you are using the simpler On/Off Controlling technique (also known as band bang approach), you take a limit of IR sensor reading from one wall and program according to that. If it crosses the limit, stop the other sides motor for some time so that the robot comes back to its center position. But beware that in this approach, with the battery voltage drop, the response from the motors changes. What I mean is that if you tune the robot to switch off motor for 100 ms in case of limit detection and it comes back to the good position, when the battery would get drained a little, the robot would need 130 or 150 ms of switching off to make the same correction. Of course it won't be able to as you have set it to 100 ms so there will be an error in the robots's position every time an adjustment is made. Eventually, these errors will pile up and the robot will collide with a wall.

If I understand correctly, all your sensors look ahead, but you want safety to the sides.

Maybe it would be better to turn the IR sensors 90deg so they check the distance to the side walls, while you keep the ultrasound sensor to look ahead.

Using the data from the three sensors, you should be able to create a limited map of the robot inside the tunnel, which will be the basis for controlling the movement.

Ahmed talked about PID (proportional integral differential, which is very complicated) and bang-bang control (which is too simple and often over-corrects). I would recommend proportional control and an example of this is on: https://www.instructables.com/How-to-Make-a-Robot-Car-Drive-Straight-and-Turn-Ex/ The website gives an example of a car trying to drive straight using a gyroscope on a flat surface, which is similar to your tunnel situation.

Proportional control is much simpler than PID, since you only need to change tune one variable, instead of three in PID. If you want a bit more complexity, you can try proportional integral control, which uses two variables.

Additionally, I am skeptical about the ultrasonic sensor, since the tunnel might form an echo chamber, creating a lot of noise interference.

You can use a simple constant step controller that does not need tuning like a PID controller. A step controller will simply start adding or subtracting a constant control value based on the input signal.

In this case the control signal involves steering. If you are too close to the left wall, then start turning right; if you are too close to the right wall then start turning left. What 'turning' means depends on your robot. If you are using a differential drive configuration then turning means slowing one wheel down and speeding the other up; the turn will be in the direction of the slower wheel. If you are using an car-like Ackermann steering, then just change the angle of the steering servo.

The input signal is the difference in distances to each wall (there would be a separate controller for the forward distance to stop the robot). We use the difference because we just want to know which wall we are closer to (and so should move away from). This handles the fact that the robot will often not be driving parallel to the walls, so the distance to the walls as measured by the sensor may be greater than the actual distance because the sensors are at an angle to the walls.

So if we use (left - right) distance, then a negative value indicates we are closer to the left wall, a positive value indicates we are closer to the right wall. We can use some zero-tolerance to treat very small differences as zero.

If we are within the zero-tolerance, do nothing. If we are closer to the left wall, then add a small constant (the step) to steering. If we are closer to the right wall then subtract that small constant from steering. What that step value is and it's sign and how it's actually applied (how it's actuated) will depend on your robot.