AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
AP1017.cpp@10:16d45e3f4be3, 2017-11-08 (annotated)
- Committer:
- tkstreet
- Date:
- Wed Nov 08 21:37:24 2017 +0000
- Revision:
- 10:16d45e3f4be3
- Parent:
- 9:1ca7d16de1c4
- Child:
- 11:ac867fa4aa10
Removed hard pin definitions. Modified debug messages. Added info about unimplemented speed feature.
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 | 9:1ca7d16de1c4 | 6 | AP1017::AP1017(DigitalOut* A, DigitalOut* B, I2C* M) : motorOn(false), dutyCycle(0.0) |
tkstreet | 0:a0435a630c5d | 7 | { |
tkstreet | 9:1ca7d16de1c4 | 8 | i2cMotor = M; |
tkstreet | 9:1ca7d16de1c4 | 9 | inA = A; |
tkstreet | 9:1ca7d16de1c4 | 10 | inB = B; |
tkstreet | 9:1ca7d16de1c4 | 11 | |
tkstreet | 6:d4d3bc82d446 | 12 | // Instantiate TCA9554A |
tkstreet | 6:d4d3bc82d446 | 13 | motor = new TCA9554A(i2cMotor, TCA9554A::SLAVE_ADDRESS_38H); // 38H->Port 0->RSV |
tkstreet | 6:d4d3bc82d446 | 14 | |
tkstreet | 6:d4d3bc82d446 | 15 | // Initialize RSV as output |
tkstreet | 6:d4d3bc82d446 | 16 | if (motor->configurePort(TCA9554A::PORT_0, TCA9554A::DIR_OUTPUT)!= TCA9554A::SUCCESS) { |
tkstreet | 6:d4d3bc82d446 | 17 | MSG("#AP1017: Error configuring TCA9554A port.\r\n"); |
tkstreet | 6:d4d3bc82d446 | 18 | } |
tkstreet | 6:d4d3bc82d446 | 19 | motor->setPortLevel(TCA9554A::PORT_0, TCA9554A::LOW); // Turn motor off |
tkstreet | 0:a0435a630c5d | 20 | } |
tkstreet | 0:a0435a630c5d | 21 | |
tkstreet | 0:a0435a630c5d | 22 | // Default destructor |
tkstreet | 0:a0435a630c5d | 23 | AP1017::~AP1017(void) |
tkstreet | 0:a0435a630c5d | 24 | { |
tkstreet | 3:f8e70f639ed0 | 25 | stop(); |
tkstreet | 6:d4d3bc82d446 | 26 | delete inA, inB, motor, i2cMotor; |
tkstreet | 0:a0435a630c5d | 27 | } |
tkstreet | 0:a0435a630c5d | 28 | |
tkstreet | 0:a0435a630c5d | 29 | /*********************** Member Functions ***********************************/ |
tkstreet | 0:a0435a630c5d | 30 | |
tkstreet | 4:c36159701cde | 31 | AP1017::Status AP1017::setDirection(AP1017::Rotation dir) |
tkstreet | 0:a0435a630c5d | 32 | { |
tkstreet | 3:f8e70f639ed0 | 33 | direction = dir; |
tkstreet | 4:c36159701cde | 34 | |
tkstreet | 3:f8e70f639ed0 | 35 | switch(direction){ |
tkstreet | 4:c36159701cde | 36 | case DIRECTION_CW: // direction = 0x00 |
tkstreet | 4:c36159701cde | 37 | if(isMotorOn()) |
tkstreet | 4:c36159701cde | 38 | { |
tkstreet | 10:16d45e3f4be3 | 39 | MSG("#Error: Cannot change direction while motor is running.\r\n"); |
tkstreet | 0:a0435a630c5d | 40 | return ERROR_MOTORON; |
tkstreet | 4:c36159701cde | 41 | }else |
tkstreet | 4:c36159701cde | 42 | { |
tkstreet | 4:c36159701cde | 43 | inA->write(1); |
tkstreet | 4:c36159701cde | 44 | inB->write(0); |
tkstreet | 10:16d45e3f4be3 | 45 | MSG("#Direction: CCW\r\n"); |
tkstreet | 4:c36159701cde | 46 | } |
tkstreet | 0:a0435a630c5d | 47 | break; |
tkstreet | 4:c36159701cde | 48 | case DIRECTION_CCW: // direction = 0x01 |
tkstreet | 4:c36159701cde | 49 | if(isMotorOn()) |
tkstreet | 4:c36159701cde | 50 | { |
tkstreet | 10:16d45e3f4be3 | 51 | MSG("#Error: Cannot change direction while motor is running.\r\n"); |
tkstreet | 0:a0435a630c5d | 52 | return ERROR_MOTORON; |
tkstreet | 4:c36159701cde | 53 | }else |
tkstreet | 4:c36159701cde | 54 | { |
tkstreet | 4:c36159701cde | 55 | inA->write(0); |
tkstreet | 4:c36159701cde | 56 | inB->write(1); |
tkstreet | 10:16d45e3f4be3 | 57 | MSG("#Direction: CW\r\n"); |
tkstreet | 4:c36159701cde | 58 | } |
tkstreet | 0:a0435a630c5d | 59 | break; |
tkstreet | 4:c36159701cde | 60 | case DIRECTION_BRAKE: // direction = 0x03 |
tkstreet | 1:4d4c77589134 | 61 | inA->write(1); |
tkstreet | 1:4d4c77589134 | 62 | inB->write(1); |
tkstreet | 10:16d45e3f4be3 | 63 | MSG("#Direction: Brake\r\n"); |
tkstreet | 0:a0435a630c5d | 64 | break; |
tkstreet | 4:c36159701cde | 65 | case DIRECTION_COAST: // direction = 0x04 |
tkstreet | 1:4d4c77589134 | 66 | inA->write(0); |
tkstreet | 1:4d4c77589134 | 67 | inB->write(0); |
tkstreet | 0:a0435a630c5d | 68 | motorOn = false; |
tkstreet | 10:16d45e3f4be3 | 69 | MSG("#Direction: Coast\r\n"); |
tkstreet | 0:a0435a630c5d | 70 | break; |
tkstreet | 0:a0435a630c5d | 71 | default: |
tkstreet | 0:a0435a630c5d | 72 | return ERROR_DIRECTION; |
tkstreet | 0:a0435a630c5d | 73 | } |
tkstreet | 0:a0435a630c5d | 74 | |
tkstreet | 0:a0435a630c5d | 75 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 76 | } |
tkstreet | 0:a0435a630c5d | 77 | |
tkstreet | 3:f8e70f639ed0 | 78 | |
tkstreet | 3:f8e70f639ed0 | 79 | AP1017::Rotation AP1017::getDirection(void) |
tkstreet | 0:a0435a630c5d | 80 | { |
tkstreet | 10:16d45e3f4be3 | 81 | MSG("#Direction: "); |
tkstreet | 10:16d45e3f4be3 | 82 | switch(direction){ |
tkstreet | 10:16d45e3f4be3 | 83 | case DIRECTION_CW: |
tkstreet | 10:16d45e3f4be3 | 84 | MSG("CW\r\n"); |
tkstreet | 10:16d45e3f4be3 | 85 | break; |
tkstreet | 10:16d45e3f4be3 | 86 | case DIRECTION_CCW: |
tkstreet | 10:16d45e3f4be3 | 87 | MSG("CCW\r\n"); |
tkstreet | 10:16d45e3f4be3 | 88 | break; |
tkstreet | 10:16d45e3f4be3 | 89 | case DIRECTION_COAST: |
tkstreet | 10:16d45e3f4be3 | 90 | MSG("Coast\r\n"); |
tkstreet | 10:16d45e3f4be3 | 91 | break; |
tkstreet | 10:16d45e3f4be3 | 92 | case DIRECTION_BRAKE: |
tkstreet | 10:16d45e3f4be3 | 93 | MSG("Brake\r\n"); |
tkstreet | 10:16d45e3f4be3 | 94 | break; |
tkstreet | 10:16d45e3f4be3 | 95 | default: |
tkstreet | 10:16d45e3f4be3 | 96 | MSG("Error: Invalid direction\r\n"); |
tkstreet | 10:16d45e3f4be3 | 97 | } |
tkstreet | 3:f8e70f639ed0 | 98 | return direction; |
tkstreet | 3:f8e70f639ed0 | 99 | } |
tkstreet | 3:f8e70f639ed0 | 100 | |
tkstreet | 0:a0435a630c5d | 101 | |
tkstreet | 3:f8e70f639ed0 | 102 | AP1017::Status AP1017::setSpeed(float dc) |
tkstreet | 3:f8e70f639ed0 | 103 | { |
tkstreet | 9:1ca7d16de1c4 | 104 | if((dc <= 100.0) && (dc >= 0.0)) |
tkstreet | 9:1ca7d16de1c4 | 105 | { |
tkstreet | 3:f8e70f639ed0 | 106 | dutyCycle = dc/100.0; |
tkstreet | 0:a0435a630c5d | 107 | |
tkstreet | 0:a0435a630c5d | 108 | if(motorOn == true){ |
tkstreet | 10:16d45e3f4be3 | 109 | MSG("Speed setting not yet implemented.\r\n"); |
tkstreet | 10:16d45e3f4be3 | 110 | //MSG("#Changed running motor speed: %.1f%%.\r\n", dc); |
tkstreet | 0:a0435a630c5d | 111 | } |
tkstreet | 0:a0435a630c5d | 112 | } |
tkstreet | 9:1ca7d16de1c4 | 113 | else |
tkstreet | 9:1ca7d16de1c4 | 114 | { |
tkstreet | 0:a0435a630c5d | 115 | dutyCycle = 0.0; |
tkstreet | 0:a0435a630c5d | 116 | return ERROR_DUTY_CYCLE; |
tkstreet | 0:a0435a630c5d | 117 | } |
tkstreet | 0:a0435a630c5d | 118 | |
tkstreet | 0:a0435a630c5d | 119 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 120 | } |
tkstreet | 0:a0435a630c5d | 121 | |
tkstreet | 0:a0435a630c5d | 122 | |
tkstreet | 3:f8e70f639ed0 | 123 | float AP1017::getSpeed(void) |
tkstreet | 0:a0435a630c5d | 124 | { |
tkstreet | 10:16d45e3f4be3 | 125 | MSG("Speed: %.1f%%\r\n.", dutyCycle*100.0); |
tkstreet | 3:f8e70f639ed0 | 126 | return dutyCycle*100.0; |
tkstreet | 0:a0435a630c5d | 127 | } |
tkstreet | 0:a0435a630c5d | 128 | |
tkstreet | 0:a0435a630c5d | 129 | |
tkstreet | 3:f8e70f639ed0 | 130 | AP1017::Status AP1017::start(void) |
tkstreet | 0:a0435a630c5d | 131 | { |
tkstreet | 6:d4d3bc82d446 | 132 | motor->setPortLevel(TCA9554A::PORT_0, TCA9554A::HIGH); // set RSV high |
tkstreet | 0:a0435a630c5d | 133 | motorOn = true; // Set ON flag |
tkstreet | 0:a0435a630c5d | 134 | |
tkstreet | 0:a0435a630c5d | 135 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 136 | } |
tkstreet | 0:a0435a630c5d | 137 | |
tkstreet | 0:a0435a630c5d | 138 | |
tkstreet | 3:f8e70f639ed0 | 139 | AP1017::Status AP1017::stop(void) |
tkstreet | 0:a0435a630c5d | 140 | { |
tkstreet | 6:d4d3bc82d446 | 141 | motor->setPortLevel(TCA9554A::PORT_0, TCA9554A::LOW); // set RSV low |
tkstreet | 0:a0435a630c5d | 142 | motorOn = false; // Set OFF flag |
tkstreet | 0:a0435a630c5d | 143 | |
tkstreet | 0:a0435a630c5d | 144 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 145 | } |
tkstreet | 0:a0435a630c5d | 146 | |
tkstreet | 3:f8e70f639ed0 | 147 | AP1017::Status AP1017::brake(void) |
tkstreet | 0:a0435a630c5d | 148 | { |
tkstreet | 0:a0435a630c5d | 149 | setDirection(DIRECTION_BRAKE); |
tkstreet | 0:a0435a630c5d | 150 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 151 | } |
tkstreet | 0:a0435a630c5d | 152 | |
tkstreet | 3:f8e70f639ed0 | 153 | AP1017::Status AP1017::coast(void) |
tkstreet | 0:a0435a630c5d | 154 | { |
tkstreet | 0:a0435a630c5d | 155 | setDirection(DIRECTION_COAST); |
tkstreet | 0:a0435a630c5d | 156 | return SUCCESS; |
tkstreet | 4:c36159701cde | 157 | } |
tkstreet | 4:c36159701cde | 158 | |
tkstreet | 4:c36159701cde | 159 | bool AP1017::isMotorOn(void) |
tkstreet | 4:c36159701cde | 160 | { |
tkstreet | 4:c36159701cde | 161 | return motorOn; |
tkstreet | 0:a0435a630c5d | 162 | } |