Mitchell Hatfield / Mbed 2 deprecated Component_Test_Interface

Dependencies:   BridgeDriver FrontPanelButtons MCP23017 SDFileSystem TextLCD mbed

Devices/Motor.cpp

Committer:
mehatfie
Date:
2014-09-19
Revision:
4:86d0d04cc055
Parent:
Motor.cpp@ 2:3e7baa3e3fec
Child:
5:e36e0538a903

File content as of revision 4:86d0d04cc055:

#include "Motor.hpp"
//#include "mbed.h"
//#include "LocalPinNames.h"
//#include "BridgeDriver.h"

//Constructor
Motor::Motor(Line lineData){
    /*
    //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8)
    if (lineData.numWords != 4){
         //Error Check, incorrect number of parameter, error out
        //return 0;
    }*/
    
    /*lcd.setAddress(0,3);
    lcd.printf("CHECK Motor Construct");
    wait(2);*/
    
    string channel = lineData.word[3]; //Parameter is a single character, so dereference the point to the word

    if ((channel.compare("A") == 0) || (channel.compare("a") == 0))
            this->motor = BridgeDriver::MOTOR_A;
    else if ((channel.compare("B") == 0) || (channel.compare("b") == 0))
            this->motor = BridgeDriver::MOTOR_B;
    else if ((channel.compare("C") == 0) || (channel.compare("c") == 0))
            this->motor = BridgeDriver::MOTOR_C;
    else if ((channel.compare("D") == 0) || (channel.compare("d") == 0))
            this->motor = BridgeDriver::MOTOR_D;
}
    
    
enum BridgeDriver::Motors Motor::getMotor(){     
    return this->motor;
}

//A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
int Motor::interpret(Line &lineData){
    /*
    lcd.setAddress(0,2);
    lcd.printf("CHECK Motor 123456");
    lcd.setAddress(0,3);
    lcd.printf("func: %s  ", lineData.word[1]);
    wait(2);*/

    //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
    //char localname[15] = lineData.word[0]; //just for the sake of following the variable easily and understanding 
    string func = lineData.word[1];
        
    /******************************************************************************/
    /***                       <Func: enableBrake>                              ***/
    /******************************************************************************/
    if (func.compare("enableBrake") == 0){
        
        if (lineData.numWords != 4){
             //Error Check, incorrect number of parameter, error out
            return 0;
        }
        
        //Initialize and Convert Parameters
        string enable = lineData.word[2];
        int enableValue = 0;
        sscanf(enable.c_str(), "%d", &enableValue);
        
        bridges.enableBraking(getMotor(), enableValue);
    }
        
    /******************************************************************************/
    /***                       <Func: forceBrake>                               ***/
    /******************************************************************************/
    else if (func.compare("forceBrake") == 0){
        
        if (lineData.numWords != 3){
             //Error Check, incorrect number of parameter, error out
            return 0;
        }
        
        bridges.forceBrake(getMotor());
    }
    
    
    /******************************************************************************/
    /***                           <Func: drive>                                ***/
    /******************************************************************************/
    else if (func.compare("drive") == 0){
        
        /*lcd.setAddress(0,2);
        lcd.printf("wrd2: %s", lineData.word[1]);
        lcd.setAddress(0,3);
        lcd.printf("TEST2  ");
        wait(2);*/

        if (lineData.numWords != 4){
             //Error Check, incorrect number of parameter, error out
            return 0;
        }
                
        //Initialize Parameters
        string speed = lineData.word[2];
        string dir = lineData.word[3];
        
        //Initialize Convertion Variables if needed
        float speedValue;
        int dirValue = 0;
        
        //Convert string to usable values
        //NOTE both atof and atoi functions return 0 if no valid conversion could be performed
        sscanf(speed.c_str(), "%f", &speedValue);
        speedValue = speedValue / 100;
        
        if (speedValue <= 0)
            return 0; //Error Out because a value gives no functionality or is wrong
                
        if (dir.compare("CC") == 0 || dir.compare("cc") == 0)
            dirValue = -1; //Turn Clockwise
        else if (dir.compare("C") == 0 || dir.compare("c") == 0)
            dirValue = 1; //Turn CounterClockwise
        else
            return 0; //Error Out since the parameter is incorrect
        
        
        bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor
        
    }
    /******************************************************************************/
    /****                           <Func: off>                                ****/
    /******************************************************************************/
    else if (func.compare("off") == 0){
        
        /*lcd.setAddress(0,2);
        lcd.printf("wrd2: %s", lineData.word[1]);
        lcd.setAddress(0,3);
        lcd.printf("TEST2  ");
        wait(1);*/
        
        if (lineData.numWords != 2){
             //Error Check, incorrect number of parameter, error out
            return 0;
        }
             
        bridges.drive(getMotor(), 0, 0); //Turn off the Motor
    }
    
    else {
        return 0;
        
    }  
    
    return 1;
}







/*
int Motor::enableBrake(){
  
    if (lineData.numWords != 4){
         //Error Check, incorrect number of parameter, error out
        return 0;
    }
    
    //Initialize and Convert Parameters
    char *enable = lineData.word[2];
    int enableValue = atoi(enable);
    
    //If the atoi returned anything besides 0, it worked properly
    if (enableValue) 
        bridges.enableBraking(getMotor(), enableValue);
    else{
         //Error Check, incorrect number of parameter, error out
        return 0;
    }
    
    return 1;
}

int Motor::forceBrake(){

    if (lineData.numWords != 3){
         //Error Check, incorrect number of parameter, error out
        return 0;
    }
    
    bridges.forceBrake(getMotor());
}
    
int Motor::drive(){
        
            if (lineData.numWords != 4){
                 //Error Check, incorrect number of parameter, error out
                return 0;
            }
                        
            //Initialize Parameters
            char *speed = lineData.word[2];
            char *dir = lineData.word[3];
            
            //Initialize Convertion Variables if needed
            float speedValue;
            int dirValue = 0;
    
            //Convert string to usable values
            //NOTE both atof and atoi functions return 0 if no valid conversion could be performed
            speedValue = atof(speed) / 100;
            
            if (speedValue <= 0)
                return 0; //Error Out because a value gives no functionality or is wrong
            
            if (strncmp(dir,"CC", 2) == 0 || strncmp(dir,"cc", 2) == 0)
                dirValue = -1; //Turn Clockwise
            else if (strncmp(dir,"C", 1) == 0 || strncmp(dir,"c", 1) == 0)
                dirValue = 1; //Turn CounterClockwise
            else
                return 0; //Error Out since the parameter is incorrect
                
            bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor
}    
*/