I'm writing a PID to control a toy car that follows a black line on a circuit. I've tuned my PID and it works at high speed for all the circuit except the winding section. For that, the error signal looks like a sine wave, and the toy car steers too much. I would like it to go close to straight, is it possible?

Edit: My car sees 100 grey points in a line ahead, and the difference between the darkest point and the middle of the visual range is the error signal. My output is the angle of a servo on the front wheels of the car, while the speed of the back motors is constant.

The desired performance would be to oscillate with an amplitude less than the amplitude of the winding road, and the actual performance is that the car steers close to the sine line for one period, and at the next max amplitude it under steers. Sorry, I can't provide graphs right now but I'll try to add some in the next days.

Is there a formula for adjusting the PID constants for the desired PID bandwidth?

  • 1
    $\begingroup$ What do you mean "the car steers too much"? It's the nature of a given PID controller that it has a certain closed loop bandwidth. If you exceed that bandwidth the PID will not be able to track the error. To improve the bandwidth you need to either improve your tuning or improve your machine... $\endgroup$
    – Guy Sirton
    Commented Mar 6, 2014 at 20:42
  • $\begingroup$ Can you post some example graphs of desired vs actual performance? Also, is your PID controlling the angle of the steering wheels or some other actuation method? $\endgroup$
    – Ian
    Commented Mar 6, 2014 at 21:18
  • $\begingroup$ There is no formula for adjusting the PID for a given closed loop bandwidth because the closed loop bandwidth is a combination of the open loop bandwidth and the PID controller. To illustrate, if your car isn't even physically capable of tracking the line no PID will make it track. $\endgroup$
    – Guy Sirton
    Commented Mar 8, 2014 at 5:29

2 Answers 2


One observation is that this is a slightly different situation than the classical control problem. Your sensor is looking ahead so you're not measuring the current (or past error) but rather you're measuring where you want to position that car in some future time. As an example, consider that your car currently has its wheels turned such that it is following a given arc to the right. Your sensor tells you that the target line is somewhat to your right. It is quite likely that you do not need to give any additional control input to hit your target and attempting to keep correcting to the right will over-steer the car.

To get the best tracking performance you need to figure out how the car drives in open loop mode. Think about it as a race car driver approaching a turn, he doesn't correct his current error as he's going through the turn, he plans his approach and only correct the tiny errors left as he is taking the turn. Your perfect driver will need very little correction as he takes into account all the parameters ahead of hitting that turn.

From a control perspective this part of your algorithm is known as feed-forward and is the most important part in getting your car to perform.

With the right feedforward in place you will still need a PID controller to take out any residual error. One problem you have though is actually figuring out what to use as an error signal. The simplest choice is to use that signal but you can probably do better. There are a number of questions that discuss some PID tuning basics and you can refer to those (e.g. What are good strategies for tuning PID loops? ).


From An Introduction to Loop-Shaping Design for PID Control:

any constant gain feedback will act as a stabilizing controller when the loop is closed. This constant gain is simply a proportional controller. The gain raises or lowers the open-loop magnitude response hence setting the cross-over frequency.


When the loop is closed the cross-over frequency is the bandwidth of the closed-loop.

So you would run your loop in Proportional only mode over that section of track. Tune your P term until you achieve the desired performance. You will then need to add back in your I and D terms and tune over the rest of the track.

It may turn out that the P term chosen to achieve the desired performance over the winding section of track produces an unacceptable response for the rest of the track. In which case it may be best to simply place a low pass filter in front of your loop and not pass any errors that are above a certain value.

  • $\begingroup$ If his controller remains stable as he raises the proportional gain then the control loop will perform better on all sections. More likely as he raises his gain he will be reducing his gain margin until at some point the system will be unstable and likely be so over all parts of the course. What the OP is asking for is not possible in general- You can't tune a PID to get any bandwidth you need. Your achievable bandwidth is limited by your system design. An interesting twist here is that the car is looking ahead. $\endgroup$
    – Guy Sirton
    Commented Mar 8, 2014 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.