Code petit robot

Dependencies:   X_NUCLEO_COMMON X_NUCLEO_IHM01A1 mbed

Committer:
julientiron
Date:
Fri Mar 25 21:27:03 2016 +0000
Revision:
0:1cb50d31c3b5
Code petit robot

Who changed what in which revision?

UserRevisionLine numberNew contents of line
julientiron 0:1cb50d31c3b5 1 /**
julientiron 0:1cb50d31c3b5 2 ******************************************************************************
julientiron 0:1cb50d31c3b5 3 * @file main.cpp
julientiron 0:1cb50d31c3b5 4 * @author Julien Tiron, FIP Télécom Bretagne
julientiron 0:1cb50d31c3b5 5 * @version V1.0.0
julientiron 0:1cb50d31c3b5 6 * @date March 23th, 2016
julientiron 0:1cb50d31c3b5 7 * @brief DoorCloser robot main code
julientiron 0:1cb50d31c3b5 8 ******************************************************************************
julientiron 0:1cb50d31c3b5 9 **/
julientiron 0:1cb50d31c3b5 10
julientiron 0:1cb50d31c3b5 11 /* Includes ------------------------------------------------------------------*/
julientiron 0:1cb50d31c3b5 12
julientiron 0:1cb50d31c3b5 13 #include "mbed.h"
julientiron 0:1cb50d31c3b5 14 #include "DevSPI.h"
julientiron 0:1cb50d31c3b5 15 #include "l6474_class.h"
julientiron 0:1cb50d31c3b5 16 #include "DevI2C.h"
julientiron 0:1cb50d31c3b5 17 #include "vl6180x_class.h"
julientiron 0:1cb50d31c3b5 18
julientiron 0:1cb50d31c3b5 19 /* Definitions ---------------------------------------------------------------*/
julientiron 0:1cb50d31c3b5 20
julientiron 0:1cb50d31c3b5 21 #define VL6180X_ADDRESS 0x29
julientiron 0:1cb50d31c3b5 22
julientiron 0:1cb50d31c3b5 23 /* Variables -----------------------------------------------------------------*/
julientiron 0:1cb50d31c3b5 24
julientiron 0:1cb50d31c3b5 25 /* Start and Stop Component */
julientiron 0:1cb50d31c3b5 26 InterruptIn startup(PC_1);
julientiron 0:1cb50d31c3b5 27 Ticker game_length;
julientiron 0:1cb50d31c3b5 28 volatile bool start = 1;
julientiron 0:1cb50d31c3b5 29 volatile bool end = 1;
julientiron 0:1cb50d31c3b5 30
julientiron 0:1cb50d31c3b5 31 /* Motor Control Component */
julientiron 0:1cb50d31c3b5 32 L6474 *motor1;
julientiron 0:1cb50d31c3b5 33 L6474 *motor2;
julientiron 0:1cb50d31c3b5 34
julientiron 0:1cb50d31c3b5 35 /* Distance Sensors Component */
julientiron 0:1cb50d31c3b5 36 DevI2C *i2c =new DevI2C(D14, D15);
julientiron 0:1cb50d31c3b5 37 VL6180X sensor1(i2c);
julientiron 0:1cb50d31c3b5 38 VL6180X sensor2(i2c);
julientiron 0:1cb50d31c3b5 39 VL6180X sensor3(i2c);
julientiron 0:1cb50d31c3b5 40
julientiron 0:1cb50d31c3b5 41 /* Functions -----------------------------------------------------------------*/
julientiron 0:1cb50d31c3b5 42
julientiron 0:1cb50d31c3b5 43 void go()
julientiron 0:1cb50d31c3b5 44 {
julientiron 0:1cb50d31c3b5 45 start = 0;
julientiron 0:1cb50d31c3b5 46 }
julientiron 0:1cb50d31c3b5 47
julientiron 0:1cb50d31c3b5 48 void stop()
julientiron 0:1cb50d31c3b5 49 {
julientiron 0:1cb50d31c3b5 50 end = 0;
julientiron 0:1cb50d31c3b5 51 }
julientiron 0:1cb50d31c3b5 52
julientiron 0:1cb50d31c3b5 53 void init_sensor(){
julientiron 0:1cb50d31c3b5 54
julientiron 0:1cb50d31c3b5 55 }
julientiron 0:1cb50d31c3b5 56
julientiron 0:1cb50d31c3b5 57 void switch_sensor(int number){
julientiron 0:1cb50d31c3b5 58
julientiron 0:1cb50d31c3b5 59 }
julientiron 0:1cb50d31c3b5 60
julientiron 0:1cb50d31c3b5 61 /* Main ----------------------------------------------------------------------*/
julientiron 0:1cb50d31c3b5 62
julientiron 0:1cb50d31c3b5 63 int main()
julientiron 0:1cb50d31c3b5 64 {
julientiron 0:1cb50d31c3b5 65 /*----- Initialization. -----*/
julientiron 0:1cb50d31c3b5 66
julientiron 0:1cb50d31c3b5 67 /* Initializing SPI bus. */
julientiron 0:1cb50d31c3b5 68 DevSPI dev_spi(D11, D12, D13);
julientiron 0:1cb50d31c3b5 69
julientiron 0:1cb50d31c3b5 70 /* Initializing Motor Control Components. */
julientiron 0:1cb50d31c3b5 71 motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
julientiron 0:1cb50d31c3b5 72 motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
julientiron 0:1cb50d31c3b5 73 if (motor1->Init() != COMPONENT_OK)
julientiron 0:1cb50d31c3b5 74 exit(EXIT_FAILURE);
julientiron 0:1cb50d31c3b5 75 if (motor2->Init() != COMPONENT_OK)
julientiron 0:1cb50d31c3b5 76 exit(EXIT_FAILURE);
julientiron 0:1cb50d31c3b5 77
julientiron 0:1cb50d31c3b5 78 /* Interrupt to start the robot */
julientiron 0:1cb50d31c3b5 79 startup.fall(&go);
julientiron 0:1cb50d31c3b5 80
julientiron 0:1cb50d31c3b5 81 /* Interrupt to stop the robot */
julientiron 0:1cb50d31c3b5 82 game_length.attach(&stop, 90); //1 minutes 30 secondes pour la Coupe
julientiron 0:1cb50d31c3b5 83
julientiron 0:1cb50d31c3b5 84 while(start) {
julientiron 0:1cb50d31c3b5 85 /* Waiting code */
julientiron 0:1cb50d31c3b5 86 }
julientiron 0:1cb50d31c3b5 87
julientiron 0:1cb50d31c3b5 88 while(end) {
julientiron 0:1cb50d31c3b5 89 /* In-game code */
julientiron 0:1cb50d31c3b5 90
julientiron 0:1cb50d31c3b5 91 /* Requesting to run backward. */
julientiron 0:1cb50d31c3b5 92 motor1->Run(StepperMotor::BWD);
julientiron 0:1cb50d31c3b5 93 motor2->Run(StepperMotor::FWD);
julientiron 0:1cb50d31c3b5 94
julientiron 0:1cb50d31c3b5 95 /* Waiting until delay has expired. */
julientiron 0:1cb50d31c3b5 96 wait_ms(3000);
julientiron 0:1cb50d31c3b5 97
julientiron 0:1cb50d31c3b5 98 motor1->HardStop();
julientiron 0:1cb50d31c3b5 99 motor2->HardStop();
julientiron 0:1cb50d31c3b5 100
julientiron 0:1cb50d31c3b5 101 motor1->WaitWhileActive();
julientiron 0:1cb50d31c3b5 102 motor2->WaitWhileActive();
julientiron 0:1cb50d31c3b5 103
julientiron 0:1cb50d31c3b5 104 /* Requesting to run backward. */
julientiron 0:1cb50d31c3b5 105 motor1->Run(StepperMotor::FWD);
julientiron 0:1cb50d31c3b5 106 motor2->Run(StepperMotor::BWD);
julientiron 0:1cb50d31c3b5 107
julientiron 0:1cb50d31c3b5 108 /* Waiting until delay has expired. */
julientiron 0:1cb50d31c3b5 109 wait_ms(3000);
julientiron 0:1cb50d31c3b5 110
julientiron 0:1cb50d31c3b5 111 motor1->HardStop();
julientiron 0:1cb50d31c3b5 112 motor2->HardStop();
julientiron 0:1cb50d31c3b5 113
julientiron 0:1cb50d31c3b5 114 motor1->WaitWhileActive();
julientiron 0:1cb50d31c3b5 115 motor2->WaitWhileActive();
julientiron 0:1cb50d31c3b5 116 }
julientiron 0:1cb50d31c3b5 117
julientiron 0:1cb50d31c3b5 118 motor1->HardStop();
julientiron 0:1cb50d31c3b5 119 motor2->HardStop();
julientiron 0:1cb50d31c3b5 120
julientiron 0:1cb50d31c3b5 121 motor1->WaitWhileActive();
julientiron 0:1cb50d31c3b5 122 motor2->WaitWhileActive();
julientiron 0:1cb50d31c3b5 123 }