AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
AP1017.cpp@4:c36159701cde, 2017-04-21 (annotated)
- Committer:
- tkstreet
- Date:
- Fri Apr 21 20:18:32 2017 +0000
- Revision:
- 4:c36159701cde
- Parent:
- 3:f8e70f639ed0
- Child:
- 5:2da4339149a6
Debugged motor functions. Added debugging serial messages. Added isMotorOn() function.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tkstreet | 0:a0435a630c5d | 1 | #include "AP1017.h" |
tkstreet | 4:c36159701cde | 2 | #include "debug.h" |
tkstreet | 0:a0435a630c5d | 3 | |
tkstreet | 0:a0435a630c5d | 4 | /******************** Constructors & Destructors ****************************/ |
tkstreet | 0:a0435a630c5d | 5 | |
tkstreet | 0:a0435a630c5d | 6 | // Default constructor |
tkstreet | 3:f8e70f639ed0 | 7 | AP1017::AP1017(void) : motorOn(false), dutyCycle(0.0) |
tkstreet | 0:a0435a630c5d | 8 | { |
tkstreet | 3:f8e70f639ed0 | 9 | motor = new PwmOut(P0_10); |
tkstreet | 3:f8e70f639ed0 | 10 | inA = new DigitalOut(P0_11); |
tkstreet | 3:f8e70f639ed0 | 11 | inB = new DigitalOut(P0_10); |
tkstreet | 0:a0435a630c5d | 12 | } |
tkstreet | 0:a0435a630c5d | 13 | |
tkstreet | 0:a0435a630c5d | 14 | // Default destructor |
tkstreet | 0:a0435a630c5d | 15 | AP1017::~AP1017(void) |
tkstreet | 0:a0435a630c5d | 16 | { |
tkstreet | 3:f8e70f639ed0 | 17 | stop(); |
tkstreet | 1:4d4c77589134 | 18 | delete inA, inB, motor; |
tkstreet | 0:a0435a630c5d | 19 | } |
tkstreet | 0:a0435a630c5d | 20 | |
tkstreet | 0:a0435a630c5d | 21 | /*********************** Member Functions ***********************************/ |
tkstreet | 0:a0435a630c5d | 22 | |
tkstreet | 4:c36159701cde | 23 | AP1017::Status AP1017::setDirection(AP1017::Rotation dir) |
tkstreet | 0:a0435a630c5d | 24 | { |
tkstreet | 3:f8e70f639ed0 | 25 | direction = dir; |
tkstreet | 4:c36159701cde | 26 | |
tkstreet | 3:f8e70f639ed0 | 27 | switch(direction){ |
tkstreet | 4:c36159701cde | 28 | case DIRECTION_CW: // direction = 0x00 |
tkstreet | 4:c36159701cde | 29 | if(isMotorOn()) |
tkstreet | 4:c36159701cde | 30 | { |
tkstreet | 4:c36159701cde | 31 | MSG("#AP1017.cpp: Error. Cannot change direction while motor is running.\r\n"); |
tkstreet | 0:a0435a630c5d | 32 | return ERROR_MOTORON; |
tkstreet | 4:c36159701cde | 33 | }else |
tkstreet | 4:c36159701cde | 34 | { |
tkstreet | 4:c36159701cde | 35 | inA->write(1); |
tkstreet | 4:c36159701cde | 36 | inB->write(0); |
tkstreet | 4:c36159701cde | 37 | MSG("#AP1017.cpp: Direction: CCW\r\n"); |
tkstreet | 4:c36159701cde | 38 | } |
tkstreet | 0:a0435a630c5d | 39 | break; |
tkstreet | 4:c36159701cde | 40 | case DIRECTION_CCW: // direction = 0x01 |
tkstreet | 4:c36159701cde | 41 | if(isMotorOn()) |
tkstreet | 4:c36159701cde | 42 | { |
tkstreet | 4:c36159701cde | 43 | MSG("#AP1017.cpp: Error. Cannot change direction while motor is running.\r\n"); |
tkstreet | 0:a0435a630c5d | 44 | return ERROR_MOTORON; |
tkstreet | 4:c36159701cde | 45 | }else |
tkstreet | 4:c36159701cde | 46 | { |
tkstreet | 4:c36159701cde | 47 | inA->write(0); |
tkstreet | 4:c36159701cde | 48 | inB->write(1); |
tkstreet | 4:c36159701cde | 49 | MSG("#AP1017.cpp: Direction: CW\r\n"); |
tkstreet | 4:c36159701cde | 50 | } |
tkstreet | 0:a0435a630c5d | 51 | break; |
tkstreet | 4:c36159701cde | 52 | case DIRECTION_BRAKE: // direction = 0x03 |
tkstreet | 1:4d4c77589134 | 53 | inA->write(1); |
tkstreet | 1:4d4c77589134 | 54 | inB->write(1); |
tkstreet | 4:c36159701cde | 55 | MSG("#AP1017.cpp: Direction: Brake\r\n"); |
tkstreet | 0:a0435a630c5d | 56 | break; |
tkstreet | 4:c36159701cde | 57 | case DIRECTION_COAST: // direction = 0x04 |
tkstreet | 1:4d4c77589134 | 58 | inA->write(0); |
tkstreet | 1:4d4c77589134 | 59 | inB->write(0); |
tkstreet | 0:a0435a630c5d | 60 | motorOn = false; |
tkstreet | 4:c36159701cde | 61 | MSG("#AP1017.cpp: Direction: Coast\r\n"); |
tkstreet | 0:a0435a630c5d | 62 | break; |
tkstreet | 0:a0435a630c5d | 63 | default: |
tkstreet | 0:a0435a630c5d | 64 | return ERROR_DIRECTION; |
tkstreet | 0:a0435a630c5d | 65 | } |
tkstreet | 0:a0435a630c5d | 66 | |
tkstreet | 0:a0435a630c5d | 67 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 68 | } |
tkstreet | 0:a0435a630c5d | 69 | |
tkstreet | 3:f8e70f639ed0 | 70 | |
tkstreet | 3:f8e70f639ed0 | 71 | AP1017::Rotation AP1017::getDirection(void) |
tkstreet | 0:a0435a630c5d | 72 | { |
tkstreet | 3:f8e70f639ed0 | 73 | return direction; |
tkstreet | 3:f8e70f639ed0 | 74 | } |
tkstreet | 3:f8e70f639ed0 | 75 | |
tkstreet | 0:a0435a630c5d | 76 | |
tkstreet | 3:f8e70f639ed0 | 77 | AP1017::Status AP1017::setSpeed(float dc) |
tkstreet | 3:f8e70f639ed0 | 78 | { |
tkstreet | 4:c36159701cde | 79 | if((dc <= 100.0) && (dc >= 0.0)){ |
tkstreet | 3:f8e70f639ed0 | 80 | dutyCycle = dc/100.0; |
tkstreet | 4:c36159701cde | 81 | MSG("#AP1017.cpp: dutyCycle = %.1f%%\r\n", dc); |
tkstreet | 0:a0435a630c5d | 82 | |
tkstreet | 0:a0435a630c5d | 83 | if(motorOn == true){ |
tkstreet | 1:4d4c77589134 | 84 | motor->write(dutyCycle); // If motor is on, keep it on |
tkstreet | 4:c36159701cde | 85 | MSG("#AP1017.cpp: Changed running motor speed.\r\n"); |
tkstreet | 0:a0435a630c5d | 86 | } |
tkstreet | 0:a0435a630c5d | 87 | } |
tkstreet | 0:a0435a630c5d | 88 | else{ |
tkstreet | 0:a0435a630c5d | 89 | dutyCycle = 0.0; |
tkstreet | 0:a0435a630c5d | 90 | |
tkstreet | 0:a0435a630c5d | 91 | return ERROR_DUTY_CYCLE; |
tkstreet | 0:a0435a630c5d | 92 | } |
tkstreet | 0:a0435a630c5d | 93 | |
tkstreet | 0:a0435a630c5d | 94 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 95 | } |
tkstreet | 0:a0435a630c5d | 96 | |
tkstreet | 0:a0435a630c5d | 97 | |
tkstreet | 3:f8e70f639ed0 | 98 | float AP1017::getSpeed(void) |
tkstreet | 0:a0435a630c5d | 99 | { |
tkstreet | 4:c36159701cde | 100 | MSG("#AP1017.cpp: Speed = %.1f%%\r\n", dutyCycle); |
tkstreet | 3:f8e70f639ed0 | 101 | return dutyCycle*100.0; |
tkstreet | 0:a0435a630c5d | 102 | } |
tkstreet | 0:a0435a630c5d | 103 | |
tkstreet | 0:a0435a630c5d | 104 | |
tkstreet | 3:f8e70f639ed0 | 105 | AP1017::Status AP1017::start(void) |
tkstreet | 0:a0435a630c5d | 106 | { |
tkstreet | 3:f8e70f639ed0 | 107 | motor->write(dutyCycle); // Set duty cycle, start motor |
tkstreet | 4:c36159701cde | 108 | MSG("#AP1017.cpp: Motor: ON\r\n"); |
tkstreet | 0:a0435a630c5d | 109 | motorOn = true; // Set ON flag |
tkstreet | 0:a0435a630c5d | 110 | |
tkstreet | 0:a0435a630c5d | 111 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 112 | } |
tkstreet | 0:a0435a630c5d | 113 | |
tkstreet | 0:a0435a630c5d | 114 | |
tkstreet | 3:f8e70f639ed0 | 115 | AP1017::Status AP1017::stop(void) |
tkstreet | 0:a0435a630c5d | 116 | { |
tkstreet | 3:f8e70f639ed0 | 117 | motor->write(0.0); // turn motor off (duty cycle saved) |
tkstreet | 4:c36159701cde | 118 | MSG("#AP1017.cpp: Motor: OFF\r\n"); |
tkstreet | 0:a0435a630c5d | 119 | motorOn = false; // Set OFF flag |
tkstreet | 0:a0435a630c5d | 120 | |
tkstreet | 0:a0435a630c5d | 121 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 122 | } |
tkstreet | 0:a0435a630c5d | 123 | |
tkstreet | 3:f8e70f639ed0 | 124 | AP1017::Status AP1017::brake(void) |
tkstreet | 0:a0435a630c5d | 125 | { |
tkstreet | 0:a0435a630c5d | 126 | setDirection(DIRECTION_BRAKE); |
tkstreet | 4:c36159701cde | 127 | MSG("#AP1017.cpp: Motor: BRAKE\r\n"); |
tkstreet | 0:a0435a630c5d | 128 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 129 | } |
tkstreet | 0:a0435a630c5d | 130 | |
tkstreet | 3:f8e70f639ed0 | 131 | AP1017::Status AP1017::coast(void) |
tkstreet | 0:a0435a630c5d | 132 | { |
tkstreet | 0:a0435a630c5d | 133 | setDirection(DIRECTION_COAST); |
tkstreet | 4:c36159701cde | 134 | MSG("#AP1017.cpp: Motor: COAST\r\n"); |
tkstreet | 0:a0435a630c5d | 135 | return SUCCESS; |
tkstreet | 4:c36159701cde | 136 | } |
tkstreet | 4:c36159701cde | 137 | |
tkstreet | 4:c36159701cde | 138 | bool AP1017::isMotorOn(void) |
tkstreet | 4:c36159701cde | 139 | { |
tkstreet | 4:c36159701cde | 140 | return motorOn; |
tkstreet | 0:a0435a630c5d | 141 | } |