Basic motor code
Dependencies: Motordriver mbed HALLFX_ENCODER Servo
main.cpp@10:9066603296bd, 2018-05-01 (annotated)
- Committer:
- jsterling30
- Date:
- Tue May 01 16:11:08 2018 +0000
- Revision:
- 10:9066603296bd
- Parent:
- 9:aa739588a86b
- Child:
- 12:b107ba24faa4
bluetooth commands with drawing
Who changed what in which revision?
User | Revision | Line number | New 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 | 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 | 10:9066603296bd | 76 | right.speed(SPEED + 0.05); |
jsterling30 | 10:9066603296bd | 77 | left.speed(-SPEED - 0.05); |
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 | wait(0.5); |
jsterling30 | 3:c4b0460d8886 | 89 | } |
jsterling30 | 3:c4b0460d8886 | 90 | |
jsterling30 | 8:e90d9ce42b33 | 91 | void turnRight(double degrees) { |
amohile3 | 4:a21d15629407 | 92 | leftEnc.reset(); |
amohile3 | 4:a21d15629407 | 93 | rightEnc.reset(); |
jsterling30 | 10:9066603296bd | 94 | right.speed(-SPEED - 0.05); |
jsterling30 | 10:9066603296bd | 95 | left.speed(SPEED + 0.05); |
jsterling30 | 8:e90d9ce42b33 | 96 | double numTicks = degrees * TicksPerDeg; |
amohile3 | 4:a21d15629407 | 97 | while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {} |
amohile3 | 4:a21d15629407 | 98 | stop(); |
jsterling30 | 8:e90d9ce42b33 | 99 | wait(0.5); |
jsterling30 | 8:e90d9ce42b33 | 100 | } |
jsterling30 | 8:e90d9ce42b33 | 101 | |
jsterling30 | 8:e90d9ce42b33 | 102 | void turn(double delta) { |
jsterling30 | 8:e90d9ce42b33 | 103 | penUp(); |
jsterling30 | 8:e90d9ce42b33 | 104 | forward(1.75); |
jsterling30 | 8:e90d9ce42b33 | 105 | if (delta > 0) { |
jsterling30 | 8:e90d9ce42b33 | 106 | turnLeft(delta); |
jsterling30 | 8:e90d9ce42b33 | 107 | } else { |
jsterling30 | 8:e90d9ce42b33 | 108 | turnRight(-delta); |
jsterling30 | 8:e90d9ce42b33 | 109 | } |
jsterling30 | 8:e90d9ce42b33 | 110 | reverse(1.75); |
jsterling30 | 8:e90d9ce42b33 | 111 | penDown(); |
amohile3 | 4:a21d15629407 | 112 | } |
amohile3 | 4:a21d15629407 | 113 | |
amohile3 | 4:a21d15629407 | 114 | void makeCircle() { |
jsterling30 | 10:9066603296bd | 115 | penDown(); |
amohile3 | 4:a21d15629407 | 116 | leftEnc.reset(); |
amohile3 | 4:a21d15629407 | 117 | rightEnc.reset(); |
jsterling30 | 10:9066603296bd | 118 | right.speed(0.4); |
jsterling30 | 8:e90d9ce42b33 | 119 | float numTicks = 2 * 360 * TicksPerDeg; |
amohile3 | 5:7572f73a78f3 | 120 | while (rightEnc.read() < numTicks) {} |
amohile3 | 4:a21d15629407 | 121 | stop(); |
amohile3 | 4:a21d15629407 | 122 | wait(0.1); |
amohile3 | 4:a21d15629407 | 123 | } |
amohile3 | 4:a21d15629407 | 124 | |
amohile3 | 9:aa739588a86b | 125 | void makeSquare(int sideLength) { |
jsterling30 | 10:9066603296bd | 126 | penDown(); |
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) { |
jsterling30 | 10:9066603296bd | 138 | penDown(); |
amohile3 | 9:aa739588a86b | 139 | forward(sideLength); |
amohile3 | 9:aa739588a86b | 140 | turn(120); |
amohile3 | 9:aa739588a86b | 141 | forward(sideLength); |
amohile3 | 9:aa739588a86b | 142 | turn(120); |
amohile3 | 9:aa739588a86b | 143 | forward(sideLength); |
amohile3 | 9:aa739588a86b | 144 | turn(120); |
amohile3 | 9:aa739588a86b | 145 | } |
amohile3 | 9:aa739588a86b | 146 | |
amohile3 | 9:aa739588a86b | 147 | void makeHexagon(int sideLength) { |
jsterling30 | 10:9066603296bd | 148 | penDown(); |
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 | forward(sideLength); |
amohile3 | 9:aa739588a86b | 160 | turn(60); |
amohile3 | 9:aa739588a86b | 161 | } |
amohile3 | 9:aa739588a86b | 162 | |
jsterling30 | 3:c4b0460d8886 | 163 | void manualCheck() { |
jsterling30 | 3:c4b0460d8886 | 164 | leftEnc.reset(); |
jsterling30 | 3:c4b0460d8886 | 165 | rightEnc.reset(); |
jsterling30 | 3:c4b0460d8886 | 166 | while (1) { |
jsterling30 | 3:c4b0460d8886 | 167 | printf("Left Ticks: %d\r\n", leftEnc.read()); |
jsterling30 | 3:c4b0460d8886 | 168 | printf("Right Ticks: %d\r\n\r\n", rightEnc.read()); |
jsterling30 | 3:c4b0460d8886 | 169 | wait(0.5); |
jsterling30 | 3:c4b0460d8886 | 170 | } |
jsterling30 | 2:8c27831ce9a2 | 171 | } |
amohile3 | 0:191d0be60c80 | 172 | |
jsterling30 | 7:6ef9b0aab972 | 173 | // command character array for bluetooth parsing |
jsterling30 | 7:6ef9b0aab972 | 174 | char command[100]; |
jsterling30 | 7:6ef9b0aab972 | 175 | |
jsterling30 | 7:6ef9b0aab972 | 176 | void blue_interrupt() { |
jsterling30 | 7:6ef9b0aab972 | 177 | // bluetooth receiver interrupt |
jsterling30 | 7:6ef9b0aab972 | 178 | char letter = (char) blue.getc(); |
jsterling30 | 7:6ef9b0aab972 | 179 | command[strlen(command)] = letter; |
jsterling30 | 7:6ef9b0aab972 | 180 | } |
jsterling30 | 7:6ef9b0aab972 | 181 | |
jsterling30 | 7:6ef9b0aab972 | 182 | void get_command() { |
jsterling30 | 7:6ef9b0aab972 | 183 | memset(command, '\0', sizeof(command)); // resetting command buffer |
jsterling30 | 7:6ef9b0aab972 | 184 | while (!strlen(command)) { // waiting for full command to be read |
jsterling30 | 7:6ef9b0aab972 | 185 | led4 = !led4; |
jsterling30 | 7:6ef9b0aab972 | 186 | wait(1); |
jsterling30 | 7:6ef9b0aab972 | 187 | } |
jsterling30 | 7:6ef9b0aab972 | 188 | } |
jsterling30 | 7:6ef9b0aab972 | 189 | |
jsterling30 | 8:e90d9ce42b33 | 190 | void drawLetter(Letter letter) { |
jsterling30 | 8:e90d9ce42b33 | 191 | double currAngle = 0; |
jsterling30 | 8:e90d9ce42b33 | 192 | for (int i = 1; i < sizeof(letter.points); i++) { |
jsterling30 | 8:e90d9ce42b33 | 193 | Transition prev = letter.points[i - 1]; |
jsterling30 | 8:e90d9ce42b33 | 194 | Transition next = letter.points[i]; |
jsterling30 | 8:e90d9ce42b33 | 195 | double desAngle = atan2(next.y - prev.y, next.x - prev.x) * 180 / PI; |
jsterling30 | 8:e90d9ce42b33 | 196 | double theta = desAngle - currAngle; |
jsterling30 | 8:e90d9ce42b33 | 197 | if (theta > 0) { |
jsterling30 | 8:e90d9ce42b33 | 198 | turnLeft(theta); |
jsterling30 | 8:e90d9ce42b33 | 199 | } else { |
jsterling30 | 8:e90d9ce42b33 | 200 | turnRight(-theta); |
jsterling30 | 8:e90d9ce42b33 | 201 | } |
jsterling30 | 8:e90d9ce42b33 | 202 | currAngle = desAngle; |
jsterling30 | 8:e90d9ce42b33 | 203 | double distance = sqrt(pow(next.y - prev.y, 2) + pow(next.x - prev.x, 2)); |
jsterling30 | 8:e90d9ce42b33 | 204 | forward(distance); |
jsterling30 | 8:e90d9ce42b33 | 205 | } |
jsterling30 | 8:e90d9ce42b33 | 206 | } |
jsterling30 | 8:e90d9ce42b33 | 207 | |
amohile3 | 0:191d0be60c80 | 208 | int main() { |
jsterling30 | 7:6ef9b0aab972 | 209 | |
jsterling30 | 8:e90d9ce42b33 | 210 | penDown(); |
jsterling30 | 8:e90d9ce42b33 | 211 | |
jsterling30 | 7:6ef9b0aab972 | 212 | blue.baud(9600); |
jsterling30 | 7:6ef9b0aab972 | 213 | blue.attach(&blue_interrupt); |
jsterling30 | 7:6ef9b0aab972 | 214 | |
jsterling30 | 7:6ef9b0aab972 | 215 | while (1) { |
jsterling30 | 7:6ef9b0aab972 | 216 | get_command(); |
jsterling30 | 7:6ef9b0aab972 | 217 | |
jsterling30 | 7:6ef9b0aab972 | 218 | const char *delimiter = " "; // space character to separate arguments |
jsterling30 | 7:6ef9b0aab972 | 219 | |
jsterling30 | 10:9066603296bd | 220 | char *arg1 = strtok(command, delimiter); |
jsterling30 | 10:9066603296bd | 221 | char *arg2 = strtok(NULL, delimiter); |
jsterling30 | 10:9066603296bd | 222 | char *arg3 = strtok(NULL, delimiter); |
jsterling30 | 7:6ef9b0aab972 | 223 | |
jsterling30 | 7:6ef9b0aab972 | 224 | if (strcmp(arg1, "draw") == 0) { |
jsterling30 | 7:6ef9b0aab972 | 225 | // first argument is draw |
jsterling30 | 7:6ef9b0aab972 | 226 | |
jsterling30 | 7:6ef9b0aab972 | 227 | if (strcmp(arg2, "square") == 0) { |
jsterling30 | 7:6ef9b0aab972 | 228 | // second argument is square |
jsterling30 | 10:9066603296bd | 229 | int sideDistance = 3; |
jsterling30 | 10:9066603296bd | 230 | if (arg3 != NULL) { |
jsterling30 | 10:9066603296bd | 231 | sideDistance = arg3[0] - '0'; |
jsterling30 | 10:9066603296bd | 232 | } |
jsterling30 | 10:9066603296bd | 233 | blue.printf("Drawing Square with side length = %d\n", sideDistance); |
jsterling30 | 10:9066603296bd | 234 | makeSquare(sideDistance); |
jsterling30 | 7:6ef9b0aab972 | 235 | } else if (strcmp(arg2, "circle") == 0) { |
jsterling30 | 7:6ef9b0aab972 | 236 | // second argument is circle |
jsterling30 | 10:9066603296bd | 237 | blue.printf("Drawing Circle\n"); |
jsterling30 | 10:9066603296bd | 238 | makeCircle(); |
jsterling30 | 10:9066603296bd | 239 | } else if (strcmp(arg2, "triangle") == 0) { |
jsterling30 | 10:9066603296bd | 240 | // second argument is triangle |
jsterling30 | 10:9066603296bd | 241 | int sideDistance = 3; |
jsterling30 | 10:9066603296bd | 242 | if (arg3 != NULL) { |
jsterling30 | 10:9066603296bd | 243 | sideDistance = arg3[0] - '0'; |
jsterling30 | 10:9066603296bd | 244 | } |
jsterling30 | 10:9066603296bd | 245 | blue.printf("Drawing Triangle with side length = %d\n", sideDistance); |
jsterling30 | 10:9066603296bd | 246 | makeTriangle(sideDistance); |
jsterling30 | 10:9066603296bd | 247 | } else if (strcmp(arg2, "hexagon") == 0) { |
jsterling30 | 10:9066603296bd | 248 | // second argument is hexagon |
jsterling30 | 10:9066603296bd | 249 | int sideDistance = 3; |
jsterling30 | 10:9066603296bd | 250 | if (arg3 != NULL) { |
jsterling30 | 10:9066603296bd | 251 | sideDistance = arg3[0] - '0'; |
jsterling30 | 10:9066603296bd | 252 | } |
jsterling30 | 10:9066603296bd | 253 | blue.printf("Drawing Hexagon with side length = %d\n", sideDistance); |
jsterling30 | 10:9066603296bd | 254 | makeHexagon(sideDistance); |
jsterling30 | 7:6ef9b0aab972 | 255 | } else { |
jsterling30 | 7:6ef9b0aab972 | 256 | // second argument is not recognized |
jsterling30 | 10:9066603296bd | 257 | blue.printf("Second argument is not recognized (must be square, circle, triangle, hexagon)\n"); |
jsterling30 | 7:6ef9b0aab972 | 258 | } |
jsterling30 | 7:6ef9b0aab972 | 259 | |
jsterling30 | 7:6ef9b0aab972 | 260 | } else if (strcmp(arg1, "write") == 0) { |
jsterling30 | 7:6ef9b0aab972 | 261 | // first argument is write |
jsterling30 | 10:9066603296bd | 262 | blue.printf("First argument is write\n"); |
jsterling30 | 10:9066603296bd | 263 | blue.printf("Second argument is %s\n", arg2); |
jsterling30 | 7:6ef9b0aab972 | 264 | } else { |
jsterling30 | 7:6ef9b0aab972 | 265 | // first argument is not recognized |
jsterling30 | 10:9066603296bd | 266 | blue.printf("ERROR: First argument is not recognized (must be draw or write)\n"); |
jsterling30 | 7:6ef9b0aab972 | 267 | } |
jsterling30 | 7:6ef9b0aab972 | 268 | |
jsterling30 | 7:6ef9b0aab972 | 269 | blue.printf("\n"); |
jsterling30 | 7:6ef9b0aab972 | 270 | } |
amohile3 | 0:191d0be60c80 | 271 | } |