Sets motor currents based on input motorvalue

motorConfig.cpp

Committer:
tvlogman
Date:
2017-10-17
Revision:
0:131a76b8848a
Child:
1:84cb991c4d28

File content as of revision 0:131a76b8848a:

#include "motorConfig.h"
#include "mbed.h"
#include "FastPWM.h"

// Member function definitions
motorConfig::motorConfig(PinName a, PinName b, PinName c, PinName d, PinName e):ledG(a),ledR(b),ledB(c),directionPin(e),pwmPin(d){
    currentState = KILLED;
    pwmPin.period(1.0/5000.0);
    directionPin = 0;
    }

void motorConfig::setMotor(float motorValue){
    switch(currentState){
                case KILLED:
                    pwmPin.write(0.0);
                    // Set motor direction
                    if (motorValue >=0){
                        // corresponds to CW rotation of motor axle
                        directionPin = 0;
                        } else if(motorValue < 0){
                        // corresponds to CCW rotation of motor axle
                        directionPin = 1;
                        }
                    ledR = 0;
                    ledG = 1;
                    ledB = 1;
                    break;
                case ACTIVE:
                    // Set motor direction
                    if (motorValue >=0){
                        // corresponds to CW rotation of motor axle
                        directionPin.write(0);
                        } else if(motorValue < 0){
                        // corresponds to CCW rotation of motor axle
                        directionPin.write(1);
                        }
                        
                    // Set motor speed
                    if (fabs(motorValue)>1){ 
                        pwmPin = 1.0;
                        }
                    else {
                        pwmPin.write(fabs(motorValue) + 0.4);
                        }
                    ledR = 1;
                    ledG = 1;
                    ledB = 0;      
                    break;
            }
    }

void motorConfig::turnMotorOn(){
    currentState = ACTIVE; 
    }
    
void motorConfig::killSwitch(){
    currentState = KILLED;
    }