Main code of our project.
Dependencies: TextLCD mbed PID
Diff: main.cpp
- Revision:
- 10:193cd7ad5718
- Parent:
- 8:a161b056971c
--- a/main.cpp Wed Oct 10 06:23:14 2018 +0000 +++ b/main.cpp Wed Oct 17 06:42:55 2018 +0000 @@ -64,19 +64,24 @@ relay4 = 0;//Relay off=1, on=0 while(mainLoop){ - pwmDC.period(0.001f); - pwmDC.write(1-0.96f); //(1-duty) - pwmSY.period(0.001f); //(1-duty) + pwmDC.period(0.00005f); + pwmDC.write(1-0.90f); //(1-duty) + pwmSY.period(0.00005f); //(1-duty) pwmSY.write(1-0.80f); //tickerPWMDC.attach(&tickerPWMDCfunction, 0.1); + pc.printf("\nAccelerating\n\r"); + wait(5); + pwmDC.write(1-0.70f); //(1-duty) + //manual synchronisation while(!synchronized){ //measure and calculate desired value - syncRMS = getVolageRMS(syncPin); - gridRMS = getVolageRMS(gridPin); + //syncRMS = getVolageRMS(syncPin); + //gridRMS = getVolageRMS(gridPin); syncFreq = getFrequency(syncPin); - gridFreq = getFrequency(gridPin); + pc.printf("syncFreq:%f\r\n",syncFreq); + //gridFreq = getFrequency(gridPin); //display values on LCD displayLCD(syncRMS, gridRMS, syncFreq, gridFreq); //voltage and frequency matching @@ -140,7 +145,7 @@ float v1Max = 0;//max readed voltage float VRMS; //RMS voltage looping = true; - timeout.attach(callback(&stopLooping), 0.020);//T=20ms because f=50Hz + timeout.attach(callback(&stopLooping), 0.050);//T=20ms because f=50Hz while(looping){ v1 = ana_pin.read()*3.3; if(v1 > v1Max){ @@ -158,7 +163,7 @@ float v1;//readed voltage float v1Max = 0;//max readed voltage looping = true; - timeout.attach(callback(&stopLooping), 0.025);//T=25>20ms because f=50Hz + timeout.attach(callback(&stopLooping), 0.050);//T=25>20ms because f=50Hz while(looping){ v1 = ana_pin.read()*3.3; if(v1 > v1Max){ @@ -178,8 +183,9 @@ 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; + bool allowedClicTimer = false; looping = true; timeout.attach(callback(&stopLooping), 1);//try to find rising edges during 1sec max while(nbrRisingEdge<2 and looping){ @@ -188,13 +194,18 @@ aboveLine = false; } if((maxReadedVoltage/2)<readedVoltage && aboveLine==false){//rising edge detected + allowedClicTimer = true; aboveLine = true; + } + if((maxReadedVoltage*2/3)<readedVoltage && allowedClicTimer==true){//rising edge detected + allowedClicTimer = false; if(nbrRisingEdge==0) timer.start(); if(nbrRisingEdge==1) timer.stop(); nbrRisingEdge++; } + } if(nbrRisingEdge!=2){ lcd.locate(13,1); @@ -204,6 +215,8 @@ freq = 1/T; //pc.printf("T: %f\r\n",T); //pc.printf("freq: %f\r\n\n",freq); + if(looping==false) + freq = 0; return freq; }