Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ESP8266_pid_mtrPos_webserver_SDcard_v2 ESP8266_pid_mtrSpeed_Webserver_SDcard ESP8266_pid_spd_and_pos_webserver_SDcard ESP8266_pid_redbot_webserver ... more
PID.h
00001 #ifndef PID_H 00002 #define PID_H 00003 #include "mbed.h" 00004 #include <algorithm> 00005 00006 /* 00007 Bryce Williams 11/19/2015 00008 00009 PID Controller Class based on Brett Beauregard's Arduino PID Library 00010 and PID blog post. 00011 00012 Brett Beauregard's blog post explains the PID code implementation very well 00013 and discusses why the actual equation is a bit different than the classical 00014 equation, i.e. he explains and implements how to overcome windup, dervative 00015 kick, etc. This class uses the same implementation, but adds interrupt 00016 driven computation. 00017 00018 Reference Links: 00019 1. Arduion Library: 00020 (http://playground.arduino.cc/Code/PIDLibrary) 00021 2. Brett Beauregard's PID Blog: 00022 (http://brettbeauregard.com/blog/2011/04/improving-the-beginners- 00023 pid-introduction/) 00024 */ 00025 00026 class PID{ 00027 public: 00028 /* 00029 Constructor for PID objects. 00030 00031 Note: PID objects use given pointers, ie setpoint, 00032 feedback, output inside interrupts. When reading/ modifying 00033 these vars make sure we don't have possible read/write 00034 conflicts if the interrupt fires. Either ensure reads/writes 00035 are atomic operations, or call the stop() method perform the 00036 read/write and then call the start() method. 00037 00038 @param setpoint The setpoint 00039 @param feedback Pointer to feedback/sensor data var 00040 @param output Pointer to the output var 00041 @param output_lower The lower bound of the output value 00042 @param output_upper The upper bount of the output value 00043 @param kp The Proportional Gain value 00044 @param ki The Integral Gain value 00045 @param kd The Derivative Gain value 00046 @param Ts The sample period at which the PID algorithm will 00047 generate an interrupt and run. 00048 */ 00049 PID(float* setpoint, float* feedback, float* output, 00050 float output_lower, float output_upper, 00051 float kp, float ki, float kd, float Ts); 00052 /* 00053 Starts PID Controller; Attaches sample() as callback to Ticker 00054 sample_timer and starts the interrupt 00055 */ 00056 void start(); 00057 /* 00058 Stops PID Contoller; detaches callback from Ticker sample_timer. 00059 Allows manual setting of output and read/write of shared vars, 00060 DON'T FORGET TO CALL stop() before read/write of shared vars! 00061 Then after call start()!!! 00062 */ 00063 void stop(); 00064 /* 00065 Increments/ decrements Gain values and Sample time 00066 by the given value. Gives a simple method to 00067 programatically step through different values; just put in a 00068 loop and go 00069 @param delta_"name" The value that will be added to its currently set value 00070 */ 00071 // void adjust_parameters(float delta_kp, float delta_ki, float delta_kd, float delta Ts); 00072 /* 00073 Overwrite Gain and Sample Time parameters with new 00074 values 00075 Note: sample_timer interrupt is disabled during update 00076 to avoid synch issues. 00077 00078 */ 00079 void set_parameters(float kp, float ki, float kd, float Ts); 00080 00081 float getKp(); 00082 float getKi(); 00083 float getKd(); 00084 float getTs(); 00085 00086 /* 00087 returns current error 00088 */ 00089 float getError(); 00090 00091 private: 00092 float _kp, _ki, _kd; // PID Gain values 00093 float _Ts; // Sample time is seconds 00094 float* _setpoint; // Pointer to setpoint value 00095 float* _feedback; // Pointer to sensor feedback value (sensor input) 00096 float* _output; // Pointer to control output value 00097 float _output_lower; // Ouput Lower Limit 00098 float _output_upper; // Output Upper Limit 00099 00100 float i_accumulator; // Integral Term accumulator 00101 float last_feedback; // Previous feedback value 00102 float error; // Feedback error term 00103 Ticker sample_timer; // Generates the sample time interrupt and calls sample() 00104 /* 00105 sample() performs next sample calculationand updates command value 00106 */ 00107 void sample(); 00108 /* 00109 Clips value to lower/ uppper 00110 @param value The value to clip 00111 @param lower The mininum allowable value 00112 @param upper The maximum allowable value 00113 @return The resulting clipped value 00114 */ 00115 float clip(float value, float lower, float upper); 00116 }; 00117 00118 #endif
Generated on Tue Jul 12 2022 20:24:38 by
