Basic motor code

Dependencies:   Motordriver mbed HALLFX_ENCODER Servo

Committer:
amohile3
Date:
Tue May 01 16:15:43 2018 +0000
Revision:
11:913a68b39fe0
Parent:
9:aa739588a86b
Added makePattern() which takes and replicates a shape around a center point to make a pattern

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 8:e90d9ce42b33 10 #define SPEED 0.32
jsterling30 3:c4b0460d8886 11 #define TICKSPERREV 390
jsterling30 7:6ef9b0aab972 12 #define DISTPERREV 8.25 // 8.25 inches per revolution
jsterling30 8:e90d9ce42b33 13 #define TicksPerDeg 2.73
jsterling30 8:e90d9ce42b33 14
jsterling30 8:e90d9ce42b33 15 #define PI 3.14159265
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 2:8c27831ce9a2 54 right.speed(SPEED);
jsterling30 2:8c27831ce9a2 55 left.speed(SPEED);
jsterling30 3:c4b0460d8886 56 while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {}
jsterling30 3:c4b0460d8886 57 stop();
jsterling30 8:e90d9ce42b33 58 wait(0.5);
jsterling30 3:c4b0460d8886 59 }
jsterling30 3:c4b0460d8886 60
jsterling30 8:e90d9ce42b33 61 void reverse(double distance) {
jsterling30 8:e90d9ce42b33 62 double numRevs = distance / DISTPERREV;
jsterling30 8:e90d9ce42b33 63 double numTicks = numRevs * TICKSPERREV;
jsterling30 8:e90d9ce42b33 64 leftEnc.reset();
jsterling30 8:e90d9ce42b33 65 rightEnc.reset();
jsterling30 8:e90d9ce42b33 66 right.speed(-SPEED);
jsterling30 8:e90d9ce42b33 67 left.speed(-SPEED);
jsterling30 8:e90d9ce42b33 68 while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {}
jsterling30 8:e90d9ce42b33 69 stop();
jsterling30 8:e90d9ce42b33 70 wait(0.5);
jsterling30 8:e90d9ce42b33 71 }
jsterling30 8:e90d9ce42b33 72
jsterling30 8:e90d9ce42b33 73 void turnLeft(double degrees) {
jsterling30 3:c4b0460d8886 74 leftEnc.reset();
jsterling30 3:c4b0460d8886 75 rightEnc.reset();
jsterling30 3:c4b0460d8886 76 right.speed(SPEED);
jsterling30 3:c4b0460d8886 77 left.speed(-SPEED);
jsterling30 8:e90d9ce42b33 78 double numTicks = degrees * TicksPerDeg;
jsterling30 8:e90d9ce42b33 79 while (leftEnc.read() < numTicks || rightEnc.read() < numTicks) {
jsterling30 8:e90d9ce42b33 80 if (leftEnc.read() >= numTicks) {
jsterling30 8:e90d9ce42b33 81 left.speed(0.0);
jsterling30 8:e90d9ce42b33 82 }
jsterling30 8:e90d9ce42b33 83 if (rightEnc.read() >= numTicks) {
jsterling30 8:e90d9ce42b33 84 right.speed(0.0);
jsterling30 8:e90d9ce42b33 85 }
jsterling30 8:e90d9ce42b33 86 }
jsterling30 2:8c27831ce9a2 87 stop();
jsterling30 8:e90d9ce42b33 88 blue.printf("Left Turn\n Left Encoder: %d\n Right Encoder: %d\n\n", leftEnc.read(), rightEnc.read());
jsterling30 8:e90d9ce42b33 89 wait(0.5);
jsterling30 3:c4b0460d8886 90 }
jsterling30 3:c4b0460d8886 91
jsterling30 8:e90d9ce42b33 92 void turnRight(double degrees) {
amohile3 4:a21d15629407 93 leftEnc.reset();
amohile3 4:a21d15629407 94 rightEnc.reset();
amohile3 4:a21d15629407 95 right.speed(-SPEED);
amohile3 4:a21d15629407 96 left.speed(SPEED);
jsterling30 8:e90d9ce42b33 97 double numTicks = degrees * TicksPerDeg;
amohile3 4:a21d15629407 98 while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {}
amohile3 4:a21d15629407 99 stop();
jsterling30 8:e90d9ce42b33 100 blue.printf("Right Turn\n Left Encoder: %d\n Right Encoder: %d\n\n", leftEnc.read(), rightEnc.read());
jsterling30 8:e90d9ce42b33 101 wait(0.5);
jsterling30 8:e90d9ce42b33 102 }
jsterling30 8:e90d9ce42b33 103
jsterling30 8:e90d9ce42b33 104 void turn(double delta) {
jsterling30 8:e90d9ce42b33 105 penUp();
jsterling30 8:e90d9ce42b33 106 forward(1.75);
jsterling30 8:e90d9ce42b33 107 if (delta > 0) {
jsterling30 8:e90d9ce42b33 108 turnLeft(delta);
jsterling30 8:e90d9ce42b33 109 } else {
jsterling30 8:e90d9ce42b33 110 turnRight(-delta);
jsterling30 8:e90d9ce42b33 111 }
jsterling30 8:e90d9ce42b33 112 reverse(1.75);
jsterling30 8:e90d9ce42b33 113 penDown();
amohile3 4:a21d15629407 114 }
amohile3 4:a21d15629407 115
amohile3 4:a21d15629407 116 void makeCircle() {
amohile3 4:a21d15629407 117 leftEnc.reset();
amohile3 4:a21d15629407 118 rightEnc.reset();
amohile3 4:a21d15629407 119 right.speed(SPEED);
jsterling30 8:e90d9ce42b33 120 float numTicks = 2 * 360 * TicksPerDeg;
amohile3 5:7572f73a78f3 121 while (rightEnc.read() < numTicks) {}
amohile3 4:a21d15629407 122 stop();
amohile3 4:a21d15629407 123 wait(0.1);
amohile3 4:a21d15629407 124 }
amohile3 4:a21d15629407 125
amohile3 9:aa739588a86b 126 void makeSquare(int sideLength) {
amohile3 9:aa739588a86b 127 forward(sideLength);
amohile3 9:aa739588a86b 128 turn(90);
amohile3 9:aa739588a86b 129 forward(sideLength);
amohile3 9:aa739588a86b 130 turn(90);
amohile3 9:aa739588a86b 131 forward(sideLength);
amohile3 9:aa739588a86b 132 turn(90);
amohile3 9:aa739588a86b 133 forward(sideLength);
amohile3 9:aa739588a86b 134 turn(90);
amohile3 9:aa739588a86b 135 }
amohile3 9:aa739588a86b 136
amohile3 9:aa739588a86b 137 void makeTriangle(int sideLength) {
amohile3 9:aa739588a86b 138 forward(sideLength);
amohile3 9:aa739588a86b 139 turn(120);
amohile3 9:aa739588a86b 140 forward(sideLength);
amohile3 9:aa739588a86b 141 turn(120);
amohile3 9:aa739588a86b 142 forward(sideLength);
amohile3 9:aa739588a86b 143 turn(120);
amohile3 9:aa739588a86b 144 }
amohile3 9:aa739588a86b 145
amohile3 9:aa739588a86b 146 void makeHexagon(int sideLength) {
amohile3 9:aa739588a86b 147 forward(sideLength);
amohile3 9:aa739588a86b 148 turn(60);
amohile3 9:aa739588a86b 149 forward(sideLength);
amohile3 9:aa739588a86b 150 turn(60);
amohile3 9:aa739588a86b 151 forward(sideLength);
amohile3 9:aa739588a86b 152 turn(60);
amohile3 9:aa739588a86b 153 forward(sideLength);
amohile3 9:aa739588a86b 154 turn(60);
amohile3 9:aa739588a86b 155 forward(sideLength);
amohile3 9:aa739588a86b 156 turn(60);
amohile3 9:aa739588a86b 157 forward(sideLength);
amohile3 9:aa739588a86b 158 turn(60);
amohile3 9:aa739588a86b 159 }
amohile3 9:aa739588a86b 160
amohile3 11:913a68b39fe0 161 void makePattern(string shape, int sideLength){
amohile3 11:913a68b39fe0 162 if (shape.compare("triangle") == 0) {
amohile3 11:913a68b39fe0 163 makeTriangle(sideLength);
amohile3 11:913a68b39fe0 164 turn(120);
amohile3 11:913a68b39fe0 165 makeTriangle(sideLength);
amohile3 11:913a68b39fe0 166 turn(120);
amohile3 11:913a68b39fe0 167 makeTriangle(sideLength);
amohile3 11:913a68b39fe0 168 turn(120);
amohile3 11:913a68b39fe0 169 } else if (shape.compare("circle") == 0) {
amohile3 11:913a68b39fe0 170 makeCircle(sideLength);
amohile3 11:913a68b39fe0 171 turn(120);
amohile3 11:913a68b39fe0 172 makeCircle(sideLength);
amohile3 11:913a68b39fe0 173 turn(120);
amohile3 11:913a68b39fe0 174 makeCircle(sideLength);
amohile3 11:913a68b39fe0 175 turn(120);
amohile3 11:913a68b39fe0 176 } else if (shape.compare("hexagon") == 0) {
amohile3 11:913a68b39fe0 177 makeHexagon(sideLength);
amohile3 11:913a68b39fe0 178 turn(120);
amohile3 11:913a68b39fe0 179 makeHexagon(sideLength);
amohile3 11:913a68b39fe0 180 turn(120);
amohile3 11:913a68b39fe0 181 makeHexagon(sideLength);
amohile3 11:913a68b39fe0 182 turn(120);
amohile3 11:913a68b39fe0 183 } else if (shape.compare("square") == 0) {
amohile3 11:913a68b39fe0 184 makeSquare(sideLength);
amohile3 11:913a68b39fe0 185 turn(120);
amohile3 11:913a68b39fe0 186 makeSquare(sideLength);
amohile3 11:913a68b39fe0 187 turn(120);
amohile3 11:913a68b39fe0 188 makeSquare(sideLength);
amohile3 11:913a68b39fe0 189 turn(120);
amohile3 11:913a68b39fe0 190 }
amohile3 11:913a68b39fe0 191 }
amohile3 11:913a68b39fe0 192
jsterling30 3:c4b0460d8886 193 void manualCheck() {
jsterling30 3:c4b0460d8886 194 leftEnc.reset();
jsterling30 3:c4b0460d8886 195 rightEnc.reset();
jsterling30 3:c4b0460d8886 196 while (1) {
jsterling30 3:c4b0460d8886 197 printf("Left Ticks: %d\r\n", leftEnc.read());
jsterling30 3:c4b0460d8886 198 printf("Right Ticks: %d\r\n\r\n", rightEnc.read());
jsterling30 3:c4b0460d8886 199 wait(0.5);
jsterling30 3:c4b0460d8886 200 }
jsterling30 2:8c27831ce9a2 201 }
amohile3 0:191d0be60c80 202
jsterling30 7:6ef9b0aab972 203 // command character array for bluetooth parsing
jsterling30 7:6ef9b0aab972 204 char command[100];
jsterling30 7:6ef9b0aab972 205
jsterling30 7:6ef9b0aab972 206 void blue_interrupt() {
jsterling30 7:6ef9b0aab972 207 // bluetooth receiver interrupt
jsterling30 7:6ef9b0aab972 208 char letter = (char) blue.getc();
jsterling30 7:6ef9b0aab972 209 command[strlen(command)] = letter;
jsterling30 7:6ef9b0aab972 210 }
jsterling30 7:6ef9b0aab972 211
jsterling30 7:6ef9b0aab972 212 void get_command() {
jsterling30 7:6ef9b0aab972 213 memset(command, '\0', sizeof(command)); // resetting command buffer
jsterling30 7:6ef9b0aab972 214 while (!strlen(command)) { // waiting for full command to be read
jsterling30 7:6ef9b0aab972 215 led4 = !led4;
jsterling30 7:6ef9b0aab972 216 wait(1);
jsterling30 7:6ef9b0aab972 217 }
jsterling30 7:6ef9b0aab972 218 }
jsterling30 7:6ef9b0aab972 219
jsterling30 8:e90d9ce42b33 220 void drawLetter(Letter letter) {
jsterling30 8:e90d9ce42b33 221 double currAngle = 0;
jsterling30 8:e90d9ce42b33 222 for (int i = 1; i < sizeof(letter.points); i++) {
jsterling30 8:e90d9ce42b33 223 Transition prev = letter.points[i - 1];
jsterling30 8:e90d9ce42b33 224 Transition next = letter.points[i];
jsterling30 8:e90d9ce42b33 225 double desAngle = atan2(next.y - prev.y, next.x - prev.x) * 180 / PI;
jsterling30 8:e90d9ce42b33 226 double theta = desAngle - currAngle;
jsterling30 8:e90d9ce42b33 227 if (theta > 0) {
jsterling30 8:e90d9ce42b33 228 turnLeft(theta);
jsterling30 8:e90d9ce42b33 229 } else {
jsterling30 8:e90d9ce42b33 230 turnRight(-theta);
jsterling30 8:e90d9ce42b33 231 }
jsterling30 8:e90d9ce42b33 232 currAngle = desAngle;
jsterling30 8:e90d9ce42b33 233 double distance = sqrt(pow(next.y - prev.y, 2) + pow(next.x - prev.x, 2));
jsterling30 8:e90d9ce42b33 234 forward(distance);
jsterling30 8:e90d9ce42b33 235 }
jsterling30 8:e90d9ce42b33 236 }
jsterling30 8:e90d9ce42b33 237
amohile3 0:191d0be60c80 238 int main() {
jsterling30 7:6ef9b0aab972 239
jsterling30 8:e90d9ce42b33 240 penDown();
amohile3 9:aa739588a86b 241 makeHexagon(3);
jsterling30 8:e90d9ce42b33 242 //makeCircle();
amohile3 9:aa739588a86b 243 // makeTriangle();
jsterling30 8:e90d9ce42b33 244 //forward(5.0);
jsterling30 8:e90d9ce42b33 245 //turn(90);
jsterling30 8:e90d9ce42b33 246
jsterling30 8:e90d9ce42b33 247 //drawLetter(let_a);
jsterling30 8:e90d9ce42b33 248 /*
jsterling30 7:6ef9b0aab972 249 blue.baud(9600);
jsterling30 7:6ef9b0aab972 250 blue.attach(&blue_interrupt);
jsterling30 7:6ef9b0aab972 251
jsterling30 7:6ef9b0aab972 252 while (1) {
jsterling30 7:6ef9b0aab972 253 get_command();
jsterling30 7:6ef9b0aab972 254 blue.printf("%s, len = %d\n", command, strlen(command));
jsterling30 7:6ef9b0aab972 255
jsterling30 7:6ef9b0aab972 256 const char *delimiter = " "; // space character to separate arguments
jsterling30 7:6ef9b0aab972 257 char *arg1;
jsterling30 7:6ef9b0aab972 258 char *arg2;
jsterling30 7:6ef9b0aab972 259
jsterling30 7:6ef9b0aab972 260 arg1 = strtok(command, delimiter);
jsterling30 7:6ef9b0aab972 261 arg2 = strtok(NULL, delimiter);
jsterling30 7:6ef9b0aab972 262
jsterling30 7:6ef9b0aab972 263 if (strcmp(arg1, "draw") == 0) {
jsterling30 7:6ef9b0aab972 264 // first argument is draw
jsterling30 7:6ef9b0aab972 265 blue.printf(" First argument is draw\n");
jsterling30 7:6ef9b0aab972 266
jsterling30 7:6ef9b0aab972 267 if (strcmp(arg2, "square") == 0) {
jsterling30 7:6ef9b0aab972 268 // second argument is square
jsterling30 7:6ef9b0aab972 269 blue.printf(" Second argument is square\n");
jsterling30 7:6ef9b0aab972 270 } else if (strcmp(arg2, "circle") == 0) {
jsterling30 7:6ef9b0aab972 271 // second argument is circle
jsterling30 7:6ef9b0aab972 272 blue.printf(" Second argument is circle\n");
jsterling30 7:6ef9b0aab972 273 } else {
jsterling30 7:6ef9b0aab972 274 // second argument is not recognized
jsterling30 7:6ef9b0aab972 275 blue.printf(" Second argument is not recognized (must be square or circle)\n");
jsterling30 7:6ef9b0aab972 276 }
jsterling30 7:6ef9b0aab972 277
jsterling30 7:6ef9b0aab972 278 } else if (strcmp(arg1, "write") == 0) {
jsterling30 7:6ef9b0aab972 279 // first argument is write
jsterling30 7:6ef9b0aab972 280 blue.printf(" First argument is write\n");
jsterling30 7:6ef9b0aab972 281 blue.printf(" Second argument is %s\n", arg2);
jsterling30 7:6ef9b0aab972 282 } else {
jsterling30 7:6ef9b0aab972 283 // first argument is not recognized
jsterling30 7:6ef9b0aab972 284 blue.printf(" First argument is not recognized (must be draw or write)\n");
jsterling30 7:6ef9b0aab972 285 }
jsterling30 7:6ef9b0aab972 286
jsterling30 7:6ef9b0aab972 287 blue.printf("\n");
jsterling30 7:6ef9b0aab972 288 }
jsterling30 8:e90d9ce42b33 289 */
jsterling30 7:6ef9b0aab972 290
jsterling30 7:6ef9b0aab972 291 /*
amohile3 5:7572f73a78f3 292 while(1) {
jsterling30 7:6ef9b0aab972 293 pen.calibrate(0.0005, 180.0);
jsterling30 7:6ef9b0aab972 294 for(float p=0; p<1.0; p += 0.1) {
jsterling30 7:6ef9b0aab972 295 pen.write(p);
jsterling30 7:6ef9b0aab972 296 wait(2);
jsterling30 7:6ef9b0aab972 297 }
amohile3 5:7572f73a78f3 298 }
jsterling30 7:6ef9b0aab972 299 */
jsterling30 7:6ef9b0aab972 300
jsterling30 7:6ef9b0aab972 301 //forward(5.0);
jsterling30 3:c4b0460d8886 302 //forward(5.0);
jsterling30 3:c4b0460d8886 303 //turnLeft(90);
amohile3 5:7572f73a78f3 304 //Should make a triangle
amohile3 5:7572f73a78f3 305 //for (int i = 0; i < 3; i++){
jsterling30 8:e90d9ce42b33 306 // forward(5.0);
jsterling30 8:e90d9ce42b33 307 // turnLeft(120);
jsterling30 8:e90d9ce42b33 308 //}
jsterling30 3:c4b0460d8886 309 //manualcheck();
jsterling30 3:c4b0460d8886 310 //forward(10.0);
amohile3 0:191d0be60c80 311 }