PID Feedback Control

Dependencies:   mbed PID

Files at this revision

API Documentation at this revision

Comitter:
mmohamed
Date:
Tue Nov 19 20:46:36 2019 +0000
Commit message:
PID;

Changed in this revision

AmpPID_Pot.cpp Show annotated file Show diff for this revision Revisions of this file
AmpRiseTime.cpp Show annotated file Show diff for this revision Revisions of this file
AnalogOut.cpp Show annotated file Show diff for this revision Revisions of this file
PID.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
plot_rise_time.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AmpPID_Pot.cpp	Tue Nov 19 20:46:36 2019 +0000
@@ -0,0 +1,74 @@
+
+#include "mbed.h"
+#include "PID.h"
+#include "time.h"
+#define RATE 0.01
+
+// PID Tuning Parameters
+
+AnalogIn kp_pin(p15);
+AnalogIn ti_pin(p16);
+AnalogIn td_pin(p17);
+
+AnalogIn pv_amp(p19);
+AnalogOut Aout(p18);
+
+DigitalOut led_1(LED1);
+DigitalOut led_2(LED2);
+Timer t;
+
+LocalFileSystem local("local");
+
+float amp = 1.5; // Desired voltage at the variable attenuator corresponding to -10 dB 
+float amp_detector = 1.686; // Desired voltage at the output of the detector with VA set at -10 dB and input power = 5 dBm
+float input_scaling_factor = amp/amp_detector;
+float scaled_input;
+
+float Kp = kp_pin.read()*10;     // values from 0.0 to 10.0
+float Ti = ti_pin.read()*1000;      // values from 0.0 to 1000.0
+float Td = td_pin.read();    // values from 0.0 to 1.0
+
+PID controller_amp(Kp, Ti, Td, RATE);
+
+
+int main() {
+
+  //Analog input from 0.0 to 3.3 V
+  controller_amp.setInputLimits(0.0, 3.3);
+  
+  //Pwm output from 0.0 to 3.3 V
+  controller_amp.setOutputLimits(0.0, 1.0);
+  
+  //If there's a bias.
+  controller_amp.setBias(0.0);
+  controller_amp.setMode(0);
+  
+  controller_amp.setSetPoint(0.0);
+  led_1 = 1;
+  t.start();
+
+ FILE *fp = fopen("/local/PID.txt", "w");  // Open "out.txt" on the local file system for writing
+
+ // while(t.read() <= 5.0){
+  while (1){
+    
+    controller_amp.setSetPoint(amp);
+    led_2 = 1;
+   
+    //Update the process variable.
+    scaled_input = pv_amp.read() * input_scaling_factor;
+    controller_amp.setProcessValue(scaled_input);
+    
+    //Set the new output.
+    Aout = controller_amp.compute();
+
+ //   fprintf(fp, "%f\t%f\t%f\n", Kp, Ti, Td);
+
+    //Wait for another loop calculation.
+    wait(RATE);
+  }
+ // fclose(fp);
+//  led_1 = 0; 
+ // led_2 = 0;
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AmpRiseTime.cpp	Tue Nov 19 20:46:36 2019 +0000
@@ -0,0 +1,74 @@
+/*
+#include "PID.h"
+#include "mbed.h"
+#include "time.h"
+#define RATE 0.0001
+
+//Kc, Ti, Td, interval
+//PID controller_amp(2.0, 100.0, 0.5, RATE);
+//PID controller_amp(1.65, 10000.0, 0.0, RATE);
+PID controller_amp(1.65, 10000.0, 0.0, RATE);
+
+
+AnalogIn pv_amp(p16);     //output from the amp detector
+PwmOut co_amp(p26);       //output from PID to adjust amplitude
+//AnalogOut co_amp(p18);
+DigitalOut myled(LED2);
+
+LocalFileSystem local("local");
+
+
+float amp = 1.5; // Desired voltage at the variable attenuator corresponding to -10 dB 
+float amp_detector = 1.686; // Desired voltage at the output of the detector with VA set at -10 dB and input power = 5 dBm
+float input_scaling_factor = amp/amp_detector;
+float scaling_factor;
+float output;
+
+int main(){
+
+  //Analog input from 0.0 to 3.3 V
+  controller_amp.setInputLimits(0.0, 3.3);
+  
+  //Pwm output from 0.0 to 3.3 V
+  controller_amp.setOutputLimits(0.0, 1.0);
+  
+  //If there's a bias.
+  controller_amp.setBias(0.0);
+  controller_amp.setMode(0);
+  
+  //We want the process variables to be V_amp
+  controller_amp.setSetPoint(0.0);
+  myled = 1;
+  
+  wait(3);
+  controller_amp.setSetPoint(amp);
+  myled = 0;
+
+  FILE *fp = fopen("/local/data.txt", "w");
+
+
+  while(1){
+    //Update the process variable.
+    scaling_factor = pv_amp.read() * input_scaling_factor;
+  //scaling_factor = pv_amp.read() * 1.5/1.686;
+    controller_amp.setProcessValue(scaling_factor);
+    
+    //Set the new output.
+    co_amp = controller_amp.compute();
+   // co_amp.write(controller_amp.compute());
+    
+    
+
+    fprintf(fp, "Input Voltage %f\n", pv_amp.read());
+  //  fprintf(fp, "Output Voltage %f\n",co_amp.write(controller_amp.compute()) );
+    fprintf(fp, "Scaling Factor %f\n", scaling_factor);
+  //  fprintf(fp, "Process Variable" "%f\n",controller_amp.setProcessValue(scaling_factor));
+    fclose(fp);
+    
+    
+    //Wait for another loop calculation.
+    wait(RATE);
+  }
+
+}
+*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AnalogOut.cpp	Tue Nov 19 20:46:36 2019 +0000
@@ -0,0 +1,40 @@
+
+
+/*
+int main() {
+AnalogOut Aout(p18);
+PwmOut Pout(p26);
+ while(1) {
+ //Aout=1.3; // 0.5*3.3V = 1.65V
+ Pout=1.0;
+ wait(1);
+ }
+}
+
+
+#include "mbed.h"
+#include "time.h"
+
+LocalFileSystem local("local");               // Create the local filesystem under the name "local"
+AnalogIn in(p15);
+AnalogOut out(p18);
+DigitalOut led(LED1);
+Timer t;
+     
+
+int main() {
+    FILE *fp = fopen("/local/amp.txt", "w");  // Open "out.txt" on the local file system for writing
+    t.start();
+    out = 0;
+        for (int i = 0; i<1000; i++){
+          fprintf(fp, "%f\t%f\n", t.read(), out.read()*3.3);
+            led = 1;
+            if(i == 500){
+                out = 1.5/3.3;
+                }
+            }   
+        fclose(fp);
+        led = 0;
+}
+
+*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PID.lib	Tue Nov 19 20:46:36 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/aberk/code/PID/#6e12a3e5af19
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Nov 19 20:46:36 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plot_rise_time.cpp	Tue Nov 19 20:46:36 2019 +0000
@@ -0,0 +1,60 @@
+/*
+
+#include "PID.h"
+#include "mbed.h"
+#include "time.h"
+#define RATE 0.0001
+
+PID controller_amp(1.45, 10000.0, 0.0, RATE);
+
+LocalFileSystem local("local");               // Create the local filesystem under the name "local"
+AnalogIn in(p16);
+//PwmOut out(p26);
+AnalogOut out(p18);
+
+DigitalOut led_1(LED1);
+DigitalOut led_2(LED2);
+Timer t;
+
+float amp = 1.5; // Desired voltage at the variable attenuator corresponding to -10 dB 
+float amp_detector = 1.686; // Desired voltage at the output of the detector with VA set at -10 dB and input power = 5 dBm
+float input_scaling_factor = amp/amp_detector;
+float scaling_factor;
+     
+
+int main() {
+
+  //Analog input from 0.0 to 3.3 V
+  controller_amp.setInputLimits(0.0, 3.3);
+  
+  //Pwm output from 0.0 to 3.3 V
+  controller_amp.setOutputLimits(0.0, 1.0);
+  
+  //If there's a bias.
+  controller_amp.setBias(0.0);
+  controller_amp.setMode(0);
+  
+  controller_amp.setSetPoint(0.0);
+  t.start();
+  
+  FILE *fp = fopen("/local/rise_PID.txt", "w");  // Open "out.txt" on the local file system for writing
+
+    while(t.read() <= 2.0){
+        fprintf(fp, "%f\t%f\t%f\n", t.read(), in.read()*3.3, out.read()*3.3);
+        led_1 = 1;
+        if (t.read() >= 1.0){
+            led_2 = 1;
+            controller_amp.setSetPoint(amp);
+            scaling_factor = in.read() * input_scaling_factor;
+            controller_amp.setProcessValue(scaling_factor);
+            out = controller_amp.compute();
+            }
+            wait(RATE);
+    }
+    fclose(fp);
+    led_1 = 0;
+    led_2 = 0;
+}
+    */
+    
+