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

Committer:
aduriseti
Date:
Thu Dec 03 01:26:44 2015 +0000
Revision:
6:61b503990cd6
Parent:
5:f704940c9c7e
Child:
7:b866e3aae05f
Turning logic works (probably)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jimmery 0:13d8a77fb1d7 1 #include "mbed.h"
jimmery 0:13d8a77fb1d7 2 #include "AVEncoder.h"
jimmery 0:13d8a77fb1d7 3
jimmery 0:13d8a77fb1d7 4 // set things
jimmery 0:13d8a77fb1d7 5 Serial pc(SERIAL_TX, SERIAL_RX);
jimmery 0:13d8a77fb1d7 6 Ticker Systicker;
jimmery 0:13d8a77fb1d7 7 Timer timer;
aduriseti 6:61b503990cd6 8 Ticker action_ticker;
aduriseti 6:61b503990cd6 9 Ticker algorithm_ticker;
jimmery 0:13d8a77fb1d7 10
jimmery 0:13d8a77fb1d7 11 PwmOut right_forward(PB_10);
jimmery 0:13d8a77fb1d7 12 PwmOut right_reverse(PA_6);
jimmery 0:13d8a77fb1d7 13 PwmOut left_forward(PA_7);
jimmery 0:13d8a77fb1d7 14 PwmOut left_reverse(PB_6);
jimmery 0:13d8a77fb1d7 15
jimmery 0:13d8a77fb1d7 16 // TODO: change our encoder pins from AnalogIn into:
jimmery 0:13d8a77fb1d7 17 // otherwise, we can also use the AVEncoder thing as well.
jimmery 0:13d8a77fb1d7 18 AVEncoder l_enco(PA_15, PB_3);
jimmery 0:13d8a77fb1d7 19 AVEncoder r_enco(PA_1, PA_10);
jimmery 0:13d8a77fb1d7 20
jimmery 0:13d8a77fb1d7 21 // gyro
jimmery 0:13d8a77fb1d7 22 AnalogIn _gyro(PA_0);
jimmery 0:13d8a77fb1d7 23 // AnalogIn gyro_cal(PC_1) ?? currently this isn't connected.
jimmery 0:13d8a77fb1d7 24
jimmery 0:13d8a77fb1d7 25 //Left Front IR
jimmery 0:13d8a77fb1d7 26 DigitalOut eLF(PC_3);
jimmery 0:13d8a77fb1d7 27 AnalogIn rLF(PC_0);
jimmery 0:13d8a77fb1d7 28 //PC_4 is an ADC
jimmery 0:13d8a77fb1d7 29 //Left Side IR
jimmery 0:13d8a77fb1d7 30 DigitalOut eLS(PC_2);
jimmery 0:13d8a77fb1d7 31 AnalogIn rLS(PC_1);
jimmery 0:13d8a77fb1d7 32
intgsull 3:40333f38771d 33 //Right Side IR
intgsull 3:40333f38771d 34 DigitalOut eRS(PC_12);
intgsull 3:40333f38771d 35 AnalogIn rRS(PA_4);
jimmery 0:13d8a77fb1d7 36
intgsull 3:40333f38771d 37 //Right Front IR
intgsull 3:40333f38771d 38 DigitalOut eRF(PC_15);
intgsull 3:40333f38771d 39 AnalogIn rRF(PB_0);
jimmery 0:13d8a77fb1d7 40
jimmery 0:13d8a77fb1d7 41 DigitalOut myled(LED1);
jimmery 0:13d8a77fb1d7 42
jimmery 0:13d8a77fb1d7 43 volatile float gyro_offset = 0;
jimmery 0:13d8a77fb1d7 44
jimmery 0:13d8a77fb1d7 45 volatile float line_prevError = 0;
jimmery 0:13d8a77fb1d7 46 volatile float enco_prevError = 0;
jimmery 0:13d8a77fb1d7 47 volatile float gyro_prevError = 0;
jimmery 0:13d8a77fb1d7 48
jimmery 0:13d8a77fb1d7 49 volatile float line_accumulator = 0;
jimmery 0:13d8a77fb1d7 50 volatile float line_decayFactor = 1;
jimmery 0:13d8a77fb1d7 51 volatile float enco_accumulator = 0;
intgsull 4:112f3d35bd2d 52 volatile float enco_decayFactor = 1.6;
jimmery 0:13d8a77fb1d7 53 volatile float gyro_accumulator = 0;
intgsull 3:40333f38771d 54 volatile float gyro_decayFactor = 1;
jimmery 0:13d8a77fb1d7 55
intgsull 4:112f3d35bd2d 56 volatile float set_speed = .75;
intgsull 4:112f3d35bd2d 57 volatile float left_speed = .75;
intgsull 4:112f3d35bd2d 58 volatile float right_speed = .75;
jimmery 0:13d8a77fb1d7 59
intgsull 4:112f3d35bd2d 60 const float left_max_speed = 5; // max speed is 6 encoder pulses per ms.
intgsull 4:112f3d35bd2d 61 const float right_max_speed = 5;
jimmery 0:13d8a77fb1d7 62
jimmery 2:82a11e992619 63 const float gyro_propo = 6.5;
jimmery 0:13d8a77fb1d7 64 const float gyro_integ = 0;
jimmery 2:82a11e992619 65 const float gyro_deriv = 10;
jimmery 0:13d8a77fb1d7 66
aduriseti 6:61b503990cd6 67 const float enco_propo = 6;
aduriseti 6:61b503990cd6 68 const float enco_integ = 10;//1;
aduriseti 6:61b503990cd6 69 const float enco_deriv =800;//.0002;
jimmery 0:13d8a77fb1d7 70
intgsull 3:40333f38771d 71 const float spin_enco_weight = 1;
jimmery 0:13d8a77fb1d7 72 const float spin_gyro_weight = 1 - spin_enco_weight;
jimmery 0:13d8a77fb1d7 73
aduriseti 6:61b503990cd6 74 const float frontWall = 0.07; //need to calibrate this threshold to a value where mouse can stop in time
aduriseti 6:61b503990cd6 75 const float open_left = 0.20;
aduriseti 6:61b503990cd6 76 const float open_right = 0.17;
intgsull 1:98efd8dd9077 77 //something like this may be useful
intgsull 1:98efd8dd9077 78
jimmery 0:13d8a77fb1d7 79 volatile float enco_error;
jimmery 0:13d8a77fb1d7 80 volatile float enco_pid;
jimmery 0:13d8a77fb1d7 81 volatile float gyro_error;
jimmery 0:13d8a77fb1d7 82 volatile float gyro_pid;
jimmery 0:13d8a77fb1d7 83 volatile float w_error;
jimmery 0:13d8a77fb1d7 84
intgsull 4:112f3d35bd2d 85 ////////////////////////////////////ir PID constants////////////////////////////////////////////////
intgsull 4:112f3d35bd2d 86 const float leftWall= 0;
intgsull 4:112f3d35bd2d 87 const float rightWall= 0; //we need to find the threshold value for when
intgsull 4:112f3d35bd2d 88 //left or right walls are present
intgsull 4:112f3d35bd2d 89 const float irOffset= 0; // difference between right and left ir sensors when
intgsull 4:112f3d35bd2d 90 //mouse is in the middle
intgsull 4:112f3d35bd2d 91 const float lirOffset= 0; //middle value for when there is only a wall on one
intgsull 4:112f3d35bd2d 92 const float rirOffset= 0; //side of the mouse (left and right)
intgsull 4:112f3d35bd2d 93 volatile float irError = 0;
intgsull 4:112f3d35bd2d 94 volatile float irErrorD = 0;
intgsull 4:112f3d35bd2d 95 volatile float irErrorI = 0;
intgsull 4:112f3d35bd2d 96 volatile float oldirError = 0;
intgsull 4:112f3d35bd2d 97 volatile float totalirError= 0; //errors
intgsull 4:112f3d35bd2d 98 const float irKp = 0;
intgsull 4:112f3d35bd2d 99 const float irKd = 0;
intgsull 4:112f3d35bd2d 100 const float irKi = 0; //constants
intgsull 4:112f3d35bd2d 101 volatile float leftD;
intgsull 4:112f3d35bd2d 102 volatile float rightD;
aduriseti 5:f704940c9c7e 103
intgsull 4:112f3d35bd2d 104 void irPID()
intgsull 4:112f3d35bd2d 105 {
intgsull 4:112f3d35bd2d 106 eLS = 1;
intgsull 4:112f3d35bd2d 107 leftD = rLS.read();
intgsull 4:112f3d35bd2d 108 eLS = 0;
intgsull 4:112f3d35bd2d 109 eRS = 1;
intgsull 4:112f3d35bd2d 110 rightD = rRS.read();
intgsull 4:112f3d35bd2d 111 eRS = 0;
intgsull 4:112f3d35bd2d 112 if(leftD > leftWall && rightD > rightWall)//walls on both sides
intgsull 4:112f3d35bd2d 113 {
intgsull 4:112f3d35bd2d 114 irError = rightD-leftD-irOffset;
intgsull 4:112f3d35bd2d 115
intgsull 4:112f3d35bd2d 116 irErrorD = irError-oldirError;
intgsull 4:112f3d35bd2d 117
intgsull 4:112f3d35bd2d 118 irErrorI += irError;
intgsull 4:112f3d35bd2d 119 }
intgsull 4:112f3d35bd2d 120 else if(leftD > leftWall) //just left wall
intgsull 4:112f3d35bd2d 121 {
intgsull 4:112f3d35bd2d 122
intgsull 4:112f3d35bd2d 123 irError = 2*(lirOffset-leftD);
intgsull 4:112f3d35bd2d 124
intgsull 4:112f3d35bd2d 125 irErrorD=irError-oldirError;
intgsull 4:112f3d35bd2d 126
intgsull 4:112f3d35bd2d 127 irErrorI += irError;
intgsull 4:112f3d35bd2d 128 }
intgsull 4:112f3d35bd2d 129 else if(rightD > rightWall)//just right wall
intgsull 4:112f3d35bd2d 130 {
intgsull 4:112f3d35bd2d 131 irError=2*(rightD-rirOffset);
intgsull 4:112f3d35bd2d 132
intgsull 4:112f3d35bd2d 133 irError = irError-oldirError;
intgsull 4:112f3d35bd2d 134
intgsull 4:112f3d35bd2d 135 irErrorI += irError;
intgsull 4:112f3d35bd2d 136 }
intgsull 4:112f3d35bd2d 137 else if(leftD < leftWall && rightD < rightWall)//no walls!! Use encoder PID
intgsull 4:112f3d35bd2d 138 {
intgsull 4:112f3d35bd2d 139 irError = 0;
intgsull 4:112f3d35bd2d 140 irErrorD = 0;
intgsull 4:112f3d35bd2d 141 irErrorI += irError;
intgsull 4:112f3d35bd2d 142 }
intgsull 4:112f3d35bd2d 143 totalirError = irError*irKp + irErrorD*irKd + irErrorI*irKi;
intgsull 4:112f3d35bd2d 144 oldirError = irError;
intgsull 4:112f3d35bd2d 145 left_speed -= totalirError;
intgsull 4:112f3d35bd2d 146 right_speed += totalirError;
intgsull 4:112f3d35bd2d 147 }
intgsull 4:112f3d35bd2d 148
jimmery 2:82a11e992619 149 // this is just so that we can maintain what state our mouse is in.
jimmery 2:82a11e992619 150 // currently this has no real use, but it may in the future.
jimmery 2:82a11e992619 151 // or we could just remove this entirely.
jimmery 2:82a11e992619 152 typedef enum
jimmery 2:82a11e992619 153 {
jimmery 2:82a11e992619 154 STOPPED,
jimmery 2:82a11e992619 155 FORWARD,
aduriseti 5:f704940c9c7e 156 TURNING,
aduriseti 5:f704940c9c7e 157 FINISHED,
jimmery 2:82a11e992619 158 UNKNOWN
jimmery 2:82a11e992619 159 } STATE;
jimmery 2:82a11e992619 160 volatile STATE mouse_state;
jimmery 2:82a11e992619 161
aduriseti 6:61b503990cd6 162 volatile int wall_in_front = 0;
aduriseti 6:61b503990cd6 163 volatile int opening_left_ahead = 0;
aduriseti 6:61b503990cd6 164 volatile int opening_right_ahead = 0;
aduriseti 6:61b503990cd6 165 volatile int opening_left;
aduriseti 6:61b503990cd6 166 volatile int opening_right;
aduriseti 6:61b503990cd6 167
jimmery 2:82a11e992619 168 void watchOut();
jimmery 2:82a11e992619 169 void offsetCalc();
jimmery 2:82a11e992619 170 void stop();
jimmery 2:82a11e992619 171
aduriseti 5:f704940c9c7e 172 void encoder_reset()
jimmery 0:13d8a77fb1d7 173 {
jimmery 0:13d8a77fb1d7 174 l_enco.reset();
jimmery 0:13d8a77fb1d7 175 r_enco.reset();
jimmery 0:13d8a77fb1d7 176 }
jimmery 0:13d8a77fb1d7 177
aduriseti 6:61b503990cd6 178 void detect_opening() {
aduriseti 6:61b503990cd6 179 eRS = 1;
aduriseti 6:61b503990cd6 180 float right_side = rRS.read();
aduriseti 6:61b503990cd6 181 eRS = 0;
aduriseti 6:61b503990cd6 182 eLS = 1;
aduriseti 6:61b503990cd6 183 float left_side = rLS.read();
aduriseti 6:61b503990cd6 184 eLS = 0;
aduriseti 6:61b503990cd6 185 if (right_side < open_right) {
aduriseti 6:61b503990cd6 186 mouse_state = STOPPED;
aduriseti 6:61b503990cd6 187 opening_right_ahead = 1; //handler for this condition sets back to 0
aduriseti 6:61b503990cd6 188 } else if (left_side < open_left) {
aduriseti 6:61b503990cd6 189 mouse_state = STOPPED;
aduriseti 6:61b503990cd6 190 opening_left_ahead = 1; //handler fot this sets back to 0 like above
aduriseti 6:61b503990cd6 191 }
aduriseti 6:61b503990cd6 192 }
aduriseti 6:61b503990cd6 193
jimmery 0:13d8a77fb1d7 194 void systick()
jimmery 0:13d8a77fb1d7 195 {
aduriseti 5:f704940c9c7e 196 //watchOut();
jimmery 2:82a11e992619 197 if ( mouse_state == STOPPED )
jimmery 2:82a11e992619 198 {
jimmery 2:82a11e992619 199 offsetCalc();
jimmery 2:82a11e992619 200 stop();
aduriseti 6:61b503990cd6 201 encoder_reset(); // maybe problems with this, reconsider
jimmery 2:82a11e992619 202 return;
aduriseti 5:f704940c9c7e 203 } else if (mouse_state == FORWARD) {
aduriseti 5:f704940c9c7e 204 watchOut();
aduriseti 6:61b503990cd6 205 //detect_opening();
aduriseti 5:f704940c9c7e 206
aduriseti 5:f704940c9c7e 207 enco_error = l_enco.getPulses() - r_enco.getPulses();
aduriseti 5:f704940c9c7e 208 gyro_error = _gyro.read() - gyro_offset;
aduriseti 5:f704940c9c7e 209
aduriseti 5:f704940c9c7e 210 enco_accumulator += enco_error;
aduriseti 5:f704940c9c7e 211 gyro_accumulator += gyro_error;
aduriseti 5:f704940c9c7e 212
aduriseti 5:f704940c9c7e 213 enco_pid = 0;
aduriseti 5:f704940c9c7e 214 enco_pid += enco_propo * enco_error;
aduriseti 5:f704940c9c7e 215 enco_pid += enco_integ * enco_accumulator;
aduriseti 5:f704940c9c7e 216 enco_pid += enco_deriv * (enco_error - enco_prevError);
aduriseti 5:f704940c9c7e 217
aduriseti 5:f704940c9c7e 218 gyro_pid = 0;
aduriseti 5:f704940c9c7e 219 gyro_pid += gyro_propo * gyro_error;
aduriseti 5:f704940c9c7e 220 gyro_pid += gyro_integ * gyro_accumulator;
aduriseti 5:f704940c9c7e 221 gyro_pid += gyro_deriv * (gyro_error - gyro_prevError);
aduriseti 5:f704940c9c7e 222
aduriseti 5:f704940c9c7e 223 w_error = spin_enco_weight * enco_pid + spin_gyro_weight * gyro_pid;
aduriseti 5:f704940c9c7e 224 left_speed = set_speed + w_error;
aduriseti 5:f704940c9c7e 225 right_speed = set_speed - w_error;
aduriseti 5:f704940c9c7e 226
aduriseti 5:f704940c9c7e 227 left_forward = left_speed / left_max_speed;
aduriseti 5:f704940c9c7e 228 left_reverse = 0;
aduriseti 5:f704940c9c7e 229 right_forward = right_speed / right_max_speed;
aduriseti 5:f704940c9c7e 230 right_reverse = 0;
aduriseti 5:f704940c9c7e 231
aduriseti 5:f704940c9c7e 232 enco_prevError = enco_error;
aduriseti 5:f704940c9c7e 233 gyro_prevError = gyro_error;
aduriseti 5:f704940c9c7e 234
aduriseti 5:f704940c9c7e 235 enco_accumulator += enco_error;
aduriseti 5:f704940c9c7e 236 gyro_accumulator += gyro_error;
aduriseti 5:f704940c9c7e 237
aduriseti 5:f704940c9c7e 238 enco_accumulator /= enco_decayFactor;
aduriseti 5:f704940c9c7e 239 gyro_accumulator /= gyro_decayFactor;
aduriseti 5:f704940c9c7e 240
aduriseti 5:f704940c9c7e 241 //reset();
jimmery 2:82a11e992619 242 }
jimmery 0:13d8a77fb1d7 243 }
jimmery 0:13d8a77fb1d7 244
jimmery 0:13d8a77fb1d7 245 // computes gyro_offset
jimmery 0:13d8a77fb1d7 246 // uses a "weighted" average.
jimmery 0:13d8a77fb1d7 247 // idea is that the current gyro offset is weighted more than previous ones.
jimmery 0:13d8a77fb1d7 248 // uses the following y(n) = 1/2 * y(n-1) + 1/2 * x(n).
jimmery 0:13d8a77fb1d7 249 // (therefore y(n) = sum of x(i)/2^i from i from 0 to n.)
jimmery 0:13d8a77fb1d7 250 // this maintains that there will be some influence from previous factors, but keeps the current value at a higher weight.
jimmery 0:13d8a77fb1d7 251 // currently this is only in the setup function. we can run this when the mouse is running in a line
jimmery 0:13d8a77fb1d7 252 // when we figure out good line running pid.
jimmery 0:13d8a77fb1d7 253 void offsetCalc()
jimmery 0:13d8a77fb1d7 254 {
jimmery 0:13d8a77fb1d7 255 gyro_offset = gyro_offset / 2 + _gyro.read() / 2;
jimmery 0:13d8a77fb1d7 256 }
jimmery 0:13d8a77fb1d7 257
jimmery 0:13d8a77fb1d7 258
intgsull 1:98efd8dd9077 259 void stop()
intgsull 1:98efd8dd9077 260 {
intgsull 1:98efd8dd9077 261 left_forward = 1;
intgsull 1:98efd8dd9077 262 left_reverse = 1;
intgsull 1:98efd8dd9077 263 right_forward = 1;
intgsull 1:98efd8dd9077 264 right_reverse = 1;
jimmery 2:82a11e992619 265 }
aduriseti 6:61b503990cd6 266
aduriseti 6:61b503990cd6 267 typedef enum
aduriseti 6:61b503990cd6 268 {
aduriseti 6:61b503990cd6 269 LEFT,
aduriseti 6:61b503990cd6 270 RIGHT
aduriseti 6:61b503990cd6 271 } TURN_DIRECTION;
aduriseti 6:61b503990cd6 272 volatile TURN_DIRECTION turn_direction;
aduriseti 6:61b503990cd6 273
aduriseti 6:61b503990cd6 274 volatile int turn_counter;
aduriseti 6:61b503990cd6 275 volatile int right_turn_count;
aduriseti 6:61b503990cd6 276 volatile int examined_left;
aduriseti 6:61b503990cd6 277 volatile int examined_right;
aduriseti 6:61b503990cd6 278
aduriseti 6:61b503990cd6 279 //time to make a 90 degree turn and move forward one cell length, repectively
aduriseti 6:61b503990cd6 280 //should be replaced with encoder counts
aduriseti 6:61b503990cd6 281 volatile int max_turn_count = 800;
aduriseti 6:61b503990cd6 282 volatile int cell_length_count = 800;
intgsull 1:98efd8dd9077 283
intgsull 1:98efd8dd9077 284 void watchOut()
intgsull 1:98efd8dd9077 285 {
aduriseti 5:f704940c9c7e 286 //if (mouse_state == FORWARD) {
aduriseti 5:f704940c9c7e 287 eRF = 1;
aduriseti 5:f704940c9c7e 288 float right = rRF.read();
aduriseti 5:f704940c9c7e 289 eRF = 0;
aduriseti 5:f704940c9c7e 290 eLF = 1;
aduriseti 5:f704940c9c7e 291 float left = rLF.read();
aduriseti 5:f704940c9c7e 292 eLF = 0;
aduriseti 5:f704940c9c7e 293 if(left > frontWall || right > frontWall)
aduriseti 5:f704940c9c7e 294 {
aduriseti 5:f704940c9c7e 295 mouse_state = STOPPED;
aduriseti 6:61b503990cd6 296 wall_in_front = 1; //handler for this condition sets back to zero
aduriseti 6:61b503990cd6 297
aduriseti 6:61b503990cd6 298 encoder_reset();
aduriseti 6:61b503990cd6 299 mouse_state = TURNING;
aduriseti 6:61b503990cd6 300 turn_direction = LEFT;
aduriseti 6:61b503990cd6 301 }
aduriseti 5:f704940c9c7e 302 }
aduriseti 5:f704940c9c7e 303
aduriseti 5:f704940c9c7e 304
aduriseti 5:f704940c9c7e 305 void turn() {
aduriseti 5:f704940c9c7e 306 if (mouse_state == TURNING) {
aduriseti 5:f704940c9c7e 307 if (turn_counter < max_turn_count) {
aduriseti 5:f704940c9c7e 308 int left_pulses = l_enco.getPulses();
aduriseti 5:f704940c9c7e 309 int right_pulses = r_enco.getPulses();
aduriseti 5:f704940c9c7e 310 turn_counter = (left_pulses + right_pulses)/2;
aduriseti 6:61b503990cd6 311
aduriseti 6:61b503990cd6 312 //maybe eventually replace this with PID control -> systicker
aduriseti 5:f704940c9c7e 313 if (turn_direction == LEFT) {
aduriseti 6:61b503990cd6 314 left_forward = 0;
aduriseti 6:61b503990cd6 315 left_reverse = set_speed / left_max_speed;
aduriseti 6:61b503990cd6 316 right_forward = set_speed / right_max_speed;
aduriseti 6:61b503990cd6 317 right_reverse = 0;
aduriseti 5:f704940c9c7e 318 } else {
aduriseti 6:61b503990cd6 319 left_forward = set_speed / left_max_speed;
aduriseti 6:61b503990cd6 320 left_reverse = 0;
aduriseti 6:61b503990cd6 321 right_forward = 0;
aduriseti 6:61b503990cd6 322 right_reverse = set_speed / right_max_speed;
aduriseti 6:61b503990cd6 323 }
aduriseti 6:61b503990cd6 324 } else {
aduriseti 6:61b503990cd6 325
aduriseti 6:61b503990cd6 326 stop();
aduriseti 6:61b503990cd6 327 turn_counter = 0;
aduriseti 5:f704940c9c7e 328 encoder_reset();
aduriseti 5:f704940c9c7e 329 mouse_state = FORWARD;
aduriseti 5:f704940c9c7e 330 }
aduriseti 5:f704940c9c7e 331 }
aduriseti 5:f704940c9c7e 332 }
aduriseti 6:61b503990cd6 333
aduriseti 6:61b503990cd6 334 volatile int forward_counter;
aduriseti 6:61b503990cd6 335
aduriseti 6:61b503990cd6 336 void move_cell() {
aduriseti 6:61b503990cd6 337 mouse_state = FORWARD;
aduriseti 6:61b503990cd6 338 forward_counter = 0.5 * (r_enco.getPulses() + l_enco.getPulses());
aduriseti 6:61b503990cd6 339 if (forward_counter > 2000) {
aduriseti 6:61b503990cd6 340 action_ticker.detach();
aduriseti 6:61b503990cd6 341 forward_counter = 0;
aduriseti 6:61b503990cd6 342 //encoder_reset();
aduriseti 6:61b503990cd6 343 mouse_state = STOPPED;
aduriseti 6:61b503990cd6 344 if (opening_left_ahead) { //takes care of case where opening on both sides - defaults to left turn
aduriseti 6:61b503990cd6 345 opening_left_ahead = 0;
aduriseti 6:61b503990cd6 346 opening_left = 1;
aduriseti 6:61b503990cd6 347 } else if (opening_right_ahead) {
aduriseti 6:61b503990cd6 348 opening_right_ahead = 0;
aduriseti 6:61b503990cd6 349 opening_right = 1;
aduriseti 6:61b503990cd6 350 } else {
aduriseti 6:61b503990cd6 351 //we fucked up
aduriseti 6:61b503990cd6 352 }
aduriseti 6:61b503990cd6 353 }
aduriseti 6:61b503990cd6 354 }
aduriseti 5:f704940c9c7e 355
aduriseti 6:61b503990cd6 356
aduriseti 6:61b503990cd6 357 void algorithm() {
aduriseti 5:f704940c9c7e 358 if (mouse_state == STOPPED) {
aduriseti 5:f704940c9c7e 359 encoder_reset();
aduriseti 6:61b503990cd6 360 mouse_state = TURNING;
aduriseti 6:61b503990cd6 361 turn_direction = LEFT;
aduriseti 6:61b503990cd6 362 //action_ticker.attach_us(&turn, 1000);
aduriseti 6:61b503990cd6 363
aduriseti 6:61b503990cd6 364 /*if (opening_left_ahead || opening_right_ahead) {
aduriseti 6:61b503990cd6 365 action_ticker.attach_us(&move_cell, 1000);
aduriseti 6:61b503990cd6 366 }
aduriseti 6:61b503990cd6 367 if (opening_left) {
aduriseti 5:f704940c9c7e 368 mouse_state = TURNING;
aduriseti 6:61b503990cd6 369 turn_direction = LEFT;
aduriseti 6:61b503990cd6 370 action_ticker.attach_us(&turn, 1000);
aduriseti 5:f704940c9c7e 371 }
aduriseti 6:61b503990cd6 372 if (opening_right) {
aduriseti 6:61b503990cd6 373 mouse_state = TURNING;
aduriseti 6:61b503990cd6 374 turn_direction = RIGHT;
aduriseti 6:61b503990cd6 375 action_ticker.attach_us(&turn, 1000);
aduriseti 6:61b503990cd6 376 }*/
intgsull 1:98efd8dd9077 377 }
intgsull 1:98efd8dd9077 378 }
intgsull 1:98efd8dd9077 379
intgsull 4:112f3d35bd2d 380
jimmery 0:13d8a77fb1d7 381 void setup()
jimmery 0:13d8a77fb1d7 382 {
jimmery 0:13d8a77fb1d7 383 pc.printf("Hello World\r\n");
jimmery 0:13d8a77fb1d7 384
jimmery 0:13d8a77fb1d7 385 eRS = 0;
jimmery 0:13d8a77fb1d7 386 eRF = 0;
jimmery 0:13d8a77fb1d7 387 eLS = 0;
jimmery 0:13d8a77fb1d7 388 eLF = 0;
jimmery 0:13d8a77fb1d7 389
aduriseti 6:61b503990cd6 390
aduriseti 6:61b503990cd6 391
jimmery 2:82a11e992619 392 mouse_state = FORWARD;
jimmery 2:82a11e992619 393
jimmery 0:13d8a77fb1d7 394 myled = 1;
jimmery 0:13d8a77fb1d7 395
jimmery 0:13d8a77fb1d7 396 for ( int i = 0; i < 1000; i++ )
jimmery 0:13d8a77fb1d7 397 {
jimmery 0:13d8a77fb1d7 398 offsetCalc();
jimmery 0:13d8a77fb1d7 399 }
jimmery 0:13d8a77fb1d7 400
jimmery 0:13d8a77fb1d7 401 //left_forward = left_speed / left_max_speed;
jimmery 0:13d8a77fb1d7 402 // left_reverse = 0;
jimmery 0:13d8a77fb1d7 403 // right_forward = right_speed / right_max_speed;
jimmery 0:13d8a77fb1d7 404 // right_reverse = 0;
jimmery 0:13d8a77fb1d7 405
jimmery 0:13d8a77fb1d7 406 wait(2);
jimmery 0:13d8a77fb1d7 407
jimmery 0:13d8a77fb1d7 408 // repeat this for some time frame.
aduriseti 6:61b503990cd6 409
jimmery 0:13d8a77fb1d7 410 Systicker.attach_us(&systick, 1000);
aduriseti 6:61b503990cd6 411
aduriseti 6:61b503990cd6 412 //encoder_reset();
aduriseti 6:61b503990cd6 413 //mouse_state = TURNING;
aduriseti 6:61b503990cd6 414 //turn_direction = LEFT;
aduriseti 6:61b503990cd6 415 action_ticker.attach_us(&turn, 1000);
aduriseti 6:61b503990cd6 416
aduriseti 5:f704940c9c7e 417 //ir_ticker.attach_us(&irPID, 100000);
aduriseti 6:61b503990cd6 418
aduriseti 6:61b503990cd6 419 //algorithm_ticker.attach_us(&algorithm, 1000);
aduriseti 6:61b503990cd6 420
aduriseti 6:61b503990cd6 421
aduriseti 6:61b503990cd6 422
aduriseti 6:61b503990cd6 423
jimmery 0:13d8a77fb1d7 424 }
jimmery 0:13d8a77fb1d7 425
jimmery 0:13d8a77fb1d7 426
jimmery 0:13d8a77fb1d7 427 int main() {
jimmery 0:13d8a77fb1d7 428 setup();
intgsull 3:40333f38771d 429
intgsull 3:40333f38771d 430
jimmery 0:13d8a77fb1d7 431 while(1) {
intgsull 3:40333f38771d 432 //
intgsull 3:40333f38771d 433 // pc.printf("left pulss %f, right pulses %f \r\n", l_enco.getPulses(), r_enco.getPulses());
intgsull 3:40333f38771d 434 // //wait(1);
aduriseti 6:61b503990cd6 435 /*eLS = 1;
aduriseti 6:61b503990cd6 436 eRS = 1;
aduriseti 6:61b503990cd6 437 float right_side = rRS.read();
aduriseti 6:61b503990cd6 438 float left_side = rLS.read();
aduriseti 6:61b503990cd6 439 pc.printf("right side IR %f, left side IR %f \r\n", right_side, left_side);
aduriseti 6:61b503990cd6 440 eLS = 0;
aduriseti 6:61b503990cd6 441 eRS = 0;*/
intgsull 3:40333f38771d 442 }
intgsull 3:40333f38771d 443 // pc.printf("left pulses %d, right pulses %d \r\n", l_enco.getPulses(), r_enco.getPulses()); //encoder testing
jimmery 0:13d8a77fb1d7 444 //wait(1);
jimmery 0:13d8a77fb1d7 445 }