Definition in file pid.h.
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Data Structures | |
struct | PID_DATA |
PID Status. More... | |
Defines | |
#define | FALSE 0 |
#define | K_D 0 |
#define | K_I 0 |
#define | K_P 1 |
#define | MAX_INT 32766 |
#define | MAXERROR 32000 |
Maximum error values. | |
#define | SCALING_FACTOR 128 |
#define | TIME_INTERVAL 157 |
#define | TRUE 1 |
Functions | |
void | Init_PID (int p_factor, int i_factor, int d_factor, struct PID_DATA *pid) |
Initialisation of PID controller parameters. | |
int | PID (int setPoint, int processValue, struct PID_DATA *pid_st) |
PID control algorithm. |
|
Definition at line 79 of file pid.h. Referenced by main(). |
|
Definition at line 34 of file pid.h. Referenced by Init(). |
|
Definition at line 32 of file pid.h. Referenced by Init(). |
|
The K_P, K_I and K_D values (P, I and D gains) need to be modified to adapt to the application at hand Definition at line 30 of file pid.h. Referenced by Init(). |
|
Definition at line 76 of file pid.h. Referenced by Init_PID(), and PID(). |
|
Maximum error values. Needed to avoid sign/overflow problems |
|
Definition at line 36 of file pid.h. Referenced by Init(). |
|
Specify the desired PID sample time interval With a 8-bit counter (255 cylces to overflow), the time interval value is calculated as follows: TIME_INTERVAL = ( desired interval [sec] ) * ( frequency [Hz] ) / 255 Definition at line 45 of file pid.h. Referenced by TIMER0_OVF_ISR(). |
|
Definition at line 80 of file pid.h. Referenced by TIMER0_OVF_ISR(). |
|
Initialisation of PID controller parameters. Initialise the variables used by the PID algorithm.
Definition at line 33 of file pid.c. References PID_DATA::D_FACTOR, PID_DATA::I_FACTOR, PID_DATA::LAST_PROCESS_VALUE, PID_DATA::MAX_ERROR, MAX_INT, PID_DATA::MAX_SUM_ERROR, PID_DATA::P_FACTOR, and PID_DATA::SUM_ERROR. Referenced by Init(). 00035 { 00036 // Start values for PID controller 00037 pid->SUM_ERROR = 0; 00038 pid->LAST_PROCESS_VALUE = 0; 00039 // Tuning constants for PID loop 00040 pid->P_FACTOR = p_factor; 00041 pid->I_FACTOR = i_factor; 00042 pid->D_FACTOR = d_factor; 00043 // Limits to avoid overflow 00044 pid->MAX_ERROR = MAX_INT / (pid->P_FACTOR + 1); 00045 pid->MAX_SUM_ERROR = MAX_INT / (pid->I_FACTOR + 1); 00046 }
|
|
PID control algorithm. Calculates output from setpoint, process value and PID status.
Definition at line 57 of file pid.c. References PID_DATA::D_FACTOR, PID_DATA::I_FACTOR, PID_DATA::LAST_PROCESS_VALUE, PID_DATA::MAX_ERROR, MAX_INT, PID_DATA::MAX_SUM_ERROR, PID_DATA::P_FACTOR, and PID_DATA::SUM_ERROR. Referenced by main(). 00058 { 00059 int error, p_term, i_term, d_term; 00060 long ret, temp; 00061 00062 error = setPoint - processValue; 00063 00064 // Calculate Pterm and limit error overflow 00065 if(error > pid_st->MAX_ERROR) 00066 p_term = MAX_INT; 00067 else if(error < -pid_st->MAX_ERROR) 00068 p_term = -MAX_INT; 00069 else 00070 p_term = pid_st->P_FACTOR * error; 00071 00072 // Calculate Iterm and limit integral runaway 00073 temp = (long)pid_st->SUM_ERROR + error; 00074 if(temp > pid_st->MAX_SUM_ERROR){ 00075 i_term = MAX_INT; 00076 pid_st->SUM_ERROR = pid_st->MAX_SUM_ERROR; 00077 } 00078 else if(temp < -pid_st->MAX_SUM_ERROR){ 00079 i_term = -MAX_INT; 00080 pid_st->SUM_ERROR = -pid_st->MAX_SUM_ERROR; 00081 } 00082 else 00083 pid_st->SUM_ERROR = temp; 00084 i_term = pid_st->I_FACTOR * pid_st->SUM_ERROR; 00085 00086 // Calculate Dterm 00087 d_term = pid_st->D_FACTOR * (pid_st->LAST_PROCESS_VALUE - processValue); 00088 00089 pid_st->LAST_PROCESS_VALUE = processValue; 00090 00091 //ret = (((long)p_term + i_term + d_term))/128; 00092 ret = (((long)p_term + i_term + d_term)) >> 7; // <--- scale back down (right shift seven bits = divide by 128) 00093 if(ret > MAX_INT) 00094 ret = MAX_INT; 00095 else if(ret < -MAX_INT) 00096 ret = -MAX_INT; 00097 return((int)ret); 00098 }
|