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: Devices/Motor.cpp
- Revision:
- 9:5a0c4c6e39c7
- Parent:
- 6:d1594fd2ec5a
- Child:
- 10:e8db892fbc52
--- a/Devices/Motor.cpp Wed Sep 24 01:46:02 2014 +0000
+++ b/Devices/Motor.cpp Wed Sep 24 22:23:00 2014 +0000
@@ -1,21 +1,13 @@
#include "Motor.hpp"
-//#include "mbed.h"
-//#include "LocalPinNames.h"
-//#include "BridgeDriver.h"
//Constructor
Motor::Motor(LineData 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);*/
-
+ //Order of Line: Command, Local_Name, MOTOR, MotorID(A,B,C,D)
+ //Since we can't return from a constructor, instead we'll flip a flag, and check it after we've added the device in interpretCommand
+ if (lineData.numWords != 4)
+ this->errorFlag = 1;
+
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))
@@ -26,7 +18,11 @@
this->motor = BridgeDriver::MOTOR_C;
else if ((channel.compare("D") == 0) || (channel.compare("d") == 0))
this->motor = BridgeDriver::MOTOR_D;
-
+
+ //MotorID not known
+ else
+ this->errorFlag = 1;
+
bridges.enablePwm(this->motor, 1);
}
@@ -37,15 +33,8 @@
//A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
int Motor::interpret(LineData &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];
/******************************************************************************/
@@ -53,16 +42,21 @@
/******************************************************************************/
if (func.compare("enableBrake") == 0){
- if (lineData.numWords != 4){
- //Error Check, incorrect number of parameter, error out
- return 0;
+ if (lineData.numWords != 3){
+ ErrorOut("Incorrect number of parameters", lineData.lineNumber);
+ return -1;
}
//Initialize and Convert Parameters
string enable = lineData.word[2];
int enableValue = 0;
- sscanf(enable.c_str(), "%d", &enableValue);
+ int numValuesFound = sscanf(enable.c_str(), "%d", &enableValue);
+ if (numValuesFound < 1){
+ ErrorOut("Parameter Unknown, enableBrake value can't be converted", lineData.lineNumber);
+ return -1;
+ }
+
bridges.enableBraking(getMotor(), enableValue);
}
@@ -71,9 +65,9 @@
/******************************************************************************/
else if (func.compare("forceBrake") == 0){
- if (lineData.numWords != 3){
- //Error Check, incorrect number of parameter, error out
- return 0;
+ if (lineData.numWords != 2){
+ ErrorOut("Incorrect number of parameters", lineData.lineNumber);
+ return -1;
}
bridges.forceBrake(getMotor());
@@ -85,15 +79,9 @@
/******************************************************************************/
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;
+ ErrorOut("Incorrect number of parameters", lineData.lineNumber);
+ return -1;
}
//Initialize Parameters
@@ -105,55 +93,62 @@
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);
+ int numValuesFound = sscanf(speed.c_str(), "%f", &speedValue);
+ if (numValuesFound < 1){
+ ErrorOut("Parameter Unknown, speed value can't be converted", lineData.lineNumber);
+ return -1;
+ }
+
+ //Speed is given as a percentage of 100, so convert it to the value needed for the bridge.drive function
speedValue = speedValue / 100;
- if (speedValue <= 0)
- return 0; //Error Out because a value gives no functionality or is wrong
-
+
+ if (speedValue <= 0 && speedValue > 1.0){
+ ErrorOut("Speed Value must be between 0 - 100", lineData.lineNumber);
+ return -1;
+ }
+
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
-
+
+ else{
+ ErrorOut("Direction Value must be C or CC", lineData.lineNumber);
+ return -1;
+ }
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;
+ ErrorOut("Incorrect number of parameters", lineData.lineNumber);
+ return -1;
}
- bridges.drive(getMotor(), 0, 0); //Turn off the Motor
+ off();
}
else {
- return 0;
-
- }
+ ErrorOut("Unknown Command for Motor Class", lineData.lineNumber);
+ return -1;
+ }
- return 1;
+ return 0; //Return success as 0 since no condition had to be met
}
-
-
+//For stopping the entire system if an error occurs, can be called from main
+int Motor::off(void){
+ bridges.drive(getMotor(), 0, 0); //Turn off the Motor
+}
/*