Main code of our project.

Dependencies:   TextLCD mbed PID

Revision:
4:886ce7eefa6e
Parent:
3:a1b11dfd26f3
Child:
5:e9766e0573d0
--- a/main.cpp	Sun Oct 07 01:19:29 2018 +0000
+++ b/main.cpp	Sun Oct 07 02:25:50 2018 +0000
@@ -16,7 +16,8 @@
 
 Timeout             timeout;
 volatile bool       looping = false;
-
+volatile bool       synchronized = false;
+volatile bool       mainLoop = true;
 
 
 
@@ -29,6 +30,7 @@
 float getVolageRMS(AnalogIn ana_pin);
 float getVolageReadedMax(AnalogIn ana_pin);
 float getFrequency(AnalogIn ana_pin);
+void displayLCD(float syncRMS, float gridRMS, float syncFreq, float gridFreq);
 
 
 
@@ -49,25 +51,42 @@
     float syncRMS, gridRMS, syncFreq, gridFreq;
     
     relaySync = 1;//Relay off=1, on=0
-    while(1){
-        /*if(getVolageReadedMax(analog_pin1) < 0.020){
-            relaySync = 0;//Relay off=1, on=0
-            lcd.locate(0,0);
-            lcd.printf("SYNC");
-        }*/
+    while(mainLoop){
+        //measure and calculate desired value
         syncRMS = getVolageRMS(syncPin);
         gridRMS = getVolageRMS(gridPin);
         syncFreq = getFrequency(syncPin);
         gridFreq = getFrequency(gridPin);
+        //display values on LCD
+        displayLCD(syncRMS, gridRMS, syncFreq, gridFreq);
         
-        lcd.locate(0,0);//(col,row)
-        lcd.printf("                ");
-        lcd.locate(0,1);//(col,row)
-        lcd.printf("                ");
-        lcd.locate(0,0);//(col,row)
-        lcd.printf("G:%3.1f@%3.1f", gridRMS, gridFreq);
-        lcd.locate(0,1);//(col,row)
-        lcd.printf("S:%3.1f@%3.1f", syncRMS, syncFreq);
+        //manual synchronisation
+        while(!synchronized){
+            //measure and calculate desired value
+            syncRMS = getVolageRMS(syncPin);
+            gridRMS = getVolageRMS(gridPin);
+            syncFreq = getFrequency(syncPin);
+            gridFreq = getFrequency(gridPin);
+            //display values on LCD
+            displayLCD(syncRMS, gridRMS, syncFreq, gridFreq);
+            //voltage and frequency matching
+            if(abs(syncRMS-gridRMS)<0.5 && abs(syncFreq-gridFreq)<0.1){
+                pc.printf("voltage and freqency OK\r\n");
+                lcd.locate(0,0);//(col,row)
+                lcd.printf("V&f OK");
+                while(!synchronized){//phase matching loop
+                    pc.printf("diff: %f\n\r", getVolageReadedMax(differentialPin));
+                    //phase matching
+                    if(getVolageReadedMax(differentialPin) < 0.350){
+                        pc.printf("SYNCHONIZATION OK\r\n\n");
+                        lcd.locate(0,1);//(col,row)
+                        lcd.printf("SYNC OK ");
+                        synchronized = true;
+                        mainLoop = false;
+                    }
+                }
+            }
+        }
     }
     
     
@@ -105,7 +124,7 @@
         }
     }
     VRMS = (v1Max+0.685)*9.32/sqrt2;
-    pc.printf("VRMS: %f\r\n",VRMS);
+    //pc.printf("VRMS: %f\r\n",VRMS);
     return VRMS;
 }
 
@@ -135,7 +154,7 @@
     float   T;//Periode
     Timer   timer;
     maxReadedVoltage = getVolageReadedMax(ana_pin);
-    pc.printf("maxReadedVoltage: %f\r\n",maxReadedVoltage);
+    //pc.printf("maxReadedVoltage: %f\r\n",maxReadedVoltage);
     bool aboveLine = true;
     looping = true;
     timeout.attach(callback(&stopLooping), 1);//try to find rising edges during 1sec max
@@ -159,7 +178,20 @@
     }
     T = timer.read();
     freq = 1/T;
-    pc.printf("T: %f\r\n",T);
-    pc.printf("freq: %f\r\n\n",freq);
+    //pc.printf("T: %f\r\n",T);
+    //pc.printf("freq: %f\r\n\n",freq);
     return freq;
+}
+
+
+// #############################################################################
+void displayLCD(float syncRMS, float gridRMS, float syncFreq, float gridFreq){
+    lcd.locate(0,0);//(col,row)
+        lcd.printf("                ");
+        lcd.locate(0,1);//(col,row)
+        lcd.printf("                ");
+        lcd.locate(0,0);//(col,row)
+        lcd.printf("G:%3.1f@%3.1f", gridRMS, gridFreq);
+        lcd.locate(0,1);//(col,row)
+        lcd.printf("S:%3.1f@%3.1f", syncRMS, syncFreq);
 }
\ No newline at end of file