Main code of our project.
Dependencies: TextLCD mbed PID
main.cpp@7:1f7d87007512, 2018-10-10 (annotated)
- Committer:
- nicovv44
- Date:
- Wed Oct 10 05:50:52 2018 +0000
- Revision:
- 7:1f7d87007512
- Parent:
- 6:756d44ed75a5
- Child:
- 8:a161b056971c
2 PWM and power OK, SYNC with relays OK.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nicovv44 | 0:1f66eaf1013d | 1 | // Hello World! for the TextLCD |
nicovv44 | 0:1f66eaf1013d | 2 | |
nicovv44 | 0:1f66eaf1013d | 3 | #include "mbed.h" |
nicovv44 | 0:1f66eaf1013d | 4 | #include "TextLCD.h" |
nicovv44 | 0:1f66eaf1013d | 5 | |
nicovv44 | 0:1f66eaf1013d | 6 | TextLCD lcd(D2, D3, D4, D5, D6, D7); // rs, e, d4-d7 |
nicovv44 | 0:1f66eaf1013d | 7 | Serial pc(USBTX, USBRX); // tx, rx |
nicovv44 | 5:e9766e0573d0 | 8 | PwmOut pwmDC(D9); |
nicovv44 | 5:e9766e0573d0 | 9 | PwmOut pwmSY(D13); |
nicovv44 | 5:e9766e0573d0 | 10 | DigitalOut relay1(D8); |
nicovv44 | 5:e9766e0573d0 | 11 | DigitalOut relay2(D14); |
nicovv44 | 5:e9766e0573d0 | 12 | DigitalOut relay3(D11); |
nicovv44 | 5:e9766e0573d0 | 13 | DigitalOut relay4(D12); |
nicovv44 | 0:1f66eaf1013d | 14 | |
nicovv44 | 3:a1b11dfd26f3 | 15 | AnalogIn syncPin(A0); |
nicovv44 | 3:a1b11dfd26f3 | 16 | AnalogIn gridPin(A1); |
nicovv44 | 3:a1b11dfd26f3 | 17 | AnalogIn differentialPin(A2); |
nicovv44 | 5:e9766e0573d0 | 18 | AnalogIn potarDC(A3); |
nicovv44 | 5:e9766e0573d0 | 19 | AnalogIn potarSY(A4); |
nicovv44 | 0:1f66eaf1013d | 20 | |
nicovv44 | 0:1f66eaf1013d | 21 | const float sqrt2 = 1.414213562; |
nicovv44 | 0:1f66eaf1013d | 22 | |
nicovv44 | 0:1f66eaf1013d | 23 | Timeout timeout; |
nicovv44 | 5:e9766e0573d0 | 24 | Ticker tickerPWMDC; |
nicovv44 | 0:1f66eaf1013d | 25 | volatile bool looping = false; |
nicovv44 | 4:886ce7eefa6e | 26 | volatile bool synchronized = false; |
nicovv44 | 4:886ce7eefa6e | 27 | volatile bool mainLoop = true; |
nicovv44 | 0:1f66eaf1013d | 28 | |
nicovv44 | 0:1f66eaf1013d | 29 | |
nicovv44 | 0:1f66eaf1013d | 30 | |
nicovv44 | 0:1f66eaf1013d | 31 | |
nicovv44 | 0:1f66eaf1013d | 32 | |
nicovv44 | 0:1f66eaf1013d | 33 | // ############################################## |
nicovv44 | 0:1f66eaf1013d | 34 | // ########## PROTOTYPES ######################## |
nicovv44 | 0:1f66eaf1013d | 35 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 36 | void stopLooping(void); |
nicovv44 | 0:1f66eaf1013d | 37 | float getVolageRMS(AnalogIn ana_pin); |
nicovv44 | 0:1f66eaf1013d | 38 | float getVolageReadedMax(AnalogIn ana_pin); |
nicovv44 | 0:1f66eaf1013d | 39 | float getFrequency(AnalogIn ana_pin); |
nicovv44 | 4:886ce7eefa6e | 40 | void displayLCD(float syncRMS, float gridRMS, float syncFreq, float gridFreq); |
nicovv44 | 5:e9766e0573d0 | 41 | void tickerPWMDCfunction(); |
nicovv44 | 0:1f66eaf1013d | 42 | |
nicovv44 | 0:1f66eaf1013d | 43 | |
nicovv44 | 0:1f66eaf1013d | 44 | |
nicovv44 | 0:1f66eaf1013d | 45 | |
nicovv44 | 0:1f66eaf1013d | 46 | |
nicovv44 | 0:1f66eaf1013d | 47 | |
nicovv44 | 0:1f66eaf1013d | 48 | |
nicovv44 | 0:1f66eaf1013d | 49 | |
nicovv44 | 0:1f66eaf1013d | 50 | |
nicovv44 | 0:1f66eaf1013d | 51 | |
nicovv44 | 0:1f66eaf1013d | 52 | |
nicovv44 | 0:1f66eaf1013d | 53 | |
nicovv44 | 0:1f66eaf1013d | 54 | |
nicovv44 | 0:1f66eaf1013d | 55 | // ############################################## |
nicovv44 | 0:1f66eaf1013d | 56 | // ########## MAIN ############################## |
nicovv44 | 0:1f66eaf1013d | 57 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 58 | int main() { |
nicovv44 | 3:a1b11dfd26f3 | 59 | float syncRMS, gridRMS, syncFreq, gridFreq; |
nicovv44 | 0:1f66eaf1013d | 60 | |
nicovv44 | 5:e9766e0573d0 | 61 | relay1 = 1;//Relay off=1, on=0 |
nicovv44 | 5:e9766e0573d0 | 62 | relay2 = 1;//Relay off=1, on=0 |
nicovv44 | 5:e9766e0573d0 | 63 | relay3 = 1;//Relay off=1, on=0 |
nicovv44 | 5:e9766e0573d0 | 64 | relay4 = 1;//Relay off=1, on=0 |
nicovv44 | 5:e9766e0573d0 | 65 | |
nicovv44 | 4:886ce7eefa6e | 66 | while(mainLoop){ |
nicovv44 | 5:e9766e0573d0 | 67 | pwmDC.period(0.001f); |
nicovv44 | 5:e9766e0573d0 | 68 | pwmDC.write(0.04f); |
nicovv44 | 5:e9766e0573d0 | 69 | pwmSY.period(0.001f); |
nicovv44 | 5:e9766e0573d0 | 70 | pwmSY.write(0.04f); |
nicovv44 | 5:e9766e0573d0 | 71 | tickerPWMDC.attach(&tickerPWMDCfunction, 0.1); |
nicovv44 | 3:a1b11dfd26f3 | 72 | |
nicovv44 | 4:886ce7eefa6e | 73 | //manual synchronisation |
nicovv44 | 4:886ce7eefa6e | 74 | while(!synchronized){ |
nicovv44 | 4:886ce7eefa6e | 75 | //measure and calculate desired value |
nicovv44 | 4:886ce7eefa6e | 76 | syncRMS = getVolageRMS(syncPin); |
nicovv44 | 4:886ce7eefa6e | 77 | gridRMS = getVolageRMS(gridPin); |
nicovv44 | 4:886ce7eefa6e | 78 | syncFreq = getFrequency(syncPin); |
nicovv44 | 4:886ce7eefa6e | 79 | gridFreq = getFrequency(gridPin); |
nicovv44 | 4:886ce7eefa6e | 80 | //display values on LCD |
nicovv44 | 4:886ce7eefa6e | 81 | displayLCD(syncRMS, gridRMS, syncFreq, gridFreq); |
nicovv44 | 4:886ce7eefa6e | 82 | //voltage and frequency matching |
nicovv44 | 4:886ce7eefa6e | 83 | if(abs(syncRMS-gridRMS)<0.5 && abs(syncFreq-gridFreq)<0.1){ |
nicovv44 | 4:886ce7eefa6e | 84 | pc.printf("voltage and freqency OK\r\n"); |
nicovv44 | 4:886ce7eefa6e | 85 | lcd.locate(0,0);//(col,row) |
nicovv44 | 4:886ce7eefa6e | 86 | lcd.printf("V&f OK"); |
nicovv44 | 4:886ce7eefa6e | 87 | while(!synchronized){//phase matching loop |
nicovv44 | 4:886ce7eefa6e | 88 | //phase matching |
nicovv44 | 7:1f7d87007512 | 89 | if(getVolageReadedMax(differentialPin) < 0.050){ |
nicovv44 | 4:886ce7eefa6e | 90 | pc.printf("SYNCHONIZATION OK\r\n\n"); |
nicovv44 | 4:886ce7eefa6e | 91 | lcd.locate(0,1);//(col,row) |
nicovv44 | 4:886ce7eefa6e | 92 | lcd.printf("SYNC OK "); |
nicovv44 | 5:e9766e0573d0 | 93 | relay1 = 0;//Relay off=1, on=0 // to hear the noise |
nicovv44 | 5:e9766e0573d0 | 94 | relay2 = 0;//Relay off=1, on=0 // to hear the noise |
nicovv44 | 5:e9766e0573d0 | 95 | relay3 = 0;//Relay off=1, on=0 // to hear the noise |
nicovv44 | 5:e9766e0573d0 | 96 | relay4 = 0;//Relay off=1, on=0 // to hear the noise |
nicovv44 | 4:886ce7eefa6e | 97 | synchronized = true; |
nicovv44 | 4:886ce7eefa6e | 98 | mainLoop = false; |
nicovv44 | 4:886ce7eefa6e | 99 | } |
nicovv44 | 4:886ce7eefa6e | 100 | } |
nicovv44 | 4:886ce7eefa6e | 101 | } |
nicovv44 | 4:886ce7eefa6e | 102 | } |
nicovv44 | 1:f31a46c62d10 | 103 | } |
nicovv44 | 1:f31a46c62d10 | 104 | |
nicovv44 | 5:e9766e0573d0 | 105 | |
nicovv44 | 1:f31a46c62d10 | 106 | |
nicovv44 | 3:a1b11dfd26f3 | 107 | |
nicovv44 | 0:1f66eaf1013d | 108 | } |
nicovv44 | 0:1f66eaf1013d | 109 | |
nicovv44 | 0:1f66eaf1013d | 110 | |
nicovv44 | 0:1f66eaf1013d | 111 | |
nicovv44 | 0:1f66eaf1013d | 112 | |
nicovv44 | 0:1f66eaf1013d | 113 | |
nicovv44 | 0:1f66eaf1013d | 114 | |
nicovv44 | 0:1f66eaf1013d | 115 | |
nicovv44 | 0:1f66eaf1013d | 116 | // ############################################## |
nicovv44 | 0:1f66eaf1013d | 117 | // ########## FUNCTIONS ######################### |
nicovv44 | 0:1f66eaf1013d | 118 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 119 | // ISR to stop loping |
nicovv44 | 0:1f66eaf1013d | 120 | void stopLooping(void) { |
nicovv44 | 0:1f66eaf1013d | 121 | looping = false;//looping is volatile bool |
nicovv44 | 0:1f66eaf1013d | 122 | } |
nicovv44 | 0:1f66eaf1013d | 123 | |
nicovv44 | 0:1f66eaf1013d | 124 | // ############################################################################# |
nicovv44 | 5:e9766e0573d0 | 125 | // ISR to update pwmDC with potarDC |
nicovv44 | 5:e9766e0573d0 | 126 | void tickerPWMDCfunction(){ |
nicovv44 | 5:e9766e0573d0 | 127 | float valuePotar1; |
nicovv44 | 5:e9766e0573d0 | 128 | float valuePotar2; |
nicovv44 | 5:e9766e0573d0 | 129 | valuePotar1 = potarDC.read(); |
nicovv44 | 7:1f7d87007512 | 130 | pwmDC.write(1-valuePotar1); |
nicovv44 | 5:e9766e0573d0 | 131 | valuePotar2 = potarSY.read(); |
nicovv44 | 7:1f7d87007512 | 132 | pwmSY.write(1-valuePotar2); |
nicovv44 | 5:e9766e0573d0 | 133 | //lcd.locate(12,0);//(col,row) |
nicovv44 | 5:e9766e0573d0 | 134 | //lcd.printf("%f",valuePotar); |
nicovv44 | 5:e9766e0573d0 | 135 | } |
nicovv44 | 5:e9766e0573d0 | 136 | |
nicovv44 | 5:e9766e0573d0 | 137 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 138 | float getVolageRMS(AnalogIn ana_pin){ |
nicovv44 | 0:1f66eaf1013d | 139 | float v1;//readed voltage |
nicovv44 | 0:1f66eaf1013d | 140 | float v1Max = 0;//max readed voltage |
nicovv44 | 0:1f66eaf1013d | 141 | float VRMS; //RMS voltage |
nicovv44 | 0:1f66eaf1013d | 142 | looping = true; |
nicovv44 | 1:f31a46c62d10 | 143 | timeout.attach(callback(&stopLooping), 0.020);//T=20ms because f=50Hz |
nicovv44 | 0:1f66eaf1013d | 144 | while(looping){ |
nicovv44 | 0:1f66eaf1013d | 145 | v1 = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 146 | if(v1 > v1Max){ |
nicovv44 | 0:1f66eaf1013d | 147 | v1Max = v1; |
nicovv44 | 0:1f66eaf1013d | 148 | } |
nicovv44 | 0:1f66eaf1013d | 149 | } |
nicovv44 | 3:a1b11dfd26f3 | 150 | VRMS = (v1Max+0.685)*9.32/sqrt2; |
nicovv44 | 4:886ce7eefa6e | 151 | //pc.printf("VRMS: %f\r\n",VRMS); |
nicovv44 | 0:1f66eaf1013d | 152 | return VRMS; |
nicovv44 | 0:1f66eaf1013d | 153 | } |
nicovv44 | 0:1f66eaf1013d | 154 | |
nicovv44 | 0:1f66eaf1013d | 155 | |
nicovv44 | 0:1f66eaf1013d | 156 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 157 | float getVolageReadedMax(AnalogIn ana_pin){ |
nicovv44 | 0:1f66eaf1013d | 158 | float v1;//readed voltage |
nicovv44 | 0:1f66eaf1013d | 159 | float v1Max = 0;//max readed voltage |
nicovv44 | 0:1f66eaf1013d | 160 | looping = true; |
nicovv44 | 1:f31a46c62d10 | 161 | timeout.attach(callback(&stopLooping), 0.025);//T=25>20ms because f=50Hz |
nicovv44 | 0:1f66eaf1013d | 162 | while(looping){ |
nicovv44 | 0:1f66eaf1013d | 163 | v1 = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 164 | if(v1 > v1Max){ |
nicovv44 | 0:1f66eaf1013d | 165 | v1Max = v1; |
nicovv44 | 0:1f66eaf1013d | 166 | } |
nicovv44 | 0:1f66eaf1013d | 167 | } |
nicovv44 | 0:1f66eaf1013d | 168 | return v1Max; |
nicovv44 | 0:1f66eaf1013d | 169 | } |
nicovv44 | 0:1f66eaf1013d | 170 | |
nicovv44 | 1:f31a46c62d10 | 171 | |
nicovv44 | 0:1f66eaf1013d | 172 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 173 | float getFrequency(AnalogIn ana_pin){ |
nicovv44 | 0:1f66eaf1013d | 174 | float freq; //frequency |
nicovv44 | 0:1f66eaf1013d | 175 | float maxReadedVoltage;//maximum voltage readed by the ADC |
nicovv44 | 0:1f66eaf1013d | 176 | float readedVoltage;//readed voltage |
nicovv44 | 0:1f66eaf1013d | 177 | int nbrRisingEdge=0;// number of rising edge detected |
nicovv44 | 0:1f66eaf1013d | 178 | float T;//Periode |
nicovv44 | 0:1f66eaf1013d | 179 | Timer timer; |
nicovv44 | 0:1f66eaf1013d | 180 | maxReadedVoltage = getVolageReadedMax(ana_pin); |
nicovv44 | 4:886ce7eefa6e | 181 | //pc.printf("maxReadedVoltage: %f\r\n",maxReadedVoltage); |
nicovv44 | 0:1f66eaf1013d | 182 | bool aboveLine = true; |
nicovv44 | 1:f31a46c62d10 | 183 | looping = true; |
nicovv44 | 1:f31a46c62d10 | 184 | timeout.attach(callback(&stopLooping), 1);//try to find rising edges during 1sec max |
nicovv44 | 1:f31a46c62d10 | 185 | while(nbrRisingEdge<2 and looping){ |
nicovv44 | 0:1f66eaf1013d | 186 | readedVoltage = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 187 | if(readedVoltage<(maxReadedVoltage/2)){//rising edge detection ready |
nicovv44 | 0:1f66eaf1013d | 188 | aboveLine = false; |
nicovv44 | 0:1f66eaf1013d | 189 | } |
nicovv44 | 0:1f66eaf1013d | 190 | if((maxReadedVoltage/2)<readedVoltage && aboveLine==false){//rising edge detected |
nicovv44 | 0:1f66eaf1013d | 191 | aboveLine = true; |
nicovv44 | 0:1f66eaf1013d | 192 | if(nbrRisingEdge==0) |
nicovv44 | 0:1f66eaf1013d | 193 | timer.start(); |
nicovv44 | 0:1f66eaf1013d | 194 | if(nbrRisingEdge==1) |
nicovv44 | 0:1f66eaf1013d | 195 | timer.stop(); |
nicovv44 | 0:1f66eaf1013d | 196 | nbrRisingEdge++; |
nicovv44 | 0:1f66eaf1013d | 197 | } |
nicovv44 | 0:1f66eaf1013d | 198 | } |
nicovv44 | 1:f31a46c62d10 | 199 | if(nbrRisingEdge!=2){ |
nicovv44 | 1:f31a46c62d10 | 200 | lcd.locate(13,1); |
nicovv44 | 1:f31a46c62d10 | 201 | lcd.printf("f!%d",nbrRisingEdge); |
nicovv44 | 1:f31a46c62d10 | 202 | } |
nicovv44 | 0:1f66eaf1013d | 203 | T = timer.read(); |
nicovv44 | 0:1f66eaf1013d | 204 | freq = 1/T; |
nicovv44 | 4:886ce7eefa6e | 205 | //pc.printf("T: %f\r\n",T); |
nicovv44 | 4:886ce7eefa6e | 206 | //pc.printf("freq: %f\r\n\n",freq); |
nicovv44 | 0:1f66eaf1013d | 207 | return freq; |
nicovv44 | 4:886ce7eefa6e | 208 | } |
nicovv44 | 4:886ce7eefa6e | 209 | |
nicovv44 | 4:886ce7eefa6e | 210 | |
nicovv44 | 4:886ce7eefa6e | 211 | // ############################################################################# |
nicovv44 | 4:886ce7eefa6e | 212 | void displayLCD(float syncRMS, float gridRMS, float syncFreq, float gridFreq){ |
nicovv44 | 4:886ce7eefa6e | 213 | lcd.locate(0,0);//(col,row) |
nicovv44 | 4:886ce7eefa6e | 214 | lcd.printf(" "); |
nicovv44 | 4:886ce7eefa6e | 215 | lcd.locate(0,1);//(col,row) |
nicovv44 | 4:886ce7eefa6e | 216 | lcd.printf(" "); |
nicovv44 | 4:886ce7eefa6e | 217 | lcd.locate(0,0);//(col,row) |
nicovv44 | 4:886ce7eefa6e | 218 | lcd.printf("G:%3.1f@%3.1f", gridRMS, gridFreq); |
nicovv44 | 4:886ce7eefa6e | 219 | lcd.locate(0,1);//(col,row) |
nicovv44 | 4:886ce7eefa6e | 220 | lcd.printf("S:%3.1f@%3.1f", syncRMS, syncFreq); |
nicovv44 | 1:f31a46c62d10 | 221 | } |