AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
AP1017.cpp
- Committer:
- tkstreet
- Date:
- 2017-04-21
- Revision:
- 5:2da4339149a6
- Parent:
- 4:c36159701cde
- Child:
- 6:d4d3bc82d446
File content as of revision 5:2da4339149a6:
#include "AP1017.h" #include "debug.h" /******************** Constructors & Destructors ****************************/ // Default constructor AP1017::AP1017(void) : motorOn(false), dutyCycle(0.0) { motor = new PwmOut(P0_10); inA = new DigitalOut(P0_11); inB = new DigitalOut(P0_9); } // Default destructor AP1017::~AP1017(void) { stop(); delete inA, inB, motor; } /*********************** 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; MSG("#AP1017.cpp: dutyCycle = %.1f%%\r\n", dc); if(motorOn == true){ motor->write(dutyCycle); // If motor is on, keep it on MSG("#AP1017.cpp: Changed running motor speed.\r\n"); } } else{ dutyCycle = 0.0; return ERROR_DUTY_CYCLE; } return SUCCESS; } float AP1017::getSpeed(void) { MSG("#AP1017.cpp: Speed = %.1f%%\r\n", dutyCycle); return dutyCycle*100.0; } AP1017::Status AP1017::start(void) { motor->write(dutyCycle); // Set duty cycle, start motor MSG("#AP1017.cpp: Motor: ON\r\n"); motorOn = true; // Set ON flag return SUCCESS; } AP1017::Status AP1017::stop(void) { motor->write(0.0); // turn motor off (duty cycle saved) MSG("#AP1017.cpp: Motor: OFF\r\n"); motorOn = false; // Set OFF flag return SUCCESS; } AP1017::Status AP1017::brake(void) { setDirection(DIRECTION_BRAKE); MSG("#AP1017.cpp: Motor: BRAKE\r\n"); return SUCCESS; } AP1017::Status AP1017::coast(void) { setDirection(DIRECTION_COAST); MSG("#AP1017.cpp: Motor: COAST\r\n"); return SUCCESS; } bool AP1017::isMotorOn(void) { return motorOn; }