PACERBOT Part 16 : Algorithm talk

PID is finally starting to work. Apparently the last issues were not entirely caused by bad tweaking but by “bad” coding as well.

Up until now, part of my code’s loop looked like that:

integral = integral + error;
derivative = error - lastError;
steerAngle = (error*Kp + 84) + integral*Ki + derivative*Kd;
lastError = error; 

This is how a PID algorithm looks from behind the scenes, nice but apparently not as good as the native PID Arduino library.

Since I have switched to using the library, the code’s loop looked like this:

pidInput = linePosition();


 steerAngle = 84 + pidOutput;


The LinePosition function provides the position of the robot over the line (from 1 to 7), and feeds it to the PID function, which outputs the change in angle the servo should undergo.

Much simpler.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s