Basic motor code

Dependencies:   Motordriver mbed HALLFX_ENCODER Servo

Committer:
jsterling30
Date:
Wed May 02 14:25:43 2018 +0000
Revision:
13:294c06406d06
Parent:
12:b107ba24faa4
demo changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
amohile3 0:191d0be60c80 1 #include "mbed.h"
amohile3 0:191d0be60c80 2 #include "motordriver.h"
jsterling30 2:8c27831ce9a2 3 #include "HALLFX_ENCODER.h"
jsterling30 7:6ef9b0aab972 4 #include "Servo.h"
jsterling30 8:e90d9ce42b33 5 #include "letters.h"
jsterling30 2:8c27831ce9a2 6
jsterling30 8:e90d9ce42b33 7 #include <math.h>
jsterling30 7:6ef9b0aab972 8 #include <string.h>
jsterling30 6:235548599e79 9
jsterling30 10:9066603296bd 10 #define SPEED 0.33
jsterling30 3:c4b0460d8886 11 #define TICKSPERREV 390
jsterling30 7:6ef9b0aab972 12 #define DISTPERREV 8.25 // 8.25 inches per revolution
jsterling30 13:294c06406d06 13 #define TicksPerDeg 2.42
jsterling30 8:e90d9ce42b33 14
jsterling30 13:294c06406d06 15 #define PI 3.1415926535
jsterling30 8:e90d9ce42b33 16 #define abs(X) ((X < 0) ? -1 * X : X)
amohile3 0:191d0be60c80 17
jsterling30 7:6ef9b0aab972 18 Motor right(p23, p6, p5, 1); // pwm, fwd, rev
jsterling30 7:6ef9b0aab972 19 Motor left(p21, p7, p8, 1); // pwm, fwd, rev
amohile3 0:191d0be60c80 20
jsterling30 2:8c27831ce9a2 21 HALLFX_ENCODER leftEnc(p15);
jsterling30 2:8c27831ce9a2 22 HALLFX_ENCODER rightEnc(p16);
jsterling30 2:8c27831ce9a2 23
jsterling30 7:6ef9b0aab972 24 Serial blue(p13,p14);
jsterling30 7:6ef9b0aab972 25 Serial pc(USBTX, USBRX);
amohile3 5:7572f73a78f3 26 DigitalOut led1(LED1);
amohile3 5:7572f73a78f3 27 DigitalOut led4(LED4);
amohile3 5:7572f73a78f3 28
jsterling30 7:6ef9b0aab972 29 Servo pen(p24);
jsterling30 6:235548599e79 30
jsterling30 8:e90d9ce42b33 31 double penUpValue = 0.8;
jsterling30 8:e90d9ce42b33 32 double penDownValue = 1.0;
jsterling30 8:e90d9ce42b33 33
jsterling30 8:e90d9ce42b33 34 void penUp() {
jsterling30 8:e90d9ce42b33 35 pen = penUpValue;
jsterling30 8:e90d9ce42b33 36 wait(0.5);
jsterling30 8:e90d9ce42b33 37 }
jsterling30 8:e90d9ce42b33 38
jsterling30 8:e90d9ce42b33 39 void penDown() {
jsterling30 8:e90d9ce42b33 40 pen = penDownValue;
jsterling30 8:e90d9ce42b33 41 wait(0.5);
jsterling30 8:e90d9ce42b33 42 }
jsterling30 8:e90d9ce42b33 43
jsterling30 2:8c27831ce9a2 44 void stop() {
jsterling30 2:8c27831ce9a2 45 right.speed(0.0);
jsterling30 2:8c27831ce9a2 46 left.speed(0.0);
jsterling30 2:8c27831ce9a2 47 }
jsterling30 2:8c27831ce9a2 48
jsterling30 8:e90d9ce42b33 49 void forward(double distance) {
jsterling30 8:e90d9ce42b33 50 double numRevs = distance / DISTPERREV;
jsterling30 8:e90d9ce42b33 51 double numTicks = numRevs * TICKSPERREV;
jsterling30 2:8c27831ce9a2 52 leftEnc.reset();
jsterling30 2:8c27831ce9a2 53 rightEnc.reset();
jsterling30 13:294c06406d06 54 double leftSpeed = SPEED;
jsterling30 13:294c06406d06 55 double rightSpeed = SPEED;
jsterling30 13:294c06406d06 56
jsterling30 13:294c06406d06 57 int leftTicksPrev = 0;
jsterling30 13:294c06406d06 58 int rightTicksPrev = 0;
jsterling30 13:294c06406d06 59 double offset = 0.01;
jsterling30 13:294c06406d06 60
jsterling30 13:294c06406d06 61 while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {
jsterling30 13:294c06406d06 62
jsterling30 13:294c06406d06 63 int leftTicks = leftEnc.read();
jsterling30 13:294c06406d06 64 int rightTicks = rightEnc.read();
jsterling30 13:294c06406d06 65
jsterling30 13:294c06406d06 66 int leftDiff = leftTicks - leftTicksPrev;
jsterling30 13:294c06406d06 67 int rightDiff = rightTicks - rightTicksPrev;
jsterling30 13:294c06406d06 68
jsterling30 13:294c06406d06 69 leftTicksPrev = leftTicks;
jsterling30 13:294c06406d06 70 rightTicksPrev = rightTicks;
jsterling30 13:294c06406d06 71
jsterling30 13:294c06406d06 72 if (leftDiff > rightDiff) {
jsterling30 13:294c06406d06 73 leftSpeed -= offset;
jsterling30 13:294c06406d06 74 rightSpeed += offset;
jsterling30 13:294c06406d06 75 } else if (leftDiff < rightDiff) {
jsterling30 13:294c06406d06 76 leftSpeed += offset;
jsterling30 13:294c06406d06 77 rightSpeed -= offset;
jsterling30 13:294c06406d06 78 }
jsterling30 13:294c06406d06 79
jsterling30 13:294c06406d06 80 right.speed(rightSpeed);
jsterling30 13:294c06406d06 81 left.speed(leftSpeed);
jsterling30 13:294c06406d06 82
jsterling30 13:294c06406d06 83 wait(0.1);
jsterling30 13:294c06406d06 84
jsterling30 13:294c06406d06 85 }
jsterling30 3:c4b0460d8886 86 stop();
jsterling30 8:e90d9ce42b33 87 wait(0.5);
jsterling30 3:c4b0460d8886 88 }
jsterling30 3:c4b0460d8886 89
jsterling30 8:e90d9ce42b33 90 void reverse(double distance) {
jsterling30 8:e90d9ce42b33 91 double numRevs = distance / DISTPERREV;
jsterling30 8:e90d9ce42b33 92 double numTicks = numRevs * TICKSPERREV;
jsterling30 13:294c06406d06 93
jsterling30 8:e90d9ce42b33 94 leftEnc.reset();
jsterling30 8:e90d9ce42b33 95 rightEnc.reset();
jsterling30 13:294c06406d06 96
jsterling30 13:294c06406d06 97 double leftSpeed = -SPEED;
jsterling30 13:294c06406d06 98 double rightSpeed = -SPEED;
jsterling30 13:294c06406d06 99
jsterling30 13:294c06406d06 100 int leftTicksPrev = 0;
jsterling30 13:294c06406d06 101 int rightTicksPrev = 0;
jsterling30 13:294c06406d06 102 double offset = 0.01;
jsterling30 13:294c06406d06 103
jsterling30 13:294c06406d06 104 while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {
jsterling30 13:294c06406d06 105
jsterling30 13:294c06406d06 106 int leftTicks = leftEnc.read();
jsterling30 13:294c06406d06 107 int rightTicks = rightEnc.read();
jsterling30 13:294c06406d06 108
jsterling30 13:294c06406d06 109 int leftDiff = leftTicks - leftTicksPrev;
jsterling30 13:294c06406d06 110 int rightDiff = rightTicks - rightTicksPrev;
jsterling30 13:294c06406d06 111
jsterling30 13:294c06406d06 112 leftTicksPrev = leftTicks;
jsterling30 13:294c06406d06 113 rightTicksPrev = rightTicks;
jsterling30 13:294c06406d06 114
jsterling30 13:294c06406d06 115 if (leftDiff > rightDiff) {
jsterling30 13:294c06406d06 116 leftSpeed += offset;
jsterling30 13:294c06406d06 117 rightSpeed -= offset;
jsterling30 13:294c06406d06 118 } else if (leftDiff < rightDiff) {
jsterling30 13:294c06406d06 119 leftSpeed -= offset;
jsterling30 13:294c06406d06 120 rightSpeed += offset;
jsterling30 13:294c06406d06 121 }
jsterling30 13:294c06406d06 122
jsterling30 13:294c06406d06 123 right.speed(rightSpeed);
jsterling30 13:294c06406d06 124 left.speed(leftSpeed);
jsterling30 13:294c06406d06 125
jsterling30 13:294c06406d06 126 wait(0.1);
jsterling30 13:294c06406d06 127
jsterling30 13:294c06406d06 128 }
jsterling30 8:e90d9ce42b33 129 stop();
jsterling30 8:e90d9ce42b33 130 wait(0.5);
jsterling30 8:e90d9ce42b33 131 }
jsterling30 8:e90d9ce42b33 132
jsterling30 8:e90d9ce42b33 133 void turnLeft(double degrees) {
jsterling30 3:c4b0460d8886 134 leftEnc.reset();
jsterling30 3:c4b0460d8886 135 rightEnc.reset();
jsterling30 13:294c06406d06 136
jsterling30 13:294c06406d06 137 double rightSpeed = SPEED + .04;
jsterling30 13:294c06406d06 138 double leftSpeed = -SPEED - .04;
jsterling30 13:294c06406d06 139
jsterling30 13:294c06406d06 140 int leftTicksPrev = 0;
jsterling30 13:294c06406d06 141 int rightTicksPrev = 0;
jsterling30 13:294c06406d06 142 double offset = 0.01;
jsterling30 13:294c06406d06 143
jsterling30 8:e90d9ce42b33 144 double numTicks = degrees * TicksPerDeg;
jsterling30 13:294c06406d06 145
jsterling30 8:e90d9ce42b33 146 while (leftEnc.read() < numTicks || rightEnc.read() < numTicks) {
jsterling30 13:294c06406d06 147
jsterling30 13:294c06406d06 148 int leftTicks = leftEnc.read();
jsterling30 13:294c06406d06 149 int rightTicks = rightEnc.read();
jsterling30 13:294c06406d06 150
jsterling30 13:294c06406d06 151 int leftDiff = leftTicks - leftTicksPrev;
jsterling30 13:294c06406d06 152 int rightDiff = rightTicks - rightTicksPrev;
jsterling30 13:294c06406d06 153
jsterling30 13:294c06406d06 154 leftTicksPrev = leftTicks;
jsterling30 13:294c06406d06 155 rightTicksPrev = rightTicks;
jsterling30 13:294c06406d06 156
jsterling30 13:294c06406d06 157 if (leftDiff > rightDiff) {
jsterling30 13:294c06406d06 158 leftSpeed += offset;
jsterling30 13:294c06406d06 159 rightSpeed += offset;
jsterling30 13:294c06406d06 160 } else if (leftDiff < rightDiff) {
jsterling30 13:294c06406d06 161 leftSpeed -= offset;
jsterling30 13:294c06406d06 162 rightSpeed -= offset;
jsterling30 8:e90d9ce42b33 163 }
jsterling30 13:294c06406d06 164
jsterling30 13:294c06406d06 165 right.speed(rightSpeed);
jsterling30 13:294c06406d06 166 left.speed(leftSpeed);
jsterling30 13:294c06406d06 167
jsterling30 13:294c06406d06 168 wait(0.1);
jsterling30 13:294c06406d06 169
jsterling30 8:e90d9ce42b33 170 }
jsterling30 2:8c27831ce9a2 171 stop();
jsterling30 8:e90d9ce42b33 172 wait(0.5);
jsterling30 3:c4b0460d8886 173 }
jsterling30 3:c4b0460d8886 174
jsterling30 8:e90d9ce42b33 175 void turnRight(double degrees) {
amohile3 4:a21d15629407 176 leftEnc.reset();
amohile3 4:a21d15629407 177 rightEnc.reset();
jsterling30 13:294c06406d06 178
jsterling30 13:294c06406d06 179 double rightSpeed = -SPEED - .04;
jsterling30 13:294c06406d06 180 double leftSpeed = SPEED + .04;
jsterling30 13:294c06406d06 181
jsterling30 13:294c06406d06 182 int leftTicksPrev = 0;
jsterling30 13:294c06406d06 183 int rightTicksPrev = 0;
jsterling30 13:294c06406d06 184 double offset = 0.01;
jsterling30 13:294c06406d06 185
jsterling30 8:e90d9ce42b33 186 double numTicks = degrees * TicksPerDeg;
jsterling30 13:294c06406d06 187
jsterling30 13:294c06406d06 188 while (leftEnc.read() < numTicks || rightEnc.read() < numTicks) {
jsterling30 13:294c06406d06 189
jsterling30 13:294c06406d06 190 int leftTicks = leftEnc.read();
jsterling30 13:294c06406d06 191 int rightTicks = rightEnc.read();
jsterling30 13:294c06406d06 192
jsterling30 13:294c06406d06 193 int leftDiff = leftTicks - leftTicksPrev;
jsterling30 13:294c06406d06 194 int rightDiff = rightTicks - rightTicksPrev;
jsterling30 13:294c06406d06 195
jsterling30 13:294c06406d06 196 leftTicksPrev = leftTicks;
jsterling30 13:294c06406d06 197 rightTicksPrev = rightTicks;
jsterling30 13:294c06406d06 198
jsterling30 13:294c06406d06 199 if (leftDiff > rightDiff) {
jsterling30 13:294c06406d06 200 leftSpeed -= offset;
jsterling30 13:294c06406d06 201 rightSpeed -= offset;
jsterling30 13:294c06406d06 202 } else if (leftDiff < rightDiff) {
jsterling30 13:294c06406d06 203 leftSpeed += offset;
jsterling30 13:294c06406d06 204 rightSpeed += offset;
jsterling30 13:294c06406d06 205 }
jsterling30 13:294c06406d06 206
jsterling30 13:294c06406d06 207 right.speed(rightSpeed);
jsterling30 13:294c06406d06 208 left.speed(leftSpeed);
jsterling30 13:294c06406d06 209
jsterling30 13:294c06406d06 210 wait(0.1);
jsterling30 13:294c06406d06 211
jsterling30 13:294c06406d06 212 }
amohile3 4:a21d15629407 213 stop();
jsterling30 8:e90d9ce42b33 214 wait(0.5);
jsterling30 8:e90d9ce42b33 215 }
jsterling30 8:e90d9ce42b33 216
jsterling30 8:e90d9ce42b33 217 void turn(double delta) {
jsterling30 8:e90d9ce42b33 218 penUp();
jsterling30 13:294c06406d06 219 forward(1.5);
jsterling30 8:e90d9ce42b33 220 if (delta > 0) {
jsterling30 8:e90d9ce42b33 221 turnLeft(delta);
jsterling30 8:e90d9ce42b33 222 } else {
jsterling30 8:e90d9ce42b33 223 turnRight(-delta);
jsterling30 8:e90d9ce42b33 224 }
jsterling30 13:294c06406d06 225 reverse(1.5);
amohile3 4:a21d15629407 226 }
amohile3 4:a21d15629407 227
amohile3 4:a21d15629407 228 void makeCircle() {
jsterling30 10:9066603296bd 229 penDown();
amohile3 4:a21d15629407 230 leftEnc.reset();
amohile3 4:a21d15629407 231 rightEnc.reset();
jsterling30 13:294c06406d06 232 right.speed(0.5);
jsterling30 8:e90d9ce42b33 233 float numTicks = 2 * 360 * TicksPerDeg;
amohile3 5:7572f73a78f3 234 while (rightEnc.read() < numTicks) {}
amohile3 4:a21d15629407 235 stop();
amohile3 4:a21d15629407 236 wait(0.1);
amohile3 4:a21d15629407 237 }
amohile3 4:a21d15629407 238
amohile3 9:aa739588a86b 239 void makeSquare(int sideLength) {
jsterling30 10:9066603296bd 240 penDown();
amohile3 9:aa739588a86b 241 forward(sideLength);
amohile3 9:aa739588a86b 242 turn(90);
jsterling30 13:294c06406d06 243 penDown();
amohile3 9:aa739588a86b 244 forward(sideLength);
amohile3 9:aa739588a86b 245 turn(90);
jsterling30 13:294c06406d06 246 penDown();
amohile3 9:aa739588a86b 247 forward(sideLength);
amohile3 9:aa739588a86b 248 turn(90);
jsterling30 13:294c06406d06 249 penDown();
amohile3 9:aa739588a86b 250 forward(sideLength);
amohile3 9:aa739588a86b 251 turn(90);
amohile3 9:aa739588a86b 252 }
amohile3 9:aa739588a86b 253
amohile3 9:aa739588a86b 254 void makeTriangle(int sideLength) {
jsterling30 10:9066603296bd 255 penDown();
amohile3 9:aa739588a86b 256 forward(sideLength);
amohile3 9:aa739588a86b 257 turn(120);
jsterling30 13:294c06406d06 258 penDown();
amohile3 9:aa739588a86b 259 forward(sideLength);
amohile3 9:aa739588a86b 260 turn(120);
jsterling30 13:294c06406d06 261 penDown();
amohile3 9:aa739588a86b 262 forward(sideLength);
amohile3 9:aa739588a86b 263 }
amohile3 9:aa739588a86b 264
amohile3 9:aa739588a86b 265 void makeHexagon(int sideLength) {
jsterling30 10:9066603296bd 266 penDown();
amohile3 9:aa739588a86b 267 forward(sideLength);
amohile3 9:aa739588a86b 268 turn(60);
jsterling30 13:294c06406d06 269 penDown();
amohile3 9:aa739588a86b 270 forward(sideLength);
amohile3 9:aa739588a86b 271 turn(60);
jsterling30 13:294c06406d06 272 penDown();
amohile3 9:aa739588a86b 273 forward(sideLength);
amohile3 9:aa739588a86b 274 turn(60);
jsterling30 13:294c06406d06 275 penDown();
amohile3 9:aa739588a86b 276 forward(sideLength);
amohile3 9:aa739588a86b 277 turn(60);
jsterling30 13:294c06406d06 278 penDown();
amohile3 9:aa739588a86b 279 forward(sideLength);
amohile3 9:aa739588a86b 280 turn(60);
jsterling30 13:294c06406d06 281 penDown();
amohile3 9:aa739588a86b 282 forward(sideLength);
amohile3 9:aa739588a86b 283 turn(60);
amohile3 9:aa739588a86b 284 }
amohile3 9:aa739588a86b 285
jsterling30 13:294c06406d06 286 void makePattern(char *shape, int sideLength){
jsterling30 13:294c06406d06 287 if (strcmp(shape, "triangle") == 0) {
jsterling30 13:294c06406d06 288 makeTriangle(sideLength);
jsterling30 13:294c06406d06 289 //turn(150);
amohile3 12:b107ba24faa4 290 makeTriangle(sideLength);
jsterling30 13:294c06406d06 291 //turn(150);
amohile3 12:b107ba24faa4 292 makeTriangle(sideLength);
jsterling30 13:294c06406d06 293 //turn(150);
jsterling30 13:294c06406d06 294 //makeTriangle(sideLength);
jsterling30 13:294c06406d06 295 } else if (strcmp(shape, "circle") == 0) {
jsterling30 13:294c06406d06 296 makeCircle();
amohile3 12:b107ba24faa4 297 turn(120);
jsterling30 13:294c06406d06 298 makeCircle();
amohile3 12:b107ba24faa4 299 turn(120);
jsterling30 13:294c06406d06 300 makeCircle();
amohile3 12:b107ba24faa4 301 turn(120);
jsterling30 13:294c06406d06 302 } else if (strcmp(shape, "hexagon") == 0) {
amohile3 12:b107ba24faa4 303 makeHexagon(sideLength);
amohile3 12:b107ba24faa4 304 turn(120);
amohile3 12:b107ba24faa4 305 makeHexagon(sideLength);
amohile3 12:b107ba24faa4 306 turn(120);
amohile3 12:b107ba24faa4 307 makeHexagon(sideLength);
amohile3 12:b107ba24faa4 308 turn(120);
jsterling30 13:294c06406d06 309 } else if (strcmp(shape, "square") == 0) {
amohile3 12:b107ba24faa4 310 makeSquare(sideLength);
amohile3 12:b107ba24faa4 311 turn(120);
amohile3 12:b107ba24faa4 312 makeSquare(sideLength);
amohile3 12:b107ba24faa4 313 turn(120);
amohile3 12:b107ba24faa4 314 makeSquare(sideLength);
amohile3 12:b107ba24faa4 315 turn(120);
amohile3 12:b107ba24faa4 316 }
amohile3 12:b107ba24faa4 317 }
amohile3 12:b107ba24faa4 318
jsterling30 3:c4b0460d8886 319 void manualCheck() {
jsterling30 3:c4b0460d8886 320 leftEnc.reset();
jsterling30 3:c4b0460d8886 321 rightEnc.reset();
jsterling30 3:c4b0460d8886 322 while (1) {
jsterling30 3:c4b0460d8886 323 printf("Left Ticks: %d\r\n", leftEnc.read());
jsterling30 3:c4b0460d8886 324 printf("Right Ticks: %d\r\n\r\n", rightEnc.read());
jsterling30 3:c4b0460d8886 325 wait(0.5);
jsterling30 3:c4b0460d8886 326 }
jsterling30 2:8c27831ce9a2 327 }
amohile3 0:191d0be60c80 328
jsterling30 7:6ef9b0aab972 329 // command character array for bluetooth parsing
jsterling30 7:6ef9b0aab972 330 char command[100];
jsterling30 7:6ef9b0aab972 331
jsterling30 7:6ef9b0aab972 332 void blue_interrupt() {
jsterling30 7:6ef9b0aab972 333 // bluetooth receiver interrupt
jsterling30 7:6ef9b0aab972 334 char letter = (char) blue.getc();
jsterling30 7:6ef9b0aab972 335 command[strlen(command)] = letter;
jsterling30 7:6ef9b0aab972 336 }
jsterling30 7:6ef9b0aab972 337
jsterling30 7:6ef9b0aab972 338 void get_command() {
jsterling30 7:6ef9b0aab972 339 memset(command, '\0', sizeof(command)); // resetting command buffer
jsterling30 7:6ef9b0aab972 340 while (!strlen(command)) { // waiting for full command to be read
jsterling30 7:6ef9b0aab972 341 led4 = !led4;
jsterling30 7:6ef9b0aab972 342 wait(1);
jsterling30 7:6ef9b0aab972 343 }
jsterling30 7:6ef9b0aab972 344 }
jsterling30 7:6ef9b0aab972 345
jsterling30 13:294c06406d06 346 Transition firstTransition(0,0,.25,.25,false);
jsterling30 13:294c06406d06 347
jsterling30 13:294c06406d06 348 void drawLetter(Transition letter[], int numSteps) {
jsterling30 13:294c06406d06 349 double currAngle = 0.0;
jsterling30 13:294c06406d06 350 for (int i = 0; i < numSteps; i++) {
jsterling30 13:294c06406d06 351 double desAngle = letter[i].desired_angle();
jsterling30 13:294c06406d06 352 double delta = desAngle - currAngle;
jsterling30 13:294c06406d06 353 double distance = letter[i].distance();
jsterling30 13:294c06406d06 354
jsterling30 13:294c06406d06 355 if (delta > 90.0) {
jsterling30 13:294c06406d06 356 delta -= 180;
jsterling30 13:294c06406d06 357 turn(delta);
jsterling30 13:294c06406d06 358 if (letter[i].draw == true) {
jsterling30 13:294c06406d06 359 penDown();
jsterling30 13:294c06406d06 360 } else {
jsterling30 13:294c06406d06 361 penUp();
jsterling30 13:294c06406d06 362 }
jsterling30 13:294c06406d06 363 reverse(distance);
jsterling30 13:294c06406d06 364 } else if (delta < -90.0) {
jsterling30 13:294c06406d06 365 delta += 180;
jsterling30 13:294c06406d06 366 turn(delta);
jsterling30 13:294c06406d06 367 if (letter[i].draw == true) {
jsterling30 13:294c06406d06 368 penDown();
jsterling30 13:294c06406d06 369 } else {
jsterling30 13:294c06406d06 370 penUp();
jsterling30 13:294c06406d06 371 }
jsterling30 13:294c06406d06 372 reverse(distance);
jsterling30 8:e90d9ce42b33 373 } else {
jsterling30 13:294c06406d06 374 turn(delta);
jsterling30 13:294c06406d06 375 if (letter[i].draw == true) {
jsterling30 13:294c06406d06 376 penDown();
jsterling30 13:294c06406d06 377 } else {
jsterling30 13:294c06406d06 378 penUp();
jsterling30 13:294c06406d06 379 }
jsterling30 13:294c06406d06 380 forward(distance);
jsterling30 8:e90d9ce42b33 381 }
jsterling30 13:294c06406d06 382
jsterling30 13:294c06406d06 383 currAngle += delta;
jsterling30 13:294c06406d06 384 }
jsterling30 13:294c06406d06 385 double delta = 0 - currAngle;
jsterling30 13:294c06406d06 386 turn(delta);
jsterling30 13:294c06406d06 387 }
jsterling30 13:294c06406d06 388
jsterling30 13:294c06406d06 389 void drawWord(char *word) {
jsterling30 13:294c06406d06 390 for (int i = 0; i < strlen(word); i++) {
jsterling30 13:294c06406d06 391 switch (word[i]) {
jsterling30 13:294c06406d06 392 case 'h':
jsterling30 13:294c06406d06 393 drawLetter(let_h, 7);
jsterling30 13:294c06406d06 394 break;
jsterling30 13:294c06406d06 395 case 'e':
jsterling30 13:294c06406d06 396 drawLetter(let_e, 7);
jsterling30 13:294c06406d06 397 break;
jsterling30 13:294c06406d06 398 case 'l':
jsterling30 13:294c06406d06 399 drawLetter(let_l, 4);
jsterling30 13:294c06406d06 400 break;
jsterling30 13:294c06406d06 401 case 'o':
jsterling30 13:294c06406d06 402 drawLetter(let_o, 6);
jsterling30 13:294c06406d06 403 break;
jsterling30 13:294c06406d06 404 default:
jsterling30 13:294c06406d06 405 blue.printf(" Letter not recognized\n");
jsterling30 13:294c06406d06 406 }
jsterling30 8:e90d9ce42b33 407 }
jsterling30 8:e90d9ce42b33 408 }
jsterling30 8:e90d9ce42b33 409
amohile3 0:191d0be60c80 410 int main() {
jsterling30 7:6ef9b0aab972 411
jsterling30 8:e90d9ce42b33 412 penDown();
jsterling30 8:e90d9ce42b33 413
jsterling30 7:6ef9b0aab972 414 blue.baud(9600);
jsterling30 7:6ef9b0aab972 415 blue.attach(&blue_interrupt);
jsterling30 7:6ef9b0aab972 416
jsterling30 7:6ef9b0aab972 417 while (1) {
jsterling30 7:6ef9b0aab972 418 get_command();
jsterling30 7:6ef9b0aab972 419
jsterling30 7:6ef9b0aab972 420 const char *delimiter = " "; // space character to separate arguments
jsterling30 7:6ef9b0aab972 421
jsterling30 10:9066603296bd 422 char *arg1 = strtok(command, delimiter);
jsterling30 10:9066603296bd 423 char *arg2 = strtok(NULL, delimiter);
jsterling30 10:9066603296bd 424 char *arg3 = strtok(NULL, delimiter);
jsterling30 7:6ef9b0aab972 425
jsterling30 7:6ef9b0aab972 426 if (strcmp(arg1, "draw") == 0) {
jsterling30 13:294c06406d06 427
jsterling30 7:6ef9b0aab972 428 // first argument is draw
jsterling30 7:6ef9b0aab972 429 if (strcmp(arg2, "square") == 0) {
jsterling30 7:6ef9b0aab972 430 // second argument is square
jsterling30 10:9066603296bd 431 int sideDistance = 3;
jsterling30 10:9066603296bd 432 if (arg3 != NULL) {
jsterling30 10:9066603296bd 433 sideDistance = arg3[0] - '0';
jsterling30 10:9066603296bd 434 }
jsterling30 10:9066603296bd 435 blue.printf("Drawing Square with side length = %d\n", sideDistance);
jsterling30 10:9066603296bd 436 makeSquare(sideDistance);
jsterling30 7:6ef9b0aab972 437 } else if (strcmp(arg2, "circle") == 0) {
jsterling30 7:6ef9b0aab972 438 // second argument is circle
jsterling30 10:9066603296bd 439 blue.printf("Drawing Circle\n");
jsterling30 10:9066603296bd 440 makeCircle();
jsterling30 10:9066603296bd 441 } else if (strcmp(arg2, "triangle") == 0) {
jsterling30 10:9066603296bd 442 // second argument is triangle
jsterling30 10:9066603296bd 443 int sideDistance = 3;
jsterling30 10:9066603296bd 444 if (arg3 != NULL) {
jsterling30 10:9066603296bd 445 sideDistance = arg3[0] - '0';
jsterling30 10:9066603296bd 446 }
jsterling30 10:9066603296bd 447 blue.printf("Drawing Triangle with side length = %d\n", sideDistance);
jsterling30 10:9066603296bd 448 makeTriangle(sideDistance);
jsterling30 10:9066603296bd 449 } else if (strcmp(arg2, "hexagon") == 0) {
jsterling30 10:9066603296bd 450 // second argument is hexagon
jsterling30 10:9066603296bd 451 int sideDistance = 3;
jsterling30 10:9066603296bd 452 if (arg3 != NULL) {
jsterling30 10:9066603296bd 453 sideDistance = arg3[0] - '0';
jsterling30 10:9066603296bd 454 }
jsterling30 10:9066603296bd 455 blue.printf("Drawing Hexagon with side length = %d\n", sideDistance);
jsterling30 10:9066603296bd 456 makeHexagon(sideDistance);
jsterling30 7:6ef9b0aab972 457 } else {
jsterling30 7:6ef9b0aab972 458 // second argument is not recognized
jsterling30 10:9066603296bd 459 blue.printf("Second argument is not recognized (must be square, circle, triangle, hexagon)\n");
jsterling30 7:6ef9b0aab972 460 }
jsterling30 7:6ef9b0aab972 461
jsterling30 7:6ef9b0aab972 462 } else if (strcmp(arg1, "write") == 0) {
jsterling30 13:294c06406d06 463
jsterling30 7:6ef9b0aab972 464 // first argument is write
jsterling30 13:294c06406d06 465 blue.printf("Writing the word: %s\n", arg2);
jsterling30 13:294c06406d06 466 drawWord(arg2);
jsterling30 13:294c06406d06 467
jsterling30 13:294c06406d06 468 } else if (strcmp(arg1, "pattern") == 0) {
jsterling30 13:294c06406d06 469
jsterling30 13:294c06406d06 470 // first argument is pattern
jsterling30 13:294c06406d06 471 if ((strcmp(arg2, "square") != 0) && (strcmp(arg2, "circle") != 0) && (strcmp(arg2, "triangle") != 0) && (strcmp(arg2, "hexagon") != 0)) {
jsterling30 13:294c06406d06 472 blue.printf("ERROR: Second argument is not recognized (square, circle, triangle, hexagon)\n");
jsterling30 13:294c06406d06 473 } else {
jsterling30 13:294c06406d06 474 int sideDistance = 3;
jsterling30 13:294c06406d06 475 if (arg3 != NULL) {
jsterling30 13:294c06406d06 476 sideDistance = arg3[0] - '0';
jsterling30 13:294c06406d06 477 }
jsterling30 13:294c06406d06 478 blue.printf("Drawing %s pattern with side length = %d\n", arg2, sideDistance);
jsterling30 13:294c06406d06 479 makePattern(arg2, sideDistance);
jsterling30 13:294c06406d06 480 }
jsterling30 13:294c06406d06 481
jsterling30 7:6ef9b0aab972 482 } else {
jsterling30 7:6ef9b0aab972 483 // first argument is not recognized
jsterling30 10:9066603296bd 484 blue.printf("ERROR: First argument is not recognized (must be draw or write)\n");
jsterling30 7:6ef9b0aab972 485 }
jsterling30 7:6ef9b0aab972 486
jsterling30 7:6ef9b0aab972 487 blue.printf("\n");
jsterling30 7:6ef9b0aab972 488 }
amohile3 0:191d0be60c80 489 }