PID Routine

PID Routine

I designed a electro-mechanical platform to keep a camera level when mounted on a vehicle for my senior capstone project at UMass Lowell in 2010. The project used voltage controlled linear actuators, done to simplify control, as the rate of lead-screw translation was proportional to the input voltage. The motor drivers would provide the voltage, I merely needed to set the desired value. A PID routine was used to compute this value.The PID equation is:

\[V_{out}(t)=P \cdot e(t)+I \cdot \int_0^t e(\tau)d\tau + D \cdot \frac{d}{dt} e(t)\]

Variable Description
\(V_{out}\) Output voltage to the motor
\(P\) Proportional Gain
\(I\) Integration Gain
\(D\) Derivative Gain
\(e(t)\) Measured Error

The gains were determined experimentally using the full platform at the start of the testing phase.

The coefficients are proportional, integration, and derivative hence PID. The error was the difference between the most recent measurement and a set point. The set point could be any angle but for this project it was the value needed to maintain zero pitch and roll. The PID routine returned the value for \(V_{out}\) used to drive the motors. When pitch and roll are are not changing this value should be zero.Selected source code for the PID algorithm and main loop is provided below. There were two separate PID instances, one for platform pitch and one for roll.

double dt;     //Time Step//Gains set to equal constants
double P;      //Proportional Gain
double I;      //Integration Gain
double D;      //Derivative Gain
double error_prior = 0.0;
double error_sum = 0.0;
double update_pid(double error_current)
{
   double retval;
   error_sum += error_current;
   retval = (
       P*error_current +
       I*error_sum*dt +
       D*(error_current - error_prior));
   error_prior = error_current;
   return retval;
}
⋮
int main() {
⋮
   double error;
   while(1) {
       //pitch axis
       error_pitch = get_pitch_measurement();
       set_motor_pitch_speed(update_pitch_pid(error_pitch));

       //roll axis
       error_roll = get_roll_measurement();
       set_motor_roll_speed(update_roll_pid(error_roll));
   }
⋮
}

The PID algorithm was tuned experimentally once the platform was completed. Through tilt tests the platform  pitch and roll was altered and the gains were adjusted until it maintained zero pitch and zero roll for the camera mount.The final platform could have been tuned better, however the final platform did a reasonable job staying level.

Sensor Board Control Board