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