jj

Dependencies:   VL6180x X_NUCLEO_COMMON X_NUCLEO_IHM01A1 mbed

Fork of 1-DoorCloser by Robotique FIP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  Julien Tiron, FIP Télécom Bretagne
00005  * @version V1.0.0
00006  * @date    March 23th, 2016
00007  * @brief   DoorCloser robot main code
00008  ******************************************************************************
00009  **/
00010 
00011 /* Includes ------------------------------------------------------------------*/
00012 
00013 #include "mbed.h"
00014 #include "DevSPI.h"
00015 #include "l6474_class.h"
00016 #include "DevI2C.h"
00017 #include "vl6180x_class.h"
00018 #include "VL6180x.h"
00019 
00020 /* Definitions ---------------------------------------------------------------*/
00021 
00022 #define VL6180X_ADDRESS 0x29
00023 
00024 /* Variables -----------------------------------------------------------------*/
00025 
00026 /* Start and  Stop Component */
00027 
00028 InterruptIn startup(PC_1);
00029 Ticker game_length;
00030 volatile bool start = 1;
00031 volatile bool end = 1;
00032 char data = 0x08 | (char)64;
00033 bool tag = true;
00034 int i2cAddres=0x70;                // Address of DS1307 is 0x68 (7 bit address)
00035 int i2c8BitAddres= i2cAddres <<1;  // Convert to 8bit addressing used by mbed
00036 volatile unsigned int step = 1;
00037 /* Motor Control Component */
00038 L6474 *motor1;
00039 L6474 *motor2;
00040 
00041 /* Distance Sensors Component */
00042 //DevI2C *i2c =new DevI2C(D14, D15);
00043 /*VL6180X sensor1(i2c);
00044 VL6180X sensor2(i2c);
00045 VL6180X sensor3(i2c);*/
00046 I2C i2c(D14, D15);
00047 VL6180x sensor(D14, D15, VL6180X_ADDRESS<<1);
00048 
00049 /* Functions -----------------------------------------------------------------*/
00050 
00051 void go()
00052 {
00053     start = 0;
00054 }
00055 
00056 void stop()
00057 {
00058     end = 0;
00059 }
00060 
00061 void init_sensor()
00062 {
00063 
00064 }
00065 
00066 void switch_sensor(int number)
00067 {
00068 
00069 }
00070 
00071 void deplacement(int distanceEnCm){
00072     int nbPas = distanceEnCm*160;
00073     motor1->Move(StepperMotor::FWD,nbPas);
00074     motor2->Move(StepperMotor::BWD,nbPas);
00075     motor1->WaitWhileActive();
00076     motor2->WaitWhileActive();
00077 }
00078 
00079 void rotation(int angleEnDegre, int diametre){
00080     int deplacementEnCm = (angleEnDegre/360)*(diametre-2)*3.14;
00081     int nbPas = 160*deplacementEnCm;
00082     motor1->Move(StepperMotor::FWD,nbPas);
00083     motor2->Move(StepperMotor::FWD,nbPas);
00084     motor1->WaitWhileActive();
00085     motor2->WaitWhileActive();
00086 }
00087 
00088 /* Main ----------------------------------------------------------------------*/
00089 
00090 int main()
00091 {
00092     /*----- Initialization. -----*/
00093 
00094     /* Initializing SPI bus. */
00095     DevSPI dev_spi(D11, D12, D13);
00096 
00097     /* Initializing Motor Control Components. */
00098     motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
00099     motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
00100     if (motor1->Init() != COMPONENT_OK)
00101         exit(EXIT_FAILURE);
00102     if (motor2->Init() != COMPONENT_OK)
00103         exit(EXIT_FAILURE);
00104     int result = i2c.write(i2c8BitAddres, &data, 1 );
00105     sensor.VL6180xDefautSettings();
00106 
00107     /* Interrupt to start the robot */
00108     startup.fall(&go);
00109 
00110     /* Interrupt to stop the robot */
00111     game_length.attach(&stop, 90); //1 minutes 30 secondes pour la Coupe
00112 
00113     while(start) {
00114         /* Waiting code */
00115 
00116     }
00117 
00118     while(end) {
00119         /* In-game code */
00120 
00121 
00122         switch (step) {
00123 
00124             case 0:
00125                 step = 1;
00126                 break;
00127 
00128             case 1 :
00129                 /* Avancer du tapis judqu'au mur */
00130                 motor1->Run(StepperMotor::FWD);
00131                 motor2->Run(StepperMotor::BWD);
00132                 while(sensor.getDistance()>30) {
00133                     //Tant que la distance est superieure on continue d'avancer
00134                     wait_ms(20);
00135                 }
00136                 motor1->Move(StepperMotor::FWD,1100);
00137                 motor2->Move(StepperMotor::BWD,1100);
00138                 motor1->WaitWhileActive();
00139                 motor2->WaitWhileActive();
00140                 motor1->HardStop();
00141                 motor2->HardStop();
00142                 step = 2 ;
00143                 break;
00144 
00145             case 2 : /* Angle 90° */
00146                 motor1->Move(StepperMotor::FWD,2100);
00147                 motor2->Move(StepperMotor::FWD,2100);
00148                 motor1->WaitWhileActive();
00149                 motor2->WaitWhileActive();
00150                 step = 3 ;
00151                 break;
00152 
00153             case 3 : /* Fermeture de 2 portes : 60cm avant + arrêt à 3 cm du mur */
00154                 motor1->Move(StepperMotor::FWD,9000); //160pas = 1cm
00155                 motor2->Move(StepperMotor::BWD,9000);
00156                 motor1->WaitWhileActive();
00157                 motor2->WaitWhileActive();
00158                 motor1->Run(StepperMotor::FWD);
00159                 motor2->Run(StepperMotor::BWD);
00160                 while(sensor.getDistance()>30) {
00161                     //Tant que la distance est superieure on continue d'avancer
00162                     wait_ms(20);
00163                 }
00164                 motor1->HardStop();
00165                 motor2->HardStop();
00166                 step = 4;
00167                 break;
00168 
00169             case 4 :
00170                 // STOP : robot arrêté
00171                 break;
00172 
00173         }
00174 
00175 
00176         /* Waiting until delay has expired. */
00177         /*wait_ms(3000);
00178 
00179         motor1->HardStop();
00180         motor2->HardStop();
00181 
00182         motor1->WaitWhileActive();
00183         motor2->WaitWhileActive();
00184 
00185         /* Requesting to run backward. */
00186         /*motor1->Run(StepperMotor::FWD);
00187         motor2->Run(StepperMotor::BWD);
00188 
00189         /* Waiting until delay has expired. */
00190         /*wait_ms(3000);
00191 
00192         motor1->HardStop();
00193         motor2->HardStop();
00194 
00195         motor1->WaitWhileActive();
00196         motor2->WaitWhileActive();*/
00197     }
00198 
00199     motor1->HardStop();
00200     motor2->HardStop();
00201 
00202     motor1->WaitWhileActive();
00203     motor2->WaitWhileActive();
00204 }