Main code of our project.

Dependencies:   TextLCD mbed PID

Files at this revision

API Documentation at this revision

Comitter:
nicovv44
Date:
Wed Oct 24 08:42:30 2018 +0000
Parent:
14:3f7e54ee1211
Commit message:
Relay for DC is OK. Attempt to get the phase angle.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Oct 17 09:10:32 2018 +0000
+++ b/main.cpp	Wed Oct 24 08:42:30 2018 +0000
@@ -6,7 +6,7 @@
  
 //Kc, Ti, Td, interval
 PID PIDcontroller1(2.9, 1.0, 0.0, PIDRATE);//frequency PID
-PID PIDcontroller2(0.2, 1.0, 0.0, PIDRATE);//amplitude PID
+PID PIDcontroller2(0.4, 1.0, 0.0, PIDRATE);//amplitude PID
 
 TextLCD             lcd(D2, D3, D4, D5, D6, D7); // rs, e, d4-d7
 Serial              pc(USBTX, USBRX); // tx, rx
@@ -24,6 +24,7 @@
 AnalogIn            differentialPin(A2);
 AnalogIn            potarDC(A3);
 AnalogIn            potarSY(A4);
+AnalogIn            currentPin(A5);
 
 const float         sqrt2 = 1.414213562;
 
@@ -49,6 +50,7 @@
 void initPID1();
 void initPID2();
 void tickerPIDfunction();
+float getPhaseAngle(AnalogIn voltage_pin, AnalogIn current_pin);
 
 
 // ##############################################
@@ -56,27 +58,29 @@
 // #############################################################################
 int main() {
     float syncRMS, gridRMS, syncFreq, gridFreq;
-
-    //initialise PIDcontrollers
-    initPID1();
-    initPID2();
     
     relay1 = 1;//Relay off=1, on=0
     relay2 = 1;//Relay off=1, on=0
     relay3 = 1;//Relay off=1, on=0
     relay4 = 0;//Relay off=1, on=0
+
+    //initialise PIDcontrollers
+    initPID1();
+    initPID2();
+    
+    
     
     
     while(mainLoop){
-        pwmDC.period(0.001f);
+        pwmDC.period(0.0002f);
         pwmDC.write(1-0.00f); //(1-duty)
-        pwmSY.period(0.001f); 
+        pwmSY.period(0.0002f); 
         pwmSY.write(1-0.00f); //(1-duty)
         
         while(userButton==1){;}
         
-        pwmDC.write(1-0.50f); //(1-duty)
-        pwmSY.write(1-0.50f); //(1-duty)
+        pwmDC.write(1-0.900f); //(1-duty)
+        pwmSY.write(1-0.80f); //(1-duty)
         //tickerPWMDC.attach(&tickerPWMDCfunction, 0.1);
         
         //pc.printf("\n\nAccelerating\r\n");
@@ -129,17 +133,17 @@
                         relay2 = 0;//Relay off=1, on=0 // to hear the noise
                         relay3 = 0;//Relay off=1, on=0 // to hear the noise
                         relay4 = 1;//Relay off=1, on=0 // to hear the noise
+                        pwmDC.write(1-0.00f); //(1-duty)
+                        pwmSY.write(1-0.00f); //(1-duty)
                         synchronized = true;
                         mainLoop = false;
                     }
                 }
             }
         }
+        while(true)
+        getPhaseAngle(currentPin, gridPin);
     }
-    
-
-    
-
 }
 
 
@@ -298,4 +302,35 @@
     lcd.locate(0,1);//(col,row)
     lcd.printf("S:%3.1f@%3.1f", syncRMS, syncFreq);
     
+}
+
+
+// #############################################################################
+float getPhaseAngle(AnalogIn voltage_pin, AnalogIn current_pin){
+    bool searchingInstant = true;
+    Timer   timer1;
+    float voltageValue;
+    //float   maxReadedVoltageVoltage, maxReadedVoltageCurrent;//maximum voltage readed by the ADC
+    //maxReadedVoltageVoltage = getVolageReadedMax(voltage_pin);
+    //maxReadedVoltageCurrent= getVolageReadedMax(current_pin);
+    //pc.printf("maxReadedVoltageVoltage: %f \t maxReadedVoltageCurrent: %f\r\n", maxReadedVoltageVoltage, maxReadedVoltageCurrent);
+    while(searchingInstant){
+        voltageValue = voltage_pin.read();
+        //pc.printf("voltageValue voltage:%f\n\r",voltageValue);
+        if(voltageValue < 0.50){
+            timer1.start();
+            searchingInstant = false;
+        }
+    }
+    searchingInstant = true;
+    while(searchingInstant){
+        voltageValue = current_pin.read();
+        //pc.printf("voltageValue current:%f\n\r",voltageValue);
+        if(voltageValue < 0.050){
+            timer1.stop();
+            searchingInstant = false;
+        }
+    }
+    pc.printf("phaseAngle:%f\n\r",timer1.read()*180/0.020);
+    return 0;
 }
\ No newline at end of file