Ovo je kopija vertije od Milana

Dependencies:   TextLCD mbed

Fork of SunflowerMach1 by Milan Draganic

MotorDrivers/Motor.cpp

Committer:
mdraganic
Date:
2013-11-11
Revision:
6:902bec57d9ae
Parent:
4:03b68322905f
Child:
10:0b8a98b1e6b1

File content as of revision 6:902bec57d9ae:

#include "Motor.h"

Motor::Motor(PinName positivePin, PinName negativePin, PinName pwmPin): 
        positiveOut(positivePin), negativeOut(negativePin), pwmOut(pwmPin) {
        
    pwmOut.period(motorPwmPeriod);
    pwmOut = motorPwmInitDutyCycle;
    
    positiveOut = 0;
    negativeOut = 0;   
}

void Motor::start() {
    
    for (float i = 1; i > 0; i -= motorPwmChangeSpeed) {
        pwmOut = i;
        wait(motorPwmWaitTime);
    }        
    pwmOut = 0;
}

void Motor::stop() {

    if(direction == 0)
        return;

    for (float i = 0; i < 1; i += motorPwmChangeSpeed) {
        pwmOut = i;
        wait(motorPwmWaitTime);
    }
    pwmOut = 1;

    positiveOut = 0;
    negativeOut = 0;
    direction = 0;
    wait_ms(1000);
}

void Motor::movePositive() {

    if(direction == 1)
        return;

    if(direction == -1)
        stop();
        
    positiveOut = 1;
    direction = 1;
    start();
}

void Motor::moveNegative() {

    if(direction == -1)
        return;

    if(direction == 1)
        stop();
        
    negativeOut = 1;
    direction = -1;
    start();
}

bool Motor::isMoving() {

    return direction == 0;
}