Motor driver library for the AP1017.
AKM Development Platform
AP1017 Motor Driver
Import libraryAP1017
Motor driver library for the AP1017.
Diff: AP1017.cpp
- Revision:
- 6:d4d3bc82d446
- Parent:
- 5:2da4339149a6
- Child:
- 8:bc70a421ef4c
--- a/AP1017.cpp Fri Apr 21 20:43:25 2017 +0000 +++ b/AP1017.cpp Fri Apr 21 23:22:06 2017 +0000 @@ -1,21 +1,36 @@ #include "AP1017.h" #include "debug.h" +#define I2C_SPEED 400000 + /******************** Constructors & Destructors ****************************/ // Default constructor AP1017::AP1017(void) : motorOn(false), dutyCycle(0.0) { - motor = new PwmOut(P0_10); + //motor = new PwmOut(P0_10); inA = new DigitalOut(P0_11); inB = new DigitalOut(P0_9); + + // Instantiate I2C + i2cMotor = new I2C(I2C_SDA0, I2C_SCL0); + i2cMotor->frequency(I2C_SPEED); + + // Instantiate TCA9554A + motor = new TCA9554A(i2cMotor, TCA9554A::SLAVE_ADDRESS_38H); // 38H->Port 0->RSV + + // Initialize RSV as output + if (motor->configurePort(TCA9554A::PORT_0, TCA9554A::DIR_OUTPUT)!= TCA9554A::SUCCESS) { + MSG("#AP1017: Error configuring TCA9554A port.\r\n"); + } + motor->setPortLevel(TCA9554A::PORT_0, TCA9554A::LOW); // Turn motor off } // Default destructor AP1017::~AP1017(void) { stop(); - delete inA, inB, motor; + delete inA, inB, motor, i2cMotor; } /*********************** Member Functions ***********************************/ @@ -81,7 +96,7 @@ MSG("#AP1017.cpp: dutyCycle = %.1f%%\r\n", dc); if(motorOn == true){ - motor->write(dutyCycle); // If motor is on, keep it on + //TODO MSG("#AP1017.cpp: Changed running motor speed.\r\n"); } } @@ -104,7 +119,7 @@ AP1017::Status AP1017::start(void) { - motor->write(dutyCycle); // Set duty cycle, start motor + motor->setPortLevel(TCA9554A::PORT_0, TCA9554A::HIGH); // set RSV high MSG("#AP1017.cpp: Motor: ON\r\n"); motorOn = true; // Set ON flag @@ -114,7 +129,7 @@ AP1017::Status AP1017::stop(void) { - motor->write(0.0); // turn motor off (duty cycle saved) + motor->setPortLevel(TCA9554A::PORT_0, TCA9554A::LOW); // set RSV low MSG("#AP1017.cpp: Motor: OFF\r\n"); motorOn = false; // Set OFF flag