mbedyp yp / Mbed 2 deprecated PROJET_GOLF_NEW_S

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