mbedyp yp / Mbed 2 deprecated PROJET_GOLF_NEW_SAVE

Dependencies:   mbed HX711 TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HX711.h"
00003 #include "TextLCD.h"
00004 
00005 // LCD and Joystick Setting
00006 TextLCD lcd(p8, p10, p11, p5, p6, p7); // rs, e, d4-d7
00007 //rs   e    d4  d5   d6    d7          
00008 DigitalOut RW(p9);
00009 
00010 DigitalOut gpo(D0);
00011 
00012 //LED MBED
00013 DigitalOut led1(LED1);
00014 DigitalOut led2(LED2);
00015 DigitalOut led3(LED3);
00016 DigitalOut led4(LED4);
00017 
00018 //LED BICOLORE
00019 DigitalOut ledB1(p29); // SI 1 LED ROUGE
00020 DigitalOut ledB2(p30); // SI 1 LED VERT
00021 
00022 //HX711
00023 HX711 scale1(p22, p21); //22 , 21
00024 HX711 scale2(p24, p23); //24 , 23
00025 
00026 // BOUTON
00027 DigitalIn Up(p12);
00028 DigitalIn Down(p13);
00029 DigitalIn Left(p14);
00030 DigitalIn Right(p15);
00031 DigitalIn Center(p16);
00032 
00033 AnalogIn scaleRaw(A3);
00034 Serial pc(USBTX, USBRX); // USB Serial Terminal
00035 float calibration_factor1 = 2325; //2325 // -7050 worked for my 440lb max scale setup
00036 float calibration_factor2 = 2178; //2178
00037 int averageSamples = 5; //NOMBRE DE MESURE
00038 
00039 int moyenneClick = 0;
00040 int boucleExecute = 0;
00041 
00042 int i = 0; //count
00043 int ii = 0; //count
00044 
00045 float m = 0; //MASSE
00046 float lg = 0; //LG
00047 float MOI = 0; // MOI
00048 float massMoy = 0; //Moyenne de la masse a chaque points
00049 
00050 float weight1 = 0; // F1
00051 float weight2 = 0; // F0
00052 
00053 //SWINGWEIGHT
00054 float convertionInches = 2.54; // 1 inches = 2,54 centimètre
00055 float calculInches = 0; // Variable pour effectuer des calculs
00056 float SW = 0; // Variable résultat SWINGWEIGHT
00057 
00058 int main(void) {
00059   //Mettre la LED bicolore en ROUGE pendant la tare
00060   ledB1 = 1;
00061   ledB2 = 0;
00062   RW = 0;
00063 
00064   pc.printf("Starting Scale\n");
00065   pc.printf("HX711 calibration sketch\n");
00066   pc.printf("Retirer tout les poids des capteurs\n");
00067   pc.printf("Une fois que la LED bicolore passe de rouge a verte placer le club\n");
00068   pc.printf("[capteur1] Press 1 or a pour augmenter le facteur etalonnage de 25\n");
00069   pc.printf("[capteur1] Press 3 or z pour diminuer le facteur etalonnage de 25\n");
00070   pc.printf("[capteur2] Press 4 or a pour augmenter le facteur etalonnage de 25\n");
00071   pc.printf("[capteur2] Press 6 or z pour diminuer le facteur etalonnage de 25\n");
00072 
00073   scale1.setScale(0);
00074   scale1.tare(); //Reset the scale to 0
00075 
00076   scale2.setScale(0);
00077   scale2.tare(); //Reset the scale to 0
00078 
00079   long zero_factor1 = scale1.averageValue(averageSamples); //Get a baseline reading / Obtenez une lecture de base CAPTEUR 1
00080   long zero_factor2 = scale2.averageValue(averageSamples); //Get a baseline reading / Obtenez une lecture de base CAPTEUR 2
00081   pc.printf("Zero factor Capteur1: %.4f\n", zero_factor1); //This can be used to remove the need to tare the scale. Useful in permanent scale projects. / Cela peut être utilisé pour supprimer le besoin de tarer la balance. Utile dans les projets à échelle permanente. CAPTEUR 1 
00082   pc.printf("Zero factor Capteur2: %.4f\n", zero_factor2); //This can be used to remove the need to tare the scale. Useful in permanent scale projects. / Cela peut être utilisé pour supprimer le besoin de tarer la balance. Utile dans les projets à échelle permanente. CAPTEUR 2
00083 
00084   // Mettre la LED en VERT pour indiquer la fin de la tare
00085   ledB1 = 0;
00086   ledB2 = 1;
00087 
00088   while (true) {
00089     scale1.setScale(calibration_factor1); //Adjust to this calibration factor / Ajuster à ce facteur d'étalonnage
00090     scale2.setScale(calibration_factor2); //Adjust to this calibration factor / Ajuster à ce facteur d'étalonnage
00091 
00092     if (moyenneClick == 0) {
00093       weight1 = scale1.getGram();
00094       weight2 = scale2.getGram();
00095     }
00096 
00097     //float raw = scaleRaw.read();
00098     pc.printf("Reading Capteur1 g : %.2f\n", weight1);
00099     //pc.printf("Raw Value: %.7f\n", raw);
00100     pc.printf("Reading Capteur2 g : %.2f\n", weight2);
00101 
00102     m = weight1 - weight2; // Calcul masse du club
00103     lg = 45 * (weight1 / m); //Longueur jusqu'aux points d'équilibre
00104 
00105     MOI = m * (lg * lg); // Calcul masse du MOI (masse inertielle)
00106 
00107     calculInches = lg / convertionInches; //Convertion en inches
00108     SW = (calculInches - 14) * m; //Calcul SWINGWEIGHT
00109 
00110     switch (i) {
00111     case 0: {
00112       //LED_BOUTON_B
00113       ledB1 = 0;
00114       ledB2 = 1;
00115 
00116       lcd.cls(); //CLEAR LCD
00117       lcd.locate(0, 0); //Ecrire sur la 1er ligne
00118       lcd.printf("F1: %.2fg\n", weight1); // Afficher le poids en grammes du capteur 1
00119 
00120       lcd.locate(0, 1); //Ecrire sur la 2ème ligne
00121       lcd.printf("F0: %.2fg\n", weight2); // Afficher le poids en grammes du capteur 2
00122 
00123       if (!Center) {
00124         //CODE: LED_BOUTON_A
00125         ledB1 = 1;
00126         ledB2 = 0;
00127 
00128         weight1 = 0;
00129         weight2 = 0;
00130 
00131         wait(1.5); // Attendre 1.5 sec
00132 
00133         while (moyenneClick == 0) {
00134 
00135           boucleExecute++;
00136 
00137           weight1 = weight1 + scale1.getGram();
00138           weight2 = weight2 + scale2.getGram();
00139 
00140           m = weight1 - weight2; // Calcul masse du club
00141 
00142           massMoy = massMoy + m;
00143 
00144           wait(0.1); // Attendre 0.1 sec
00145 
00146           massMoy = massMoy / boucleExecute;
00147 
00148           lcd.cls(); //CLEAR LCD
00149           lcd.locate(0, 0); //Ecrire sur la 1er ligne
00150           lcd.printf("Nombre de points\n"); // Afficher le nombre de points
00151 
00152           lcd.locate(0, 1); //Ecrire sur la 1er ligne
00153           lcd.printf("%d   m: %.2fg\n", boucleExecute, massMoy); // Afficher le nombre de points
00154 
00155           //LED VERT
00156           ledB1 = 0;
00157           ledB2 = 1;
00158 
00159           //LED ROUGE
00160           ledB1 = 1;
00161           ledB2 = 0;
00162 
00163           massMoy = 0;
00164 
00165           if (!Center) {
00166             //LED ROUGE
00167             ledB1 = 0;
00168             ledB2 = 1;
00169             moyenneClick = 1; //Mettre moyenneClick a 1
00170             wait(1); // Attendre 1 sec
00171           }
00172 
00173         }
00174 
00175         weight1 = weight1 / boucleExecute;
00176         weight2 = weight2 / boucleExecute;
00177 
00178         wait(0.5); // Attendre 0.5 sec
00179       } else if (!Down) {
00180         i = 1;
00181         //CODE: LED_BOUTON_A
00182         ledB1 = 1;
00183         ledB2 = 0;
00184 
00185         wait(0.5); // Attendre 0.5 sec
00186       } else if (!Up) {
00187         i = 3;
00188         //CODE: LED_BOUTON_A
00189         ledB1 = 1;
00190         ledB2 = 0;
00191 
00192         wait(0.5); // Attendre 0.5 sec
00193       }
00194     }
00195     break;
00196 
00197     case 1: {
00198       //LED_BOUTON_B
00199       ledB1 = 0;
00200       ledB2 = 1;
00201 
00202       lcd.cls(); //CLEAR LCD
00203       lcd.locate(0, 0); //Ecrire sur la 1er ligne
00204       lcd.printf("m = %.2fg\n", m);
00205 
00206       lg = 45 * (weight1 / m);
00207       lcd.locate(0, 1); //Ecrire sur la 2ème ligne
00208       lcd.printf("lg = %.2fcm\n", lg);
00209 
00210       if (!Down) {
00211         i = 2;
00212         //CODE: LED_BOUTON_A
00213         ledB1 = 1;
00214         ledB2 = 0;
00215         wait(0.5); // Attendre 0.5 sec
00216       } else if (!Up) {
00217         i = 0;
00218         //CODE: LED_BOUTON_A
00219         ledB1 = 1;
00220         ledB2 = 0;
00221 
00222         wait(0.5); // Attendre 0.5 sec
00223       }
00224 
00225     }
00226     break;
00227 
00228     case 2: {
00229       //LED_BOUTON_B
00230       ledB1 = 0;
00231       ledB2 = 1;
00232 
00233       lcd.cls(); //CLEAR LCD
00234       lcd.locate(0, 0); //Ecrire sur la 1er ligne
00235       lcd.printf("MOI:\n");
00236       lcd.locate(0, 1); //Ecrire sur la 2ème ligne
00237       lcd.printf("%.2f kg.cm2\n", MOI / 1000);
00238 
00239       if (!Down) {
00240         i = 3;
00241         //CODE: LED_BOUTON_A
00242         ledB1 = 1;
00243         ledB2 = 0;
00244 
00245         wait(0.5); // Attendre 0.5 sec
00246       } else if (!Up) {
00247         i = 1;
00248         //CODE: LED_BOUTON_A
00249         ledB1 = 1;
00250         ledB2 = 0;
00251 
00252         wait(0.5); // Attendre 0.5 sec
00253       }
00254     }
00255     break;
00256 
00257     case 3: {
00258       //LED_BOUTON_B
00259       ledB1 = 0;
00260       ledB2 = 1;
00261 
00262       lcd.cls(); //CLEAR LCD
00263       lcd.locate(0, 0); //Ecrire sur la 1er ligne
00264 
00265       lcd.printf("SW = %.2f\n", SW);
00266 
00267       lcd.locate(0, 1); //Ecrire sur la 2ème ligne
00268 
00269       // Résultat de SW (SWINGWEIGHT / inertie) place le club dans une catégorie
00270       if (SW < 5250) {
00271         lcd.printf("SWINGWEIGHT NULL\n", SW);
00272       } else if (SW > 5250 && SW < 5300) {
00273         lcd.printf("SWINGWEIGHT = B4\n");
00274       } else if (SW > 5300 && SW < 5350) {
00275         lcd.printf("SWINGWEIGHT = B5\n");
00276       } else if (SW > 5350 && SW < 5400) {
00277         lcd.printf("SWINGWEIGHT = B6\n");
00278       } else if (SW > 5400 && SW < 5450) {
00279         lcd.printf("SWINGWEIGHT = B7\n");
00280       } else if (SW > 5450 && SW < 5500) {
00281         lcd.printf("SWINGWEIGHT = B8\n");
00282       } else if (SW > 5500 && SW < 5550) {
00283         lcd.printf("SWINGWEIGHT = B9\n");
00284       } else if (SW > 5550 && SW < 5600) {
00285         lcd.printf("SWINGWEIGHT = C0\n");
00286       } else if (SW > 5600 && SW < 5650) {
00287         lcd.printf("SWINGWEIGHT = C1\n");
00288       } else if (SW > 5650 && SW < 5700) {
00289         lcd.printf("SWINGWEIGHT = C2\n");
00290       } else if (SW > 5700 && SW < 5750) {
00291         lcd.printf("SWINGWEIGHT = C3\n");
00292       } else if (SW > 5750 && SW < 5800) {
00293         lcd.printf("SWINGWEIGHT = C4\n");
00294       } else if (SW > 5800 && SW < 5850) {
00295         lcd.printf("SWINGWEIGHT = C5\n");
00296       } else if (SW > 5850 && SW < 5900) {
00297         lcd.printf("SWINGWEIGHT = C6\n");
00298       } else if (SW > 5900 && SW < 5950) {
00299         lcd.printf("SWINGWEIGHT = C7\n");
00300       } else if (SW > 5950 && SW < 6000) {
00301         lcd.printf("SWINGWEIGHT = C8\n");
00302       } else if (SW > 6000 && SW < 6050) {
00303         lcd.printf("SWINGWEIGHT = C9\n");
00304       } else if (SW > 6050 && SW < 6100) {
00305         lcd.printf("SWINGWEIGHT = D0\n");
00306       } else if (SW > 6100 && SW < 6150) {
00307         lcd.printf("SWINGWEIGHT = D1\n");
00308       } else if (SW > 6150 && SW < 6200) {
00309         lcd.printf("SWINGWEIGHT = D2\n");
00310       } else if (SW > 6200 && SW < 6250) {
00311         lcd.printf("SWINGWEIGHT = D3\n");
00312       } else if (SW > 6250 && SW < 6300) {
00313         lcd.printf("SWINGWEIGHT = D4\n");
00314       } else if (SW > 6300 && SW < 6350) {
00315         lcd.printf("SWINGWEIGHT = D5\n");
00316       } else if (SW > 6350 && SW < 6400) {
00317         lcd.printf("SWINGWEIGHT = D6\n");
00318       } else if (SW > 6400 && SW < 6450) {
00319         lcd.printf("SWINGWEIGHT = D7\n");
00320       } else if (SW > 6450 && SW < 6500) {
00321         lcd.printf("SWINGWEIGHT = D8\n");
00322       } else if (SW > 6500) {
00323         lcd.printf("SWINGWEIGHT = D9\n");
00324       }
00325 
00326       if (!Down) {
00327         i = 4;
00328         //CODE: LED_BOUTON_A
00329         ledB1 = 1;
00330         ledB2 = 0;
00331 
00332         wait(0.5); // Attendre 0.5 sec
00333       } else if (!Up) {
00334         i = 2;
00335         //CODE: LED_BOUTON_A
00336         ledB1 = 1;
00337         ledB2 = 0;
00338 
00339         wait(0.5); // Attendre 0.5 sec
00340       }
00341     }
00342     break;
00343 
00344     case 4: {
00345       //LED_BOUTON_B
00346       ledB1 = 0;
00347       ledB2 = 1;
00348 
00349       lcd.cls(); //CLEAR LCD
00350       lcd.locate(0, 0); //Ecrire sur la 1er ligne
00351 
00352       lcd.printf("N points moyenne");
00353 
00354       lcd.locate(0, 1); //Ecrire sur la 2eme ligne
00355       lcd.printf("%d\n", boucleExecute);
00356 
00357       if (!Down) {
00358         i = 0;
00359         //CODE: LED_BOUTON_A
00360         ledB1 = 1;
00361         ledB2 = 0;
00362 
00363         wait(0.5); // Attendre 0.5 sec
00364       } else if (!Up) {
00365         i = 3;
00366         //CODE: LED_BOUTON_A
00367         ledB1 = 1;
00368         ledB2 = 0;
00369 
00370         wait(0.5); // Attendre 0.5 sec
00371       }
00372     }
00373     break;
00374     }
00375     /*
00376     CODE INFORMATION:
00377     LED_BOUTON_A: Mettre la LED bicolore en ROUGE pour que l'utilisateur sache que son action a était pris en compte
00378     LED_BOUTON_B: Mettre la LED bicolore en VERT à la fin de l'action utilisateur
00379     
00380     */
00381 
00382     if (pc.readable()) {
00383       char temp = pc.getc();
00384       if (temp == '1' || temp == 'a')
00385         calibration_factor1 -= 25;
00386       else if (temp == '3' || temp == 'z')
00387         calibration_factor1 += 25;
00388       else if (temp == '4' || temp == 'q')
00389         calibration_factor2 -= 25;
00390       else if (temp == '6' || temp == 's')
00391         calibration_factor2 += 25;
00392     }
00393     gpo = !gpo; // toggle pin
00394     wait(0.2); // Attendre 0.2 sec
00395   }
00396 }