Data Structures | Functions
lib/pid.h File Reference

Generic PID (Proportional Integral Derivative) controller implementation. More...

#include <kern/global.h>

Data Structures

struct  pid_controller

Functions

void init_pid (struct pid_controller *pid, float kp, float ki, float kd, float(*input)(), void(*output)(float value))
float update_pid_input (struct pid_controller *pid, float current_val)
float update_pid (struct pid_controller *pid)
uint8_t dispatch_pid (struct pid_controller *pid, float tolerance, uint8_t(*stop)())

Detailed Description

A PID is a commonly used feedback mechanism used to correct error between a measured process variable and a desired setpoint.

A Common use in robotics (especially in 6.270) is the position control of a motor. In this case the process variable is the wheels angular position, and the setpoint is a desired target position (eg: 720 degrees, or two wheel rotations forward). The wheel position is measured with shaft encoders and the motor is commanded by the PID controller to compensate for the error between the current wheel position and the desired goal.

This allows the user to command a desired amount of wheel rotation (and hence distance travelled). With a PID controller on each wheel the user can perform reasonably accurate motions (straight lines, in place turns, etc).

For more information see http://en.wikipedia.org/wiki/PID_controller


Function Documentation

uint8_t dispatch_pid ( struct pid_controller pid,
float  tolerance,
uint8_t(*)()  stop 
)

Dispatch to a routine that will drive the PID system until the output equals the goal within tolerance. This routine blocks and only returns when done or 'stop' is non-zero and returns non-zero.

Parameters:
pidPID controller
tolerancehow close to get towards the goal
stopreturn when stop() is true
void init_pid ( struct pid_controller pid,
float  kp,
float  ki,
float  kd,
float(*)()  input,
void(*)(float value)  output 
)

Initialize a PID controller structure. This function takes an empty pid_controller structure and initializes its member variables.

Parameters:
pidempty PID to initialize
kpproportional constant
kiintegral constant
kdderivative constant
inputinput function
outputoutput function
float update_pid ( struct pid_controller pid)

Perform a single step of the PID loop. The update_pid() steps are as follows:

  • check the pid is enabled
  • call pid->input() and store the result in current
  • calculate error (pid->goal - current)
  • calculate P, I, D components and sum result
  • call pid->output() with result
Parameters:
pidPID controller
float update_pid_input ( struct pid_controller pid,
float  current_val 
)

Perform a single step of the PID loop. like update_pid() except takes a current value as an argument

Parameters:
pidPID controller
current_valCurrent control value