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.
Dependencies: mbed PID Ticker_HelloWorld millis
Revision 1:b71be4a00291, committed 2020-12-14
- Comitter:
- nikitakl
- Date:
- Mon Dec 14 14:25:39 2020 +0000
- Parent:
- 0:5014bf742e9b
- Commit message:
- PID based on ; http://brettbeauregard.com/blog/2011/04/improving-the-beginner%e2%80%99s-pid-sample-time/
Changed in this revision
diff -r 5014bf742e9b -r b71be4a00291 PID.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PID.cpp Mon Dec 14 14:25:39 2020 +0000
@@ -0,0 +1,98 @@
+#include "mbed.h"
+#include "millis.h"
+//#include "SoftwarePWM.h"
+//#include "USBDevice.h"
+#define threshold 0.003 // 1/330
+unsigned long lastTime;
+double Input, Output;
+double Setpoint=0.0757575757575758;
+double errSum, lastErr;
+double kp, ki, kd;
+int SampleTime = 1000; //1 sec
+
+
+AnalogIn temperature(p20);
+PwmOut fanControl(p21);
+//static BufferedSerial pc(USBTX, USBRX);
+
+Ticker temperatureTicker;
+
+int readTemperatureFlag=0;
+
+void tickerFlagOn()
+{
+ //led2 = !led2;
+ readTemperatureFlag=1;
+}
+
+void Compute()
+{
+ printf("PID is functioning\n\r");
+ unsigned long now = millis();
+ int timeChange = (now - lastTime);
+ if(timeChange>=SampleTime)
+ {
+ /*Compute all the working error variables*/
+ double error = Setpoint - Input;
+ errSum += error;
+ double dErr = (error - lastErr);
+
+ /*Compute PID Output*/
+ Output = kp * error + ki * errSum + kd * dErr;
+
+ /*Remember some variables for next time*/
+ lastErr = error;
+ lastTime = now;
+ }
+ printf("PID loop has ended\n\r");
+}
+
+void SetTunings(double Kp, double Ki, double Kd)
+{
+ double SampleTimeInSec = ((double)SampleTime)/1000;
+ kp = Kp;
+ ki = Ki * SampleTimeInSec;
+ kd = Kd / SampleTimeInSec;
+}
+
+void SetSampleTime(int NewSampleTime)
+{
+ if (NewSampleTime > 0)
+ {
+ double ratio = (double)NewSampleTime
+ / (double)SampleTime;
+ ki *= ratio;
+ kd /= ratio;
+ SampleTime = (unsigned long)NewSampleTime;
+ }
+}
+int main()
+{
+
+ float oldTemperature = temperature.read();
+ float newTemperature= oldTemperature;
+ temperatureTicker.attach(&tickerFlagOn, 0.2); // the address of the function to be attached (tickerFlagOn) and the interval (0.2 seconds)
+
+ while (1)
+ {
+ if (readTemperatureFlag)
+ {
+ readTemperatureFlag=0;
+ newTemperature = temperature.read();
+ if ((newTemperature < (oldTemperature-threshold)) || (newTemperature > (oldTemperature+threshold)))
+ {
+ oldTemperature = newTemperature; // These are for the threshold
+ printf("Temperature= %.1f degrees\n\r", 3.3*oldTemperature*100);
+ }
+ }
+ //Setpoint=0.0757575757575758;
+ //Setpoint=25/330;
+ Input=oldTemperature;
+ SetTunings(1, 1, 1);
+ Compute();
+ fanControl.write(Output);
+ printf("fanSpeed=%f \n \r", Output);
+ wait(1);
+ }
+
+}
\ No newline at end of file
diff -r 5014bf742e9b -r b71be4a00291 PID.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PID.lib Mon Dec 14 14:25:39 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/aberk/code/PID/#6e12a3e5af19
diff -r 5014bf742e9b -r b71be4a00291 blog_PID.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/blog_PID.lib Mon Dec 14 14:25:39 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/Ticker_HelloWorld/#5014bf742e9b
diff -r 5014bf742e9b -r b71be4a00291 main.cpp
--- a/main.cpp Thu Feb 14 14:30:22 2013 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-#include "mbed.h"
-
-Ticker flipper;
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-
-void flip() {
- led2 = !led2;
-}
-
-int main() {
- led2 = 1;
- flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
-
- // spin in a main loop. flipper will interrupt it to call flip
- while(1) {
- led1 = !led1;
- wait(0.2);
- }
-}
\ No newline at end of file
diff -r 5014bf742e9b -r b71be4a00291 millis.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/millis.lib Mon Dec 14 14:25:39 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/hudakz/code/millis/#ac7586424119