AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
AP1017.cpp
- Committer:
- tkstreet
- Date:
- 2017-05-19
- Revision:
- 9:1ca7d16de1c4
- Parent:
- 8:bc70a421ef4c
- Child:
- 10:16d45e3f4be3
File content as of revision 9:1ca7d16de1c4:
#include "AP1017.h" #include "debug.h" /******************** Constructors & Destructors ****************************/ // Default constructor AP1017::AP1017(DigitalOut* A, DigitalOut* B, I2C* M) : motorOn(false), dutyCycle(0.0) { i2cMotor = M; inA = A; inB = B; // 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, i2cMotor; } /*********************** Member Functions ***********************************/ AP1017::Status AP1017::setDirection(AP1017::Rotation dir) { direction = dir; switch(direction){ case DIRECTION_CW: // direction = 0x00 if(isMotorOn()) { MSG("#AP1017.cpp: Error. Cannot change direction while motor is running.\r\n"); return ERROR_MOTORON; }else { inA->write(1); inB->write(0); MSG("#AP1017.cpp: Direction: CCW\r\n"); } break; case DIRECTION_CCW: // direction = 0x01 if(isMotorOn()) { MSG("#AP1017.cpp: Error. Cannot change direction while motor is running.\r\n"); return ERROR_MOTORON; }else { inA->write(0); inB->write(1); MSG("#AP1017.cpp: Direction: CW\r\n"); } break; case DIRECTION_BRAKE: // direction = 0x03 inA->write(1); inB->write(1); MSG("#AP1017.cpp: Direction: Brake\r\n"); break; case DIRECTION_COAST: // direction = 0x04 inA->write(0); inB->write(0); motorOn = false; MSG("#AP1017.cpp: Direction: Coast\r\n"); break; default: return ERROR_DIRECTION; } return SUCCESS; } AP1017::Rotation AP1017::getDirection(void) { return direction; } AP1017::Status AP1017::setSpeed(float dc) { if((dc <= 100.0) && (dc >= 0.0)) { dutyCycle = dc/100.0; if(motorOn == true){ MSG("#AP1017.cpp: Changed running motor speed.\r\n"); } } else { dutyCycle = 0.0; return ERROR_DUTY_CYCLE; } return SUCCESS; } float AP1017::getSpeed(void) { return dutyCycle*100.0; } AP1017::Status AP1017::start(void) { motor->setPortLevel(TCA9554A::PORT_0, TCA9554A::HIGH); // set RSV high motorOn = true; // Set ON flag return SUCCESS; } AP1017::Status AP1017::stop(void) { motor->setPortLevel(TCA9554A::PORT_0, TCA9554A::LOW); // set RSV low motorOn = false; // Set OFF flag return SUCCESS; } AP1017::Status AP1017::brake(void) { setDirection(DIRECTION_BRAKE); return SUCCESS; } AP1017::Status AP1017::coast(void) { setDirection(DIRECTION_COAST); return SUCCESS; } bool AP1017::isMotorOn(void) { return motorOn; }