![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Component Test's Software to work with "Universal Controller Box" - Software is an interpreter or "compiler" for programs to be done with a .txt file and read off of the SD Card
Dependencies: BridgeDriver FrontPanelButtons MCP23017 SDFileSystem TextLCD mbed
Devices/Motor.cpp@16:2482d226cf4d, 2014-10-03 (annotated)
- Committer:
- mehatfie
- Date:
- Fri Oct 03 21:33:46 2014 +0000
- Revision:
- 16:2482d226cf4d
- Parent:
- 14:953820302fb7
- Updated Errors; - Initializes SD Card to work every time; - Cycle timer starts immediately on first cycle
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mehatfie | 2:3e7baa3e3fec | 1 | #include "Motor.hpp" |
mehatfie | 2:3e7baa3e3fec | 2 | |
mehatfie | 2:3e7baa3e3fec | 3 | //Constructor |
mehatfie | 5:e36e0538a903 | 4 | Motor::Motor(LineData lineData){ |
mehatfie | 14:953820302fb7 | 5 | this->errorFlag = 0; |
mehatfie | 2:3e7baa3e3fec | 6 | |
mehatfie | 9:5a0c4c6e39c7 | 7 | //Order of Line: Command, Local_Name, MOTOR, MotorID(A,B,C,D) |
mehatfie | 9:5a0c4c6e39c7 | 8 | //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 |
mehatfie | 9:5a0c4c6e39c7 | 9 | if (lineData.numWords != 4) |
mehatfie | 9:5a0c4c6e39c7 | 10 | this->errorFlag = 1; |
mehatfie | 14:953820302fb7 | 11 | |
mehatfie | 2:3e7baa3e3fec | 12 | string channel = lineData.word[3]; //Parameter is a single character, so dereference the point to the word |
mehatfie | 2:3e7baa3e3fec | 13 | |
mehatfie | 2:3e7baa3e3fec | 14 | if ((channel.compare("A") == 0) || (channel.compare("a") == 0)) |
mehatfie | 2:3e7baa3e3fec | 15 | this->motor = BridgeDriver::MOTOR_A; |
mehatfie | 2:3e7baa3e3fec | 16 | else if ((channel.compare("B") == 0) || (channel.compare("b") == 0)) |
mehatfie | 2:3e7baa3e3fec | 17 | this->motor = BridgeDriver::MOTOR_B; |
mehatfie | 2:3e7baa3e3fec | 18 | else if ((channel.compare("C") == 0) || (channel.compare("c") == 0)) |
mehatfie | 2:3e7baa3e3fec | 19 | this->motor = BridgeDriver::MOTOR_C; |
mehatfie | 2:3e7baa3e3fec | 20 | else if ((channel.compare("D") == 0) || (channel.compare("d") == 0)) |
mehatfie | 2:3e7baa3e3fec | 21 | this->motor = BridgeDriver::MOTOR_D; |
mehatfie | 9:5a0c4c6e39c7 | 22 | |
mehatfie | 9:5a0c4c6e39c7 | 23 | //MotorID not known |
mehatfie | 9:5a0c4c6e39c7 | 24 | else |
mehatfie | 9:5a0c4c6e39c7 | 25 | this->errorFlag = 1; |
mehatfie | 9:5a0c4c6e39c7 | 26 | |
mehatfie | 6:d1594fd2ec5a | 27 | bridges.enablePwm(this->motor, 1); |
mehatfie | 2:3e7baa3e3fec | 28 | } |
mehatfie | 2:3e7baa3e3fec | 29 | |
mehatfie | 2:3e7baa3e3fec | 30 | |
mehatfie | 2:3e7baa3e3fec | 31 | enum BridgeDriver::Motors Motor::getMotor(){ |
mehatfie | 2:3e7baa3e3fec | 32 | return this->motor; |
mehatfie | 2:3e7baa3e3fec | 33 | } |
mehatfie | 2:3e7baa3e3fec | 34 | |
mehatfie | 2:3e7baa3e3fec | 35 | //A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on] |
mehatfie | 5:e36e0538a903 | 36 | int Motor::interpret(LineData &lineData){ |
mehatfie | 9:5a0c4c6e39c7 | 37 | |
mehatfie | 2:3e7baa3e3fec | 38 | //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,....... |
mehatfie | 2:3e7baa3e3fec | 39 | string func = lineData.word[1]; |
mehatfie | 2:3e7baa3e3fec | 40 | |
mehatfie | 2:3e7baa3e3fec | 41 | /******************************************************************************/ |
mehatfie | 2:3e7baa3e3fec | 42 | /*** <Func: enableBrake> ***/ |
mehatfie | 2:3e7baa3e3fec | 43 | /******************************************************************************/ |
mehatfie | 2:3e7baa3e3fec | 44 | if (func.compare("enableBrake") == 0){ |
mehatfie | 2:3e7baa3e3fec | 45 | |
mehatfie | 9:5a0c4c6e39c7 | 46 | if (lineData.numWords != 3){ |
mehatfie | 9:5a0c4c6e39c7 | 47 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 48 | return -1; |
mehatfie | 2:3e7baa3e3fec | 49 | } |
mehatfie | 2:3e7baa3e3fec | 50 | |
mehatfie | 2:3e7baa3e3fec | 51 | //Initialize and Convert Parameters |
mehatfie | 2:3e7baa3e3fec | 52 | string enable = lineData.word[2]; |
mehatfie | 2:3e7baa3e3fec | 53 | int enableValue = 0; |
mehatfie | 2:3e7baa3e3fec | 54 | |
mehatfie | 9:5a0c4c6e39c7 | 55 | int numValuesFound = sscanf(enable.c_str(), "%d", &enableValue); |
mehatfie | 9:5a0c4c6e39c7 | 56 | if (numValuesFound < 1){ |
mehatfie | 9:5a0c4c6e39c7 | 57 | ErrorOut("Parameter Unknown, enableBrake value can't be converted", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 58 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 59 | } |
mehatfie | 10:e8db892fbc52 | 60 | |
mehatfie | 10:e8db892fbc52 | 61 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality |
mehatfie | 10:e8db892fbc52 | 62 | if (DummyMode) |
mehatfie | 10:e8db892fbc52 | 63 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 10:e8db892fbc52 | 64 | |
mehatfie | 2:3e7baa3e3fec | 65 | bridges.enableBraking(getMotor(), enableValue); |
mehatfie | 2:3e7baa3e3fec | 66 | } |
mehatfie | 2:3e7baa3e3fec | 67 | |
mehatfie | 2:3e7baa3e3fec | 68 | /******************************************************************************/ |
mehatfie | 2:3e7baa3e3fec | 69 | /*** <Func: forceBrake> ***/ |
mehatfie | 2:3e7baa3e3fec | 70 | /******************************************************************************/ |
mehatfie | 2:3e7baa3e3fec | 71 | else if (func.compare("forceBrake") == 0){ |
mehatfie | 2:3e7baa3e3fec | 72 | |
mehatfie | 9:5a0c4c6e39c7 | 73 | if (lineData.numWords != 2){ |
mehatfie | 9:5a0c4c6e39c7 | 74 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 75 | return -1; |
mehatfie | 2:3e7baa3e3fec | 76 | } |
mehatfie | 2:3e7baa3e3fec | 77 | |
mehatfie | 10:e8db892fbc52 | 78 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality |
mehatfie | 10:e8db892fbc52 | 79 | if (DummyMode) |
mehatfie | 10:e8db892fbc52 | 80 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 10:e8db892fbc52 | 81 | |
mehatfie | 2:3e7baa3e3fec | 82 | bridges.forceBrake(getMotor()); |
mehatfie | 11:bc9cd2869f95 | 83 | |
mehatfie | 11:bc9cd2869f95 | 84 | //Record the settings for Pause and Resume |
mehatfie | 11:bc9cd2869f95 | 85 | currDir = 0; |
mehatfie | 11:bc9cd2869f95 | 86 | currSpeed = 0; |
mehatfie | 2:3e7baa3e3fec | 87 | } |
mehatfie | 2:3e7baa3e3fec | 88 | |
mehatfie | 2:3e7baa3e3fec | 89 | |
mehatfie | 2:3e7baa3e3fec | 90 | /******************************************************************************/ |
mehatfie | 2:3e7baa3e3fec | 91 | /*** <Func: drive> ***/ |
mehatfie | 2:3e7baa3e3fec | 92 | /******************************************************************************/ |
mehatfie | 2:3e7baa3e3fec | 93 | else if (func.compare("drive") == 0){ |
mehatfie | 2:3e7baa3e3fec | 94 | |
mehatfie | 2:3e7baa3e3fec | 95 | if (lineData.numWords != 4){ |
mehatfie | 9:5a0c4c6e39c7 | 96 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 97 | return -1; |
mehatfie | 2:3e7baa3e3fec | 98 | } |
mehatfie | 2:3e7baa3e3fec | 99 | |
mehatfie | 2:3e7baa3e3fec | 100 | //Initialize Parameters |
mehatfie | 2:3e7baa3e3fec | 101 | string speed = lineData.word[2]; |
mehatfie | 2:3e7baa3e3fec | 102 | string dir = lineData.word[3]; |
mehatfie | 2:3e7baa3e3fec | 103 | |
mehatfie | 2:3e7baa3e3fec | 104 | //Initialize Convertion Variables if needed |
mehatfie | 2:3e7baa3e3fec | 105 | float speedValue; |
mehatfie | 2:3e7baa3e3fec | 106 | int dirValue = 0; |
mehatfie | 2:3e7baa3e3fec | 107 | |
mehatfie | 2:3e7baa3e3fec | 108 | //Convert string to usable values |
mehatfie | 9:5a0c4c6e39c7 | 109 | int numValuesFound = sscanf(speed.c_str(), "%f", &speedValue); |
mehatfie | 9:5a0c4c6e39c7 | 110 | if (numValuesFound < 1){ |
mehatfie | 9:5a0c4c6e39c7 | 111 | ErrorOut("Parameter Unknown, speed value can't be converted", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 112 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 113 | } |
mehatfie | 9:5a0c4c6e39c7 | 114 | |
mehatfie | 9:5a0c4c6e39c7 | 115 | //Speed is given as a percentage of 100, so convert it to the value needed for the bridge.drive function |
mehatfie | 2:3e7baa3e3fec | 116 | speedValue = speedValue / 100; |
mehatfie | 2:3e7baa3e3fec | 117 | |
mehatfie | 9:5a0c4c6e39c7 | 118 | |
mehatfie | 9:5a0c4c6e39c7 | 119 | if (speedValue <= 0 && speedValue > 1.0){ |
mehatfie | 9:5a0c4c6e39c7 | 120 | ErrorOut("Speed Value must be between 0 - 100", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 121 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 122 | } |
mehatfie | 9:5a0c4c6e39c7 | 123 | |
mehatfie | 2:3e7baa3e3fec | 124 | if (dir.compare("CC") == 0 || dir.compare("cc") == 0) |
mehatfie | 2:3e7baa3e3fec | 125 | dirValue = -1; //Turn Clockwise |
mehatfie | 2:3e7baa3e3fec | 126 | else if (dir.compare("C") == 0 || dir.compare("c") == 0) |
mehatfie | 2:3e7baa3e3fec | 127 | dirValue = 1; //Turn CounterClockwise |
mehatfie | 9:5a0c4c6e39c7 | 128 | |
mehatfie | 9:5a0c4c6e39c7 | 129 | else{ |
mehatfie | 9:5a0c4c6e39c7 | 130 | ErrorOut("Direction Value must be C or CC", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 131 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 132 | } |
mehatfie | 2:3e7baa3e3fec | 133 | |
mehatfie | 10:e8db892fbc52 | 134 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality |
mehatfie | 10:e8db892fbc52 | 135 | if (DummyMode) |
mehatfie | 10:e8db892fbc52 | 136 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 10:e8db892fbc52 | 137 | |
mehatfie | 2:3e7baa3e3fec | 138 | bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor |
mehatfie | 11:bc9cd2869f95 | 139 | |
mehatfie | 11:bc9cd2869f95 | 140 | //Record the settings for Pause and Resume |
mehatfie | 11:bc9cd2869f95 | 141 | currDir = dirValue; |
mehatfie | 11:bc9cd2869f95 | 142 | currSpeed = speedValue; |
mehatfie | 2:3e7baa3e3fec | 143 | } |
mehatfie | 9:5a0c4c6e39c7 | 144 | |
mehatfie | 9:5a0c4c6e39c7 | 145 | |
mehatfie | 2:3e7baa3e3fec | 146 | /******************************************************************************/ |
mehatfie | 2:3e7baa3e3fec | 147 | /**** <Func: off> ****/ |
mehatfie | 2:3e7baa3e3fec | 148 | /******************************************************************************/ |
mehatfie | 2:3e7baa3e3fec | 149 | else if (func.compare("off") == 0){ |
mehatfie | 2:3e7baa3e3fec | 150 | |
mehatfie | 2:3e7baa3e3fec | 151 | if (lineData.numWords != 2){ |
mehatfie | 9:5a0c4c6e39c7 | 152 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 153 | return -1; |
mehatfie | 2:3e7baa3e3fec | 154 | } |
mehatfie | 10:e8db892fbc52 | 155 | |
mehatfie | 10:e8db892fbc52 | 156 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality |
mehatfie | 10:e8db892fbc52 | 157 | if (DummyMode) |
mehatfie | 10:e8db892fbc52 | 158 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 10:e8db892fbc52 | 159 | |
mehatfie | 9:5a0c4c6e39c7 | 160 | off(); |
mehatfie | 2:3e7baa3e3fec | 161 | } |
mehatfie | 2:3e7baa3e3fec | 162 | |
mehatfie | 2:3e7baa3e3fec | 163 | else { |
mehatfie | 9:5a0c4c6e39c7 | 164 | ErrorOut("Unknown Command for Motor Class", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 165 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 166 | } |
mehatfie | 2:3e7baa3e3fec | 167 | |
mehatfie | 9:5a0c4c6e39c7 | 168 | return 0; //Return success as 0 since no condition had to be met |
mehatfie | 2:3e7baa3e3fec | 169 | } |
mehatfie | 2:3e7baa3e3fec | 170 | |
mehatfie | 2:3e7baa3e3fec | 171 | |
mehatfie | 2:3e7baa3e3fec | 172 | |
mehatfie | 9:5a0c4c6e39c7 | 173 | //For stopping the entire system if an error occurs, can be called from main |
mehatfie | 9:5a0c4c6e39c7 | 174 | int Motor::off(void){ |
mehatfie | 9:5a0c4c6e39c7 | 175 | bridges.drive(getMotor(), 0, 0); //Turn off the Motor |
mehatfie | 11:bc9cd2869f95 | 176 | |
mehatfie | 11:bc9cd2869f95 | 177 | //Record the settings for Pause and Resume, and exit |
mehatfie | 11:bc9cd2869f95 | 178 | currDir = 0; |
mehatfie | 11:bc9cd2869f95 | 179 | currSpeed = 0; |
mehatfie | 10:e8db892fbc52 | 180 | return 0; |
mehatfie | 9:5a0c4c6e39c7 | 181 | } |
mehatfie | 2:3e7baa3e3fec | 182 | |
mehatfie | 2:3e7baa3e3fec | 183 | |
mehatfie | 11:bc9cd2869f95 | 184 | //Stop the motor without changing the previous known settings, so that it will be saved on resume |
mehatfie | 11:bc9cd2869f95 | 185 | int Motor::pause(void){ |
mehatfie | 11:bc9cd2869f95 | 186 | bridges.drive(getMotor(), 0, 0); //Turn off the Motor |
mehatfie | 11:bc9cd2869f95 | 187 | return 0; |
mehatfie | 2:3e7baa3e3fec | 188 | } |
mehatfie | 2:3e7baa3e3fec | 189 | |
mehatfie | 11:bc9cd2869f95 | 190 | //Resume the motor using its previously known settings |
mehatfie | 11:bc9cd2869f95 | 191 | int Motor::resume(void){ |
mehatfie | 11:bc9cd2869f95 | 192 | bridges.drive(getMotor(), this->currDir, this->currSpeed); //Resume Motor from it's last known state |
mehatfie | 11:bc9cd2869f95 | 193 | return 0; |
mehatfie | 2:3e7baa3e3fec | 194 | } |