AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
Diff: AP1017.cpp
- Revision:
- 3:f8e70f639ed0
- Parent:
- 1:4d4c77589134
- Child:
- 4:c36159701cde
--- a/AP1017.cpp Mon Apr 17 19:17:25 2017 +0000 +++ b/AP1017.cpp Wed Apr 19 23:13:04 2017 +0000 @@ -3,45 +3,42 @@ /******************** Constructors & Destructors ****************************/ // Default constructor -AP1017::AP1017(void) : motorOn(false), dutyCycle(0.0), freq_hz(0.0), - pulseWidth_us(0.0), pwmPeriod_us(0.0) +AP1017::AP1017(void) : motorOn(false), dutyCycle(0.0) { - motor = NULL; - inA = NULL; - inB = NULL; + motor = new PwmOut(P0_10); + inA = new DigitalOut(P0_11); + inB = new DigitalOut(P0_10); } // Default destructor AP1017::~AP1017(void) { - stopMotor(); + stop(); delete inA, inB, motor; } /*********************** Member Functions ***********************************/ -AP1017::Status AP1017::setDirection(char dir) +AP1017::Status AP1017::setDirection(Rotation dir) { + direction = dir; - switch(dir){ + switch(direction){ case DIRECTION_CW: if(motorOn == true && dir == DIRECTION_CCW) return ERROR_MOTORON; inA->write(1); inB->write(0); - motorOn = true; break; case DIRECTION_CCW: if(motorOn == true && dir == DIRECTION_CW) return ERROR_MOTORON; inA->write(0); inB->write(1); - motorOn = true; break; case DIRECTION_BRAKE: inA->write(1); inB->write(1); - motorOn = true; break; case DIRECTION_COAST: inA->write(0); @@ -55,21 +52,24 @@ return SUCCESS; } -AP1017::Status AP1017::setDutyCycle(float dc) + +AP1017::Rotation AP1017::getDirection(void) { - if((dutyCycle <= 1.00) && (dutyCycle >= 0.0)){ - dutyCycle = dc; + return direction; +} + - pulseWidth_us = dutyCycle * pwmPeriod_us; // Override pulse width +AP1017::Status AP1017::setSpeed(float dc) +{ + if((dutyCycle <= 100.0) && (dutyCycle >= 0.0)){ + dutyCycle = dc/100.0; if(motorOn == true){ motor->write(dutyCycle); // If motor is on, keep it on } - } else{ dutyCycle = 0.0; - pulseWidth_us = 0.0; return ERROR_DUTY_CYCLE; } @@ -78,76 +78,36 @@ } -AP1017::Status AP1017::setFrequency(float freq) +float AP1017::getSpeed(void) { - if(freq > MAX_FREQUENCY){ - return ERROR_FREQUENCY; - } - - freq_hz = freq; - pwmPeriod_us = (1/freq)/0.000001; // Calculate the period - - return SUCCESS; + return dutyCycle*100.0; } -AP1017::Status AP1017::setPeriod_us(float per) +AP1017::Status AP1017::start(void) { - if(per < 0){ - return ERROR_PERIOD; - } - - pwmPeriod_us = per; - - freq_hz = 1/(per * 0.000001); // Calculate the frequency - - if(motorOn == true){ // If motor on, keep it on - motor->period_us(pwmPeriod_us); - } - - return SUCCESS; -} - - - AP1017::Status AP1017::setPulseWidth_us(float pw) - { - if((pw <= 0.0) || (pw > pwmPeriod_us)){ - return ERROR_PULSEWIDTH; - } - - dutyCycle = pw/pwmPeriod_us; // Override duty cycle - pulseWidth_us = pw; - - return SUCCESS; - } - - -AP1017::Status AP1017::startMotor(void) -{ - motor->period_us(pwmPeriod_us); // Set the period - motor->pulsewidth_us(pulseWidth_us); // Set the pulse width - motor->write(dutyCycle); // Set duty cycle, start motor + motor->write(dutyCycle); // Set duty cycle, start motor motorOn = true; // Set ON flag return SUCCESS; } -AP1017::Status AP1017::stopMotor(void) +AP1017::Status AP1017::stop(void) { - motor->write(0.0); // turn motor off (duty cycle saved) + motor->write(0.0); // turn motor off (duty cycle saved) motorOn = false; // Set OFF flag return SUCCESS; } -AP1017::Status AP1017::brakeMotor(void) +AP1017::Status AP1017::brake(void) { setDirection(DIRECTION_BRAKE); return SUCCESS; } -AP1017::Status AP1017::coastMotor(void) +AP1017::Status AP1017::coast(void) { setDirection(DIRECTION_COAST); return SUCCESS;