Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: TextLCD mbed angleandposition
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #include "math.h" 00004 #include "angleandposition.h" 00005 00006 00007 #define Nmax 200 // nombre d'echantillonnages sur une période 00008 #define nb_echantillon_max 10000 // 1s/20ms(temps acquisition pour une période) * Techantillonnage = 50 * 200 = 10 000 soit 10 000 echantillons par seconde 00009 #define ID 0x09 // ID commun entre les modules XBEE 00010 00011 // Entrees/Sorties : 00012 AnalogIn Iana(p16); // intensité mesurée 00013 AnalogIn Uana(p17); // tension mesurée 00014 DigitalOut led_temoin(LED1); // LED témoin (sur la carte) 00015 00016 // Objets : 00017 Serial pc(USBTX, USBRX); // (tx, rx) communication par port serie pour l'aide au débogage 00018 TextLCD lcd(p21, p22, p23, p24, p25, p26, TextLCD::LCD16x2); // (d4-d7, rs, e) afficheur LCD 00019 Serial xbee(p9, p10); // (tx,rx) module XBee 00020 00021 // Interruptions : 00022 InterruptIn bouton_gauche(p27); // interruption du bouton gauche 00023 InterruptIn bouton_droit(p28); // interruption du bouton droit 00024 00025 // Fonctions d'échantillonnages : 00026 Ticker echantillonnage; // déclaration d'un programme d'interruption 00027 Ticker echantillonnage_puissance; // déclaration d'un programme d'interruption 00028 00029 // Fichiers externes : 00030 LocalFileSystem local("local"); // fichier créer afin de stocker des valeurs 00031 00032 // Variables : 00033 char pos_menu = 0; // choix du menu à afficher 00034 char trame[14]; // tableau servant à l'envoi des trames 00035 float U0, U20, U2n_1, Un, U2n, Veff, Ufinal = 0;//variable pour le calcul de la tension 00036 float I0, I20, I2n_1, In, I2n, Ieff, Ifinal = 0;//variable pour le calcul de l'intensite 00037 float P = 0, S, Q, phi, energie_1 = 0, energie = 0; 00038 00039 int ancien_en = 0; // La LED s'allume tous les Wh 00040 int compteur_echantillonnage = 0; 00041 00042 00043 void calcul() { // fonction qui effectue les calculs (tesion intensite,puissance etc. 00044 00045 if (compteur_echantillonnage == 0) { 00046 00047 Veff = 0; 00048 Ieff = 0; 00049 00050 U0 = Uana; 00051 U0 = U0 - 0.5; 00052 U20 = U0 * U0; 00053 U2n_1 = U20; 00054 00055 00056 I0 = Iana; 00057 I0 = I0 - 0.5; 00058 I20 = I0 * I0; 00059 I2n_1 = I20; 00060 00061 compteur_echantillonnage++; 00062 } 00063 00064 00065 00066 if (compteur_echantillonnage > 0 && (compteur_echantillonnage < nb_echantillon_max)) { 00067 00068 Un = Uana; 00069 Un = Un - 0.5; 00070 U2n = Un * Un; 00071 00072 Veff = (U2n + U2n_1) / 2 + Veff; 00073 U2n_1 = U2n; 00074 00075 00076 In = Iana; 00077 In = In - 0.5; 00078 I2n = In * In; 00079 00080 Ieff = (I2n + I2n_1) / 2 + Ieff; 00081 I2n_1 = I2n; 00082 00083 compteur_echantillonnage++; 00084 } 00085 00086 00087 00088 if (compteur_echantillonnage >= nb_echantillon_max) { 00089 00090 Ufinal = 707.1*sqrt(Veff/nb_echantillon_max); // 707.1 = 2*sqrt(2)*250 (250 étant 250V, la tension maximale du secteur) 00091 Ifinal = 14.14*sqrt(Ieff/nb_echantillon_max); // 14.14 = 2*sqrt(2)*5 (5 étant 5A, l'intensité maximale du secteur) 00092 00093 S = Veff*Ieff; // puissance apparente 00094 phi = acos(P/S); 00095 Q = P * tan(phi); // puisance réactive 00096 } 00097 00098 } 00099 00100 void puissance() { // calcul de la puissance toutes les secondes par interruption 00101 00102 compteur_echantillonnage = 0; // réinitialisation du compteur pour la fonction calcul() 00103 00104 P = Ufinal * Ifinal; // puissance 00105 energie_1 = (energie_1 + P) / 3600; // énergie en Ws et non en Wh 00106 energie = energie + energie_1; // calcul de l'énergie consommée au total depuis le démarrage du microcontroleur 00107 00108 if(energie > ancien_en + 1) { // si l'énergie augmente d'un Wh 00109 00110 led_temoin = 1; // allumer la LED 00111 wait_ms(500); 00112 led_temoin = 0; // éteindre la LED 00113 00114 ancien_en = energie; // on garde en mémoire l'ancienne valeur de l'énergie 00115 00116 FILE* pfile = fopen ("/local/log.txt",""); // ouverture du fichier log.txt 00117 fprintf(pfile,"En = %.4f Wh\n", energie); // écriture sur le fichier de l'énergie 00118 fclose(pfile); // fermeture du fichier 00119 } 00120 } 00121 00122 void menu() { // affichage de l'écran 00123 00124 lcd.cls(); // on efface les anciennes donnees 00125 00126 switch(pos_menu) { 00127 00128 case 0: 00129 lcd.printf("En = %.4f Wh\n",energie); // on affiche 4 nombres après la virgule 00130 break; 00131 case 1: 00132 lcd.printf("Ueff = %.4f V\n",Ufinal); 00133 lcd.printf("Ieff = %.4f A\n",Ifinal); 00134 break; 00135 case 2: 00136 lcd.printf("P = %.4f W\n",P); 00137 lcd.printf("Q = %.4f Var\n",Q); 00138 break; 00139 case 3: 00140 lcd.printf("S = %.4f VA\n",S); 00141 lcd.printf("cos(phi)= %.0f deg\n",cos(phi)); 00142 break; 00143 default: 00144 lcd.printf("En = %.4f Wh\n",energie); 00145 break; 00146 } 00147 00148 wait_ms(200); // délai afin que l'affichage des nouvelles valeurs soit suffisament lent pour être lisible 00149 } 00150 00151 00152 void interupt_btn_gauche() { // interruption lors d'un appui sur le bouton gauche 00153 00154 wait_ms(10); // délai d'attente pour que les parasites, liés à la pression du bouton, ne redéclanchent la fonction 00155 00156 if(pos_menu == 0) { // si l'on arrive à la position 0 du menu 00157 pos_menu = 4; // on retournes à la 3ème position du menu (max) 00158 } else pos_menu--; // on passe à l'affichage de gauche 00159 } 00160 00161 00162 void interupt_btn_droit() { // interruption lors d'un appui sur le bouton droit 00163 00164 wait_ms(10); // délai d'attente pour que les parasites, liés à la pression du bouton, ne redéclanchent la fonction 00165 00166 if(pos_menu == 3) { // si l'on arrive à la 3ème position du menu (max) 00167 pos_menu = 0; // on retournes à la position 0 00168 } else pos_menu++; // on passe à l'affichage de droite 00169 } 00170 00171 00172 void envoi() {//fonction qui cree et envoie une trame au module Xbee 00173 uint16_t trame_4 = (Ufinal*100); 00174 uint16_t trame_6 = (Ifinal*100); 00175 uint16_t trame_8 = (P*100); 00176 uint16_t trame_10 = (energie*100); 00177 char sum; 00178 00179 00180 trame[1] = 0x55; 00181 trame[2] = ID; 00182 trame[3] = Ufinal*100; 00183 trame[4] = trame_4 >> 8; 00184 trame[5] = Ifinal*100; 00185 trame[6] = trame_6 >> 8; 00186 trame[7] = P*100; 00187 trame[8] = trame_8 >> 8; 00188 trame[9] = energie*100; 00189 trame[10] = trame_10 >> 8; 00190 trame[11] = trame_10 >> 16; 00191 trame[12] = trame_10 >> 24; 00192 for(int i = 1; i < 13; i++) { sum = sum + trame[i]; };// checksum 00193 sum = sum ; 00194 trame[13] = sum; 00195 00196 for(int i = 1; i < 14; i++) { 00197 00198 xbee.putc(trame[i]); 00199 wait(0.01); 00200 pc.printf("%d \t",trame[i]); 00201 } 00202 pc.printf("\r"); 00203 00204 } 00205 00206 int main() { 00207 00208 bouton_gauche.mode(PullUp); 00209 bouton_droit.mode(PullUp); 00210 00211 bouton_gauche.fall(&interupt_btn_gauche); 00212 bouton_droit.fall(&interupt_btn_droit); 00213 00214 led_temoin = 0; // la LED témoin est éteinte 00215 00216 echantillonnage.attach(&calcul, 0.0001); // interruption toutes les 100µs 00217 echantillonnage_puissance.attach(&puissance, 1); // interruption toutes les 1s 00218 envoi(); 00219 while(1) { // boucle infinie 00220 00221 menu(); // fonction d'affichage 00222 envoi(); // fonction d'envoi des trames 00223 wait(10); 00224 } 00225 }
Generated on Wed Jul 13 2022 16:15:55 by
1.7.2