Basic motor code

Dependencies:   Motordriver mbed HALLFX_ENCODER Servo

Committer:
amohile3
Date:
Sat Apr 21 19:48:25 2018 +0000
Revision:
4:a21d15629407
Parent:
3:c4b0460d8886
Child:
5:7572f73a78f3
Added turnRight() and makeCircle() methods

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 2:8c27831ce9a2 4
jsterling30 2:8c27831ce9a2 5 #define SPEED 0.2
jsterling30 3:c4b0460d8886 6 #define TICKSPERREV 390
jsterling30 2:8c27831ce9a2 7 #define DISTPERREV 8.25 // 8.25 inches per revolution
jsterling30 3:c4b0460d8886 8 #define TicksPerDeg 2.756
amohile3 0:191d0be60c80 9
jsterling30 2:8c27831ce9a2 10 Motor right(p23,p6,p5, 1); // pwm, fwd, rev
amohile3 0:191d0be60c80 11 Motor left(p21, p7, p8, 1); // pwm, fwd, rev
amohile3 0:191d0be60c80 12
jsterling30 2:8c27831ce9a2 13 HALLFX_ENCODER leftEnc(p15);
jsterling30 2:8c27831ce9a2 14 HALLFX_ENCODER rightEnc(p16);
jsterling30 2:8c27831ce9a2 15
jsterling30 2:8c27831ce9a2 16 void stop() {
jsterling30 2:8c27831ce9a2 17 right.speed(0.0);
jsterling30 2:8c27831ce9a2 18 left.speed(0.0);
jsterling30 2:8c27831ce9a2 19 }
jsterling30 2:8c27831ce9a2 20
jsterling30 2:8c27831ce9a2 21 void forward(float distance) {
jsterling30 2:8c27831ce9a2 22 float numRevs = distance / DISTPERREV;
jsterling30 2:8c27831ce9a2 23 float numTicks = numRevs * TICKSPERREV;
jsterling30 2:8c27831ce9a2 24 leftEnc.reset();
jsterling30 2:8c27831ce9a2 25 rightEnc.reset();
jsterling30 2:8c27831ce9a2 26 printf("Left Ticks Start: %d\r\n", leftEnc.read());
jsterling30 2:8c27831ce9a2 27 printf("Right Ticks Start: %d\r\n", rightEnc.read());
jsterling30 2:8c27831ce9a2 28 right.speed(SPEED);
jsterling30 2:8c27831ce9a2 29 left.speed(SPEED);
jsterling30 3:c4b0460d8886 30 while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {}
jsterling30 3:c4b0460d8886 31 stop();
jsterling30 3:c4b0460d8886 32 wait(0.1);
jsterling30 2:8c27831ce9a2 33 printf("Left Ticks End: %d\r\n", leftEnc.read());
jsterling30 3:c4b0460d8886 34 printf("Right Ticks End: %d\r\n", rightEnc.read());
jsterling30 3:c4b0460d8886 35 }
jsterling30 3:c4b0460d8886 36
jsterling30 3:c4b0460d8886 37 void turnLeft(int degrees) {
jsterling30 3:c4b0460d8886 38 leftEnc.reset();
jsterling30 3:c4b0460d8886 39 rightEnc.reset();
jsterling30 3:c4b0460d8886 40 right.speed(SPEED);
jsterling30 3:c4b0460d8886 41 left.speed(-SPEED);
jsterling30 3:c4b0460d8886 42 float numTicks = degrees * TicksPerDeg;
jsterling30 3:c4b0460d8886 43 while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {}
jsterling30 2:8c27831ce9a2 44 stop();
jsterling30 3:c4b0460d8886 45 wait(0.1);
jsterling30 3:c4b0460d8886 46 }
jsterling30 3:c4b0460d8886 47
amohile3 4:a21d15629407 48 void turnRight(int degrees) {
amohile3 4:a21d15629407 49 leftEnc.reset();
amohile3 4:a21d15629407 50 rightEnc.reset();
amohile3 4:a21d15629407 51 right.speed(-SPEED);
amohile3 4:a21d15629407 52 left.speed(SPEED);
amohile3 4:a21d15629407 53 float numTicks = degrees * TicksPerDeg;
amohile3 4:a21d15629407 54 while (leftEnc.read() < numTicks && rightEnc.read() < numTicks) {}
amohile3 4:a21d15629407 55 stop();
amohile3 4:a21d15629407 56 wait(0.1);
amohile3 4:a21d15629407 57 }
amohile3 4:a21d15629407 58
amohile3 4:a21d15629407 59 void makeCircle() {
amohile3 4:a21d15629407 60 leftEnc.reset();
amohile3 4:a21d15629407 61 rightEnc.reset();
amohile3 4:a21d15629407 62 right.speed(SPEED);
amohile3 4:a21d15629407 63 float numTicks = 360 * TicksPerDeg;
amohile3 4:a21d15629407 64 while (rightEnc.read() < numticks) {}
amohile3 4:a21d15629407 65 stop();
amohile3 4:a21d15629407 66 wait(0.1);
amohile3 4:a21d15629407 67 }
amohile3 4:a21d15629407 68
jsterling30 3:c4b0460d8886 69 void manualCheck() {
jsterling30 3:c4b0460d8886 70 leftEnc.reset();
jsterling30 3:c4b0460d8886 71 rightEnc.reset();
jsterling30 3:c4b0460d8886 72 while (1) {
jsterling30 3:c4b0460d8886 73 printf("Left Ticks: %d\r\n", leftEnc.read());
jsterling30 3:c4b0460d8886 74 printf("Right Ticks: %d\r\n\r\n", rightEnc.read());
jsterling30 3:c4b0460d8886 75 wait(0.5);
jsterling30 3:c4b0460d8886 76 }
jsterling30 2:8c27831ce9a2 77 }
amohile3 0:191d0be60c80 78
amohile3 0:191d0be60c80 79 int main() {
jsterling30 3:c4b0460d8886 80 //forward(5.0);
jsterling30 3:c4b0460d8886 81 //turnLeft(90);
amohile3 4:a21d15629407 82 for (int i = 0; i < 3; i++){
amohile3 4:a21d15629407 83 forward(5.0);
amohile3 4:a21d15629407 84 turnLeft(120);
amohile3 4:a21d15629407 85 }
jsterling30 3:c4b0460d8886 86 //manualcheck();
jsterling30 3:c4b0460d8886 87 //forward(10.0);
amohile3 0:191d0be60c80 88 }