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.