pid.c File Reference


Detailed Description

General PID implementation for AVR.

Discrete PID controller implementation. Set up by giving P/I/D terms to Init_PID(), and uses a struct PID_DATA to store internal values.

Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
$Name$
Revision
456
$RCSfile$
Date
2006-02-16 12:46:13 +0100 (to, 16 feb 2006)

Definition in file pid.c.

#include "pid.h"
#include "stdint.h"

Include dependency graph for pid.c:

Go to the source code of this file.

Functions

int16_t pid_Controller (int16_t setPoint, int16_t processValue, struct PID_DATA *pid_st)
 PID control algorithm.
void pid_Init (int16_t p_factor, int16_t i_factor, int16_t d_factor, struct PID_DATA *pid)
 Initialisation of PID controller parameters.
void pid_Reset_Integrator (pidData_t *pid_st)
 Resets the integrator.


Function Documentation

int16_t pid_Controller ( int16_t  setPoint,
int16_t  processValue,
struct PID_DATA pid_st 
)

PID control algorithm.

Calculates output from setpoint, process value and PID status.

Parameters:
setPoint Desired value.
processValue Measured value.
pid_st PID status struct.

Definition at line 59 of file pid.c.

References PID_DATA::D_Factor, PID_DATA::I_Factor, PID_DATA::lastProcessValue, MAX_I_TERM, MAX_INT, PID_DATA::maxError, PID_DATA::maxSumError, PID_DATA::P_Factor, SCALING_FACTOR, and PID_DATA::sumError.

Referenced by main().

00060 {
00061   int16_t error, p_term, d_term;
00062   int32_t i_term, ret, temp;
00063 
00064   error = setPoint - processValue;
00065 
00066   // Calculate Pterm and limit error overflow
00067   if (error > pid_st->maxError){
00068     p_term = MAX_INT;
00069   }
00070   else if (error < -pid_st->maxError){
00071     p_term = -MAX_INT;
00072   }
00073   else{
00074     p_term = pid_st->P_Factor * error;
00075   }
00076 
00077   // Calculate Iterm and limit integral runaway
00078   temp = pid_st->sumError + error;
00079   if(temp > pid_st->maxSumError){
00080     i_term = MAX_I_TERM;
00081     pid_st->sumError = pid_st->maxSumError;
00082   }
00083   else if(temp < -pid_st->maxSumError){
00084     i_term = -MAX_I_TERM;
00085     pid_st->sumError = -pid_st->maxSumError;
00086   }
00087   else{
00088     pid_st->sumError = temp;
00089     i_term = pid_st->I_Factor * pid_st->sumError;
00090   }
00091 
00092   // Calculate Dterm
00093   d_term = pid_st->D_Factor * (pid_st->lastProcessValue - processValue);
00094 
00095   pid_st->lastProcessValue = processValue;
00096 
00097   ret = (p_term + i_term + d_term) / SCALING_FACTOR;
00098   if(ret > MAX_INT){
00099     ret = MAX_INT;
00100   }
00101   else if(ret < -MAX_INT){
00102     ret = -MAX_INT;
00103   }
00104 
00105   return((int16_t)ret);
00106 }

void pid_Init ( int16_t  p_factor,
int16_t  i_factor,
int16_t  d_factor,
struct PID_DATA pid 
)

Initialisation of PID controller parameters.

Initialise the variables used by the PID algorithm.

Parameters:
p_factor Proportional term.
i_factor Integral term.
d_factor Derivate term.
pid Struct with PID status.

Definition at line 35 of file pid.c.

References PID_DATA::D_Factor, PID_DATA::I_Factor, PID_DATA::lastProcessValue, MAX_I_TERM, MAX_INT, PID_DATA::maxError, PID_DATA::maxSumError, PID_DATA::P_Factor, and PID_DATA::sumError.

Referenced by Init().

00037 {
00038   // Start values for PID controller
00039   pid->sumError = 0;
00040   pid->lastProcessValue = 0;
00041   // Tuning constants for PID loop
00042   pid->P_Factor = p_factor;
00043   pid->I_Factor = i_factor;
00044   pid->D_Factor = d_factor;
00045   // Limits to avoid overflow
00046   pid->maxError = MAX_INT / (pid->P_Factor + 1);
00047   pid->maxSumError = MAX_I_TERM / (pid->I_Factor + 1);
00048 }

void pid_Reset_Integrator ( pidData_t pid_st  ) 

Resets the integrator.

Calling this function will reset the integrator in the PID regulator.

Definition at line 112 of file pid.c.

References PID_DATA::sumError.

00113 {
00114   pid_st->sumError = 0;
00115 }


Generated on Mon Sep 17 09:08:12 2007 for AVR221 - PID controller by  doxygen 1.5.2