# Extending the 3pi RC example

G’day gurus,
heh heh you know I’ve got a curly one for you if I start like that

I’ve been reading the code layout and tutorial on the PID line follower. I worked out you can adjust it to make the 3Pi work faster and quicker.
I also worked out this is the line that is where you make the adjustments ( I think )

int power_difference = proportional/20 + integral/10000 + derivative*3/2;

Unfortunatley the explanation that goes with it is not on a low enough level for me to get the guist of what to do to adjust them. Obviously you change the numbers and that would be simple, but I need to know what numbers change what. I think “proportional/20” is the motor speed, but that’s it.

I suppose I could just put random numbers in the slots but that ain’t learning, It’s just guessing.

Good news is, my second 3Pi arrived this arvo and when the red expansion board arrives, It’ll be set up for RC with wireless camera attached… when will the fun ever stop?

clear skies
Stephen

Hi Stephen,

The numbers you want to adjust are the constant multipliers of the proportional, integral, and derivative terms. In the example, these multipliers are 1/20, 1/10000, and 3/2, respectively. For the most part, coming up with good constants will be the result of a lot of trial and error, with starting values coming from an educated guess. I think the best way to learn really is to tweak the numbers one at a time and observe the result. A good starting point for the proportional constant is one that gives a reasonable power difference when you are a little bit off the line. Wikipedia also has a good article on PID.

- Ben

G’day Ben,
thanks for that. The link to wiki was excellent and explained a lot. Now my brain hurts and I’m running numbers in my sleep heh heh.

I’m building a larger track this w/end and will start working on the adjustments. I assume you adjust the parameters in the code, save as ‘what ever’ and load it to the 3Pi. Watch and take note of any difference and adjust again etc etc… till it runs flat out and dead accurate, or something close

I’ll keep the details and if it survives, I’ll post a vid with details.

thanks and clear skies
Stephen

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