AP1017 library for the Rev.E hardware with expanded capabilities.

Fork of AP1017 by AKM Development Platform

AP1017.cpp

Committer:
tkstreet
Date:
2017-04-19
Revision:
3:f8e70f639ed0
Parent:
1:4d4c77589134
Child:
4:c36159701cde

File content as of revision 3:f8e70f639ed0:

#include "AP1017.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_10);
}

// Default destructor
AP1017::~AP1017(void)
{
    stop();
    delete inA, inB, motor;
}

/*********************** Member Functions ***********************************/

AP1017::Status AP1017::setDirection(Rotation dir)
{
    direction = dir;

    switch(direction){
        case DIRECTION_CW:
            if(motorOn == true && dir == DIRECTION_CCW)
                return ERROR_MOTORON;
            inA->write(1);
            inB->write(0);
            break;
        case DIRECTION_CCW:
            if(motorOn == true && dir == DIRECTION_CW)
                return ERROR_MOTORON;
            inA->write(0);
            inB->write(1);
            break;
        case DIRECTION_BRAKE:
            inA->write(1);
            inB->write(1);
            break;
        case DIRECTION_COAST:
            inA->write(0);
            inB->write(0);
            motorOn = false;
            break;
        default:
            return ERROR_DIRECTION;
    }

    return SUCCESS;
}


AP1017::Rotation AP1017::getDirection(void)
{
    return direction;
}


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;

        return ERROR_DUTY_CYCLE;
    }

    return SUCCESS;
}


float AP1017::getSpeed(void)
{
    return dutyCycle*100.0;
}


AP1017::Status AP1017::start(void)
{
    motor->write(dutyCycle);                // Set duty cycle, start motor
    motorOn = true;                         // Set ON flag

    return SUCCESS;
}


AP1017::Status AP1017::stop(void)
{
    motor->write(0.0);                      // turn motor off (duty cycle saved)
    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;
}