Main code of our project.
Dependencies: TextLCD mbed PID
main.cpp
- Committer:
- nicovv44
- Date:
- 2018-10-09
- Revision:
- 5:e9766e0573d0
- Parent:
- 4:886ce7eefa6e
- Child:
- 6:756d44ed75a5
File content as of revision 5:e9766e0573d0:
// Hello World! for the TextLCD #include "mbed.h" #include "TextLCD.h" TextLCD lcd(D2, D3, D4, D5, D6, D7); // rs, e, d4-d7 Serial pc(USBTX, USBRX); // tx, rx PwmOut pwmDC(D9); PwmOut pwmSY(D13); DigitalOut relay1(D8); DigitalOut relay2(D14); DigitalOut relay3(D11); DigitalOut relay4(D12); AnalogIn syncPin(A0); AnalogIn gridPin(A1); AnalogIn differentialPin(A2); AnalogIn potarDC(A3); AnalogIn potarSY(A4); const float sqrt2 = 1.414213562; Timeout timeout; Ticker tickerPWMDC; volatile bool looping = false; volatile bool synchronized = false; volatile bool mainLoop = true; // ############################################## // ########## PROTOTYPES ######################## // ############################################################################# void stopLooping(void); 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); void tickerPWMDCfunction(); // ############################################## // ########## MAIN ############################## // ############################################################################# int main() { float syncRMS, gridRMS, syncFreq, gridFreq; relay1 = 1;//Relay off=1, on=0 relay2 = 1;//Relay off=1, on=0 relay3 = 1;//Relay off=1, on=0 relay4 = 1;//Relay off=1, on=0 while(mainLoop){ pwmDC.period(0.001f); pwmDC.write(0.04f); pwmSY.period(0.001f); pwmSY.write(0.04f); tickerPWMDC.attach(&tickerPWMDCfunction, 0.1); //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 "); relay1 = 0;//Relay off=1, on=0 // to hear the noise relay2 = 0;//Relay off=1, on=0 // to hear the noise relay3 = 0;//Relay off=1, on=0 // to hear the noise relay4 = 0;//Relay off=1, on=0 // to hear the noise synchronized = true; mainLoop = false; } } } } } } // ############################################## // ########## FUNCTIONS ######################### // ############################################################################# // ISR to stop loping void stopLooping(void) { looping = false;//looping is volatile bool } // ############################################################################# // ISR to update pwmDC with potarDC void tickerPWMDCfunction(){ float valuePotar1; float valuePotar2; valuePotar1 = potarDC.read(); pwmDC.write(valuePotar1); valuePotar2 = potarSY.read(); pwmSY.write(valuePotar2); //lcd.locate(12,0);//(col,row) //lcd.printf("%f",valuePotar); } // ############################################################################# float getVolageRMS(AnalogIn ana_pin){ float v1;//readed voltage float v1Max = 0;//max readed voltage float VRMS; //RMS voltage looping = true; timeout.attach(callback(&stopLooping), 0.020);//T=20ms because f=50Hz while(looping){ v1 = ana_pin.read()*3.3; if(v1 > v1Max){ v1Max = v1; } } VRMS = (v1Max+0.685)*9.32/sqrt2; //pc.printf("VRMS: %f\r\n",VRMS); return VRMS; } // ############################################################################# float getVolageReadedMax(AnalogIn ana_pin){ float v1;//readed voltage float v1Max = 0;//max readed voltage looping = true; timeout.attach(callback(&stopLooping), 0.025);//T=25>20ms because f=50Hz while(looping){ v1 = ana_pin.read()*3.3; if(v1 > v1Max){ v1Max = v1; } } return v1Max; } // ############################################################################# float getFrequency(AnalogIn ana_pin){ float freq; //frequency float maxReadedVoltage;//maximum voltage readed by the ADC float readedVoltage;//readed voltage int nbrRisingEdge=0;// number of rising edge detected float T;//Periode Timer timer; maxReadedVoltage = getVolageReadedMax(ana_pin); //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 while(nbrRisingEdge<2 and looping){ readedVoltage = ana_pin.read()*3.3; if(readedVoltage<(maxReadedVoltage/2)){//rising edge detection ready aboveLine = false; } if((maxReadedVoltage/2)<readedVoltage && aboveLine==false){//rising edge detected aboveLine = true; if(nbrRisingEdge==0) timer.start(); if(nbrRisingEdge==1) timer.stop(); nbrRisingEdge++; } } if(nbrRisingEdge!=2){ lcd.locate(13,1); lcd.printf("f!%d",nbrRisingEdge); } T = timer.read(); freq = 1/T; //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); }