Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BridgeDriver FrontPanelButtons MCP23017 SDFileSystem TextLCD mbed
Diff: Motor.cpp
- Revision:
- 2:3e7baa3e3fec
- Parent:
- 0:22618cf06f45
--- a/Motor.cpp Tue Sep 16 18:27:41 2014 +0000 +++ b/Motor.cpp Thu Sep 18 23:38:11 2014 +0000 @@ -1,181 +1,224 @@ -//#include "Motor.hpp" +#include "Motor.hpp" //#include "mbed.h" //#include "LocalPinNames.h" //#include "BridgeDriver.h" -// -////Constructor -//Motor::Motor(){ -// /* -// //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; -// }*/ -// -// char channel = *lineData.word[3]; //Parameter is a single character, so dereference the point to the word -// -// switch (channel){ -// case 'A': -// case 'a': -// this->motor = BridgeDriver::MOTOR_A; -// break; -// case 'B': -// case 'b': -// this->motor = BridgeDriver::MOTOR_B; -// break; -// case 'C': -// case 'c': -// this->motor = BridgeDriver::MOTOR_C; -// break; -// case 'D': -// case 'd': -// this->motor = BridgeDriver::MOTOR_D; -// break; -// } -//} -// -// -//enum BridgeDriver::Motors Motor::getMotor(){ -// return this->motor; -//} -// -////A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on] -//int Motor::interpret(){ -// -// -// //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,....... -// char *localname = lineData.word[0]; //just for the sake of following the variable easily and understanding -// char *func = lineData.word[1]; -// -// /******************************************************************************/ -// /*** <Func: enableBrake> ***/ -// /******************************************************************************/ -// if (strncmp(func,"enableBrake", 11) == 0){ -// -// 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); -// -// bridges.enableBraking(getMotor(), enableValue); -// } -// -// /******************************************************************************/ -// /*** <Func: forceBrake> ***/ -// /******************************************************************************/ -// else if (strncmp(func,"forceBrake", 10) == 0){ -// -// if (lineData.numWords != 3){ -// //Error Check, incorrect number of parameter, error out -// return 0; -// } -// -// bridges.forceBrake(getMotor()); -// } -// -// -// /******************************************************************************/ -// /*** <Func: drive> ***/ -// /******************************************************************************/ -// -// -// /******************************************************************************/ -// /**** <Func: off> ****/ -// /******************************************************************************/ -// else if (strncmp(func,"off", 3) == 0){ -// -// 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 -//} -//*/ -// -// -// \ No newline at end of file + +//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 +} +*/ + + + \ No newline at end of file