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
Revision 13:5f08195456a4, committed 2015-12-15
- Comitter:
- jimmery
- Date:
- Tue Dec 15 08:56:36 2015 +0000
- Parent:
- 12:0849b16c2672
- Commit message:
- HUGE RESTRUCTURING OF THE CODE.
Changed in this revision
diff -r 0849b16c2672 -r 5f08195456a4 main.cpp --- a/main.cpp Sat Dec 05 00:26:26 2015 +0000 +++ b/main.cpp Tue Dec 15 08:56:36 2015 +0000 @@ -1,455 +1,82 @@ #include "mbed.h" -#include "AVEncoder.h" - -// set things -Serial pc(SERIAL_TX, SERIAL_RX); -Ticker Systicker; - -PwmOut right_forward(PB_10); -PwmOut right_reverse(PA_6); -PwmOut left_forward(PA_7); -PwmOut left_reverse(PB_6); - -// TODO: change our encoder pins from AnalogIn into: -// otherwise, we can also use the AVEncoder thing as well. -AVEncoder l_enco(PA_15, PB_3); -AVEncoder r_enco(PA_1, PA_10); - -// gyro -AnalogIn _gyro(PA_0); -// AnalogIn gyro_cal(PC_1) ?? currently this isn't connected. - -//Left Front IR -DigitalOut eLF(PC_3); -AnalogIn rLF(PC_0); - //PC_4 is an ADC -//Left Side IR -DigitalOut eLS(PC_2); -AnalogIn rLS(PC_1); - -//Right Side IR -DigitalOut eRS(PC_12); -AnalogIn rRS(PA_4); - -//Right Front IR -DigitalOut eRF(PC_15); -AnalogIn rRF(PB_0); - -DigitalOut myled(LED1); - -volatile float gyro_offset = 0; - -volatile float line_prevError = 0; -volatile float enco_prevError = 0; -volatile float gyro_prevError = 0; - -volatile float line_accumulator = 0; -volatile float line_decayFactor = 1; -volatile float enco_accumulator = 0; -volatile float enco_decayFactor = 1; -volatile float gyro_accumulator = 0; -volatile float gyro_decayFactor = 1.1; - -volatile float set_speed = .75; -volatile float left_speed = .75; -volatile float right_speed = .75; - -const float left_max_speed = 5; // max speed is 6 encoder pulses per ms. -const float right_max_speed = 5; - -const float gyro_propo = 6.5; -const float gyro_integ = 0; -const float gyro_deriv = 10; - -const float enco_propo = 6; -const float enco_integ = 0;//1; -const float enco_deriv = 1000;//.0002; - -const float spin_enco_weight = 1; -const float spin_gyro_weight = 1 - spin_enco_weight; - -const float base_RS = 0.2; -const float base_LS = 0.1; -const float base_RF = 0.002; -const float base_LF = 0.15; - -const float thresh_LF = 0.3; -const float thresh_RF = 0.0045; // TODO: CAUTION USING THIS ALONE. - -const float open_left = 0.22; //calibrate these two -const float open_right = 0.18; -const float wall_left = 0; -const float wall_right = 0; - -volatile float lf_val; -volatile float rf_val; -volatile float ls_val; -volatile float rs_val; - -volatile float enco_error; -volatile float enco_pid; -volatile float gyro_error; -volatile float gyro_pid; -volatile float w_error; -typedef enum -{ - LEFT, - RIGHT, - INVALID -} TURN_DIRECTION; -volatile TURN_DIRECTION turn_direction; - -//volatile int turn_counter; -volatile int right_turn_count; -volatile int examined_left; -volatile int examined_right; - -//time to make a 90 degree turn and move forward one cell length, repectively -//should be replaced with encoder counts -const int lmax_turn_count = 620; //I think we need different constants for left/right -const int rmax_turn_count = 0; -const int cell_length_count = 875; -const float gyro_90 = 0.595; - -volatile int forward_counter = 0; - -// this is just so that we can maintain what state our mouse is in. -// currently this has no real use, but it may in the future. -// or we could just remove this entirely. -typedef enum -{ - STOPPED, - FORWARD, - TURNING, - MOVECELL, - FINISHED, - UNKNOWN -} STATE; -volatile STATE mouse_state; - -volatile int wall_in_front = 0; -volatile int opening_left_ahead = 0; -volatile int opening_right_ahead = 0; -volatile int opening_left = 0; -volatile int opening_right = 0; - -void Scan(); -void offsetCalc(); -void stop(); +#include "pid.h" +#include "sensors.h" +#include "mouse.h" -void encoder_reset() -{ - l_enco.reset(); - r_enco.reset(); - - enco_accumulator = 0; - gyro_accumulator = 0; -} - -//combining detect opening and watch out -bool forward_wall_detected() -{ - return lf_val > thresh_LF; -} - -bool side_wall_detected() -{ - if (rs_val < open_right) - { - mouse_state = MOVECELL; - turn_direction = RIGHT; - return true; - } - else if (ls_val < open_left) - { - mouse_state = MOVECELL; - turn_direction = LEFT; - return true; - } - - return false; -} - -// updates PID values. -void scan() -{ - eRS = 1; - wait_us(50); - rs_val = rRS.read(); - eRS = 0; - - eLS = 1; - wait_us(50); - ls_val = rLS.read(); - eLS = 0; - - eLF = 1; - wait_us(50); - lf_val = rLF.read(); -} - +Ticker Systicker; +Mouse m; void systick() { - scan(); // get the IR values. + m.scan(); // get the IR values. // these values are useful regardless of what mouse_state we are in. - switch (mouse_state) + switch (m.mouse_state) { case STOPPED: - offsetCalc(); - encoder_reset(); + m.offsetCalc(); + m.pid_reset(); + m.sensor_reset(); break; case FORWARD: - if ( forward_wall_detected() ) + if ( m.forward_wall_detected() ) { - mouse_state = STOPPED; - turn_direction = LEFT; + m.mouse_state = STOPPED; + m.turn_direction = LEFT; return; } - -// if ( side_wall_detected() ) -// { -// return; -// } - - enco_error = l_enco.getPulses() - r_enco.getPulses(); - gyro_error = _gyro.read() - gyro_offset; - - enco_accumulator += enco_error; - gyro_accumulator += gyro_error; - - enco_pid = 0; - enco_pid += enco_propo * enco_error; - enco_pid += enco_integ * enco_accumulator; - enco_pid += enco_deriv * (enco_error - enco_prevError); - - gyro_pid = 0; - gyro_pid += gyro_propo * gyro_error; - gyro_pid += gyro_integ * gyro_accumulator; - gyro_pid += gyro_deriv * (gyro_error - gyro_prevError); - - w_error = spin_enco_weight * enco_pid + spin_gyro_weight * gyro_pid; - left_speed = set_speed + w_error; - right_speed = set_speed - w_error; - - enco_prevError = enco_error; - gyro_prevError = gyro_error; - - enco_accumulator += enco_error; - gyro_accumulator += gyro_error; - - enco_accumulator /= enco_decayFactor; - gyro_accumulator /= gyro_decayFactor; + m.run_pid(); + // TODO we need to start saving walls and stuff. break; default: // don't do anything. break; } } - -// computes gyro_offset -// uses a "weighted" average. -// idea is that the current gyro offset is weighted more than previous ones. - // uses the following y(n) = 1/2 * y(n-1) + 1/2 * x(n). - // (therefore y(n) = sum of x(i)/2^i from i from 0 to n.) - // this maintains that there will be some influence from previous factors, but keeps the current value at a higher weight. -// currently this is only in the setup function. we can run this when the mouse is running in a line -// when we figure out good line running pid. -void offsetCalc() -{ - gyro_offset = gyro_offset / 2 + _gyro.read() / 2; -} - - -void stop() -{ - left_forward = 0; - left_reverse = 0; - right_forward = 0; - right_reverse = 0; -} + +void setup() +{ + Mouse m; -void turn() { - // reopen for business. - - float angle_change = 0; - - switch(turn_direction) - { - case LEFT: - while (angle_change < gyro_90) - { - left_forward = 0; - right_forward = set_speed / right_max_speed; - left_reverse = set_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_speed / left_max_speed; - right_forward = 0; - left_reverse = 0; - right_forward = set_speed / right_max_speed; - - pc.printf("gyro read: %f\r\n", _gyro.read()); - angle_change += gyro_offset - _gyro.read(); - } - break; - default: - // error state. - break; - } - -// while ( turn_counter < lmax_turn_count ) -// { -// left_pulses = l_enco.getPulses(); -// right_pulses = r_enco.getPulses(); -// -// turn_counter = ((left_pulses - base_left_pulses) + (right_pulses - base_right_pulses)) / 2; -// -// switch(turn_direction) -// { -// case LEFT: -// left_forward = 0; -// right_forward = set_speed / right_max_speed; -// left_reverse = set_speed / left_max_speed; -// right_reverse = 0; -// break; -// case RIGHT: -// left_forward = set_speed / left_max_speed; -// right_forward = 0; -// left_reverse = 0; -// right_reverse = set_speed / right_max_speed; -// break; -// default: -// // invalid state. did not set a turn direction. -// mouse_state = STOPPED; -// break; -// } -// -// } -} - -// TODO move_cell is not complete. this is copied code from systick. -void 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; - } - encoder_reset(); //right? prepare for turn - } - } -} - -void setup() -{ - eRS = 0; - eRF = 0; - eLS = 0; - eLF = 0; - - mouse_state = FORWARD; - turn_direction = INVALID; - myled = 1; - for ( int i = 0; i < 1000; i++ ) - { - offsetCalc(); - } + Systicker.attach_us(&systick, 1000); wait(2); - - Systicker.attach_us(&systick, 1000); } int main() { setup(); while(1) { - switch (mouse_state) + switch (m.mouse_state) { case STOPPED: - stop(); + m.stop(); wait(1); - if ( turn_direction == INVALID ) + if ( m.turn_direction == INVALID ) { - mouse_state = FORWARD; + m.mouse_state = FORWARD; } else { - mouse_state = TURNING; + m.mouse_state = TURNING; } break; case FORWARD: - left_forward = left_speed / left_max_speed; - left_reverse = 0; - right_forward = right_speed / right_max_speed; - right_reverse = 0; +// left_forward = left_speed / left_max_speed; +// left_reverse = 0; +// right_forward = right_speed / right_max_speed; +// right_reverse = 0; break; case TURNING: - turn(); - mouse_state = STOPPED; - turn_direction = INVALID; + m.turn(); + m.mouse_state = STOPPED; + m.turn_direction = INVALID; break; case MOVECELL: - move_cell(); - mouse_state = STOPPED; + m.move_cell(); + m.mouse_state = STOPPED; break; default: // idk lmao. show some error message? break; } } -} - - -void test_IR_sensors() -{ - eRS = 1; - wait_us(50); - float right_side = rRS.read(); - eRS = 0; - eLS = 1; - wait_us(50); - float left_side = rLS.read(); - eLS = 0; - eRF = 1; - wait_us(50); - float right_front = rRF.read(); - eRF = 0; - eLF = 1; - wait_us(50); - float left_front = rLF.read(); - eLF = 0; - pc.printf("right side: %f, left side: %f right_front: %f, left_front: %f \r\n", right_side, left_side, right_front, left_front); - wait(1); -} - -void test_motors() -{ - encoder_reset(); - - right_forward = 1; - left_forward = 1; - right_reverse = 0; - left_reverse = 0; - - wait(10); - pc.printf("left pulses: %d, right pulses: %d", l_enco.getPulses(), r_enco.getPulses()); } \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 maze.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/maze.cpp Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,1 @@ +#include "maze.h" \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 maze.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/maze.h Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,9 @@ +// IDEA IS TO HAVE THE ALGORITHM DONE IN HERE. +#ifndef MAZE_H +#define MAZE_H + +// MAZE DIMENSIONS. +const int MAX_ROW = 16; +const int MAX_COL = MAX_ROW; + +#endif \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 mouse.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mouse.cpp Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,88 @@ +// 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 +// } +// } +} \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 mouse.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mouse.h Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,105 @@ +// HAS EVERYTHING RELATED TO THE MOUSE. +// includes implementations from: +// mouse.cpp, which includes all motor related actions. +// sensors.cpp, which includes all sensor information. +// pid.cpp, which includes the pid constants/algorithm. +// maze.cpp? when that gets implemented in the future. + +#ifndef MOUSE_H +#define MOUSE_H + +#include "AVEncoder.h" + +typedef enum +{ + LEFT, + RIGHT, + AROUND, + INVALID +} TURN_DIRECTION; + +// this is just so that we can maintain what state our mouse is in. +// currently this has no real use, but it may in the future. +// or we could just remove this entirely. +typedef enum +{ + STOPPED, + FORWARD, + TURNING, + MOVECELL, + FINISHED, + UNKNOWN +} STATE; + +class Mouse +{ +public: + Mouse(); + void stop(); + void setForward( float left_speed, float right_speed ); + void turn( TURN_DIRECTION dir = LEFT, int n_times = 1 ); + void move_cell(); + + void scan(); + void sensor_reset(); + void offsetCalc(); + bool forward_wall_detected(); + bool side_opening_detected(); + + void run_pid(); + void pid_reset(); + + // TODO move into private eventually. + volatile TURN_DIRECTION turn_direction; + volatile STATE mouse_state; +private: + // essential mouse information. + volatile int row; + volatile int col; + + volatile float left_speed; + volatile float right_speed; + + // [-----------------------------------SENSOR INFORMATION-----------------------------------] + // [-------------------------------IMPLEMENTED IN SENSORS.CPP-------------------------------] + void sensor_init(); + + volatile float gyro_offset; + + volatile float lf_val; + volatile float rf_val; + volatile float ls_val; + volatile float rs_val; + + volatile int wall_in_front; + volatile int opening_left_ahead; + volatile int opening_right_ahead; + volatile int opening_left; + volatile int opening_right; + + // [------------------------------------PID INFORMATION.------------------------------------] + // [---------------------------------IMPLEMENTED IN PID.CPP---------------------------------] + // helper functions + float rotational_pid(); + float translational_pid(); + //void pid_reset(); + + // helper data members. (not really needed to be shown - for implementation purposes) + volatile float line_prevError; + volatile float enco_prevError; + volatile float gyro_prevError; + + volatile float set_w; + volatile float set_x; + + volatile float spin_enco_weight; + volatile float spin_gyro_weight; + + // [-------------------------------------TEST FUNCTIONS-------------------------------------] + // [--------------------------------IMPLEMENTED IN TEST.CPP.--------------------------------] + + void test_IR_sensors(); + void test_motors(); +}; + +#endif \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 pid.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pid.cpp Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,71 @@ +#include "pid.h" +#include "mouse.h" +#include "pinouts.h" + +void Mouse::run_pid() +{ + float w_error = rotational_pid(); + float x_error = translational_pid(); + + left_speed = set_x_speed + w_error + x_error; + right_speed = set_x_speed - w_error + x_error; +} + +float Mouse::rotational_pid() +{ + // this may come into play when we start learning curve turns. + //set_w += set_w_speed; + + float enco_error = l_enco.getPulses() - r_enco.getPulses(); + float gyro_error = _gyro.read() - gyro_offset; + + float enco_pid = 0; + enco_pid += enco_propo * enco_error; + enco_pid += enco_deriv * (enco_error - enco_prevError); + + float gyro_pid = 0; + gyro_pid += gyro_propo * gyro_error; + gyro_pid += gyro_deriv * (gyro_error - gyro_prevError); + + float w_error = spin_enco_weight * enco_pid + spin_gyro_weight * gyro_pid; + + // set previous error. + enco_prevError = enco_error; + gyro_prevError = gyro_error; + + // as time goes on, the gyro becomes less and less weighted. + spin_gyro_weight /= gyro_falloff; + spin_enco_weight = 1 - spin_gyro_weight; + + return w_error; +} + +float Mouse::translational_pid() +{ + set_x += 2 * set_x_speed; + + float line_error = l_enco.getPulses() + r_enco.getPulses() - set_x; + + float line_pid = 0; + line_pid += line_propo * line_error; + line_pid += line_deriv * (line_error - line_prevError); + + line_prevError = line_error; + + return line_error; +} + +void Mouse::pid_reset() +{ + spin_enco_weight = 0.5; + spin_gyro_weight = 1 - spin_enco_weight; + + line_prevError = 0; + enco_prevError = 0; + gyro_prevError = 0; + + set_w = 0; + set_x = 0; + + // accumulator reset here? if we use integral. +} \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 pid.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pid.h Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,21 @@ +#ifndef PID_H +#define PID_H + +const float set_x_speed = .75; +const float set_w_speed = 0; + +const float left_max_speed = 5; // max speed is 6 encoder pulses per ms. +const float right_max_speed = 5; + +const float line_propo = 0; +const float line_deriv = 0; + +const float gyro_propo = 6.5; +const float gyro_deriv = 10; + +const float enco_propo = 6; +const float enco_deriv = 1000;//.0002; + +const float gyro_falloff = 1.0005; + +#endif \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 pinouts.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pinouts.cpp Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,39 @@ +#include "mouse.h" +#include "pinouts.h" + +#include "AVEncoder.h" +#include "mbed.h" + +Serial pc(SERIAL_TX, SERIAL_RX); + +PwmOut right_forward(PB_10); +PwmOut right_reverse(PA_6); +PwmOut left_forward(PA_7); +PwmOut left_reverse(PB_6); + +// TODO: change our encoder pins from AnalogIn into: +// otherwise, we can also use the AVEncoder thing as well. +AVEncoder l_enco(PA_15, PB_3); +AVEncoder r_enco(PA_1, PA_10); + +// gyro +AnalogIn _gyro(PA_0); +// AnalogIn gyro_cal(PC_1) ?? currently this isn't connected. + +//Left Front IR +DigitalOut eLF(PC_3); +AnalogIn rLF(PC_0); + //PC_4 is an ADC +//Left Side IR +DigitalOut eLS(PC_2); +AnalogIn rLS(PC_1); + +//Right Side IR +DigitalOut eRS(PC_12); +AnalogIn rRS(PA_4); + +//Right Front IR +DigitalOut eRF(PC_15); +AnalogIn rRF(PB_0); + +DigitalOut myled(LED1);
diff -r 0849b16c2672 -r 5f08195456a4 pinouts.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pinouts.h Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,30 @@ +#ifndef PINOUTS_H +#define PINOUTS_H + +extern Serial pc; + +extern PwmOut right_forward; +extern PwmOut right_reverse; +extern PwmOut left_forward; +extern PwmOut left_reverse; + +extern AVEncoder l_enco; +extern AVEncoder r_enco; + +extern AnalogIn _gyro; + +extern DigitalOut eLF; +extern AnalogIn rLF; + +extern DigitalOut eLS; +extern AnalogIn rLS; + +extern DigitalOut eRS; +extern AnalogIn rRS; + +extern DigitalOut eRF; +extern AnalogIn rRF; + +extern DigitalOut myled; + +#endif \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 sensors.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensors.cpp Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,85 @@ +#include "sensors.h" +#include "mbed.h" +#include "mouse.h" +#include "pinouts.h" + +// updates PID values. +void Mouse::scan() +{ + eRS = 1; + wait_us(50); + rs_val = rRS.read(); + eRS = 0; + + eRF = 1; + wait_us(50); + rf_val = rRF.read(); + + eLS = 1; + wait_us(50); + ls_val = rLS.read(); + eLS = 0; + + eLF = 1; + wait_us(50); + lf_val = rLF.read(); +} + +bool Mouse::forward_wall_detected() +{ + return lf_val > thresh_LF; +} + +bool Mouse::side_opening_detected() +{ + if (rs_val < open_right) + { + mouse_state = MOVECELL; + turn_direction = RIGHT; + return true; + } + else if (ls_val < open_left) + { + mouse_state = MOVECELL; + turn_direction = LEFT; + return true; + } + + return false; +} + +// computes gyro_offset +// uses a "weighted" average. +// idea is that the current gyro offset is weighted more than previous ones. + // uses the following y(n) = 1/2 * y(n-1) + 1/2 * x(n). + // (therefore y(n) = sum of x(i)/2^i from i from 0 to n.) + // this maintains that there will be some influence from previous factors, but keeps the current value at a higher weight. +// currently this is only in the setup function. we can run this when the mouse is running in a line +// when we figure out good line running pid. +void Mouse::offsetCalc() +{ + gyro_offset = gyro_offset / 2 + _gyro.read() / 2; +} + +void Mouse::sensor_reset() +{ + l_enco.reset(); + r_enco.reset(); +} + +void Mouse::sensor_init() +{ + gyro_offset = 0; + + lf_val = 0; + rf_val = 0; + ls_val = 0; + rs_val = 0; + + eLF = 0; + eRF = 0; + eLS = 0; + eRS = 0; + + sensor_reset(); +} \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 sensors.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensors.h Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,24 @@ +#ifndef SENSORS_H +#define SENSORS_H + +const float base_RS = 0.2; +const float base_LS = 0.1; +const float base_RF = 0.002; +const float base_LF = 0.15; + +const float thresh_LF = 0.3; +const float thresh_RF = 0.0045; // TODO: CAUTION USING THIS ALONE. + +const float open_left = 0.22; //calibrate these two +const float open_right = 0.18; +const float wall_left = 0; +const float wall_right = 0; + +// MAZE/MOUSE CONSTANTS. +const int cell_length_count = 2000; + +//time to make a 90 degree turn and move forward one cell length, repectively +//should be replaced with encoder counts +const float gyro_90 = 0.595; + +#endif \ No newline at end of file
diff -r 0849b16c2672 -r 5f08195456a4 test.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.cpp Tue Dec 15 08:56:36 2015 +0000 @@ -0,0 +1,23 @@ +#include "mouse.h" +#include "pinouts.h" + +void Mouse::test_IR_sensors() +{ + scan(); + + pc.printf("right side: %f, left side: %f right_front: %f, left_front: %f \r\n", rs_val, ls_val, rf_val, lf_val); + wait(1); +} + +void Mouse::test_motors() +{ + sensor_reset(); + + right_forward = 1; + left_forward = 1; + right_reverse = 0; + left_reverse = 0; + + wait(10); + pc.printf("left pulses: %d, right pulses: %d", l_enco.getPulses(), r_enco.getPulses()); +} \ No newline at end of file