Andrew Olguin / Mbed 2 deprecated RTOS_Controller_v3

Dependencies:   IMU MODSERIAL Servo mbed

Fork of RTOS_Controller_v3 by Marco Rubio

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PID.h Source File

PID.h

00001 #include "mbed.h"
00002 #ifndef PID_v1_h
00003 #define PID_v1_h
00004 #define LIBRARY_VERSION 1.0.0
00005 
00006 class PID
00007 {
00008   public:
00009 
00010   //Constants used in some of the functions below
00011   #define AUTOMATIC 1
00012   #define MANUAL    0
00013   #define DIRECT  0
00014   #define REVERSE  1
00015 
00016   //commonly used functions **************************************************************************
00017     PID(double*, double*, double*,        // * constructor.  links the PID to the Input, Output, and 
00018         double, double, double, int);     //   Setpoint.  Initial tuning parameters are also set here
00019     
00020     void SetMode(int Mode);               // * sets PID to either Manual (0) or Auto (non-0)
00021 
00022     bool Compute();                       // * performs the PID calculation.  it should be
00023                                           //   called every time loop() cycles. ON/OFF and
00024                                           //   calculation frequency can be set using SetMode
00025                                           //   SetSampleTime respectively
00026 
00027     void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but
00028                                           //it's likely the user will want to change this depending on
00029                                           //the application
00030     
00031 
00032 
00033   //available but not commonly used functions ********************************************************
00034     void SetTunings(double, double,       // * While most users will set the tunings once in the 
00035                     double);              //   constructor, this function gives the user the option
00036                                           //   of changing tunings during runtime for Adaptive control
00037     void SetControllerDirection(int);     // * Sets the Direction, or "Action" of the controller. DIRECT
00038                                           //   means the output will increase when error is positive. REVERSE
00039                                           //   means the opposite.  it's very unlikely that this will be needed
00040                                           //   once it is set in the constructor.
00041     void SetSampleTime(int);              // * sets the frequency, in Milliseconds, with which 
00042                                           //   the PID calculation is performed.  default is 100
00043                                           
00044                                           
00045                                           
00046   //Display functions ****************************************************************
00047     double GetKp();                       // These functions query the pid for interal values.
00048     double GetKi();                       //  they were created mainly for the pid front-end,
00049     double GetKd();                       // where it's important to know what is actually 
00050     int GetMode();                        //  inside the PID.
00051     int GetDirection();                   //
00052 
00053   private:
00054     void Initialize();
00055     
00056     double dispKp;              // * we'll hold on to the tuning parameters in user-entered 
00057     double dispKi;              //   format for display purposes
00058     double dispKd;              //
00059     
00060     double kp;                  // * (P)roportional Tuning Parameter
00061     double ki;                  // * (I)ntegral Tuning Parameter
00062     double kd;                  // * (D)erivative Tuning Parameter
00063 
00064     int controllerDirection;
00065 
00066     double *myInput;              // * Pointers to the Input, Output, and Setpoint variables
00067     double *myOutput;             //   This creates a hard link between the variables and the 
00068     double *mySetpoint;           //   PID, freeing the user from having to constantly tell us
00069                                   //   what these values are.  with pointers we'll just know.
00070               
00071     unsigned long lastTime;
00072     double ITerm, lastInput;
00073 
00074     unsigned long SampleTime;
00075     double outMin, outMax;
00076     bool inAuto;
00077     Timer t;
00078 };
00079 #endif
00080 
00081