Amplitude Modulation Code

Dependencies:   HIDScope mbed

Fork of SymbitronValves by First Last

Revision:
2:e7421003d1c9
Parent:
1:926025d703f4
Child:
3:28efa5d4ebe2
--- a/main.cpp	Mon Jun 29 14:00:34 2015 +0000
+++ b/main.cpp	Mon Jun 29 14:47:36 2015 +0000
@@ -38,7 +38,7 @@
 
 Valve::Valve(PinName open, PinName close): _open(open), _close(close)
 {
-    InitializeValve(0.15,0.006);
+    InitializeValve(0.05,0.006);
 }
 
 void Valve::InitializeValve(float period, float deadtime)
@@ -128,7 +128,7 @@
     _integrator += error*_dt;
     output  = error * _p;
     output += _integrator * _i;
-    output += ((error-_previous_error)/_dt)*_d;
+    output += ((_previous_error-error)/_dt)*_d;
     _previous_error = error;
     return output;
 }
@@ -142,18 +142,42 @@
 }
 
 int main() {
-    PIDcontroller pid(0.01,0,0,0.2);
-    Valve v1(V1_OPEN, V1_CLOSE);
+    PIDcontroller pid(0.001,0,0.000,0.2);
+    //Valve v1(V1_OPEN, V1_CLOSE);
     Valve v2(V2_OPEN, V2_CLOSE);
     Valve v3(V3_OPEN, V3_CLOSE);
     Valve v4(V4_OPEN, V4_CLOSE);
+    DigitalOut v1open(V1_OPEN);
+    DigitalOut v1close(V1_CLOSE);
     while(1) {
         float pid_out;
+        float setpoint;
         static float time = 0;
-        wait(0.2);
+        wait(0.1);
         time +=0.2;
-        pid_out = pid.control(slider.readPercentage()*10000, getPressure_Pa());
-        v1.write(pid_out,false)
+        setpoint = slider.readPercentage()*10000;
+        pid_out = pid.control(setpoint, getPressure_Pa());
+        //if(pid_out > 0.1)
+        //    v1.write(0.1);
+        //if(pid_out < -0.1)
+         //   v1.write(-0.1);
+        if(abs(setpoint - getPressure_Pa())<3000)
+        {
+            
+            if(setpoint - getPressure_Pa()  < 0)
+            {
+                while(setpoint< getPressure_Pa())
+                    v1open = 1;
+            }
+            else
+            {
+                while(setpoint > getPressure_Pa())
+                    v1close = 1;
+            }
+            v1open = v1close = 0;
+                
+        }
+        //v1.write(pid_out,false);
         v2.write(0);//sin(time));
         v3.write(0);
         v4.write(0);//sin(time),false);