EE149
/
FinalProject
Final Project files for mBed development.
main.c@41:1b6d8664d7a7, 2014-12-18 (annotated)
- Committer:
- alecguertin
- Date:
- Thu Dec 18 03:31:58 2014 +0000
- Revision:
- 41:1b6d8664d7a7
- Parent:
- 40:0199bad6c979
- Child:
- 42:727612987d77
added fsm logic to drawing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lsaristo | 1:7e0243c27ecb | 1 | /** |
lsaristo | 1:7e0243c27ecb | 2 | * @file driver.c |
lsaristo | 29:459ff10d2a07 | 3 | * @brief Primary control logic for robot. |
lsaristo | 1:7e0243c27ecb | 4 | * |
lsaristo | 29:459ff10d2a07 | 5 | * @author John Wilkey |
lsaristo | 29:459ff10d2a07 | 6 | * @author Alec Guertin |
lsaristo | 29:459ff10d2a07 | 7 | * @author Chester Chu |
lsaristo | 1:7e0243c27ecb | 8 | */ |
lsaristo | 9:3a0433c391cb | 9 | #include "main.h" |
John Wilkey |
5:01882c3de2dc | 10 | |
lsaristo | 29:459ff10d2a07 | 11 | m3pi pi; /**< m3pi Object for all control operations. */ |
lsaristo | 29:459ff10d2a07 | 12 | Timer timer; /**< Timer used to wait in timerWait. */ |
lsaristo | 29:459ff10d2a07 | 13 | DigitalIn start_button(p21); /**< Pi start button input on mBed pin 32 */ |
lsaristo | 29:459ff10d2a07 | 14 | int draw; /**< Boolean for drawing/moving. */ |
lsaristo | 9:3a0433c391cb | 15 | |
lsaristo | 29:459ff10d2a07 | 16 | /* 4 mBed onboard LEDs */ |
lsaristo | 29:459ff10d2a07 | 17 | DigitalOut oled_1(LED1), oled_2(LED2), oled_3(LED3), oled_4(LED4); |
lsaristo | 7:6e5cc24e1ce7 | 18 | |
lsaristo | 29:459ff10d2a07 | 19 | /* 8 LEDs driven from mBed digital outs. */ |
lsaristo | 29:459ff10d2a07 | 20 | DigitalOut pin13(p13), pin14(p14), pin15(p15), pin16(p16), |
lsaristo | 29:459ff10d2a07 | 21 | pin17(p17), pin18(p18), pin19(p19), pin20(p20); |
lsaristo | 9:3a0433c391cb | 22 | |
alecguertin | 15:14d4e7021125 | 23 | /* Local File System */ |
alecguertin | 15:14d4e7021125 | 24 | LocalFileSystem local("local"); |
lsaristo | 9:3a0433c391cb | 25 | |
alecguertin | 41:1b6d8664d7a7 | 26 | enum RobotState { |
alecguertin | 41:1b6d8664d7a7 | 27 | ERROR, |
alecguertin | 41:1b6d8664d7a7 | 28 | PARSE, |
alecguertin | 41:1b6d8664d7a7 | 29 | TURN, |
alecguertin | 41:1b6d8664d7a7 | 30 | MOVE, |
alecguertin | 41:1b6d8664d7a7 | 31 | DRAW, |
alecguertin | 41:1b6d8664d7a7 | 32 | }; |
alecguertin | 41:1b6d8664d7a7 | 33 | |
lsaristo | 29:459ff10d2a07 | 34 | /** @brief Entry point. Main loop. */ |
John Wilkey |
5:01882c3de2dc | 35 | int main() |
John Wilkey |
5:01882c3de2dc | 36 | { |
alecguertin | 39:cc8691700d2a | 37 | Timer caltimer; |
alecguertin | 39:cc8691700d2a | 38 | FILE *ps_file; |
alecguertin | 41:1b6d8664d7a7 | 39 | RobotState state, last_state; |
alecguertin | 39:cc8691700d2a | 40 | double dist; |
alecguertin | 39:cc8691700d2a | 41 | float delta_a, angle, cal_time; |
alecguertin | 39:cc8691700d2a | 42 | size_t bytes_read; |
alecguertin | 41:1b6d8664d7a7 | 43 | int corner, err, init, not_done, x, y, last_x, last_y, dim_x, dim_y, offset; |
alecguertin | 39:cc8691700d2a | 44 | char instbuf[INST_BUF_SIZE]; |
alecguertin | 39:cc8691700d2a | 45 | char *cur, *next; |
alecguertin | 39:cc8691700d2a | 46 | angle = 0; |
alecguertin | 41:1b6d8664d7a7 | 47 | x = 0; |
alecguertin | 41:1b6d8664d7a7 | 48 | y = 0; |
alecguertin | 39:cc8691700d2a | 49 | last_x = 0; |
alecguertin | 39:cc8691700d2a | 50 | last_y = 0; |
alecguertin | 39:cc8691700d2a | 51 | bytes_read = 0; |
alecguertin | 39:cc8691700d2a | 52 | offset = 0; |
alecguertin | 41:1b6d8664d7a7 | 53 | init = 0; |
alecguertin | 41:1b6d8664d7a7 | 54 | not_done = 1; |
alecguertin | 41:1b6d8664d7a7 | 55 | caltimer = Timer(); |
alecguertin | 26:a74edc4c6acb | 56 | |
alecguertin | 39:cc8691700d2a | 57 | /* First calibrate robot and figure out space dimensions. */ |
alecguertin | 39:cc8691700d2a | 58 | robot_printf(0, "%f mV", pi.battery()); |
alecguertin | 39:cc8691700d2a | 59 | |
alecguertin | 39:cc8691700d2a | 60 | /* Calibrate robot reflective sensors to line. */ |
alecguertin | 39:cc8691700d2a | 61 | pi.sensor_auto_calibrate(); |
alecguertin | 39:cc8691700d2a | 62 | timerWait(0.2); |
alecguertin | 39:cc8691700d2a | 63 | |
alecguertin | 39:cc8691700d2a | 64 | /* Find left bottom right corner of frame. */ |
alecguertin | 39:cc8691700d2a | 65 | corner = find_corner(1); |
alecguertin | 39:cc8691700d2a | 66 | if (!corner) { |
alecguertin | 39:cc8691700d2a | 67 | robot_printf(0, "no left\n"); |
alecguertin | 39:cc8691700d2a | 68 | while (1); |
lsaristo | 21:0c80a5d89ea3 | 69 | } |
alecguertin | 39:cc8691700d2a | 70 | left(HALF_TURN); |
alecguertin | 39:cc8691700d2a | 71 | |
alecguertin | 39:cc8691700d2a | 72 | /* |
alecguertin | 39:cc8691700d2a | 73 | * Find bottom left corner of frame and |
alecguertin | 39:cc8691700d2a | 74 | * and measure length of frame side. |
alecguertin | 39:cc8691700d2a | 75 | */ |
alecguertin | 22:46b9d9b2e35c | 76 | caltimer.start(); |
alecguertin | 39:cc8691700d2a | 77 | corner = find_corner(0); |
alecguertin | 39:cc8691700d2a | 78 | if (!corner) { |
alecguertin | 39:cc8691700d2a | 79 | robot_printf(0, "no right\n"); |
alecguertin | 39:cc8691700d2a | 80 | while (1); |
alecguertin | 39:cc8691700d2a | 81 | } |
alecguertin | 22:46b9d9b2e35c | 82 | caltimer.stop(); |
alecguertin | 39:cc8691700d2a | 83 | cal_time = caltimer.read_ms(); |
alecguertin | 39:cc8691700d2a | 84 | |
alecguertin | 39:cc8691700d2a | 85 | /* Back up into corner (coordinate x=0,y=0). */ |
alecguertin | 39:cc8691700d2a | 86 | right(HALF_TURN); |
lsaristo | 32:8b589710632b | 87 | timerWait(0.2); |
lsaristo | 34:3066686d5152 | 88 | backward(400); |
alecguertin | 39:cc8691700d2a | 89 | |
alecguertin | 39:cc8691700d2a | 90 | /* Open instruction file. */ |
alecguertin | 15:14d4e7021125 | 91 | ps_file = fopen("/local/test.ps", "r"); |
chstrchu | 20:76718145b403 | 92 | if (ps_file == NULL) { |
chstrchu | 20:76718145b403 | 93 | return 1; |
chstrchu | 20:76718145b403 | 94 | } |
alecguertin | 25:2c7717684d09 | 95 | |
alecguertin | 39:cc8691700d2a | 96 | /* Read beginning of file into buffer. */ |
alecguertin | 39:cc8691700d2a | 97 | memset(instbuf, 0, INST_BUF_SIZE); |
alecguertin | 39:cc8691700d2a | 98 | bytes_read = fread(instbuf, sizeof(char), INST_BUF_SIZE-1, ps_file); |
alecguertin | 22:46b9d9b2e35c | 99 | if (bytes_read == 0) { |
alecguertin | 22:46b9d9b2e35c | 100 | return 1; |
alecguertin | 22:46b9d9b2e35c | 101 | } |
alecguertin | 39:cc8691700d2a | 102 | |
alecguertin | 39:cc8691700d2a | 103 | /* Read first line of file - the dimensions of the frame. */ |
alecguertin | 19:47759cf4f9b9 | 104 | err = sscanf(instbuf, "%d/%d", &dim_x, &dim_y); |
alecguertin | 19:47759cf4f9b9 | 105 | if (err != 2) { |
alecguertin | 39:cc8691700d2a | 106 | robot_printf(0, "sscanf1"); |
lsaristo | 34:3066686d5152 | 107 | while(1); |
alecguertin | 19:47759cf4f9b9 | 108 | } |
alecguertin | 19:47759cf4f9b9 | 109 | cur = strchr(instbuf, '\n'); |
chstrchu | 20:76718145b403 | 110 | cur++; |
alecguertin | 39:cc8691700d2a | 111 | offset = instbuf+INST_BUF_SIZE-cur-1; |
chstrchu | 20:76718145b403 | 112 | memcpy(instbuf, cur, offset); |
alecguertin | 39:cc8691700d2a | 113 | /* |
alecguertin | 39:cc8691700d2a | 114 | * File open and buffer setup. Begin reading instructions and |
alecguertin | 39:cc8691700d2a | 115 | * moving robot. Refill buffer after each instruction. |
alecguertin | 39:cc8691700d2a | 116 | */ |
alecguertin | 41:1b6d8664d7a7 | 117 | state = PARSE; |
alecguertin | 41:1b6d8664d7a7 | 118 | last_state = PARSE; |
alecguertin | 41:1b6d8664d7a7 | 119 | while (not_done) { |
alecguertin | 41:1b6d8664d7a7 | 120 | switch (state) { |
alecguertin | 41:1b6d8664d7a7 | 121 | case PARSE: |
alecguertin | 41:1b6d8664d7a7 | 122 | if (!init) { |
alecguertin | 41:1b6d8664d7a7 | 123 | next = strchr(cur, '\n'); |
alecguertin | 41:1b6d8664d7a7 | 124 | if (next == NULL) { |
alecguertin | 41:1b6d8664d7a7 | 125 | last_state = state; |
alecguertin | 41:1b6d8664d7a7 | 126 | state = ERROR; |
alecguertin | 41:1b6d8664d7a7 | 127 | break; |
alecguertin | 41:1b6d8664d7a7 | 128 | } |
alecguertin | 41:1b6d8664d7a7 | 129 | cur = next+1; |
alecguertin | 41:1b6d8664d7a7 | 130 | offset = instbuf+INST_BUF_SIZE-cur-1; |
alecguertin | 41:1b6d8664d7a7 | 131 | memcpy(instbuf, cur, offset); |
alecguertin | 41:1b6d8664d7a7 | 132 | init = 1; |
alecguertin | 41:1b6d8664d7a7 | 133 | } |
alecguertin | 41:1b6d8664d7a7 | 134 | |
alecguertin | 41:1b6d8664d7a7 | 135 | memset(instbuf+offset, 0, INST_BUF_SIZE-offset); |
alecguertin | 41:1b6d8664d7a7 | 136 | bytes_read = fread(instbuf+offset, sizeof(char), INST_BUF_SIZE-1-offset, ps_file); |
alecguertin | 41:1b6d8664d7a7 | 137 | if (instbuf[0] == '\0') { |
alecguertin | 41:1b6d8664d7a7 | 138 | pen_up(); |
alecguertin | 41:1b6d8664d7a7 | 139 | not_done = 0; |
alecguertin | 41:1b6d8664d7a7 | 140 | break; |
alecguertin | 41:1b6d8664d7a7 | 141 | } |
alecguertin | 41:1b6d8664d7a7 | 142 | |
alecguertin | 41:1b6d8664d7a7 | 143 | last_x = x; |
alecguertin | 41:1b6d8664d7a7 | 144 | last_y = y; |
alecguertin | 41:1b6d8664d7a7 | 145 | cur = instbuf; |
alecguertin | 41:1b6d8664d7a7 | 146 | err = retrieve_inst(instbuf, &x, &y, &draw); |
alecguertin | 41:1b6d8664d7a7 | 147 | if (err == 0) { |
alecguertin | 41:1b6d8664d7a7 | 148 | last_state = state; |
alecguertin | 41:1b6d8664d7a7 | 149 | state = ERROR; |
alecguertin | 41:1b6d8664d7a7 | 150 | break; |
alecguertin | 41:1b6d8664d7a7 | 151 | } else { |
alecguertin | 41:1b6d8664d7a7 | 152 | last_state = state; |
alecguertin | 41:1b6d8664d7a7 | 153 | state = TURN; |
alecguertin | 41:1b6d8664d7a7 | 154 | break; |
alecguertin | 41:1b6d8664d7a7 | 155 | } |
alecguertin | 41:1b6d8664d7a7 | 156 | |
alecguertin | 41:1b6d8664d7a7 | 157 | case TURN: |
alecguertin | 41:1b6d8664d7a7 | 158 | delta_a = compute_turn_angle(last_x, last_y, x, y, angle); |
alecguertin | 41:1b6d8664d7a7 | 159 | angle += delta_a; |
alecguertin | 41:1b6d8664d7a7 | 160 | if (angle >= FULL_TURN) { |
alecguertin | 41:1b6d8664d7a7 | 161 | angle -= FULL_TURN; |
alecguertin | 41:1b6d8664d7a7 | 162 | } |
alecguertin | 41:1b6d8664d7a7 | 163 | left(delta_a); |
alecguertin | 41:1b6d8664d7a7 | 164 | if (draw) { |
alecguertin | 41:1b6d8664d7a7 | 165 | last_state = state; |
alecguertin | 41:1b6d8664d7a7 | 166 | state = DRAW; |
alecguertin | 41:1b6d8664d7a7 | 167 | break; |
alecguertin | 41:1b6d8664d7a7 | 168 | } else { |
alecguertin | 41:1b6d8664d7a7 | 169 | last_state = state; |
alecguertin | 41:1b6d8664d7a7 | 170 | state = MOVE; |
alecguertin | 41:1b6d8664d7a7 | 171 | break; |
alecguertin | 41:1b6d8664d7a7 | 172 | } |
alecguertin | 41:1b6d8664d7a7 | 173 | case DRAW: |
alecguertin | 41:1b6d8664d7a7 | 174 | dist = fabs(distance(last_x, last_y, x, y)); |
alecguertin | 41:1b6d8664d7a7 | 175 | dist = CAL_FACTOR*cal_time*(dist/(double)dim_x); |
alecguertin | 41:1b6d8664d7a7 | 176 | pen_down(); |
alecguertin | 41:1b6d8664d7a7 | 177 | forward(dist); |
alecguertin | 41:1b6d8664d7a7 | 178 | pen_up(); |
alecguertin | 41:1b6d8664d7a7 | 179 | last_state = state; |
alecguertin | 41:1b6d8664d7a7 | 180 | state = PARSE; |
alecguertin | 41:1b6d8664d7a7 | 181 | break; |
alecguertin | 41:1b6d8664d7a7 | 182 | case MOVE: |
alecguertin | 41:1b6d8664d7a7 | 183 | dist = fabs(distance(last_x, last_y, x, y)); |
alecguertin | 41:1b6d8664d7a7 | 184 | dist = CAL_FACTOR*cal_time*(dist/(double)dim_x); |
alecguertin | 41:1b6d8664d7a7 | 185 | forward(dist); |
alecguertin | 41:1b6d8664d7a7 | 186 | last_state = state; |
alecguertin | 41:1b6d8664d7a7 | 187 | state = PARSE; |
alecguertin | 41:1b6d8664d7a7 | 188 | break; |
alecguertin | 41:1b6d8664d7a7 | 189 | case ERROR: |
alecguertin | 41:1b6d8664d7a7 | 190 | robot_printf(0, "E:%d", last_state); |
alecguertin | 41:1b6d8664d7a7 | 191 | while(1); |
alecguertin | 41:1b6d8664d7a7 | 192 | } |
alecguertin | 41:1b6d8664d7a7 | 193 | } |
alecguertin | 41:1b6d8664d7a7 | 194 | /* |
alecguertin | 17:c72c092fcdf7 | 195 | while (1) { |
alecguertin | 39:cc8691700d2a | 196 | memset(instbuf+offset, 0, INST_BUF_SIZE-offset); |
alecguertin | 39:cc8691700d2a | 197 | bytes_read = fread(instbuf+offset, sizeof(char), INST_BUF_SIZE-1-offset, ps_file); |
alecguertin | 27:44a4a0abc8ee | 198 | if (instbuf[0] == '\0') { |
alecguertin | 39:cc8691700d2a | 199 | robot_printf(0, "%s", instbuf+1); |
alecguertin | 40:0199bad6c979 | 200 | pen_up(); |
alecguertin | 28:9976a94efa83 | 201 | while(1); |
alecguertin | 27:44a4a0abc8ee | 202 | } |
alecguertin | 28:9976a94efa83 | 203 | |
alecguertin | 17:c72c092fcdf7 | 204 | cur = instbuf; |
alecguertin | 22:46b9d9b2e35c | 205 | err = retrieve_inst(instbuf, &x, &y, &draw); |
alecguertin | 22:46b9d9b2e35c | 206 | if (err == 0) { |
alecguertin | 39:cc8691700d2a | 207 | robot_printf(0, "noinst"); |
alecguertin | 22:46b9d9b2e35c | 208 | return 1; |
alecguertin | 22:46b9d9b2e35c | 209 | } |
lsaristo | 32:8b589710632b | 210 | |
alecguertin | 39:cc8691700d2a | 211 | if (0 && !draw && init) { |
alecguertin | 39:cc8691700d2a | 212 | float pos; |
alecguertin | 39:cc8691700d2a | 213 | right(angle+QUARTER_TURN); |
alecguertin | 39:cc8691700d2a | 214 | find_line(); |
alecguertin | 39:cc8691700d2a | 215 | forward(100); |
alecguertin | 39:cc8691700d2a | 216 | pi.left(CAL_SPEED); |
alecguertin | 39:cc8691700d2a | 217 | pos = pi.line_position(); |
alecguertin | 39:cc8691700d2a | 218 | while (pos > CORRECTION_THRESHOLD || pos < 0) { |
alecguertin | 39:cc8691700d2a | 219 | pos = pi.line_position(); |
lsaristo | 34:3066686d5152 | 220 | } |
alecguertin | 39:cc8691700d2a | 221 | pi.stop(); |
alecguertin | 39:cc8691700d2a | 222 | find_corner(1); |
alecguertin | 39:cc8691700d2a | 223 | left(180); |
alecguertin | 39:cc8691700d2a | 224 | find_corner(0); |
alecguertin | 39:cc8691700d2a | 225 | right(HALF_TURN); |
alecguertin | 39:cc8691700d2a | 226 | timerWait(0.5); |
alecguertin | 39:cc8691700d2a | 227 | backward(400); |
alecguertin | 39:cc8691700d2a | 228 | angle = 0; |
alecguertin | 39:cc8691700d2a | 229 | last_x = 0; |
alecguertin | 39:cc8691700d2a | 230 | last_y = 0; |
alecguertin | 23:e4616259a7f0 | 231 | } |
lsaristo | 32:8b589710632b | 232 | |
alecguertin | 39:cc8691700d2a | 233 | init = 1; |
alecguertin | 39:cc8691700d2a | 234 | delta_a = compute_turn_angle(last_x, last_y, x, y, angle); |
alecguertin | 25:2c7717684d09 | 235 | angle += delta_a; |
alecguertin | 39:cc8691700d2a | 236 | if (angle >= FULL_TURN) { |
alecguertin | 39:cc8691700d2a | 237 | angle -= FULL_TURN; |
alecguertin | 28:9976a94efa83 | 238 | } |
alecguertin | 39:cc8691700d2a | 239 | robot_printf(0, "a:%f", delta_a); |
alecguertin | 25:2c7717684d09 | 240 | left(delta_a); |
alecguertin | 19:47759cf4f9b9 | 241 | |
alecguertin | 40:0199bad6c979 | 242 | dist = fabs(distance(last_x, last_y, x, y)); |
alecguertin | 40:0199bad6c979 | 243 | robot_printf(0, "d:%f", CAL_FACTOR*cal_time*(dist/(double)dim_x)); |
alecguertin | 40:0199bad6c979 | 244 | |
alecguertin | 22:46b9d9b2e35c | 245 | if (draw) { |
alecguertin | 40:0199bad6c979 | 246 | pen_down(); |
alecguertin | 22:46b9d9b2e35c | 247 | } else { |
alecguertin | 40:0199bad6c979 | 248 | pen_up(); |
alecguertin | 22:46b9d9b2e35c | 249 | } |
alecguertin | 22:46b9d9b2e35c | 250 | |
alecguertin | 39:cc8691700d2a | 251 | forward(CAL_FACTOR*cal_time*(dist/(double)dim_x)); |
alecguertin | 40:0199bad6c979 | 252 | pen_up(); |
alecguertin | 22:46b9d9b2e35c | 253 | last_x = x; |
alecguertin | 22:46b9d9b2e35c | 254 | last_y = y; |
alecguertin | 39:cc8691700d2a | 255 | |
alecguertin | 22:46b9d9b2e35c | 256 | next = strchr(cur, '\n'); |
alecguertin | 22:46b9d9b2e35c | 257 | if (next == NULL) { |
alecguertin | 39:cc8691700d2a | 258 | robot_printf(0, "nonext"); |
alecguertin | 22:46b9d9b2e35c | 259 | break; |
alecguertin | 17:c72c092fcdf7 | 260 | } |
alecguertin | 22:46b9d9b2e35c | 261 | cur = next+1; |
alecguertin | 39:cc8691700d2a | 262 | offset = instbuf+INST_BUF_SIZE-cur-1; |
alecguertin | 17:c72c092fcdf7 | 263 | memcpy(instbuf, cur, offset); |
alecguertin | 15:14d4e7021125 | 264 | } |
alecguertin | 41:1b6d8664d7a7 | 265 | */ |
alecguertin | 39:cc8691700d2a | 266 | robot_printf(0, "Done"); |
lsaristo | 34:3066686d5152 | 267 | pi.stop(); |
alecguertin | 40:0199bad6c979 | 268 | pen_up(); |
alecguertin | 27:44a4a0abc8ee | 269 | while (1); |
John Wilkey |
5:01882c3de2dc | 270 | } |
lsaristo | 8:12d780f7443e | 271 | |
alecguertin | 39:cc8691700d2a | 272 | float compute_turn_angle(int last_x, int last_y, int x, int y, float angle) |
alecguertin | 39:cc8691700d2a | 273 | { |
alecguertin | 39:cc8691700d2a | 274 | int delta_x, delta_y; |
alecguertin | 39:cc8691700d2a | 275 | float theta, delta_a; |
alecguertin | 39:cc8691700d2a | 276 | delta_x = x-last_x; |
alecguertin | 39:cc8691700d2a | 277 | delta_y = y-last_y; |
alecguertin | 39:cc8691700d2a | 278 | if (delta_y == 0) { |
alecguertin | 39:cc8691700d2a | 279 | if (delta_x < 0) { |
alecguertin | 39:cc8691700d2a | 280 | theta = HALF_TURN; |
alecguertin | 39:cc8691700d2a | 281 | } else { |
alecguertin | 39:cc8691700d2a | 282 | theta = 0; |
alecguertin | 39:cc8691700d2a | 283 | } |
alecguertin | 39:cc8691700d2a | 284 | } else if (delta_x == 0) { |
alecguertin | 39:cc8691700d2a | 285 | if (delta_y < 0) { |
alecguertin | 39:cc8691700d2a | 286 | theta = -QUARTER_TURN; |
alecguertin | 39:cc8691700d2a | 287 | } else { |
alecguertin | 39:cc8691700d2a | 288 | theta = QUARTER_TURN; |
alecguertin | 39:cc8691700d2a | 289 | } |
alecguertin | 39:cc8691700d2a | 290 | } else { |
alecguertin | 39:cc8691700d2a | 291 | theta = atan(((double) delta_y)/((double) delta_x)); |
alecguertin | 39:cc8691700d2a | 292 | theta *= RAD_TO_DEG; |
alecguertin | 39:cc8691700d2a | 293 | } |
alecguertin | 39:cc8691700d2a | 294 | if (delta_x < 0 && delta_y > 0) { |
alecguertin | 39:cc8691700d2a | 295 | theta += HALF_TURN; |
alecguertin | 39:cc8691700d2a | 296 | } |
alecguertin | 39:cc8691700d2a | 297 | delta_a = theta-angle; |
alecguertin | 39:cc8691700d2a | 298 | |
alecguertin | 39:cc8691700d2a | 299 | if (delta_x < 0 && delta_y < 0) { |
alecguertin | 39:cc8691700d2a | 300 | delta_a += HALF_TURN; |
alecguertin | 39:cc8691700d2a | 301 | } |
alecguertin | 39:cc8691700d2a | 302 | |
alecguertin | 39:cc8691700d2a | 303 | if (delta_a > HALF_TURN) { |
alecguertin | 39:cc8691700d2a | 304 | delta_a -= FULL_TURN; |
alecguertin | 39:cc8691700d2a | 305 | } else if (delta_a < -HALF_TURN) { |
alecguertin | 39:cc8691700d2a | 306 | delta_a = FULL_TURN + delta_a; |
alecguertin | 39:cc8691700d2a | 307 | } |
alecguertin | 39:cc8691700d2a | 308 | return delta_a; |
alecguertin | 39:cc8691700d2a | 309 | } |
alecguertin | 39:cc8691700d2a | 310 | |
alecguertin | 39:cc8691700d2a | 311 | void robot_printf(int line, const char *format, ...) |
alecguertin | 39:cc8691700d2a | 312 | { |
alecguertin | 39:cc8691700d2a | 313 | char buf[18]; |
alecguertin | 39:cc8691700d2a | 314 | va_list args; |
alecguertin | 39:cc8691700d2a | 315 | pi.cls(); |
alecguertin | 39:cc8691700d2a | 316 | pi.locate(0,line); |
alecguertin | 39:cc8691700d2a | 317 | va_start(args, format); |
alecguertin | 39:cc8691700d2a | 318 | vsprintf(buf, format, args); |
alecguertin | 39:cc8691700d2a | 319 | pi.printf("%s", buf); |
alecguertin | 39:cc8691700d2a | 320 | va_end(args); |
alecguertin | 39:cc8691700d2a | 321 | } |
alecguertin | 39:cc8691700d2a | 322 | |
alecguertin | 39:cc8691700d2a | 323 | float distance(int x1, int y1, int x2, int y2) |
alecguertin | 39:cc8691700d2a | 324 | { |
alecguertin | 39:cc8691700d2a | 325 | return sqrt(pow((double) (x2-x1),2) + pow((double) (y2-y1), 2)); |
alecguertin | 39:cc8691700d2a | 326 | } |
alecguertin | 39:cc8691700d2a | 327 | |
alecguertin | 39:cc8691700d2a | 328 | void find_line() |
alecguertin | 39:cc8691700d2a | 329 | { |
alecguertin | 39:cc8691700d2a | 330 | float pos, pos1; |
alecguertin | 39:cc8691700d2a | 331 | pi.forward(DRIVE_SPEED); |
alecguertin | 39:cc8691700d2a | 332 | pos1 = pi.line_position(); |
alecguertin | 39:cc8691700d2a | 333 | pos = pos1; |
alecguertin | 39:cc8691700d2a | 334 | while (fabs(pos1-pos) < 0.1 || pos == -1) { |
alecguertin | 39:cc8691700d2a | 335 | pos = pi.line_position(); |
alecguertin | 39:cc8691700d2a | 336 | robot_printf(0, "%f\n", pos); |
alecguertin | 39:cc8691700d2a | 337 | } |
alecguertin | 39:cc8691700d2a | 338 | robot_printf(0, "f:%f\n", pos); |
alecguertin | 39:cc8691700d2a | 339 | pi.stop(); |
alecguertin | 39:cc8691700d2a | 340 | } |
alecguertin | 39:cc8691700d2a | 341 | |
alecguertin | 39:cc8691700d2a | 342 | int find_corner(int left) |
alecguertin | 39:cc8691700d2a | 343 | { |
alecguertin | 39:cc8691700d2a | 344 | float pos; |
alecguertin | 39:cc8691700d2a | 345 | float threshold = CORNER_THRESHOLD; |
alecguertin | 39:cc8691700d2a | 346 | if (left) { |
alecguertin | 39:cc8691700d2a | 347 | threshold *= -1; |
alecguertin | 39:cc8691700d2a | 348 | } |
alecguertin | 39:cc8691700d2a | 349 | do { |
alecguertin | 39:cc8691700d2a | 350 | pos = pi.line_position(); |
alecguertin | 39:cc8691700d2a | 351 | if(pos > CORRECTION_THRESHOLD) { |
alecguertin | 39:cc8691700d2a | 352 | pi.right_motor(CAL_SPEED); |
alecguertin | 39:cc8691700d2a | 353 | pi.left_motor(CAL_SPEED-CORRECTION_SPEED); |
alecguertin | 39:cc8691700d2a | 354 | } else if(pos < -CORRECTION_THRESHOLD) { |
alecguertin | 39:cc8691700d2a | 355 | pi.left_motor(CAL_SPEED); |
alecguertin | 39:cc8691700d2a | 356 | pi.right_motor(CAL_SPEED-CORRECTION_SPEED); |
alecguertin | 39:cc8691700d2a | 357 | } else { |
alecguertin | 39:cc8691700d2a | 358 | pi.right_motor(CAL_SPEED); |
alecguertin | 39:cc8691700d2a | 359 | pi.left_motor(CAL_SPEED); |
alecguertin | 39:cc8691700d2a | 360 | } |
alecguertin | 39:cc8691700d2a | 361 | robot_printf(0, "Pos: %f", pos); |
alecguertin | 39:cc8691700d2a | 362 | } while(pos != -1 && ((left && pos > threshold) || (!left && pos < threshold))); |
alecguertin | 39:cc8691700d2a | 363 | pi.stop(); |
alecguertin | 39:cc8691700d2a | 364 | if(pos == -1) { |
alecguertin | 39:cc8691700d2a | 365 | oled_1 = 1; |
alecguertin | 39:cc8691700d2a | 366 | return 0; |
alecguertin | 39:cc8691700d2a | 367 | } |
alecguertin | 39:cc8691700d2a | 368 | return 1; |
alecguertin | 39:cc8691700d2a | 369 | } |
alecguertin | 39:cc8691700d2a | 370 | |
alecguertin | 17:c72c092fcdf7 | 371 | int retrieve_inst(char *buf, int *x, int *y, int *draw) |
alecguertin | 17:c72c092fcdf7 | 372 | { |
alecguertin | 17:c72c092fcdf7 | 373 | int matches; |
alecguertin | 17:c72c092fcdf7 | 374 | char *srch; |
alecguertin | 17:c72c092fcdf7 | 375 | matches = sscanf(buf, "%d %d", x, y); |
alecguertin | 17:c72c092fcdf7 | 376 | if (matches != 2) { |
alecguertin | 39:cc8691700d2a | 377 | robot_printf(0, "nomatch"); |
alecguertin | 17:c72c092fcdf7 | 378 | return 0; |
alecguertin | 17:c72c092fcdf7 | 379 | } |
alecguertin | 17:c72c092fcdf7 | 380 | srch = strchr(buf, ' '); |
alecguertin | 17:c72c092fcdf7 | 381 | srch++; |
chstrchu | 20:76718145b403 | 382 | srch = strchr(srch, ' '); |
alecguertin | 17:c72c092fcdf7 | 383 | srch++; |
alecguertin | 17:c72c092fcdf7 | 384 | if (srch[0] == 'm') { |
alecguertin | 17:c72c092fcdf7 | 385 | *draw = 0; |
chstrchu | 20:76718145b403 | 386 | } else if (srch[0] == 'l') { |
alecguertin | 17:c72c092fcdf7 | 387 | *draw = 1; |
alecguertin | 17:c72c092fcdf7 | 388 | } else { |
alecguertin | 39:cc8691700d2a | 389 | robot_printf(0, "%.8s", srch); |
alecguertin | 17:c72c092fcdf7 | 390 | return 0; |
alecguertin | 17:c72c092fcdf7 | 391 | } |
alecguertin | 17:c72c092fcdf7 | 392 | return 1; |
alecguertin | 17:c72c092fcdf7 | 393 | } |
alecguertin | 17:c72c092fcdf7 | 394 | |
lsaristo | 29:459ff10d2a07 | 395 | void forward(int amt) |
lsaristo | 8:12d780f7443e | 396 | { |
lsaristo | 12:1aa6b8a74136 | 397 | Timer t; |
lsaristo | 34:3066686d5152 | 398 | float ms = 0; |
lsaristo | 34:3066686d5152 | 399 | float last_ms = 0; |
alecguertin | 39:cc8691700d2a | 400 | robot_printf(0, "Fwd %d", amt); |
lsaristo | 30:3211e2962441 | 401 | t.start(); |
lsaristo | 34:3066686d5152 | 402 | pi.left_motor(DRIVE_SPEED+.002); |
lsaristo | 21:0c80a5d89ea3 | 403 | pi.right_motor(DRIVE_SPEED); |
lsaristo | 34:3066686d5152 | 404 | ms = t.read_ms(); |
lsaristo | 34:3066686d5152 | 405 | while(ms < amt) { |
alecguertin | 37:1d51cf101b03 | 406 | if (ms-last_ms > 500) { |
lsaristo | 34:3066686d5152 | 407 | t.stop(); |
alecguertin | 37:1d51cf101b03 | 408 | right(1.5); |
lsaristo | 34:3066686d5152 | 409 | last_ms = ms; |
lsaristo | 34:3066686d5152 | 410 | t.start(); |
lsaristo | 34:3066686d5152 | 411 | pi.left_motor(DRIVE_SPEED+.002); |
lsaristo | 34:3066686d5152 | 412 | pi.right_motor(DRIVE_SPEED); |
lsaristo | 34:3066686d5152 | 413 | } |
lsaristo | 34:3066686d5152 | 414 | ms = t.read_ms(); |
lsaristo | 34:3066686d5152 | 415 | } |
lsaristo | 12:1aa6b8a74136 | 416 | pi.stop(); |
lsaristo | 8:12d780f7443e | 417 | } |
lsaristo | 8:12d780f7443e | 418 | |
lsaristo | 29:459ff10d2a07 | 419 | void backward(int amt) |
lsaristo | 8:12d780f7443e | 420 | { |
lsaristo | 12:1aa6b8a74136 | 421 | Timer t; |
lsaristo | 12:1aa6b8a74136 | 422 | t.start(); |
alecguertin | 22:46b9d9b2e35c | 423 | pi.backward(.5*DRIVE_SPEED); |
chstrchu | 20:76718145b403 | 424 | while(t.read_ms() < amt); |
lsaristo | 12:1aa6b8a74136 | 425 | pi.stop(); |
lsaristo | 8:12d780f7443e | 426 | } |
lsaristo | 8:12d780f7443e | 427 | |
alecguertin | 39:cc8691700d2a | 428 | void left(float deg) |
alecguertin | 39:cc8691700d2a | 429 | { |
alecguertin | 39:cc8691700d2a | 430 | Timer t; |
alecguertin | 39:cc8691700d2a | 431 | deg += DEGREE_CORRECTION; |
alecguertin | 39:cc8691700d2a | 432 | if(deg < 0) { |
alecguertin | 39:cc8691700d2a | 433 | return right(-1*deg); |
alecguertin | 39:cc8691700d2a | 434 | } |
alecguertin | 39:cc8691700d2a | 435 | float amt = (((float)deg)/FULL_TURN)*TIME_FACT*1000; |
alecguertin | 39:cc8691700d2a | 436 | pi.left(TURN_SPEED); |
alecguertin | 39:cc8691700d2a | 437 | t.start(); |
alecguertin | 39:cc8691700d2a | 438 | while(t.read_us() < amt); |
alecguertin | 39:cc8691700d2a | 439 | pi.stop(); |
alecguertin | 39:cc8691700d2a | 440 | } |
alecguertin | 39:cc8691700d2a | 441 | |
lsaristo | 29:459ff10d2a07 | 442 | void right(float deg) |
lsaristo | 8:12d780f7443e | 443 | { |
alecguertin | 39:cc8691700d2a | 444 | Timer t; |
alecguertin | 39:cc8691700d2a | 445 | deg += DEGREE_CORRECTION; |
lsaristo | 24:b797563776fc | 446 | if(deg < 0) { |
lsaristo | 24:b797563776fc | 447 | return left(-1*deg); |
lsaristo | 24:b797563776fc | 448 | } |
alecguertin | 39:cc8691700d2a | 449 | float amt = ((((float)deg)/FULL_TURN)*TIME_FACT*1000); |
lsaristo | 12:1aa6b8a74136 | 450 | t.start(); |
lsaristo | 30:3211e2962441 | 451 | pi.right(TURN_SPEED); |
alecguertin | 37:1d51cf101b03 | 452 | while(t.read_us() < amt); |
lsaristo | 12:1aa6b8a74136 | 453 | pi.stop(); |
lsaristo | 8:12d780f7443e | 454 | } |
lsaristo | 8:12d780f7443e | 455 | |
lsaristo | 21:0c80a5d89ea3 | 456 | void timerWait(float seconds) |
lsaristo | 21:0c80a5d89ea3 | 457 | { |
lsaristo | 21:0c80a5d89ea3 | 458 | Timer t; |
lsaristo | 21:0c80a5d89ea3 | 459 | t.start(); |
lsaristo | 21:0c80a5d89ea3 | 460 | while(t.read_us() < 1000000*seconds); |
alecguertin | 40:0199bad6c979 | 461 | } |
alecguertin | 40:0199bad6c979 | 462 | |
alecguertin | 40:0199bad6c979 | 463 | void pen_down() |
alecguertin | 40:0199bad6c979 | 464 | { |
alecguertin | 40:0199bad6c979 | 465 | oled_3 = 1; |
alecguertin | 40:0199bad6c979 | 466 | } |
alecguertin | 40:0199bad6c979 | 467 | |
alecguertin | 40:0199bad6c979 | 468 | void pen_up() |
alecguertin | 40:0199bad6c979 | 469 | { |
alecguertin | 40:0199bad6c979 | 470 | oled_3 = 0; |
alecguertin | 40:0199bad6c979 | 471 | } |