PID Feedback Control

Dependencies:   mbed PID

Committer:
mmohamed
Date:
Tue Nov 19 20:46:36 2019 +0000
Revision:
0:7e0c2b0edbc8
PID;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmohamed 0:7e0c2b0edbc8 1 /*
mmohamed 0:7e0c2b0edbc8 2 #include "PID.h"
mmohamed 0:7e0c2b0edbc8 3 #include "mbed.h"
mmohamed 0:7e0c2b0edbc8 4 #include "time.h"
mmohamed 0:7e0c2b0edbc8 5 #define RATE 0.0001
mmohamed 0:7e0c2b0edbc8 6
mmohamed 0:7e0c2b0edbc8 7 //Kc, Ti, Td, interval
mmohamed 0:7e0c2b0edbc8 8 //PID controller_amp(2.0, 100.0, 0.5, RATE);
mmohamed 0:7e0c2b0edbc8 9 //PID controller_amp(1.65, 10000.0, 0.0, RATE);
mmohamed 0:7e0c2b0edbc8 10 PID controller_amp(1.65, 10000.0, 0.0, RATE);
mmohamed 0:7e0c2b0edbc8 11
mmohamed 0:7e0c2b0edbc8 12
mmohamed 0:7e0c2b0edbc8 13 AnalogIn pv_amp(p16); //output from the amp detector
mmohamed 0:7e0c2b0edbc8 14 PwmOut co_amp(p26); //output from PID to adjust amplitude
mmohamed 0:7e0c2b0edbc8 15 //AnalogOut co_amp(p18);
mmohamed 0:7e0c2b0edbc8 16 DigitalOut myled(LED2);
mmohamed 0:7e0c2b0edbc8 17
mmohamed 0:7e0c2b0edbc8 18 LocalFileSystem local("local");
mmohamed 0:7e0c2b0edbc8 19
mmohamed 0:7e0c2b0edbc8 20
mmohamed 0:7e0c2b0edbc8 21 float amp = 1.5; // Desired voltage at the variable attenuator corresponding to -10 dB
mmohamed 0:7e0c2b0edbc8 22 float amp_detector = 1.686; // Desired voltage at the output of the detector with VA set at -10 dB and input power = 5 dBm
mmohamed 0:7e0c2b0edbc8 23 float input_scaling_factor = amp/amp_detector;
mmohamed 0:7e0c2b0edbc8 24 float scaling_factor;
mmohamed 0:7e0c2b0edbc8 25 float output;
mmohamed 0:7e0c2b0edbc8 26
mmohamed 0:7e0c2b0edbc8 27 int main(){
mmohamed 0:7e0c2b0edbc8 28
mmohamed 0:7e0c2b0edbc8 29 //Analog input from 0.0 to 3.3 V
mmohamed 0:7e0c2b0edbc8 30 controller_amp.setInputLimits(0.0, 3.3);
mmohamed 0:7e0c2b0edbc8 31
mmohamed 0:7e0c2b0edbc8 32 //Pwm output from 0.0 to 3.3 V
mmohamed 0:7e0c2b0edbc8 33 controller_amp.setOutputLimits(0.0, 1.0);
mmohamed 0:7e0c2b0edbc8 34
mmohamed 0:7e0c2b0edbc8 35 //If there's a bias.
mmohamed 0:7e0c2b0edbc8 36 controller_amp.setBias(0.0);
mmohamed 0:7e0c2b0edbc8 37 controller_amp.setMode(0);
mmohamed 0:7e0c2b0edbc8 38
mmohamed 0:7e0c2b0edbc8 39 //We want the process variables to be V_amp
mmohamed 0:7e0c2b0edbc8 40 controller_amp.setSetPoint(0.0);
mmohamed 0:7e0c2b0edbc8 41 myled = 1;
mmohamed 0:7e0c2b0edbc8 42
mmohamed 0:7e0c2b0edbc8 43 wait(3);
mmohamed 0:7e0c2b0edbc8 44 controller_amp.setSetPoint(amp);
mmohamed 0:7e0c2b0edbc8 45 myled = 0;
mmohamed 0:7e0c2b0edbc8 46
mmohamed 0:7e0c2b0edbc8 47 FILE *fp = fopen("/local/data.txt", "w");
mmohamed 0:7e0c2b0edbc8 48
mmohamed 0:7e0c2b0edbc8 49
mmohamed 0:7e0c2b0edbc8 50 while(1){
mmohamed 0:7e0c2b0edbc8 51 //Update the process variable.
mmohamed 0:7e0c2b0edbc8 52 scaling_factor = pv_amp.read() * input_scaling_factor;
mmohamed 0:7e0c2b0edbc8 53 //scaling_factor = pv_amp.read() * 1.5/1.686;
mmohamed 0:7e0c2b0edbc8 54 controller_amp.setProcessValue(scaling_factor);
mmohamed 0:7e0c2b0edbc8 55
mmohamed 0:7e0c2b0edbc8 56 //Set the new output.
mmohamed 0:7e0c2b0edbc8 57 co_amp = controller_amp.compute();
mmohamed 0:7e0c2b0edbc8 58 // co_amp.write(controller_amp.compute());
mmohamed 0:7e0c2b0edbc8 59
mmohamed 0:7e0c2b0edbc8 60
mmohamed 0:7e0c2b0edbc8 61
mmohamed 0:7e0c2b0edbc8 62 fprintf(fp, "Input Voltage %f\n", pv_amp.read());
mmohamed 0:7e0c2b0edbc8 63 // fprintf(fp, "Output Voltage %f\n",co_amp.write(controller_amp.compute()) );
mmohamed 0:7e0c2b0edbc8 64 fprintf(fp, "Scaling Factor %f\n", scaling_factor);
mmohamed 0:7e0c2b0edbc8 65 // fprintf(fp, "Process Variable" "%f\n",controller_amp.setProcessValue(scaling_factor));
mmohamed 0:7e0c2b0edbc8 66 fclose(fp);
mmohamed 0:7e0c2b0edbc8 67
mmohamed 0:7e0c2b0edbc8 68
mmohamed 0:7e0c2b0edbc8 69 //Wait for another loop calculation.
mmohamed 0:7e0c2b0edbc8 70 wait(RATE);
mmohamed 0:7e0c2b0edbc8 71 }
mmohamed 0:7e0c2b0edbc8 72
mmohamed 0:7e0c2b0edbc8 73 }
mmohamed 0:7e0c2b0edbc8 74 */