Basic motor code
Dependencies: Motordriver mbed HALLFX_ENCODER Servo
main.cpp@2:8c27831ce9a2, 2018-04-20 (annotated)
- Committer:
- jsterling30
- Date:
- Fri Apr 20 16:53:24 2018 +0000
- Revision:
- 2:8c27831ce9a2
- Parent:
- 1:560118dc2f99
- Child:
- 3:c4b0460d8886
tick measurements
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 | 2:8c27831ce9a2 | 4 | |
jsterling30 | 2:8c27831ce9a2 | 5 | #define SPEED 0.2 |
jsterling30 | 2:8c27831ce9a2 | 6 | #define TICKSPERREV 384 |
jsterling30 | 2:8c27831ce9a2 | 7 | #define DISTPERREV 8.25 // 8.25 inches per revolution |
amohile3 | 0:191d0be60c80 | 8 | |
jsterling30 | 2:8c27831ce9a2 | 9 | Motor right(p23,p6,p5, 1); // pwm, fwd, rev |
amohile3 | 0:191d0be60c80 | 10 | Motor left(p21, p7, p8, 1); // pwm, fwd, rev |
amohile3 | 0:191d0be60c80 | 11 | |
jsterling30 | 2:8c27831ce9a2 | 12 | HALLFX_ENCODER leftEnc(p15); |
jsterling30 | 2:8c27831ce9a2 | 13 | HALLFX_ENCODER rightEnc(p16); |
jsterling30 | 2:8c27831ce9a2 | 14 | |
jsterling30 | 2:8c27831ce9a2 | 15 | void stop() { |
jsterling30 | 2:8c27831ce9a2 | 16 | right.speed(0.0); |
jsterling30 | 2:8c27831ce9a2 | 17 | left.speed(0.0); |
jsterling30 | 2:8c27831ce9a2 | 18 | } |
jsterling30 | 2:8c27831ce9a2 | 19 | |
jsterling30 | 2:8c27831ce9a2 | 20 | void forward(float distance) { |
jsterling30 | 2:8c27831ce9a2 | 21 | float numRevs = distance / DISTPERREV; |
jsterling30 | 2:8c27831ce9a2 | 22 | float numTicks = numRevs * TICKSPERREV; |
jsterling30 | 2:8c27831ce9a2 | 23 | leftEnc.reset(); |
jsterling30 | 2:8c27831ce9a2 | 24 | rightEnc.reset(); |
jsterling30 | 2:8c27831ce9a2 | 25 | printf("Left Ticks Start: %d\r\n", leftEnc.read()); |
jsterling30 | 2:8c27831ce9a2 | 26 | printf("Right Ticks Start: %d\r\n", rightEnc.read()); |
jsterling30 | 2:8c27831ce9a2 | 27 | right.speed(SPEED); |
jsterling30 | 2:8c27831ce9a2 | 28 | left.speed(SPEED); |
jsterling30 | 2:8c27831ce9a2 | 29 | while (leftEnc.read() < numTicks || rightEnc.read() < numTicks) {} |
jsterling30 | 2:8c27831ce9a2 | 30 | printf("Left Ticks End: %d\r\n", leftEnc.read()); |
jsterling30 | 2:8c27831ce9a2 | 31 | printf("Right Ticks End: %d\r\n", rightEnc.read()); |
jsterling30 | 2:8c27831ce9a2 | 32 | stop(); |
jsterling30 | 2:8c27831ce9a2 | 33 | } |
amohile3 | 0:191d0be60c80 | 34 | |
amohile3 | 0:191d0be60c80 | 35 | int main() { |
jsterling30 | 2:8c27831ce9a2 | 36 | //leftEnc.reset(); |
jsterling30 | 2:8c27831ce9a2 | 37 | //rightEnc.reset(); |
jsterling30 | 2:8c27831ce9a2 | 38 | //while (1) { |
jsterling30 | 2:8c27831ce9a2 | 39 | // printf("Left Ticks: %d\r\n", leftEnc.read()); |
jsterling30 | 2:8c27831ce9a2 | 40 | // printf("Right Ticks: %d\r\n\r\n", rightEnc.read()); |
jsterling30 | 2:8c27831ce9a2 | 41 | // wait(0.5); |
jsterling30 | 2:8c27831ce9a2 | 42 | //} |
jsterling30 | 2:8c27831ce9a2 | 43 | forward(10.0); |
amohile3 | 0:191d0be60c80 | 44 | } |