control DC motor using pwm
Dependencies: Motordriver mbed
Fork of Motor_HelloWorld by
pwm-motor.cpp@1:a96dd8db5c11, 2016-12-11 (annotated)
- Committer:
- faif
- Date:
- Sun Dec 11 21:58:45 2016 +0000
- Revision:
- 1:a96dd8db5c11
Control a DC toy motor using PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
faif | 1:a96dd8db5c11 | 1 | #include "mbed.h" |
faif | 1:a96dd8db5c11 | 2 | #include "motordriver.h" |
faif | 1:a96dd8db5c11 | 3 | #include "pwm-motor.h" |
faif | 1:a96dd8db5c11 | 4 | |
faif | 1:a96dd8db5c11 | 5 | static const char CLS[] = "\x1B[2J"; // VT100 erase screen |
faif | 1:a96dd8db5c11 | 6 | static const char HOME[] = "\x1B[H"; // VT100 home |
faif | 1:a96dd8db5c11 | 7 | |
faif | 1:a96dd8db5c11 | 8 | // in seconds |
faif | 1:a96dd8db5c11 | 9 | static const int move_delay = 0.02; |
faif | 1:a96dd8db5c11 | 10 | |
faif | 1:a96dd8db5c11 | 11 | enum Direction |
faif | 1:a96dd8db5c11 | 12 | { |
faif | 1:a96dd8db5c11 | 13 | left, |
faif | 1:a96dd8db5c11 | 14 | right, |
faif | 1:a96dd8db5c11 | 15 | }; |
faif | 1:a96dd8db5c11 | 16 | |
faif | 1:a96dd8db5c11 | 17 | struct Movement |
faif | 1:a96dd8db5c11 | 18 | { |
faif | 1:a96dd8db5c11 | 19 | public: |
faif | 1:a96dd8db5c11 | 20 | float start; |
faif | 1:a96dd8db5c11 | 21 | float stop; |
faif | 1:a96dd8db5c11 | 22 | float step; |
faif | 1:a96dd8db5c11 | 23 | float delay; |
faif | 1:a96dd8db5c11 | 24 | Direction direction; |
faif | 1:a96dd8db5c11 | 25 | |
faif | 1:a96dd8db5c11 | 26 | Movement() {} |
faif | 1:a96dd8db5c11 | 27 | |
faif | 1:a96dd8db5c11 | 28 | Movement(float startValue, |
faif | 1:a96dd8db5c11 | 29 | float stopValue, |
faif | 1:a96dd8db5c11 | 30 | float stepValue, |
faif | 1:a96dd8db5c11 | 31 | float delayValue, |
faif | 1:a96dd8db5c11 | 32 | Direction dir) : |
faif | 1:a96dd8db5c11 | 33 | start(startValue), |
faif | 1:a96dd8db5c11 | 34 | stop(stopValue), |
faif | 1:a96dd8db5c11 | 35 | step(stepValue), |
faif | 1:a96dd8db5c11 | 36 | delay(delayValue), |
faif | 1:a96dd8db5c11 | 37 | direction(dir) {} |
faif | 1:a96dd8db5c11 | 38 | }; |
faif | 1:a96dd8db5c11 | 39 | |
faif | 1:a96dd8db5c11 | 40 | Serial pc(USBTX, USBRX); |
faif | 1:a96dd8db5c11 | 41 | |
faif | 1:a96dd8db5c11 | 42 | int main() |
faif | 1:a96dd8db5c11 | 43 | { |
faif | 1:a96dd8db5c11 | 44 | clear_screen(); |
faif | 1:a96dd8db5c11 | 45 | show_menu(); |
faif | 1:a96dd8db5c11 | 46 | |
faif | 1:a96dd8db5c11 | 47 | Motor motor(p21, p15, p17, 1); // PWM, Forward, Reverse, Can break |
faif | 1:a96dd8db5c11 | 48 | Movement leftMovement(-1.0, 1.0, 0.01, move_delay, left); |
faif | 1:a96dd8db5c11 | 49 | Movement rightMovement(1.0, -1.0, 0.01, move_delay, right); |
faif | 1:a96dd8db5c11 | 50 | |
faif | 1:a96dd8db5c11 | 51 | while (true) |
faif | 1:a96dd8db5c11 | 52 | { |
faif | 1:a96dd8db5c11 | 53 | Movement currentMovement; |
faif | 1:a96dd8db5c11 | 54 | switch(pc.getc()) |
faif | 1:a96dd8db5c11 | 55 | { |
faif | 1:a96dd8db5c11 | 56 | case 'r': |
faif | 1:a96dd8db5c11 | 57 | pc.printf("rotating right...\n\r"); |
faif | 1:a96dd8db5c11 | 58 | currentMovement = rightMovement; |
faif | 1:a96dd8db5c11 | 59 | move(motor, currentMovement); |
faif | 1:a96dd8db5c11 | 60 | break; |
faif | 1:a96dd8db5c11 | 61 | case 'l': |
faif | 1:a96dd8db5c11 | 62 | pc.printf("rotating left...\n\r"); |
faif | 1:a96dd8db5c11 | 63 | currentMovement = leftMovement; |
faif | 1:a96dd8db5c11 | 64 | move(motor, currentMovement); |
faif | 1:a96dd8db5c11 | 65 | break; |
faif | 1:a96dd8db5c11 | 66 | case 's': |
faif | 1:a96dd8db5c11 | 67 | pc.printf("stopping motor...\n\r"); |
faif | 1:a96dd8db5c11 | 68 | stop(motor); |
faif | 1:a96dd8db5c11 | 69 | break; |
faif | 1:a96dd8db5c11 | 70 | } |
faif | 1:a96dd8db5c11 | 71 | |
faif | 1:a96dd8db5c11 | 72 | } |
faif | 1:a96dd8db5c11 | 73 | } |
faif | 1:a96dd8db5c11 | 74 | |
faif | 1:a96dd8db5c11 | 75 | void clear_screen() |
faif | 1:a96dd8db5c11 | 76 | { |
faif | 1:a96dd8db5c11 | 77 | pc.printf(CLS); |
faif | 1:a96dd8db5c11 | 78 | pc.printf(HOME); |
faif | 1:a96dd8db5c11 | 79 | } |
faif | 1:a96dd8db5c11 | 80 | |
faif | 1:a96dd8db5c11 | 81 | void show_menu() |
faif | 1:a96dd8db5c11 | 82 | { |
faif | 1:a96dd8db5c11 | 83 | pc.printf("\rMotor controls:\n\r"); |
faif | 1:a96dd8db5c11 | 84 | pc.printf("l. Move left (counter-clockwise)\n\r"); |
faif | 1:a96dd8db5c11 | 85 | pc.printf("r. Move right (clockwise)\n\r"); |
faif | 1:a96dd8db5c11 | 86 | pc.printf("s. Stop\n\r"); |
faif | 1:a96dd8db5c11 | 87 | } |
faif | 1:a96dd8db5c11 | 88 | |
faif | 1:a96dd8db5c11 | 89 | void move(Motor& motor, Movement& movement) |
faif | 1:a96dd8db5c11 | 90 | { |
faif | 1:a96dd8db5c11 | 91 | for (float s = movement.start; |
faif | 1:a96dd8db5c11 | 92 | movement.direction == left ? s < movement.stop : s > movement.stop; |
faif | 1:a96dd8db5c11 | 93 | movement.direction == left ? s += movement.step : s -= movement.step) |
faif | 1:a96dd8db5c11 | 94 | { |
faif | 1:a96dd8db5c11 | 95 | motor.speed(s); |
faif | 1:a96dd8db5c11 | 96 | wait(movement.delay); |
faif | 1:a96dd8db5c11 | 97 | } |
faif | 1:a96dd8db5c11 | 98 | } |
faif | 1:a96dd8db5c11 | 99 | |
faif | 1:a96dd8db5c11 | 100 | |
faif | 1:a96dd8db5c11 | 101 | void stop(Motor& motor) |
faif | 1:a96dd8db5c11 | 102 | { |
faif | 1:a96dd8db5c11 | 103 | motor.stop(0.0); |
faif | 1:a96dd8db5c11 | 104 | } |