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;
steerServo.write(round(steerAngle));
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();

pid.Compute();

 steerAngle = 84 + pidOutput;
steerServo.write(round(steerAngle));

 

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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s