Re: follow the line issue
A PID controller
consists of three different terms that contribute to the control signal
(output), Proportional, Integral, and Derivative (which is where the name PID controller comes from). The amount that each contribute to the output is controlled by three scaling factors that are usually called KP, KI, and KD, respectively.
The code that MHTS posted implements just the Proportional controller, as the control signal is a scaled version of the error signal. You can see the KP defined constant in the code. The error signal
is the difference between where you want the measured value (such as a sensor reading) to be (this desired value is called the setpoint
) and where it actually is right now. The Integral controller uses a scaled version of the error that has been summed over time (if you have a calculus background, it's actually the time integral of the error signal). The Derivative controller uses a scaled version of how much the error signal has changed recently (again, with calculus, this is actually the time derivative of the error signal).
If you're interested in implementing a full PID controller instead of just the P controller that MHTS provided, there's some pseudocode in the Wikipedia article I linked above, or you can check out a great document called "PID Without a PhD