pid.h File Reference


Detailed Description

Header file for pid.c.

Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
$Name$
Revision
2571
$RCSfile$
Date
2007-10-08 15:53:50 +0200 (ma, 08 okt 2007)

Definition in file pid.h.

#include "stdint.h"

Include dependency graph for 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 MAX_I_TERM   (MAX_LONG / 2)
#define MAX_INT   INT16_MAX
 Maximum values.
#define MAX_LONG   INT32_MAX
#define SCALING_FACTOR   256
#define TRUE   (!FALSE)

Typedefs

typedef PID_DATA pidData_t
 PID Status.

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.


Define Documentation

#define FALSE   0

Definition at line 58 of file pid.h.

Referenced by ActualDirectionUpdate(), CommutationTicksUpdate(), HallChangeISR(), main(), MotorSynchronizedUpdate(), and Timer1OverflowISR().

#define MAX_I_TERM   (MAX_LONG / 2)

Definition at line 55 of file pid.h.

Referenced by pid_Controller(), and pid_Init().

#define MAX_INT   INT16_MAX

Maximum values.

Needed to avoid sign/overflow problems

Definition at line 53 of file pid.h.

Referenced by pid_Controller(), and pid_Init().

#define MAX_LONG   INT32_MAX

Definition at line 54 of file pid.h.

#define SCALING_FACTOR   256

Definition at line 25 of file pid.h.

Referenced by pid_Controller().

#define TRUE   (!FALSE)

Definition at line 59 of file pid.h.

Referenced by CommutationTicksUpdate(), and MotorSynchronizedUpdate().


Typedef Documentation

typedef struct PID_DATA pidData_t

PID Status.

Setpoints and data used by the PID control algorithm


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 64 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 SpeedController().

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

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 40 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 main().

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

void pid_Reset_Integrator ( pidData_t pid_st  ) 

Resets the integrator.

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

Definition at line 117 of file pid.c.

References PID_DATA::sumError.

Referenced by CommutationTicksUpdate(), and FaultProtectionISR().

00118 {
00119   pid_st->sumError = 0;
00120 }


Generated on Wed Oct 10 15:31:45 2007 for AVR449 by  doxygen 1.5.2