Code petit robot

Dependencies:   X_NUCLEO_COMMON X_NUCLEO_IHM01A1 mbed

main.cpp

Committer:
julientiron
Date:
2016-03-25
Revision:
0:1cb50d31c3b5

File content as of revision 0:1cb50d31c3b5:

/**
 ******************************************************************************
 * @file    main.cpp
 * @author  Julien Tiron, FIP Télécom Bretagne
 * @version V1.0.0
 * @date    March 23th, 2016
 * @brief   DoorCloser robot main code
 ******************************************************************************
 **/

/* Includes ------------------------------------------------------------------*/

#include "mbed.h"
#include "DevSPI.h"
#include "l6474_class.h"
#include "DevI2C.h"
#include "vl6180x_class.h"

/* Definitions ---------------------------------------------------------------*/

#define VL6180X_ADDRESS 0x29

/* Variables -----------------------------------------------------------------*/

/* Start and  Stop Component */
InterruptIn startup(PC_1);
Ticker game_length;
volatile bool start = 1;
volatile bool end = 1;

/* Motor Control Component */
L6474 *motor1;
L6474 *motor2;

/* Distance Sensors Component */
DevI2C *i2c =new DevI2C(D14, D15); 
VL6180X sensor1(i2c);
VL6180X sensor2(i2c);
VL6180X sensor3(i2c);

/* Functions -----------------------------------------------------------------*/

void go()
{
    start = 0;
}

void stop()
{
    end = 0;
}

void init_sensor(){
    
}
    
void switch_sensor(int number){
    
}

/* Main ----------------------------------------------------------------------*/

int main()
{
    /*----- Initialization. -----*/

    /* Initializing SPI bus. */
    DevSPI dev_spi(D11, D12, D13);

    /* Initializing Motor Control Components. */
    motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
    motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
    if (motor1->Init() != COMPONENT_OK)
        exit(EXIT_FAILURE);
    if (motor2->Init() != COMPONENT_OK)
        exit(EXIT_FAILURE);

    /* Interrupt to start the robot */
    startup.fall(&go);
    
    /* Interrupt to stop the robot */
    game_length.attach(&stop, 90); //1 minutes 30 secondes pour la Coupe

    while(start) {
        /* Waiting code */
    }
    
    while(end) {
        /* In-game code */
        
        /* Requesting to run backward. */
        motor1->Run(StepperMotor::BWD);
        motor2->Run(StepperMotor::FWD);

        /* Waiting until delay has expired. */
        wait_ms(3000);
        
        motor1->HardStop();
        motor2->HardStop();

        motor1->WaitWhileActive();
        motor2->WaitWhileActive();
        
        /* Requesting to run backward. */
        motor1->Run(StepperMotor::FWD);
        motor2->Run(StepperMotor::BWD);

        /* Waiting until delay has expired. */
        wait_ms(3000);
        
        motor1->HardStop();
        motor2->HardStop();

        motor1->WaitWhileActive();
        motor2->WaitWhileActive();
    }
    
    motor1->HardStop();
    motor2->HardStop();

    motor1->WaitWhileActive();
    motor2->WaitWhileActive();
}