SWARM-Bot Firmware  v1.0
Mobile robot OS - Embedded C/C++
_pid_.h File Reference

Header file for pid.c. More...

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

Classes

struct  PID_DATA
 PID Status. More...
 

Macros

#define SCALING_FACTOR   128
 
#define MAX_INT   INT16_MAX
 Maximum values. More...
 
#define MAX_LONG   INT32_MAX
 
#define MAX_I_TERM   (MAX_LONG / 2)
 
#define FALSE   0
 
#define TRUE   1
 

Typedefs

typedef struct PID_DATA pidData_t
 PID Status. More...
 

Functions

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

Variables

volatile uint8_t flag
 

Detailed Description

Header file for pid.c.

  • File: pid.h
  • Compiler: GCC-AVR
  • Supported devices: Tested on 328p
  • AppNote: Discrete PID controller
Author
Swarm robot graduation project workgroub
Mechatronics Program for the Distinguished
$Name$
Revision
456

$RCSfile$

Date
021

Definition in file _pid_.h.

Macro Definition Documentation

◆ MAX_INT

#define MAX_INT   INT16_MAX

Maximum values.

Needed to avoid sign/overflow problems

Typedef Documentation

◆ pidData_t

typedef struct PID_DATA pidData_t

PID Status.

Setpoints and data used by the PID control algorithm

Function Documentation

◆ pid_Controller()

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
setPointDesired value.
processValueMeasured value.
pid_stPID status struct.
59 {
60  int16_t error, p_term, d_term;
61  int32_t i_term, ret, temp;
62 
63  error = setPoint - processValue;
64 
65  // Calculate Pterm and limit error overflow
66  if (error > pid_st->maxError){
67  p_term = MAX_INT;
68  }
69  else if (error < -pid_st->maxError){
70  p_term = -MAX_INT;
71  }
72  else{
73  p_term = pid_st->P_Factor * error;
74  }
75 
76  // Calculate Iterm and limit integral runaway
77  temp = pid_st->sumError + error;
78  if(temp > pid_st->maxSumError){
79  i_term = MAX_I_TERM;
80  pid_st->sumError = pid_st->maxSumError;
81  }
82  else if(temp < -pid_st->maxSumError){
83  i_term = -MAX_I_TERM;
84  pid_st->sumError = -pid_st->maxSumError;
85  }
86  else{
87  pid_st->sumError = temp;
88  i_term = pid_st->I_Factor * pid_st->sumError;
89  }
90 
91  // Calculate Dterm
92  d_term = pid_st->D_Factor * (pid_st->lastProcessValue - processValue);
93 
94  pid_st->lastProcessValue = processValue;
95 
96  ret = (p_term + i_term + d_term) / SCALING_FACTOR;
97  if(ret > MAX_INT){
98  ret = MAX_INT;
99  }
100  else if(ret < -MAX_INT){
101  ret = -MAX_INT;
102  }
103 
104  return((int16_t)ret);
105 }
#define MAX_INT
Maximum values.
Definition: _pid_.h:51
int16_t I_Factor
The Integral tuning constant, multiplied with SCALING_FACTOR.
Definition: _pid_.h:37
int16_t P_Factor
The Proportional tuning constant, multiplied with SCALING_FACTOR.
Definition: _pid_.h:35
int16_t D_Factor
The Derivative tuning constant, multiplied with SCALING_FACTOR.
Definition: _pid_.h:39
int16_t maxError
Maximum allowed error, avoid overflow.
Definition: _pid_.h:41
int16_t lastProcessValue
Last process value, used to find derivative of process value.
Definition: _pid_.h:31
int32_t sumError
Summation of errors, used for integrate calculations.
Definition: _pid_.h:33
int32_t maxSumError
Maximum allowed sumerror, avoid overflow.
Definition: _pid_.h:43

References MAX_INT, PID_DATA::maxError, PID_DATA::maxSumError, PID_DATA::P_Factor, and PID_DATA::sumError.

Referenced by _update_controller().

Here is the caller graph for this function:

◆ pid_Init()

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_factorProportional term.
i_factorIntegral term.
d_factorDerivate term.
pidStruct with PID status.
36 {
37  // Start values for PID controller
38  pid->sumError = 0;
39  pid->lastProcessValue = 0;
40  // Tuning constants for PID loop
41  pid->P_Factor = p_factor;
42  pid->I_Factor = i_factor;
43  pid->D_Factor = d_factor;
44  // Limits to avoid overflow
45  pid->maxError = MAX_INT / (pid->P_Factor + 1);
46  pid->maxSumError = MAX_I_TERM / (pid->I_Factor + 1);
47 }

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

Referenced by _init_dc_control().

Here is the caller graph for this function:

◆ pid_Reset_Integrator()

void pid_Reset_Integrator ( pidData_t pid_st)

Resets the integrator.

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

112 {
113  pid_st->sumError = 0;
114 }

References PID_DATA::sumError.