Rémi Gui / Mbed 2 deprecated Auto_Motor

Dependencies:   SRF08 Servo mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "CAR.h"
00003 #include "SRF08.h"
00004 #include "Servo.h"
00005 
00006 #define BAT 7.5
00007 
00008 DigitalOut S2 (p11);
00009 AnalogIn opt (p15);
00010 //AnalogIn batterie (p20);
00011 Serial bth(p13,p14);
00012 //Serial sbt(p9, p10); // tx, rx
00013 SRF08 srf08(p28, p27, 0xE0);
00014 //Servo myservo(p26);
00015 
00016 float aMes[]= {0,0,0};
00017 PwmOut servo(p26);
00018 int iState = 1;
00019 bool bState =0;
00020 
00021 char Adress = 128;
00022 char action;
00023 CAR robot(p9,p10);
00024 //CAR robot;
00025 
00026 void act()
00027 {
00028    //bth.printf("Batterie : %.2f\n\r", batterie.read()*36.30);
00029     action=bth.getc();
00030     switch(action) {
00031         case 'a':
00032             bth.printf("avancer\n\r");
00033             robot.avancer(70);
00034             break;
00035         case 's':
00036             bth.printf("stop\n\r");
00037             robot.arreter();
00038             break;
00039         case 'u':
00040             bth.printf("arret\n\r");
00041             robot.arreter();
00042             break;
00043         case 'r':
00044             bth.printf("reculer\n\r");
00045             robot.reculer(70);
00046             break;
00047         case 'd':
00048             bth.printf("droite\n\r");
00049             robot.tourner_droite(70);
00050             break;
00051         case 'g':
00052             bth.printf("gauche\n\r");
00053             robot.tourner_gauche(70);
00054             break;
00055         case 'h':
00056             bth.printf("demi tour horaire\n\r");
00057             robot.demi_tour_droite(100);
00058             break;
00059         case'l':
00060             bth.printf("demi tour left\n\r");
00061             robot.demi_tour_gauche(100);
00062             break;
00063         case 'o':
00064             bState = !bState;
00065             //iState = 0;
00066             break;
00067     }
00068     action='0';
00069 }
00070 
00071 
00072 ////////////////////////////////////////////
00073 
00074 int main()
00075 {
00076     S2=0;
00077     int visu;
00078     bth.baud(57600);
00079     bth.attach(&act);
00080     bState = 1;
00081     servo.period(0.02);
00082     while(1) {
00083         if (bState) {
00084             switch (iState) {
00085 
00086                 case 0:
00087 
00088                     if (srf08.read() <40 and srf08.read() >0 ) {
00089                         iState = 1;
00090                         bth.printf("stop\n\r");
00091                         robot.arreter();
00092                     } else {
00093                         servo.write(0.08);
00094                         bth.printf("avancer\n\r");
00095                         robot.avancer(50);
00096                     }
00097                     break;
00098 
00099                 case 1 :
00100                     servo.write(0.05);
00101                     wait (0.4);
00102                     aMes[1] = srf08.read();
00103 
00104                     servo.write(0.095);
00105                     wait (0.4);
00106                     aMes[2]= srf08.read();
00107 
00108                     servo.write(0.08);
00109                     
00110                     bth.printf("capt 1 : %.2f     capt 2 : %.2f",aMes[1], aMes[2]);
00111                     //if (aMes[1] <80 and aMes[1] >0)
00112                     if (aMes[2]>40) {
00113                         action = 'g';
00114                         bth.printf("gauche\n\r");
00115                         robot.tourner_gauche(70);
00116                     //} else if (aMes[2] <80 and aMes[2] >0) {
00117                     } else if (aMes[1]>40) {
00118                         bth.printf("droite\n\r");
00119                         robot.tourner_droite(70);
00120                     } else {
00121                         bth.printf("demi tour horaire\n\r");
00122                         robot.demi_tour_droite(100);
00123                     }
00124                     iState = 0;
00125                     break;
00126             }
00127         }
00128         visu=opt.read_u16();
00129         visu=422006/(visu-3475);
00130         //bth.printf("Measured range : %.2f cm\n",srf08.read());
00131     }
00132 }
00133