Controller for Seagoat in the RoboSub competition

Dependencies:   Servo mbed

Fork of ESC by Matteo Terruzzi

Committer:
gelmes
Date:
Mon Jul 04 18:56:23 2016 +0000
Revision:
3:5ffe7e9c0bb3
Quaternions are holding me back

Who changed what in which revision?

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