this code is completely restructured, but should do the same thing. did not want to directly commit, since it may not work at all. compiles though.

Dependencies:   AVEncoder mbed-src-AV

Fork of Test by 2015-2016_Mouserat

mouse.cpp

Committer:
jimmery
Date:
2015-12-15
Revision:
13:5f08195456a4

File content as of revision 13:5f08195456a4:

// CONTAINS ALL MOUSE MOTOR MOVEMENTS. AND LOGIC RELATING TO MOVEMENT.

#include "mouse.h"
#include "pinouts.h"
#include "sensors.h"

Mouse::Mouse()
{
    sensor_init();
    pid_reset();
    
    mouse_state = STOPPED;
    turn_direction = INVALID;
    // TODO complete.
}

void Mouse::stop()
{
    left_forward = 0;
    left_reverse = 0;
    right_forward = 0;
    right_reverse = 0;
}

// TODO: consider a logic change. whether it's curve turns or whatever. 
// or just a change from x_speed to w_speed or something. but we should maybe try to
// separate the x_speed from the w_speed. 

// TODO: consider having separate conditions for turning around as well. added in TURN_DIRECTION.
void Mouse::turn(TURN_DIRECTION dir, int n_times) 
{
    float angle_change = 0;
    
    switch(dir)
    {
    case LEFT:
        while (angle_change < gyro_90)
        {
            left_forward = 0;
            right_forward = set_x_speed / right_max_speed;
            left_reverse = set_x_speed / left_max_speed;
            right_reverse = 0;
            
            pc.printf("gyro read: %f\r\n", _gyro.read());
            angle_change += _gyro.read() - gyro_offset;
        }
        break;
    case RIGHT:
        while (angle_change < gyro_90)
        {
            left_forward = set_x_speed / left_max_speed;
            right_forward = 0;
            left_reverse = 0;
            right_forward = set_x_speed / right_max_speed;
            
            pc.printf("gyro read: %f\r\n", _gyro.read());
            angle_change += gyro_offset - _gyro.read();
        }
        break;
    default:
        // error state.
        break;
    }
}

// TODO move_cell is not complete. this is copied code from systick.
void Mouse::move_cell() {
    //bacl in business boys.
//    if (mouse_state == MOVECELL) 
//    {
//        forward_counter = 0.5 * (r_enco.getPulses() + l_enco.getPulses());
//        if (forward_counter > 2000) 
//        {
//            forward_counter = 0;
//            mouse_state = TURNING;
//                    if (opening_left_ahead) 
//                    { 
//                    opening_left_ahead = 0;
//                    turn_direction = LEFT;
//                    } 
//                    else if (opening_right_ahead) 
//                    {
//                    opening_right_ahead = 0;
//                    } 
//            sensor_reset();   //right? prepare for turn
//        }
//    }
}