AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
AP1017.cpp@3:f8e70f639ed0, 2017-04-19 (annotated)
- Committer:
- tkstreet
- Date:
- Wed Apr 19 23:13:04 2017 +0000
- Revision:
- 3:f8e70f639ed0
- Parent:
- 1:4d4c77589134
- Child:
- 4:c36159701cde
Simplified motor driver by reducing instruction set to basic operations.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tkstreet | 0:a0435a630c5d | 1 | #include "AP1017.h" |
tkstreet | 0:a0435a630c5d | 2 | |
tkstreet | 0:a0435a630c5d | 3 | /******************** Constructors & Destructors ****************************/ |
tkstreet | 0:a0435a630c5d | 4 | |
tkstreet | 0:a0435a630c5d | 5 | // Default constructor |
tkstreet | 3:f8e70f639ed0 | 6 | AP1017::AP1017(void) : motorOn(false), dutyCycle(0.0) |
tkstreet | 0:a0435a630c5d | 7 | { |
tkstreet | 3:f8e70f639ed0 | 8 | motor = new PwmOut(P0_10); |
tkstreet | 3:f8e70f639ed0 | 9 | inA = new DigitalOut(P0_11); |
tkstreet | 3:f8e70f639ed0 | 10 | inB = new DigitalOut(P0_10); |
tkstreet | 0:a0435a630c5d | 11 | } |
tkstreet | 0:a0435a630c5d | 12 | |
tkstreet | 0:a0435a630c5d | 13 | // Default destructor |
tkstreet | 0:a0435a630c5d | 14 | AP1017::~AP1017(void) |
tkstreet | 0:a0435a630c5d | 15 | { |
tkstreet | 3:f8e70f639ed0 | 16 | stop(); |
tkstreet | 1:4d4c77589134 | 17 | delete inA, inB, motor; |
tkstreet | 0:a0435a630c5d | 18 | } |
tkstreet | 0:a0435a630c5d | 19 | |
tkstreet | 0:a0435a630c5d | 20 | /*********************** Member Functions ***********************************/ |
tkstreet | 0:a0435a630c5d | 21 | |
tkstreet | 3:f8e70f639ed0 | 22 | AP1017::Status AP1017::setDirection(Rotation dir) |
tkstreet | 0:a0435a630c5d | 23 | { |
tkstreet | 3:f8e70f639ed0 | 24 | direction = dir; |
tkstreet | 0:a0435a630c5d | 25 | |
tkstreet | 3:f8e70f639ed0 | 26 | switch(direction){ |
tkstreet | 0:a0435a630c5d | 27 | case DIRECTION_CW: |
tkstreet | 0:a0435a630c5d | 28 | if(motorOn == true && dir == DIRECTION_CCW) |
tkstreet | 0:a0435a630c5d | 29 | return ERROR_MOTORON; |
tkstreet | 1:4d4c77589134 | 30 | inA->write(1); |
tkstreet | 1:4d4c77589134 | 31 | inB->write(0); |
tkstreet | 0:a0435a630c5d | 32 | break; |
tkstreet | 0:a0435a630c5d | 33 | case DIRECTION_CCW: |
tkstreet | 0:a0435a630c5d | 34 | if(motorOn == true && dir == DIRECTION_CW) |
tkstreet | 0:a0435a630c5d | 35 | return ERROR_MOTORON; |
tkstreet | 1:4d4c77589134 | 36 | inA->write(0); |
tkstreet | 1:4d4c77589134 | 37 | inB->write(1); |
tkstreet | 0:a0435a630c5d | 38 | break; |
tkstreet | 0:a0435a630c5d | 39 | case DIRECTION_BRAKE: |
tkstreet | 1:4d4c77589134 | 40 | inA->write(1); |
tkstreet | 1:4d4c77589134 | 41 | inB->write(1); |
tkstreet | 0:a0435a630c5d | 42 | break; |
tkstreet | 0:a0435a630c5d | 43 | case DIRECTION_COAST: |
tkstreet | 1:4d4c77589134 | 44 | inA->write(0); |
tkstreet | 1:4d4c77589134 | 45 | inB->write(0); |
tkstreet | 0:a0435a630c5d | 46 | motorOn = false; |
tkstreet | 0:a0435a630c5d | 47 | break; |
tkstreet | 0:a0435a630c5d | 48 | default: |
tkstreet | 0:a0435a630c5d | 49 | return ERROR_DIRECTION; |
tkstreet | 0:a0435a630c5d | 50 | } |
tkstreet | 0:a0435a630c5d | 51 | |
tkstreet | 0:a0435a630c5d | 52 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 53 | } |
tkstreet | 0:a0435a630c5d | 54 | |
tkstreet | 3:f8e70f639ed0 | 55 | |
tkstreet | 3:f8e70f639ed0 | 56 | AP1017::Rotation AP1017::getDirection(void) |
tkstreet | 0:a0435a630c5d | 57 | { |
tkstreet | 3:f8e70f639ed0 | 58 | return direction; |
tkstreet | 3:f8e70f639ed0 | 59 | } |
tkstreet | 3:f8e70f639ed0 | 60 | |
tkstreet | 0:a0435a630c5d | 61 | |
tkstreet | 3:f8e70f639ed0 | 62 | AP1017::Status AP1017::setSpeed(float dc) |
tkstreet | 3:f8e70f639ed0 | 63 | { |
tkstreet | 3:f8e70f639ed0 | 64 | if((dutyCycle <= 100.0) && (dutyCycle >= 0.0)){ |
tkstreet | 3:f8e70f639ed0 | 65 | dutyCycle = dc/100.0; |
tkstreet | 0:a0435a630c5d | 66 | |
tkstreet | 0:a0435a630c5d | 67 | if(motorOn == true){ |
tkstreet | 1:4d4c77589134 | 68 | motor->write(dutyCycle); // If motor is on, keep it on |
tkstreet | 0:a0435a630c5d | 69 | } |
tkstreet | 0:a0435a630c5d | 70 | } |
tkstreet | 0:a0435a630c5d | 71 | else{ |
tkstreet | 0:a0435a630c5d | 72 | dutyCycle = 0.0; |
tkstreet | 0:a0435a630c5d | 73 | |
tkstreet | 0:a0435a630c5d | 74 | return ERROR_DUTY_CYCLE; |
tkstreet | 0:a0435a630c5d | 75 | } |
tkstreet | 0:a0435a630c5d | 76 | |
tkstreet | 0:a0435a630c5d | 77 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 78 | } |
tkstreet | 0:a0435a630c5d | 79 | |
tkstreet | 0:a0435a630c5d | 80 | |
tkstreet | 3:f8e70f639ed0 | 81 | float AP1017::getSpeed(void) |
tkstreet | 0:a0435a630c5d | 82 | { |
tkstreet | 3:f8e70f639ed0 | 83 | return dutyCycle*100.0; |
tkstreet | 0:a0435a630c5d | 84 | } |
tkstreet | 0:a0435a630c5d | 85 | |
tkstreet | 0:a0435a630c5d | 86 | |
tkstreet | 3:f8e70f639ed0 | 87 | AP1017::Status AP1017::start(void) |
tkstreet | 0:a0435a630c5d | 88 | { |
tkstreet | 3:f8e70f639ed0 | 89 | motor->write(dutyCycle); // Set duty cycle, start motor |
tkstreet | 0:a0435a630c5d | 90 | motorOn = true; // Set ON flag |
tkstreet | 0:a0435a630c5d | 91 | |
tkstreet | 0:a0435a630c5d | 92 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 93 | } |
tkstreet | 0:a0435a630c5d | 94 | |
tkstreet | 0:a0435a630c5d | 95 | |
tkstreet | 3:f8e70f639ed0 | 96 | AP1017::Status AP1017::stop(void) |
tkstreet | 0:a0435a630c5d | 97 | { |
tkstreet | 3:f8e70f639ed0 | 98 | motor->write(0.0); // turn motor off (duty cycle saved) |
tkstreet | 0:a0435a630c5d | 99 | motorOn = false; // Set OFF flag |
tkstreet | 0:a0435a630c5d | 100 | |
tkstreet | 0:a0435a630c5d | 101 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 102 | } |
tkstreet | 0:a0435a630c5d | 103 | |
tkstreet | 3:f8e70f639ed0 | 104 | AP1017::Status AP1017::brake(void) |
tkstreet | 0:a0435a630c5d | 105 | { |
tkstreet | 0:a0435a630c5d | 106 | setDirection(DIRECTION_BRAKE); |
tkstreet | 0:a0435a630c5d | 107 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 108 | } |
tkstreet | 0:a0435a630c5d | 109 | |
tkstreet | 3:f8e70f639ed0 | 110 | AP1017::Status AP1017::coast(void) |
tkstreet | 0:a0435a630c5d | 111 | { |
tkstreet | 0:a0435a630c5d | 112 | setDirection(DIRECTION_COAST); |
tkstreet | 0:a0435a630c5d | 113 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 114 | } |