Julien BLT / Mbed 2 deprecated mbed_ProjetRobot_Confettis

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define DEBUG 0
00004 
00005 // Constantes PIN :
00006 #define PIN_MG D6
00007 #define PIN_MD D8
00008 #define PIN_CPTG A1
00009 #define PIN_CPTD A2
00010 #define PIN_JACK D3
00011 
00012 // Déclaration du timers :
00013     Timer StopCar;
00014 
00015 // Déclaration des I/O :
00016     // Capteurs :
00017         AnalogIn CapteurG(PIN_CPTG);
00018         AnalogIn CapteurD(PIN_CPTD);
00019     // Moteurs :
00020         PwmOut MoteurG(PIN_MG);
00021         PwmOut MoteurD(PIN_MD);
00022     // Interupteur :
00023         DigitalIn Jack(PIN_JACK);
00024 
00025 // Fonction principal :
00026 int main()
00027 {
00028     MoteurG.period_us(100);
00029     MoteurD.period_us(100);
00030     MoteurG.pulsewidth_us(100);
00031     MoteurD.pulsewidth_us(100);
00032     
00033     int etat=0;
00034     int jack;
00035     float cptG, cptD;
00036     float const kMD=2.5, kMG=0;// kMD=2.5, kMG=0
00037     
00038     // Boucle principal :
00039     while(1)
00040     {
00041         // Lecture des entrées :
00042         jack = Jack.read();
00043         cptG = CapteurG.read();
00044         cptD = CapteurD.read();
00045         
00046         #if DEBUG == 1
00047         printf("ETAT = %d\n\rCAPTEUR G = %f\n\rCAPTEUR D = %f\n\r",etat,cptG,cptD);
00048         #endif // DEBUG == 1
00049         
00050         // Switch des transitions :
00051         switch(etat)
00052         {
00053             case 0:
00054                 if(jack==1) etat=1;
00055                 break;
00056             case 1:
00057                 if((cptG<=0.1) && (cptD<=0.1)) etat=2;
00058                 break;
00059             case 2:
00060                 if((cptG<=0.1) && (cptD<=0.1)) etat=3;
00061                 break;
00062             case 3:
00063                 if(StopCar.read()>0.5) etat=4;
00064                 else if((cptG>0.5) || (cptD>0.5)) etat=1;
00065                 break;
00066             case 4:
00067                 if(jack==0) etat=0;
00068                 break;
00069             default:
00070                 etat = 0;
00071                 break;
00072         }
00073         // Fin switch des transitions.
00074         
00075         // Switch des actions :
00076         switch(etat)
00077         {
00078             case 0:
00079                 MoteurG.pulsewidth_us(100);
00080                 MoteurD.pulsewidth_us(100);
00081                 break;
00082             case 1:
00083                 MoteurG.pulsewidth_us(70-kMG);
00084                 MoteurD.pulsewidth_us(70-kMD);
00085                 StopCar.reset();
00086                 break;
00087             case 2:
00088                 MoteurG.pulsewidth_us(70-kMG);
00089                 MoteurD.pulsewidth_us(70-kMD);
00090                 StopCar.start();
00091                 break;
00092             case 3:
00093                 MoteurG.pulsewidth_us(70-kMG);
00094                 MoteurD.pulsewidth_us(70-kMD);
00095                 break;
00096             case 4:
00097                 MoteurG.pulsewidth_us(100);
00098                 MoteurD.pulsewidth_us(100);
00099                 break;
00100             default:
00101                 etat = 0;
00102                 break;
00103         }
00104         // Fin switch des actions.
00105     }
00106     // Fin boucle principal.
00107 }
00108 // fin fonction principal.