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 :
- lobodol & YMFC ignoring the time constant. how does it effect the pid calculations
- 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