# Keeping two wheeled wall following robot straight

I have a two-wheeled (two DC motors) robot that needs to follow the wall beside the robot.

The issue is that the DC motors spin at different rates (because they are not identical, of course), so the robot does not go straight when the same voltage is provided to each motor.

How can I use IR distance sensors (and op-amps) to keep the distance from the wall constant?

(the robot must travel parallel to the wall)

You can use the principle of proportional error control. Do the IR sensors have an analog output? If so, you can build an entirely analog control loop using op amps. If not, it's probably easier to use a microcontroller to do the control digitally, with a pair of motor controllers to handle the output.

Either way, you want to arrange that the commanded motor speeds are as follows:

$$V_1 = V_{nom} + k_p (d - d_{nom})$$ $$V_2 = V_{nom} - k_p (d - d_{nom})$$

Where $V_1$ and $V_2$ are the commanded motor speeds or voltages, k_p is the proportional gain (this can be adjusted to trade off responsiveness and residual error against stability), d is the measured distance from the wall and d_nom is the nominal distance.

More complicated control loops could involve integral and derivative terms; the latter could be implemented by using distance sensors at the front and rear of the vehicle so that their difference is proportional to the rate of approach or closure. But I think a simple proportional controller as describe should work just fine.

• Thanks! The IR sensors do have analog output, so the control loop using op amps is ideal. Just a couple of follow-up questions; How do I calculate k_p? How should I set up the op-amp circuits to get the k_p*(d+-d_nom) terms? – Jack C Nov 13 '14 at 18:18
• Haha, someone migrated this to the Robotics SE but those follow-up questions are definitely more suited to the electronics SE, particularly since the EE SE has an inline schematic editor. Perhaps you could post your second follow-up as a standalone question there ("How can I implement a proportional error control scheme with op amps?") As for choosing k_p, it's difficult to say since its units are somewhat weird (volts per meter) and its value will depend on the sensor and motor characteristics, and the vehicle dynamics. Best to start small and make it adjustable. – pericynthion Nov 13 '14 at 18:24
• Thanks for the help! Would upvote but no reputation :P – Jack C Nov 13 '14 at 18:25

Looks like you could use PI or PID regulator (see my answer there).

The P-regulator (proportional) suggested by pericynthion should work, but you have to tune it well and won't be same for another similar robot if the difference changes. PI regulator can compensate on-the-fly and deduce the error. D-part can be used for fine-tuning.

Use the distance (measured by IR-sensor) as a feed for PID regulator and desired distance as the input. Output will be the difference of the regulation for the two motors (actually the Kp from the answer you got from pericynthion).

What I suggest to you is to use a PI or PID regulator for each motor like pericynthion said, but I'm afraid is not sufficient to keep your robot stright, so you need to keep the the two wheels at the same speed and synchronized, for example you can implement odometery in your UC.

• The input to the regulator is not the motor's speed, it's the distance from the wall. So the control loop can be closed around that without the need for odometry. – pericynthion Nov 13 '14 at 22:24