Florent Gourmelen / Mbed 2 deprecated Module_acquisition

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "math.h"
00004 
00005 #define ID 0x25
00006 
00007 
00008 DigitalOut led(p5);
00009 DigitalIn BP1(p6);
00010 DigitalIn BP2(p7);
00011 
00012 AnalogIn Uu(p17);
00013 AnalogIn Ui(p16);
00014 
00015 Ticker echantillonnage;
00016 Ticker envoie_trame;
00017 
00018 TextLCD lcd(p21, p22, p23, p24, p25, p26,TextLCD::LCD20x4); // rs, e, d4-d7
00019 Serial pc(USBTX, USBRX);
00020 //Serial xbee(p13, p14); // (tx,rx) module XBee
00021 
00022 float Un;
00023 float Un_1;
00024 float Aire;
00025 
00026 float Un_i;
00027 float Un_1_i;
00028 float Aire_i;
00029 
00030 float Aire_p;
00031 
00032 
00033 int cpt_echant;
00034 int cpt_trame;
00035 
00036 float Ueff;
00037 float Ieff;
00038 float P;
00039 float Pconso = 0;
00040 int Pconso_int;
00041 
00042 char Tab_trame[14];
00043 
00044 //float Pconso_led;
00045 
00046 void trame(){
00047     uint16_t trame_4 = Ueff*10;
00048     uint16_t trame_6 = Ieff*10;
00049     uint16_t trame_8 = P*10;
00050     uint32_t trame_10 = Pconso*10;
00051     char sum;
00052     
00053     Tab_trame[0] = 0x55;
00054     Tab_trame[1] = 0x55;            
00055     Tab_trame[2] = ID;           
00056     Tab_trame[3] = Ueff*10;
00057     Tab_trame[4] = trame_4 >> 8;
00058     Tab_trame[5] = Ieff*10;
00059     Tab_trame[6] = trame_6 >> 8;
00060     Tab_trame[7] = P*10;
00061     Tab_trame[8] = trame_8 >> 8;
00062     Tab_trame[9] = Pconso*10;
00063     Tab_trame[10] = trame_10 >> 8;
00064     Tab_trame[11] = trame_10 >> 16;
00065     Tab_trame[12] = trame_10 >> 24;
00066     for(int i = 1; i < 13; i++) { sum = sum + Tab_trame[i]; };  // calcul du check sum
00067     Tab_trame[13] = sum;            // Check sum       
00068     
00069     for(int i = 0; i < 14; i++) {// boucle d'envoi des trames
00070         
00071         pc.putc(Tab_trame[i]);
00072         wait(0.01);
00073         //pc.printf("%c\n",Tab_trame[i]);
00074     } 
00075     //pc.printf("\n\r");
00076 }
00077 
00078 //-----------------------calcul--------------------------------------
00079 void echantillon(){
00080     
00081     if(cpt_echant == 0){
00082         Un_1 = 2*(Uu.read()-0.5);
00083         Un_1_i = 2*(Ui.read()-0.5);
00084         cpt_echant++;
00085     }
00086     
00087     if(cpt_echant > 0 && cpt_echant < 10000){
00088         //Un_1 = Un;             
00089         Un =2*( Uu.read()-0.5);   
00090         Un_i =2*( Ui.read()-0.5);
00091             
00092         Aire = Aire + ((Un_1*Un_1 + Un*Un)/2);
00093         Aire_i = Aire_i + ((Un_1_i*Un_1_i + Un_i*Un_i)/2);
00094         Aire_p = Aire_p + ((Un_1*Un_1_i + Un_i*Un)/2);
00095         
00096         Un_1 = Un; 
00097         Un_1_i = Un_i; 
00098         cpt_echant++;
00099     }
00100     
00101     if(cpt_echant == 10000){
00102         Ueff = 250*1.41421*sqrt(Aire/10000); //1.41421 = racine(2);
00103         Ieff = 5*1.41421*sqrt(Aire_i/10000);
00104         P = 1250*1.41421*sqrt(Aire_p/10000); //1250 = 2*250
00105         
00106         Aire=0;
00107         Aire_i = 0;
00108         Aire_p = 0;
00109         
00110         cpt_echant = 0;
00111         
00112         //calcul clignotement led
00113         Pconso = P + Pconso;
00114         Pconso_int = Pconso;
00115         
00116     }    
00117 }
00118 //-------------------------------------------------------------------
00119 
00120 
00121 int main()
00122 {
00123     echantillonnage.attach(&echantillon, 0.0001);
00124     envoie_trame.attach(&trame, 2);
00125     
00126     //pc.printf("un=%f\n\r",Uu.read()); 
00127     wait(1);
00128     while(1) {
00129         
00130         if((Pconso/3600) - (Pconso_int >=1)){//permet de faire clignoter la led
00131             //Pconso_led = 0;
00132             led = !led;
00133             wait(0.2);
00134             led = !led;
00135         }
00136         
00137         lcd.cls();
00138         lcd.locate(0, 0);
00139         lcd.printf("Ueff = %0.2f V\n", Ueff);
00140         
00141         lcd.locate(0, 1);
00142         lcd.printf("Ieff = %0.2f A\n", Ieff);
00143         
00144         lcd.locate(0, 2);
00145         lcd.printf("P = %0.2f W\n", P);
00146         
00147         lcd.locate(0, 3);
00148         lcd.printf("Pconso = %0.2f Wh\n", Pconso/(3600.0));
00149         
00150         //pc.printf("Ueff = %f\n\r", Ueff);
00151         //pc.printf("Ieff = %f\n\r", Ieff);
00152         //pc.printf("P = %f\n\r", P);
00153         wait(1);
00154 
00155     }
00156 }