- Main Page
- Data Structures
- Files
- Related Pages

- File List
- Globals

# pid.h File Reference

## Detailed Description

Header file for pid.c.

- File: pid.h
- Compiler: IAR EWAAVR 4.11A
- Supported devices: All AVR devices can be used.
- AppNote: AVR221 - Discrete PID controller

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.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 128
#define | TRUE 1
## 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 main().

#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 128

Definition at line 25 of file pid.h.

Referenced by Init(), and pid_Controller().

#define TRUE 1

Definition at line 59 of file pid.h.

Referenced by TIMER0_OVF_ISR().

## 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 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 }