SP1 vesr 3
Dependencies: Sht31 WifiEsp8266 PWM EADOG
main.cpp@0:45857c3d5769, 2021-06-08 (annotated)
- Committer:
- petit
- Date:
- Tue Jun 08 10:39:36 2021 +0000
- Revision:
- 0:45857c3d5769
SP1 vers 3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
petit | 0:45857c3d5769 | 1 | #include "mbed.h" |
petit | 0:45857c3d5769 | 2 | #include "EADOG.h" |
petit | 0:45857c3d5769 | 3 | #include "Small_7.h" |
petit | 0:45857c3d5769 | 4 | #include "pwm.h" |
petit | 0:45857c3d5769 | 5 | #include "Fonts/ArialR12x14.h" |
petit | 0:45857c3d5769 | 6 | #include "wifiesp8266.h" |
petit | 0:45857c3d5769 | 7 | #include "Sht31.h" |
petit | 0:45857c3d5769 | 8 | |
petit | 0:45857c3d5769 | 9 | EADOG lcd(PC_12, PC_10, PA_1, PA_0, PA_4, DOGM128); // MOSI, SCL, Reset, A0, CS |
petit | 0:45857c3d5769 | 10 | Sht31 capt(PB_9,PB_8); |
petit | 0:45857c3d5769 | 11 | InterruptIn helice(PD_2); |
petit | 0:45857c3d5769 | 12 | InterruptIn VoieA(PB_13); //interuption de la voie A port P |
petit | 0:45857c3d5769 | 13 | DigitalIn VoieB(PA_10); //InterruptIn VoieB(PA_10); |
petit | 0:45857c3d5769 | 14 | WifiEsp8266 Wifi(PA_11,PA_12); // WifiEsp8266(PinName Txd, PinName Rxd); |
petit | 0:45857c3d5769 | 15 | DigitalOut Led(LED1); |
petit | 0:45857c3d5769 | 16 | |
petit | 0:45857c3d5769 | 17 | float VitesseAir = 0; //initialisation de la VitesseAir |
petit | 0:45857c3d5769 | 18 | float Frequencehelice = 0; |
petit | 0:45857c3d5769 | 19 | int Temperature = 0; |
petit | 0:45857c3d5769 | 20 | int Humidite = 0; |
petit | 0:45857c3d5769 | 21 | unsigned char ConsVitFluxAir =0; |
petit | 0:45857c3d5769 | 22 | float FrequenceCalc = 0; |
petit | 0:45857c3d5769 | 23 | bool NewFrequence = false; |
petit | 0:45857c3d5769 | 24 | |
petit | 0:45857c3d5769 | 25 | // mesure frequence helice |
petit | 0:45857c3d5769 | 26 | Timer timer; |
petit | 0:45857c3d5769 | 27 | void MesPeriode(void) |
petit | 0:45857c3d5769 | 28 | { |
petit | 0:45857c3d5769 | 29 | Led= 1; |
petit | 0:45857c3d5769 | 30 | FrequenceCalc = timer.read(); |
petit | 0:45857c3d5769 | 31 | timer.reset(); |
petit | 0:45857c3d5769 | 32 | FrequenceCalc = 1.0/FrequenceCalc/2.0; |
petit | 0:45857c3d5769 | 33 | NewFrequence = true; |
petit | 0:45857c3d5769 | 34 | Led=0; |
petit | 0:45857c3d5769 | 35 | } |
petit | 0:45857c3d5769 | 36 | |
petit | 0:45857c3d5769 | 37 | // codeur |
petit | 0:45857c3d5769 | 38 | bool BoutonChange; |
petit | 0:45857c3d5769 | 39 | unsigned char inc100(unsigned char _i) |
petit | 0:45857c3d5769 | 40 | { |
petit | 0:45857c3d5769 | 41 | if (_i<100) { |
petit | 0:45857c3d5769 | 42 | _i++; |
petit | 0:45857c3d5769 | 43 | BoutonChange = true; |
petit | 0:45857c3d5769 | 44 | } |
petit | 0:45857c3d5769 | 45 | return _i; |
petit | 0:45857c3d5769 | 46 | } |
petit | 0:45857c3d5769 | 47 | unsigned char dec0(unsigned char _i) |
petit | 0:45857c3d5769 | 48 | { |
petit | 0:45857c3d5769 | 49 | if (_i>0) { |
petit | 0:45857c3d5769 | 50 | _i--; |
petit | 0:45857c3d5769 | 51 | BoutonChange = true; |
petit | 0:45857c3d5769 | 52 | } |
petit | 0:45857c3d5769 | 53 | return _i; |
petit | 0:45857c3d5769 | 54 | } |
petit | 0:45857c3d5769 | 55 | void FrontMontantA(void) |
petit | 0:45857c3d5769 | 56 | { |
petit | 0:45857c3d5769 | 57 | if (VoieB.read()==0) ConsVitFluxAir=inc100(ConsVitFluxAir); |
petit | 0:45857c3d5769 | 58 | else ConsVitFluxAir=dec0(ConsVitFluxAir); |
petit | 0:45857c3d5769 | 59 | } |
petit | 0:45857c3d5769 | 60 | void FrontDescA(void) //fond descene |
petit | 0:45857c3d5769 | 61 | { |
petit | 0:45857c3d5769 | 62 | if(VoieB.read()==1) ConsVitFluxAir=inc100(ConsVitFluxAir); //jai rmplacé 0'' |
petit | 0:45857c3d5769 | 63 | else ConsVitFluxAir=dec0(ConsVitFluxAir); |
petit | 0:45857c3d5769 | 64 | } |
petit | 0:45857c3d5769 | 65 | |
petit | 0:45857c3d5769 | 66 | // pour acquisition entree. |
petit | 0:45857c3d5769 | 67 | bool acquerir = false; |
petit | 0:45857c3d5769 | 68 | Ticker TickerAcq; |
petit | 0:45857c3d5769 | 69 | void TraitAcq(void) |
petit | 0:45857c3d5769 | 70 | { |
petit | 0:45857c3d5769 | 71 | acquerir = true; |
petit | 0:45857c3d5769 | 72 | } |
petit | 0:45857c3d5769 | 73 | //drapeau pour indiquer nouvel affichage |
petit | 0:45857c3d5769 | 74 | bool affichage = false; |
petit | 0:45857c3d5769 | 75 | int main() |
petit | 0:45857c3d5769 | 76 | { |
petit | 0:45857c3d5769 | 77 | Serial PC(USBTX,USBRX); |
petit | 0:45857c3d5769 | 78 | PC.baud(Baud); |
petit | 0:45857c3d5769 | 79 | PC.printf("SP1 start\r\n"); |
petit | 0:45857c3d5769 | 80 | AnalogIn EntVitesseAir(PC_5); //pour faire varier VitesseAir |
petit | 0:45857c3d5769 | 81 | char WifiPhrase[maxdata]; |
petit | 0:45857c3d5769 | 82 | Led =1; |
petit | 0:45857c3d5769 | 83 | // mesure frequence helice |
petit | 0:45857c3d5769 | 84 | timer.reset(); |
petit | 0:45857c3d5769 | 85 | timer.start(); |
petit | 0:45857c3d5769 | 86 | // moteur pwm |
petit | 0:45857c3d5769 | 87 | InitPwmMot(); // initilise PWM : voir lib T = 20ms, Largeur Imp 1ms |
petit | 0:45857c3d5769 | 88 | |
petit | 0:45857c3d5769 | 89 | // codeur |
petit | 0:45857c3d5769 | 90 | ConsVitFluxAir = 0; |
petit | 0:45857c3d5769 | 91 | BoutonChange = true; |
petit | 0:45857c3d5769 | 92 | VoieA.fall(&FrontDescA); //frond descendant |
petit | 0:45857c3d5769 | 93 | VoieA.rise(&FrontMontantA); //frond montant |
petit | 0:45857c3d5769 | 94 | |
petit | 0:45857c3d5769 | 95 | // ticker acquisition toutes les secondes |
petit | 0:45857c3d5769 | 96 | lcd.locate(0,1); |
petit | 0:45857c3d5769 | 97 | lcd.printf("Cons Vit Air");//on affiche la consigne |
petit | 0:45857c3d5769 | 98 | lcd.locate(0,14); |
petit | 0:45857c3d5769 | 99 | lcd.printf("Vitesse");//on affiche la vitesse |
petit | 0:45857c3d5769 | 100 | lcd.locate(0,27); |
petit | 0:45857c3d5769 | 101 | lcd.printf("Temperature");//on affiche la température |
petit | 0:45857c3d5769 | 102 | lcd.locate(0,40); |
petit | 0:45857c3d5769 | 103 | lcd.printf("Humidite");//on affiche l'humidité |
petit | 0:45857c3d5769 | 104 | lcd.locate(0,53); |
petit | 0:45857c3d5769 | 105 | lcd.printf("Freq. Helice");//on afiche la fréquence d'helice |
petit | 0:45857c3d5769 | 106 | helice.rise(&MesPeriode); |
petit | 0:45857c3d5769 | 107 | TickerAcq.attach(&TraitAcq,0.1); // ticker toutes les secondes pour lire vitesse flux air |
petit | 0:45857c3d5769 | 108 | while (true) { |
petit | 0:45857c3d5769 | 109 | // wifi |
petit | 0:45857c3d5769 | 110 | if (Wifi.dataready()) { |
petit | 0:45857c3d5769 | 111 | strcpy(WifiPhrase,Wifi.read()); |
petit | 0:45857c3d5769 | 112 | PC.printf("%s\r\n",WifiPhrase); // pour phase de test |
petit | 0:45857c3d5769 | 113 | switch (WifiPhrase[1]) { |
petit | 0:45857c3d5769 | 114 | case 'F': |
petit | 0:45857c3d5769 | 115 | ConsVitFluxAir=inc100(ConsVitFluxAir); |
petit | 0:45857c3d5769 | 116 | Wifi.printf("$B%u\r\n",ConsVitFluxAir); |
petit | 0:45857c3d5769 | 117 | lcd.locate(60,1); |
petit | 0:45857c3d5769 | 118 | lcd.printf("%03u%%\r\n",ConsVitFluxAir);//on affiche la consigne |
petit | 0:45857c3d5769 | 119 | break; |
petit | 0:45857c3d5769 | 120 | case 'G' : |
petit | 0:45857c3d5769 | 121 | ConsVitFluxAir=dec0(ConsVitFluxAir); |
petit | 0:45857c3d5769 | 122 | Wifi.printf("$B%u\r\n",ConsVitFluxAir); |
petit | 0:45857c3d5769 | 123 | lcd.locate(60,1); |
petit | 0:45857c3d5769 | 124 | lcd.printf("%03u%%\r\n",ConsVitFluxAir);//on affiche la consigne |
petit | 0:45857c3d5769 | 125 | break; |
petit | 0:45857c3d5769 | 126 | default : |
petit | 0:45857c3d5769 | 127 | ; |
petit | 0:45857c3d5769 | 128 | } |
petit | 0:45857c3d5769 | 129 | } |
petit | 0:45857c3d5769 | 130 | if (NewFrequence==true) { |
petit | 0:45857c3d5769 | 131 | Frequencehelice = FrequenceCalc; |
petit | 0:45857c3d5769 | 132 | NewFrequence = false; |
petit | 0:45857c3d5769 | 133 | } |
petit | 0:45857c3d5769 | 134 | // acquisition des données toutes les secondes |
petit | 0:45857c3d5769 | 135 | if (acquerir == true) { |
petit | 0:45857c3d5769 | 136 | VitesseAir = EntVitesseAir.read(); |
petit | 0:45857c3d5769 | 137 | VitesseAir = VitesseAir*(72.0*2.0*3.46/5.0);//Avec N, on peut trouver VitesseAir |
petit | 0:45857c3d5769 | 138 | // hum et temperature |
petit | 0:45857c3d5769 | 139 | Temperature = capt.readTemperature(); |
petit | 0:45857c3d5769 | 140 | Humidite = capt.readHumidity(); |
petit | 0:45857c3d5769 | 141 | Wifi.printf("$Z,%0.0f,%0i,%0i\r\n",VitesseAir,Temperature,Humidite); |
petit | 0:45857c3d5769 | 142 | affichage = true; |
petit | 0:45857c3d5769 | 143 | } |
petit | 0:45857c3d5769 | 144 | if (BoutonChange==true) { |
petit | 0:45857c3d5769 | 145 | SetPwmMot(ConsVitFluxAir); // on peut commande moteur 50% max -> 15ms |
petit | 0:45857c3d5769 | 146 | BoutonChange = false; |
petit | 0:45857c3d5769 | 147 | lcd.locate(60,1); |
petit | 0:45857c3d5769 | 148 | lcd.printf("%03u%%\r\n",ConsVitFluxAir);//on affiche la consigne |
petit | 0:45857c3d5769 | 149 | Wifi.printf("$B%u\r\n",ConsVitFluxAir); |
petit | 0:45857c3d5769 | 150 | // wait(0.001); |
petit | 0:45857c3d5769 | 151 | } |
petit | 0:45857c3d5769 | 152 | |
petit | 0:45857c3d5769 | 153 | if (affichage == true) { |
petit | 0:45857c3d5769 | 154 | lcd.locate(50,14); |
petit | 0:45857c3d5769 | 155 | lcd.printf("%03.0fkm/h",VitesseAir);//on affiche la vitesse |
petit | 0:45857c3d5769 | 156 | lcd.locate(60,27); |
petit | 0:45857c3d5769 | 157 | lcd.printf("%03iC\r\n",Temperature);//on affiche la température |
petit | 0:45857c3d5769 | 158 | lcd.locate(60,40); |
petit | 0:45857c3d5769 | 159 | lcd.printf("%03i%%\r\n",Humidite);//on affiche l'humidité |
petit | 0:45857c3d5769 | 160 | lcd.locate(60,53); |
petit | 0:45857c3d5769 | 161 | lcd.printf("%03.0fHz\r\n",Frequencehelice);//on afiche la fréquence d'helice |
petit | 0:45857c3d5769 | 162 | affichage = false; |
petit | 0:45857c3d5769 | 163 | } |
petit | 0:45857c3d5769 | 164 | } |
petit | 0:45857c3d5769 | 165 | } |