ported pid library from imu

Dependents:   wheelchaircontrol wheelchaircontrolRos

Committer:
ryanlin97
Date:
Fri Aug 17 20:46:39 2018 +0000
Revision:
1:45383b49536c
Parent:
0:539e3b3b91e1
adjust for jesus;

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