PID implementation

Hi,

I am looking at code references for simple PID implementation.

these are the few implementations

from : YMFC Flight Controller

  pid_error_temp = gyro_pitch_input - pid_pitch_setpoint;
  pid_i_mem_pitch += pid_i_gain_pitch * pid_error_temp;
  if(pid_i_mem_pitch > pid_max_pitch)pid_i_mem_pitch = pid_max_pitch;
  else if(pid_i_mem_pitch < pid_max_pitch * -1)pid_i_mem_pitch = pid_max_pitch * -1;

  pid_output_pitch = pid_p_gain_pitch * pid_error_temp + pid_i_mem_pitch + pid_d_gain_pitch * (pid_error_temp - pid_last_pitch_d_error);
  if(pid_output_pitch > pid_max_pitch)pid_output_pitch = pid_max_pitch;
  else if(pid_output_pitch < pid_max_pitch * -1)pid_output_pitch = pid_max_pitch * -1;

  pid_last_pitch_d_error = pid_error_temp;

from : GitHub - lobodol/drone-flight-controller: A quadcopter flight controller based on Arduino Uno

 error_sum[PITCH] += errors[PITCH];
 deltaErr[PITCH] = errors[PITCH] - previous_error[PITCH];
 previous_error[PITCH] = errors[PITCH];
 pitch_pid = (errors[PITCH] * Kp[PITCH]) + (error_sum[PITCH] * Ki[PITCH]) + (deltaErr[PITCH] * Kd[PITCH]);

from : How is the PID constants Kp KI Kd calculated? - #3 by zhomeslice - Project Guidance - Arduino Forum

 double PTerm = kp * error;
  integral += error * (double) (timeChange * .000001);
  ITerm = ki * integral;
  // Derivative term using angle change
  derivative = (input - lastInput)  / (double)(timeChange * .000001);
  DTerm =  (-kd * derivative);
  //Compute PID Output
  double output = PTerm + ITerm + DTerm ;

and brettbeauregard - Improving the Beginner’s PID

void Compute()
{
   /*How long since we last calculated*/
   unsigned long now = millis();
   double timeChange = (double)(now - lastTime);
  
   /*Compute all the working error variables*/
   double error = Setpoint - Input;
   errSum += (error * timeChange);
   double dErr = (error - lastErr) / timeChange;
  
   /*Compute PID Output*/
   Output = kp * error + ki * errSum + kd * dErr;
  
   /*Remember some variables for next time*/
   lastErr = error;
   lastTime = now;
}

can any one give me explanations for following :

  1. lobodol & YMFC ignoring the time constant. how does it effect the pid calculations
  2. YMFC code the i term is
  pid_i_mem_pitch += pid_i_gain_pitch * pid_error_temp;

why he multiplying with error ?
labodol is just adding the previous error with present error and other two are multiplying it with time change

anilkunchalaece:

  1. lobodol & YMFC ignoring the time constant. how does it effect the pid calculations

"Time constant" has a specific well defined meaning in control theory that comes into play when designing / implementing / tuning a control strategy.

Did you mean to write "time difference" or "time change" or "delta time" or any other phrase used for a change in time?

Yes.

  pid_i_mem_pitch += pid_i_gain_pitch * pid_error_temp;

is equal to

  pid_i_mem_pitch = pid_i_mem_pitch +( pid_i_gain_pitch * pid_error_temp);

Multiplication done first then the addition

anilkunchalaece:
is equal to

Yes.

Any idea why is he doing like that ?

isn't it supposed to be

  pid_i_mem_pitch += pid_error_temp
pid_i_mem_pitch  *= pid_i_gain_pitch

No because that would multiply the current pitch position. If the quadcopter is at 1 degree tilt then it would be 1gain. If it was currently at 45 degrees then it would be 45gain. You don't pitch 45 times harder because you're currently at 45 degrees.

Note that the real calculations are done in radians, not degrees, but it's usually easier for humans to think in degrees.

MorganS:
No because that would multiply the current pitch position. If the quadcopter is at 1 degree tilt then it would be 1gain. If it was currently at 45 degrees then it would be 45gain. You don't pitch 45 times harder because you're currently at 45 degrees.

I didnt understand.

What it should be done is Add the PrevError with PresentError and Multiply with the I-Gain

But what the code means (i think)

Multiplying the PresentError With I-Gain and adding it to the PrevError