ported pid library from imu

Dependents:   wheelchaircontrol wheelchaircontrolRos

Committer:
ryanlin97
Date:
Thu Aug 09 16:36:32 2018 +0000
Revision:
0:539e3b3b91e1
Child:
1:45383b49536c
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryanlin97 0:539e3b3b91e1 1 #ifndef PID_v1_h
ryanlin97 0:539e3b3b91e1 2 #define PID_v1_h
ryanlin97 0:539e3b3b91e1 3 #define LIBRARY_VERSION 1.2.1
ryanlin97 0:539e3b3b91e1 4
ryanlin97 0:539e3b3b91e1 5 #include "mbed.h"
ryanlin97 0:539e3b3b91e1 6
ryanlin97 0:539e3b3b91e1 7 class PID
ryanlin97 0:539e3b3b91e1 8 {
ryanlin97 0:539e3b3b91e1 9
ryanlin97 0:539e3b3b91e1 10
ryanlin97 0:539e3b3b91e1 11 public:
ryanlin97 0:539e3b3b91e1 12
ryanlin97 0:539e3b3b91e1 13 //Constants used in some of the functions below
ryanlin97 0:539e3b3b91e1 14 #define AUTOMATIC 1
ryanlin97 0:539e3b3b91e1 15 #define MANUAL 0
ryanlin97 0:539e3b3b91e1 16 #define DIRECT 0
ryanlin97 0:539e3b3b91e1 17 #define REVERSE 1
ryanlin97 0:539e3b3b91e1 18 #define P_ON_M 0
ryanlin97 0:539e3b3b91e1 19 #define P_ON_E 1
ryanlin97 0:539e3b3b91e1 20
ryanlin97 0:539e3b3b91e1 21 //commonly used functions **************************************************************************
ryanlin97 0:539e3b3b91e1 22 PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
ryanlin97 0:539e3b3b91e1 23 double, double, double, int, int);// Setpoint. Initial tuning parameters are also set here.
ryanlin97 0:539e3b3b91e1 24 // (overload for specifying proportional mode)
ryanlin97 0:539e3b3b91e1 25
ryanlin97 0:539e3b3b91e1 26 PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
ryanlin97 0:539e3b3b91e1 27 double, double, double, int); // Setpoint. Initial tuning parameters are also set here
ryanlin97 0:539e3b3b91e1 28
ryanlin97 0:539e3b3b91e1 29 void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
ryanlin97 0:539e3b3b91e1 30
ryanlin97 0:539e3b3b91e1 31 bool Compute(); // * performs the PID calculation. it should be
ryanlin97 0:539e3b3b91e1 32 // called every time loop() cycles. ON/OFF and
ryanlin97 0:539e3b3b91e1 33 // calculation frequency can be set using SetMode
ryanlin97 0:539e3b3b91e1 34 // SetSampleTime respectively
ryanlin97 0:539e3b3b91e1 35
ryanlin97 0:539e3b3b91e1 36 void SetOutputLimits(double, double); // * clamps the output to a specific range. 0-255 by default, but
ryanlin97 0:539e3b3b91e1 37 // it's likely the user will want to change this depending on
ryanlin97 0:539e3b3b91e1 38 // the application
ryanlin97 0:539e3b3b91e1 39
ryanlin97 0:539e3b3b91e1 40
ryanlin97 0:539e3b3b91e1 41
ryanlin97 0:539e3b3b91e1 42 //available but not commonly used functions ********************************************************
ryanlin97 0:539e3b3b91e1 43 void SetTunings(double, double, // * While most users will set the tunings once in the
ryanlin97 0:539e3b3b91e1 44 double); // constructor, this function gives the user the option
ryanlin97 0:539e3b3b91e1 45 // of changing tunings during runtime for Adaptive control
ryanlin97 0:539e3b3b91e1 46 void SetTunings(double, double, // * overload for specifying proportional mode
ryanlin97 0:539e3b3b91e1 47 double, int);
ryanlin97 0:539e3b3b91e1 48
ryanlin97 0:539e3b3b91e1 49 void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
ryanlin97 0:539e3b3b91e1 50 // means the output will increase when error is positive. REVERSE
ryanlin97 0:539e3b3b91e1 51 // means the opposite. it's very unlikely that this will be needed
ryanlin97 0:539e3b3b91e1 52 // once it is set in the constructor.
ryanlin97 0:539e3b3b91e1 53 void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
ryanlin97 0:539e3b3b91e1 54 // the PID calculation is performed. default is 100
ryanlin97 0:539e3b3b91e1 55
ryanlin97 0:539e3b3b91e1 56
ryanlin97 0:539e3b3b91e1 57 double *myInput; // * Pointers to the Input, Output, and Setpoint variables
ryanlin97 0:539e3b3b91e1 58 double *myOutput; // This creates a hard link between the variables and the
ryanlin97 0:539e3b3b91e1 59 double *mySetpoint;
ryanlin97 0:539e3b3b91e1 60
ryanlin97 0:539e3b3b91e1 61 //Display functions ****************************************************************
ryanlin97 0:539e3b3b91e1 62 double GetKp(); // These functions query the pid for interal values.
ryanlin97 0:539e3b3b91e1 63 double GetKi(); // they were created mainly for the pid front-end,
ryanlin97 0:539e3b3b91e1 64 double GetKd(); // where it's important to know what is actually
ryanlin97 0:539e3b3b91e1 65 int GetMode(); // inside the PID.
ryanlin97 0:539e3b3b91e1 66 int GetDirection(); //
ryanlin97 0:539e3b3b91e1 67
ryanlin97 0:539e3b3b91e1 68 private:
ryanlin97 0:539e3b3b91e1 69 void Initialize();
ryanlin97 0:539e3b3b91e1 70
ryanlin97 0:539e3b3b91e1 71 double dispKp; // * we'll hold on to the tuning parameters in user-entered
ryanlin97 0:539e3b3b91e1 72 double dispKi; // format for display purposes
ryanlin97 0:539e3b3b91e1 73 double dispKd; //
ryanlin97 0:539e3b3b91e1 74
ryanlin97 0:539e3b3b91e1 75 double kp; // * (P)roportional Tuning Parameter
ryanlin97 0:539e3b3b91e1 76 double ki; // * (I)ntegral Tuning Parameter
ryanlin97 0:539e3b3b91e1 77 double kd; // * (D)erivative Tuning Parameter
ryanlin97 0:539e3b3b91e1 78
ryanlin97 0:539e3b3b91e1 79 int controllerDirection;
ryanlin97 0:539e3b3b91e1 80 int pOn;
ryanlin97 0:539e3b3b91e1 81
ryanlin97 0:539e3b3b91e1 82 // double *myInput; // * Pointers to the Input, Output, and Setpoint variables
ryanlin97 0:539e3b3b91e1 83 // double *myOutput; // This creates a hard link between the variables and the
ryanlin97 0:539e3b3b91e1 84 // double *mySetpoint; // PID, freeing the user from having to constantly tell us
ryanlin97 0:539e3b3b91e1 85 // what these values are. with pointers we'll just know.
ryanlin97 0:539e3b3b91e1 86
ryanlin97 0:539e3b3b91e1 87 unsigned long lastTime;
ryanlin97 0:539e3b3b91e1 88 double outputSum, lastInput;
ryanlin97 0:539e3b3b91e1 89
ryanlin97 0:539e3b3b91e1 90 unsigned long SampleTime;
ryanlin97 0:539e3b3b91e1 91 double outMin, outMax;
ryanlin97 0:539e3b3b91e1 92 bool inAuto, pOnE;
ryanlin97 0:539e3b3b91e1 93
ryanlin97 0:539e3b3b91e1 94 };
ryanlin97 0:539e3b3b91e1 95 #endif
ryanlin97 0:539e3b3b91e1 96
ryanlin97 0:539e3b3b91e1 97