Dependencies:   TextLCD mbed PID

Committer:
nicovv44
Date:
Sun Oct 07 02:25:50 2018 +0000
Revision:
4:886ce7eefa6e
Parent:
3:a1b11dfd26f3
Child:
5:e9766e0573d0
Manual synchronisation is fully working.

Who changed what in which revision?

UserRevisionLine numberNew 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 1:f31a46c62d10 8 //PwmOut pwm1(D9);
nicovv44 1:f31a46c62d10 9 DigitalOut relaySync(D8);
nicovv44 0:1f66eaf1013d 10
nicovv44 3:a1b11dfd26f3 11 AnalogIn syncPin(A0);
nicovv44 3:a1b11dfd26f3 12 AnalogIn gridPin(A1);
nicovv44 3:a1b11dfd26f3 13 AnalogIn differentialPin(A2);
nicovv44 0:1f66eaf1013d 14
nicovv44 0:1f66eaf1013d 15 const float sqrt2 = 1.414213562;
nicovv44 0:1f66eaf1013d 16
nicovv44 0:1f66eaf1013d 17 Timeout timeout;
nicovv44 0:1f66eaf1013d 18 volatile bool looping = false;
nicovv44 4:886ce7eefa6e 19 volatile bool synchronized = false;
nicovv44 4:886ce7eefa6e 20 volatile bool mainLoop = true;
nicovv44 0:1f66eaf1013d 21
nicovv44 0:1f66eaf1013d 22
nicovv44 0:1f66eaf1013d 23
nicovv44 0:1f66eaf1013d 24
nicovv44 0:1f66eaf1013d 25
nicovv44 0:1f66eaf1013d 26 // ##############################################
nicovv44 0:1f66eaf1013d 27 // ########## PROTOTYPES ########################
nicovv44 0:1f66eaf1013d 28 // #############################################################################
nicovv44 0:1f66eaf1013d 29 void stopLooping(void);
nicovv44 0:1f66eaf1013d 30 float getVolageRMS(AnalogIn ana_pin);
nicovv44 0:1f66eaf1013d 31 float getVolageReadedMax(AnalogIn ana_pin);
nicovv44 0:1f66eaf1013d 32 float getFrequency(AnalogIn ana_pin);
nicovv44 4:886ce7eefa6e 33 void displayLCD(float syncRMS, float gridRMS, float syncFreq, float gridFreq);
nicovv44 0:1f66eaf1013d 34
nicovv44 0:1f66eaf1013d 35
nicovv44 0:1f66eaf1013d 36
nicovv44 0:1f66eaf1013d 37
nicovv44 0:1f66eaf1013d 38
nicovv44 0:1f66eaf1013d 39
nicovv44 0:1f66eaf1013d 40
nicovv44 0:1f66eaf1013d 41
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 // ########## MAIN ##############################
nicovv44 0:1f66eaf1013d 49 // #############################################################################
nicovv44 0:1f66eaf1013d 50 int main() {
nicovv44 3:a1b11dfd26f3 51 float syncRMS, gridRMS, syncFreq, gridFreq;
nicovv44 0:1f66eaf1013d 52
nicovv44 1:f31a46c62d10 53 relaySync = 1;//Relay off=1, on=0
nicovv44 4:886ce7eefa6e 54 while(mainLoop){
nicovv44 4:886ce7eefa6e 55 //measure and calculate desired value
nicovv44 3:a1b11dfd26f3 56 syncRMS = getVolageRMS(syncPin);
nicovv44 3:a1b11dfd26f3 57 gridRMS = getVolageRMS(gridPin);
nicovv44 3:a1b11dfd26f3 58 syncFreq = getFrequency(syncPin);
nicovv44 3:a1b11dfd26f3 59 gridFreq = getFrequency(gridPin);
nicovv44 4:886ce7eefa6e 60 //display values on LCD
nicovv44 4:886ce7eefa6e 61 displayLCD(syncRMS, gridRMS, syncFreq, gridFreq);
nicovv44 3:a1b11dfd26f3 62
nicovv44 4:886ce7eefa6e 63 //manual synchronisation
nicovv44 4:886ce7eefa6e 64 while(!synchronized){
nicovv44 4:886ce7eefa6e 65 //measure and calculate desired value
nicovv44 4:886ce7eefa6e 66 syncRMS = getVolageRMS(syncPin);
nicovv44 4:886ce7eefa6e 67 gridRMS = getVolageRMS(gridPin);
nicovv44 4:886ce7eefa6e 68 syncFreq = getFrequency(syncPin);
nicovv44 4:886ce7eefa6e 69 gridFreq = getFrequency(gridPin);
nicovv44 4:886ce7eefa6e 70 //display values on LCD
nicovv44 4:886ce7eefa6e 71 displayLCD(syncRMS, gridRMS, syncFreq, gridFreq);
nicovv44 4:886ce7eefa6e 72 //voltage and frequency matching
nicovv44 4:886ce7eefa6e 73 if(abs(syncRMS-gridRMS)<0.5 && abs(syncFreq-gridFreq)<0.1){
nicovv44 4:886ce7eefa6e 74 pc.printf("voltage and freqency OK\r\n");
nicovv44 4:886ce7eefa6e 75 lcd.locate(0,0);//(col,row)
nicovv44 4:886ce7eefa6e 76 lcd.printf("V&f OK");
nicovv44 4:886ce7eefa6e 77 while(!synchronized){//phase matching loop
nicovv44 4:886ce7eefa6e 78 pc.printf("diff: %f\n\r", getVolageReadedMax(differentialPin));
nicovv44 4:886ce7eefa6e 79 //phase matching
nicovv44 4:886ce7eefa6e 80 if(getVolageReadedMax(differentialPin) < 0.350){
nicovv44 4:886ce7eefa6e 81 pc.printf("SYNCHONIZATION OK\r\n\n");
nicovv44 4:886ce7eefa6e 82 lcd.locate(0,1);//(col,row)
nicovv44 4:886ce7eefa6e 83 lcd.printf("SYNC OK ");
nicovv44 4:886ce7eefa6e 84 synchronized = true;
nicovv44 4:886ce7eefa6e 85 mainLoop = false;
nicovv44 4:886ce7eefa6e 86 }
nicovv44 4:886ce7eefa6e 87 }
nicovv44 4:886ce7eefa6e 88 }
nicovv44 4:886ce7eefa6e 89 }
nicovv44 1:f31a46c62d10 90 }
nicovv44 1:f31a46c62d10 91
nicovv44 1:f31a46c62d10 92
nicovv44 1:f31a46c62d10 93 // specify period first
nicovv44 1:f31a46c62d10 94 //pwm1.period(0.1f); // 4 second period
nicovv44 1:f31a46c62d10 95 //pwm1.write(0.50f); // 50% duty cycle, relative to period
nicovv44 3:a1b11dfd26f3 96
nicovv44 0:1f66eaf1013d 97 }
nicovv44 0:1f66eaf1013d 98
nicovv44 0:1f66eaf1013d 99
nicovv44 0:1f66eaf1013d 100
nicovv44 0:1f66eaf1013d 101
nicovv44 0:1f66eaf1013d 102
nicovv44 0:1f66eaf1013d 103
nicovv44 0:1f66eaf1013d 104
nicovv44 0:1f66eaf1013d 105 // ##############################################
nicovv44 0:1f66eaf1013d 106 // ########## FUNCTIONS #########################
nicovv44 0:1f66eaf1013d 107 // #############################################################################
nicovv44 0:1f66eaf1013d 108 // ISR to stop loping
nicovv44 0:1f66eaf1013d 109 void stopLooping(void) {
nicovv44 0:1f66eaf1013d 110 looping = false;//looping is volatile bool
nicovv44 0:1f66eaf1013d 111 }
nicovv44 0:1f66eaf1013d 112
nicovv44 0:1f66eaf1013d 113 // #############################################################################
nicovv44 0:1f66eaf1013d 114 float getVolageRMS(AnalogIn ana_pin){
nicovv44 0:1f66eaf1013d 115 float v1;//readed voltage
nicovv44 0:1f66eaf1013d 116 float v1Max = 0;//max readed voltage
nicovv44 0:1f66eaf1013d 117 float VRMS; //RMS voltage
nicovv44 0:1f66eaf1013d 118 looping = true;
nicovv44 1:f31a46c62d10 119 timeout.attach(callback(&stopLooping), 0.020);//T=20ms because f=50Hz
nicovv44 0:1f66eaf1013d 120 while(looping){
nicovv44 0:1f66eaf1013d 121 v1 = ana_pin.read()*3.3;
nicovv44 0:1f66eaf1013d 122 if(v1 > v1Max){
nicovv44 0:1f66eaf1013d 123 v1Max = v1;
nicovv44 0:1f66eaf1013d 124 }
nicovv44 0:1f66eaf1013d 125 }
nicovv44 3:a1b11dfd26f3 126 VRMS = (v1Max+0.685)*9.32/sqrt2;
nicovv44 4:886ce7eefa6e 127 //pc.printf("VRMS: %f\r\n",VRMS);
nicovv44 0:1f66eaf1013d 128 return VRMS;
nicovv44 0:1f66eaf1013d 129 }
nicovv44 0:1f66eaf1013d 130
nicovv44 0:1f66eaf1013d 131
nicovv44 0:1f66eaf1013d 132 // #############################################################################
nicovv44 0:1f66eaf1013d 133 float getVolageReadedMax(AnalogIn ana_pin){
nicovv44 0:1f66eaf1013d 134 float v1;//readed voltage
nicovv44 0:1f66eaf1013d 135 float v1Max = 0;//max readed voltage
nicovv44 0:1f66eaf1013d 136 looping = true;
nicovv44 1:f31a46c62d10 137 timeout.attach(callback(&stopLooping), 0.025);//T=25>20ms because f=50Hz
nicovv44 0:1f66eaf1013d 138 while(looping){
nicovv44 0:1f66eaf1013d 139 v1 = ana_pin.read()*3.3;
nicovv44 0:1f66eaf1013d 140 if(v1 > v1Max){
nicovv44 0:1f66eaf1013d 141 v1Max = v1;
nicovv44 0:1f66eaf1013d 142 }
nicovv44 0:1f66eaf1013d 143 }
nicovv44 0:1f66eaf1013d 144 return v1Max;
nicovv44 0:1f66eaf1013d 145 }
nicovv44 0:1f66eaf1013d 146
nicovv44 1:f31a46c62d10 147
nicovv44 0:1f66eaf1013d 148 // #############################################################################
nicovv44 0:1f66eaf1013d 149 float getFrequency(AnalogIn ana_pin){
nicovv44 0:1f66eaf1013d 150 float freq; //frequency
nicovv44 0:1f66eaf1013d 151 float maxReadedVoltage;//maximum voltage readed by the ADC
nicovv44 0:1f66eaf1013d 152 float readedVoltage;//readed voltage
nicovv44 0:1f66eaf1013d 153 int nbrRisingEdge=0;// number of rising edge detected
nicovv44 0:1f66eaf1013d 154 float T;//Periode
nicovv44 0:1f66eaf1013d 155 Timer timer;
nicovv44 0:1f66eaf1013d 156 maxReadedVoltage = getVolageReadedMax(ana_pin);
nicovv44 4:886ce7eefa6e 157 //pc.printf("maxReadedVoltage: %f\r\n",maxReadedVoltage);
nicovv44 0:1f66eaf1013d 158 bool aboveLine = true;
nicovv44 1:f31a46c62d10 159 looping = true;
nicovv44 1:f31a46c62d10 160 timeout.attach(callback(&stopLooping), 1);//try to find rising edges during 1sec max
nicovv44 1:f31a46c62d10 161 while(nbrRisingEdge<2 and looping){
nicovv44 0:1f66eaf1013d 162 readedVoltage = ana_pin.read()*3.3;
nicovv44 0:1f66eaf1013d 163 if(readedVoltage<(maxReadedVoltage/2)){//rising edge detection ready
nicovv44 0:1f66eaf1013d 164 aboveLine = false;
nicovv44 0:1f66eaf1013d 165 }
nicovv44 0:1f66eaf1013d 166 if((maxReadedVoltage/2)<readedVoltage && aboveLine==false){//rising edge detected
nicovv44 0:1f66eaf1013d 167 aboveLine = true;
nicovv44 0:1f66eaf1013d 168 if(nbrRisingEdge==0)
nicovv44 0:1f66eaf1013d 169 timer.start();
nicovv44 0:1f66eaf1013d 170 if(nbrRisingEdge==1)
nicovv44 0:1f66eaf1013d 171 timer.stop();
nicovv44 0:1f66eaf1013d 172 nbrRisingEdge++;
nicovv44 0:1f66eaf1013d 173 }
nicovv44 0:1f66eaf1013d 174 }
nicovv44 1:f31a46c62d10 175 if(nbrRisingEdge!=2){
nicovv44 1:f31a46c62d10 176 lcd.locate(13,1);
nicovv44 1:f31a46c62d10 177 lcd.printf("f!%d",nbrRisingEdge);
nicovv44 1:f31a46c62d10 178 }
nicovv44 0:1f66eaf1013d 179 T = timer.read();
nicovv44 0:1f66eaf1013d 180 freq = 1/T;
nicovv44 4:886ce7eefa6e 181 //pc.printf("T: %f\r\n",T);
nicovv44 4:886ce7eefa6e 182 //pc.printf("freq: %f\r\n\n",freq);
nicovv44 0:1f66eaf1013d 183 return freq;
nicovv44 4:886ce7eefa6e 184 }
nicovv44 4:886ce7eefa6e 185
nicovv44 4:886ce7eefa6e 186
nicovv44 4:886ce7eefa6e 187 // #############################################################################
nicovv44 4:886ce7eefa6e 188 void displayLCD(float syncRMS, float gridRMS, float syncFreq, float gridFreq){
nicovv44 4:886ce7eefa6e 189 lcd.locate(0,0);//(col,row)
nicovv44 4:886ce7eefa6e 190 lcd.printf(" ");
nicovv44 4:886ce7eefa6e 191 lcd.locate(0,1);//(col,row)
nicovv44 4:886ce7eefa6e 192 lcd.printf(" ");
nicovv44 4:886ce7eefa6e 193 lcd.locate(0,0);//(col,row)
nicovv44 4:886ce7eefa6e 194 lcd.printf("G:%3.1f@%3.1f", gridRMS, gridFreq);
nicovv44 4:886ce7eefa6e 195 lcd.locate(0,1);//(col,row)
nicovv44 4:886ce7eefa6e 196 lcd.printf("S:%3.1f@%3.1f", syncRMS, syncFreq);
nicovv44 1:f31a46c62d10 197 }