Extending the 3pi RC example

Pretty much. The integral term isn’t very important when it comes to line-following, so I’d recommend you focus just on the proportional and derivative terms (you can make the integral constant 0). The most important term for you will be the proportional term, so you might try adjusting that one first (with derivative constant set to 0) and then add in derivative later. Before you start, try making an educated guess as to approximately how big the proportional term should be. You can do this by noting that the proportional constant will convert your number from a sensor reading into a motor speed. For example, if the possible range of sensor readings is -2000 to 2000 and the possible range of motor power differentials is -255 to 255, you might want to start with a proportional constant that is closer to 1/10. If you use one that’s much smaller than this, such as 1/100, the maximum power differential your 3pi will ever encounter will be 20 out of a possible 255, which most likely won’t be enough to keep it on the line when it encounters a sharp turn. If you use a proportionality constant of 1, your 3pi will max out its power differential when it’s only slightly off the line, which will cause it to swerve violently and will almost certainly lead to unstable behavior.

Something else to consider is that the derivative term is the difference between the last two errors, which makes it typically much smaller than the proportional term, so you will need a much larger derivative constant in order for the derivative term to have any meaningful affect on the motor power differential.

Note that the sample line-following code we provided works, especially at slower speeds, but it is deliberately unoptimized so that there is room for improvement. You can use those values as safe starting points, but don’t be surprised if the best values are several times larger or smaller. The actual ideal constants will depend on the 3pi’s maximum speed and how sharp the turns are on your line course.

Good luck; let us know how it goes!

- Ben