TomYumBoys / Mbed 2 deprecated MM2017

Dependencies:   mbed

main.cpp

Committer:
kolanery
Date:
2017-05-27
Revision:
28:600932142201
Parent:
27:b980fce784ea
Child:
29:2224bc8bb49d

File content as of revision 28:600932142201:

#include "drivecontrol.h"
#include "io_modules.h"
#include "mbed.h"
#include "maze_solver.h"
//// PIN ASSIGNMENTS
AnalogIn battery(PA_3);
DigitalOut led_1(PB_12);
DigitalOut led_2(PB_13);
DigitalOut led_3(PB_14);
DigitalOut led_4(PB_15);
Serial serial (PA_9, PA_10);
// CONTROL CONSTANTS
const unsigned char  STRAIGHT = 0, LEFT = 1, RIGHT = 2, UTURN = 3;
const int START_POS = 0, END_POS = 0;
const int CONTROL = 1;
Mouse * my_mouse;
unsigned char state; 
int move = 0;

void check_battery () {
    // pc.baud(9600);
    // using the serial functions will have an impact on timing.
    // serial.printf("voltage value is: %3.3f%%\r\n", battery.read()*100.0f);
    // serial.printf("normalized: 0x%04X \r\n", battery.read_u16());
    if (battery.read() < 0.70f){
        // flash led
        led_1 = 1;
        led_2 = 1;
        led_3 = 1;
        led_4 = 1;
    }
}

void flash_led (int _led_1, int _led_2, int _led_3, int _led_4) {
    led_1 = _led_1;
    led_2 = _led_2;
    led_3 = _led_3;
    led_4 = _led_4;
}

void print_debug_message(){
    serial.printf("Next mouse position <%u,%u> \r\n", my_mouse->mouse_x, my_mouse->mouse_y);
    serial.printf("next state: %u \r\n", state);
    serial.printf("previous mouse direction <%d> \r\n", my_mouse->get_prev_direction());
    serial.printf("next cell direction <%d> \r\n", my_mouse->get_next_cell_direction());
  
    move++;
}
int main() {
    DriveControl * driver = new DriveControl (START_POS, END_POS);
    my_mouse = new Mouse(driver);
    driver->resetEncoders();
 
    int count = 0;
    bool finished_traverse_one_cell = true;
    
    
    wait(2);
    while(CONTROL) {
        check_battery();
        wait_ms(1);
        if (driver->right_wall_pid_debug()) {
            flash_led(1,0,1,1);
        }

        //wait(2);
        //driver->print_serial_ports();
        
       if (finished_traverse_one_cell == true) {
            
            state = my_mouse->solve_maze();
            wait(1);
//            print_debug_message();
//            serial.printf("left wall: %d, right wall:%d, front wall: %d \r\n", driver->has_left_wall(), driver->has_right_wall(), driver->has_front_wall());
//            serial.printf("--- \r\n");
//            wait(2);
            if(state == RIGHT){
                flash_led(0,0,1,1);
                wait(0.25);
                driver->resetEncoders();
            }
            else if(state == LEFT){
                flash_led(1,1,0,0);
                wait(0.25);
                driver->resetEncoders();
            }
            else if(state == UTURN){
                flash_led(1,1,1,0);
                wait(0.25);
                driver->resetEncoders();
                count = 0;
            }else{
                //TODO
                //Add error handling
                state = STRAIGHT;
                
                flash_led(1, 0, 0, 0);
            }
            
            finished_traverse_one_cell = false;
        }
        
        if (state == STRAIGHT) {
            if (!driver->should_finish_drive_forward() && !driver-> has_front_wall()) {
                driver->drive_forward();
               
            }
            else {
                driver->clear_pid();
                driver->stop();
                wait(0.25);
                driver->resetEncoders();
                finished_traverse_one_cell = true;
                continue;
            }
        }
        if (state == RIGHT) {
            if (!driver->should_finish_turn_right()) {
                driver->turn_right();
            }
            else {
                driver->stop();
                state = STRAIGHT;
                wait(0.25);
                driver->resetEncoders();
                driver->clear_pid();
               // wait(0.25);
                continue;
            }
        }
        if (state == LEFT) {
            if (!driver->should_finish_turn_left()) {
                driver->turn_left();
            }
            else {
                driver->stop();
                state = STRAIGHT;
                wait(0.25);
                driver->resetEncoders();
                driver->clear_pid();
                //wait(0.25);
                continue;
            }
        }
        
        if (state == UTURN){
            if (!driver->should_finish_turn_left()) {
                driver->turn_left();
            }
            else {
                driver->stop();
                driver->resetEncoders();
                wait(0.25);
                if(count == 1) {
                    state = STRAIGHT;
                }
                count ++;//
                continue;
            }
        }
    }
}