xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Files | Data Structures | Macros | Functions | Variables
PID-Controller

Simple implementation for multiple floating-point PID Controllers. More...

Files

file  pid.h
 PID Library Header.
 
file  pid.c
 PID Library Implementation.
 

Data Structures

struct  PIDState
 Data Structure for a single PID Controller. More...
 

Macros

#define ROLL   0
 Roll index for pidTarget, pidOutput and pidStates. More...
 
#define PITCH   1
 Pitch index for pidTarget, pidOutput and pidStates. More...
 

Functions

void pidInit (void)
 Initialize Roll and Pitch PID. More...
 
void pidTask (void)
 Step the Roll and Pitch PID Controllers. More...
 
void pidSet (PIDState *pid, double kp, double ki, double kd, double min, double max, double iMin, double iMax)
 Set the parameters of a PID controller. More...
 
double pidExecute (double should, double is, PIDState *state)
 Execute a single PID Control Step. More...
 

Variables

double pidTarget [2]
 Roll and Pitch target angles. More...
 
double pidOutput [2]
 Roll and Pitch PID Output. More...
 
PIDState pidStates [2]
 Roll and Pitch PID States. More...
 
PIDState pidStates [2]
 Roll and Pitch PID States. More...
 
double pidTarget [2]
 Roll and Pitch target angles. More...
 
double pidOutput [2]
 Roll and Pitch PID Output. More...
 

Detailed Description

Simple implementation for multiple floating-point PID Controllers.

Macro Definition Documentation

#define PITCH   1

Pitch index for pidTarget, pidOutput and pidStates.

Examples:
uartFlight.c.

Definition at line 61 of file pid.h.

Referenced by pidTask(), and setMotorSpeeds().

#define ROLL   0

Roll index for pidTarget, pidOutput and pidStates.

Examples:
uartFlight.c.

Definition at line 60 of file pid.h.

Referenced by pidTask(), and setMotorSpeeds().

Function Documentation

double pidExecute ( double  should,
double  is,
PIDState state 
)

Execute a single PID Control Step.

Parameters
shouldTarget value
isMeasured value
statePID State
Returns
PID Output

Definition at line 54 of file pid.c.

References getSystemTime(), PIDState::intMax, PIDState::intMin, PIDState::kd, PIDState::ki, PIDState::kp, PIDState::last, PIDState::lastError, PIDState::outMax, PIDState::outMin, and PIDState::sumError.

Referenced by pidTask().

54  {
55  time_t now = getSystemTime();
56  double timeChange = (double)(now - state->last);
57  double error = should - is;
58  double newErrorSum = state->sumError + (error * timeChange);
59  if ((newErrorSum >= state->intMin) && (newErrorSum <= state->intMax))
60  state->sumError = newErrorSum; // Prevent Integral Windup
61  double dError = (error - state->lastError) / timeChange;
62  double output = (state->kp * error) + (state->ki * state->sumError) + (state->kd * dError);
63  state->lastError = error;
64  state->last = now;
65  if (output > state->outMax) {
66  output = state->outMax;
67  }
68  if (output < state->outMin) {
69  output = state->outMin;
70  }
71  return output;
72 }
void pidInit ( void  )

Initialize Roll and Pitch PID.

Stores the PID States in pidStates. Also resets pidTarget to zero.

Examples:
uartFlight.c.

Definition at line 74 of file pid.c.

References PID_D, PID_I, PID_INTMAX, PID_INTMIN, PID_OUTMAX, PID_OUTMIN, PID_P, pidSet(), and pidTarget.

74  {
75  for (uint8_t i = 0; i < 2; i++) {
77  pidTarget[i] = 0.0;
78  }
79 }
void pidSet ( PIDState pid,
double  kp,
double  ki,
double  kd,
double  min,
double  max,
double  iMin,
double  iMax 
)

Set the parameters of a PID controller.

The state variables will be reset to zero.

Parameters
pidPIDState to be changed.
kpNew Proportional constant.
kiNew Integral constant.
kdNew Derivative constant.
minNew minimum Output.
maxNew maximum Output.
iMinNew minimal Integral Sum.
iMaxNew maximal Integral Sum.
Examples:
uartFlight.c.

Definition at line 81 of file pid.c.

References PIDState::intMax, PIDState::intMin, PIDState::kd, PIDState::ki, PIDState::kp, PIDState::last, PIDState::lastError, PIDState::outMax, PIDState::outMin, and PIDState::sumError.

Referenced by pidInit().

81  {
82  pid->kp = kp;
83  pid->ki = ki;
84  pid->kd = kd;
85  pid->outMin = min;
86  pid->outMax = max;
87  pid->intMin = iMin;
88  pid->intMax = iMax;
89  pid->lastError = 0;
90  pid->sumError = 0;
91  pid->last = 0;
92 }
void pidTask ( void  )

Step the Roll and Pitch PID Controllers.

Placing their output in pidOutput and reading the input from pidTarget and the global orientation Angles.

Examples:
uartFlight.c.

Definition at line 94 of file pid.c.

References orientation, pidExecute(), pidOutput, pidTarget, Angles::pitch, PITCH, Angles::roll, and ROLL.

Variable Documentation

double pidOutput[2]

Roll and Pitch PID Output.

Definition at line 52 of file pid.c.

Referenced by pidTask(), and setTask().

double pidOutput[2]

Roll and Pitch PID Output.

Examples:
uartFlight.c.

Definition at line 52 of file pid.c.

Referenced by pidTask(), and setTask().

PIDState pidStates[2]

Roll and Pitch PID States.

Definition at line 50 of file pid.c.

PIDState pidStates[2]

Roll and Pitch PID States.

Examples:
uartFlight.c.

Definition at line 50 of file pid.c.

double pidTarget[2]

Roll and Pitch target angles.

Definition at line 51 of file pid.c.

Referenced by pidInit(), and pidTask().

double pidTarget[2]

Roll and Pitch target angles.

Examples:
uartFlight.c.

Definition at line 51 of file pid.c.

Referenced by pidInit(), and pidTask().