![](/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/CAN_Device.cpp@16:2482d226cf4d, 2014-10-03 (annotated)
- Committer:
- mehatfie
- Date:
- Fri Oct 03 21:33:46 2014 +0000
- Revision:
- 16:2482d226cf4d
- Parent:
- 15:c944ee3c8a5b
- 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 | 16:2482d226cf4d | 1 | #include "CAN_Device.hpp" |
mehatfie | 16:2482d226cf4d | 2 | |
mehatfie | 16:2482d226cf4d | 3 | //Constructor |
mehatfie | 16:2482d226cf4d | 4 | CAN_Device::CAN_Device(LineData lineData){ |
mehatfie | 16:2482d226cf4d | 5 | |
mehatfie | 16:2482d226cf4d | 6 | //Order of Line: Command, Local_Name, CAN_DEVICE, selectedCAN(1,2), freq |
mehatfie | 16:2482d226cf4d | 7 | //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 | 16:2482d226cf4d | 8 | if (lineData.numWords != 5) |
mehatfie | 16:2482d226cf4d | 9 | this->errorFlag = 1; |
mehatfie | 16:2482d226cf4d | 10 | |
mehatfie | 16:2482d226cf4d | 11 | string channelstr = lineData.word[3], freqstr = lineData.word[4]; //Parameters are numbers |
mehatfie | 16:2482d226cf4d | 12 | int tempChannel = -1, tempFreq = -1; |
mehatfie | 16:2482d226cf4d | 13 | int numValuesFound = sscanf(channelstr.c_str(), "%d", &tempChannel); |
mehatfie | 16:2482d226cf4d | 14 | if (numValuesFound < 1) |
mehatfie | 16:2482d226cf4d | 15 | this->errorFlag = 1; |
mehatfie | 16:2482d226cf4d | 16 | |
mehatfie | 16:2482d226cf4d | 17 | numValuesFound = sscanf(freqstr.c_str(), "%d", &tempFreq); |
mehatfie | 16:2482d226cf4d | 18 | if (numValuesFound < 1) |
mehatfie | 16:2482d226cf4d | 19 | this->errorFlag = 1; |
mehatfie | 16:2482d226cf4d | 20 | |
mehatfie | 16:2482d226cf4d | 21 | //channel must be either CAN1 or CAN2 |
mehatfie | 16:2482d226cf4d | 22 | //CAN 1 is on the Back of the Box, CAN2 is on the Front of the Box |
mehatfie | 16:2482d226cf4d | 23 | if (tempChannel == 1 || tempChannel == 2){ |
mehatfie | 16:2482d226cf4d | 24 | if (tempChannel == 1){ |
mehatfie | 16:2482d226cf4d | 25 | this->canDevice = new CAN (p30, p29); |
mehatfie | 15:c944ee3c8a5b | 26 | // this->pinRD = p30; |
mehatfie | 15:c944ee3c8a5b | 27 | // this->pinTD = p29; |
mehatfie | 16:2482d226cf4d | 28 | } |
mehatfie | 16:2482d226cf4d | 29 | else if (tempChannel == 2){ |
mehatfie | 16:2482d226cf4d | 30 | this->canDevice = new CAN (p9, p10); |
mehatfie | 15:c944ee3c8a5b | 31 | // this->pinRD = p9; |
mehatfie | 15:c944ee3c8a5b | 32 | // this->pinTD = p10; |
mehatfie | 16:2482d226cf4d | 33 | } |
mehatfie | 16:2482d226cf4d | 34 | |
mehatfie | 16:2482d226cf4d | 35 | this->selectedCAN = tempChannel; |
mehatfie | 16:2482d226cf4d | 36 | } |
mehatfie | 16:2482d226cf4d | 37 | |
mehatfie | 16:2482d226cf4d | 38 | else |
mehatfie | 16:2482d226cf4d | 39 | this->errorFlag = 1; |
mehatfie | 16:2482d226cf4d | 40 | |
mehatfie | 16:2482d226cf4d | 41 | //should have a frequency greater than 0 |
mehatfie | 16:2482d226cf4d | 42 | if (tempFreq > 0) |
mehatfie | 16:2482d226cf4d | 43 | this->canDevice->frequency(tempFreq); |
mehatfie | 16:2482d226cf4d | 44 | else |
mehatfie | 16:2482d226cf4d | 45 | this->errorFlag = 1; |
mehatfie | 16:2482d226cf4d | 46 | } |
mehatfie | 16:2482d226cf4d | 47 | |
mehatfie | 16:2482d226cf4d | 48 | |
mehatfie | 11:bc9cd2869f95 | 49 | //int CAN_Device::getSelectedCAN(){ |
mehatfie | 11:bc9cd2869f95 | 50 | // return this->selectedCAN; |
mehatfie | 11:bc9cd2869f95 | 51 | //} |
mehatfie | 11:bc9cd2869f95 | 52 | // |
mehatfie | 11:bc9cd2869f95 | 53 | //int CAN_Device::getFreq(){ |
mehatfie | 11:bc9cd2869f95 | 54 | // return this->freq; |
mehatfie | 11:bc9cd2869f95 | 55 | //} |
mehatfie | 16:2482d226cf4d | 56 | |
mehatfie | 16:2482d226cf4d | 57 | //A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on] |
mehatfie | 16:2482d226cf4d | 58 | int CAN_Device::CAN_Device(LineData &lineData){ |
mehatfie | 11:bc9cd2869f95 | 59 | // |
mehatfie | 15:c944ee3c8a5b | 60 | // //Initialize the CAN device |
mehatfie | 15:c944ee3c8a5b | 61 | // CAN canDevice(pinRD, pinTD); |
mehatfie | 15:c944ee3c8a5b | 62 | // canDevice.frequency(125000); |
mehatfie | 16:2482d226cf4d | 63 | |
mehatfie | 16:2482d226cf4d | 64 | //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,....... |
mehatfie | 16:2482d226cf4d | 65 | string func = lineData.word[1]; |
mehatfie | 16:2482d226cf4d | 66 | |
mehatfie | 16:2482d226cf4d | 67 | /******************************************************************************/ |
mehatfie | 16:2482d226cf4d | 68 | /*** <Func: write> ***/ |
mehatfie | 16:2482d226cf4d | 69 | /******************************************************************************/ |
mehatfie | 16:2482d226cf4d | 70 | if (func.compare("write") == 0){ |
mehatfie | 16:2482d226cf4d | 71 | //line looks like: Local_Name, write, ID_val, length_val, data |
mehatfie | 16:2482d226cf4d | 72 | if (lineData.numWords < 5){ |
mehatfie | 16:2482d226cf4d | 73 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 74 | return -1; |
mehatfie | 16:2482d226cf4d | 75 | } |
mehatfie | 16:2482d226cf4d | 76 | |
mehatfie | 16:2482d226cf4d | 77 | //Initialize and Convert Parameters |
mehatfie | 16:2482d226cf4d | 78 | string _id = lineData.word[2]; |
mehatfie | 16:2482d226cf4d | 79 | string _length = lineData.word[3]; |
mehatfie | 16:2482d226cf4d | 80 | string _data = lineData.word[4]; |
mehatfie | 16:2482d226cf4d | 81 | int _idValue = 0; |
mehatfie | 16:2482d226cf4d | 82 | char _lengthValue = 0; |
mehatfie | 16:2482d226cf4d | 83 | |
mehatfie | 16:2482d226cf4d | 84 | int numValuesFound = sscanf(_id.c_str(), "%d", &_idValue); |
mehatfie | 16:2482d226cf4d | 85 | if (numValuesFound < 1){ |
mehatfie | 16:2482d226cf4d | 86 | ErrorOut("Parameter Unknown, id value can't be converted", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 87 | return -1; |
mehatfie | 16:2482d226cf4d | 88 | } |
mehatfie | 16:2482d226cf4d | 89 | |
mehatfie | 16:2482d226cf4d | 90 | int numValuesFound = sscanf(_length.c_str(), "%c", &_lengthValue); |
mehatfie | 16:2482d226cf4d | 91 | if (numValuesFound < 1){ |
mehatfie | 16:2482d226cf4d | 92 | ErrorOut("Parameter Unknown, length value can't be converted", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 93 | return -1; |
mehatfie | 16:2482d226cf4d | 94 | } |
mehatfie | 16:2482d226cf4d | 95 | |
mehatfie | 16:2482d226cf4d | 96 | int numDataVals = numWords - 4; //number of data bytes present in the line |
mehatfie | 16:2482d226cf4d | 97 | const char* dataBytes[numDataVals]; |
mehatfie | 16:2482d226cf4d | 98 | int i = 0; |
mehatfie | 16:2482d226cf4d | 99 | for (i = 0; i < numDataVals; i++){ |
mehatfie | 16:2482d226cf4d | 100 | string thisDataByte = lineData.word[4 + i]; |
mehatfie | 16:2482d226cf4d | 101 | int numValuesFound = sscanf(thisDataByte.c_str(), "%c", &dataBytes[i]); |
mehatfie | 16:2482d226cf4d | 102 | if (numValuesFound < 1){ |
mehatfie | 16:2482d226cf4d | 103 | ErrorOut("Parameter Unknown, a data byte %d can't be converted", (i + 1), lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 104 | return -1; |
mehatfie | 16:2482d226cf4d | 105 | } |
mehatfie | 16:2482d226cf4d | 106 | } |
mehatfie | 16:2482d226cf4d | 107 | |
mehatfie | 16:2482d226cf4d | 108 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality |
mehatfie | 16:2482d226cf4d | 109 | if (DummyMode) |
mehatfie | 16:2482d226cf4d | 110 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 16:2482d226cf4d | 111 | |
mehatfie | 16:2482d226cf4d | 112 | |
mehatfie | 16:2482d226cf4d | 113 | canDevice->write(CANMessage(_idValue, &dataBytes, _lengthValue)); |
mehatfie | 16:2482d226cf4d | 114 | } |
mehatfie | 16:2482d226cf4d | 115 | |
mehatfie | 16:2482d226cf4d | 116 | /******************************************************************************/ |
mehatfie | 16:2482d226cf4d | 117 | /*** <Func: read> ***/ |
mehatfie | 16:2482d226cf4d | 118 | /******************************************************************************/ |
mehatfie | 16:2482d226cf4d | 119 | else if (func.compare("forceBrake") == 0){ |
mehatfie | 16:2482d226cf4d | 120 | |
mehatfie | 16:2482d226cf4d | 121 | if (lineData.numWords != 2){ |
mehatfie | 16:2482d226cf4d | 122 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 123 | return -1; |
mehatfie | 16:2482d226cf4d | 124 | } |
mehatfie | 16:2482d226cf4d | 125 | |
mehatfie | 16:2482d226cf4d | 126 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality |
mehatfie | 16:2482d226cf4d | 127 | if (DummyMode) |
mehatfie | 16:2482d226cf4d | 128 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 16:2482d226cf4d | 129 | |
mehatfie | 16:2482d226cf4d | 130 | bridges.forceBrake(getMotor()); |
mehatfie | 16:2482d226cf4d | 131 | } |
mehatfie | 16:2482d226cf4d | 132 | |
mehatfie | 16:2482d226cf4d | 133 | |
mehatfie | 16:2482d226cf4d | 134 | /******************************************************************************/ |
mehatfie | 16:2482d226cf4d | 135 | /*** <Func: drive> ***/ |
mehatfie | 16:2482d226cf4d | 136 | /******************************************************************************/ |
mehatfie | 16:2482d226cf4d | 137 | else if (func.compare("drive") == 0){ |
mehatfie | 16:2482d226cf4d | 138 | |
mehatfie | 16:2482d226cf4d | 139 | if (lineData.numWords != 4){ |
mehatfie | 16:2482d226cf4d | 140 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 141 | return -1; |
mehatfie | 16:2482d226cf4d | 142 | } |
mehatfie | 16:2482d226cf4d | 143 | |
mehatfie | 16:2482d226cf4d | 144 | //Initialize Parameters |
mehatfie | 16:2482d226cf4d | 145 | string speed = lineData.word[2]; |
mehatfie | 16:2482d226cf4d | 146 | string dir = lineData.word[3]; |
mehatfie | 16:2482d226cf4d | 147 | |
mehatfie | 16:2482d226cf4d | 148 | //Initialize Convertion Variables if needed |
mehatfie | 16:2482d226cf4d | 149 | float speedValue; |
mehatfie | 16:2482d226cf4d | 150 | int dirValue = 0; |
mehatfie | 16:2482d226cf4d | 151 | |
mehatfie | 16:2482d226cf4d | 152 | //Convert string to usable values |
mehatfie | 16:2482d226cf4d | 153 | int numValuesFound = sscanf(speed.c_str(), "%f", &speedValue); |
mehatfie | 16:2482d226cf4d | 154 | if (numValuesFound < 1){ |
mehatfie | 16:2482d226cf4d | 155 | ErrorOut("Parameter Unknown, speed value can't be converted", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 156 | return -1; |
mehatfie | 16:2482d226cf4d | 157 | } |
mehatfie | 16:2482d226cf4d | 158 | |
mehatfie | 16:2482d226cf4d | 159 | //Speed is given as a percentage of 100, so convert it to the value needed for the bridge.drive function |
mehatfie | 16:2482d226cf4d | 160 | speedValue = speedValue / 100; |
mehatfie | 16:2482d226cf4d | 161 | |
mehatfie | 16:2482d226cf4d | 162 | |
mehatfie | 16:2482d226cf4d | 163 | if (speedValue <= 0 && speedValue > 1.0){ |
mehatfie | 16:2482d226cf4d | 164 | ErrorOut("Speed Value must be between 0 - 100", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 165 | return -1; |
mehatfie | 16:2482d226cf4d | 166 | } |
mehatfie | 16:2482d226cf4d | 167 | |
mehatfie | 16:2482d226cf4d | 168 | if (dir.compare("CC") == 0 || dir.compare("cc") == 0) |
mehatfie | 16:2482d226cf4d | 169 | dirValue = -1; //Turn Clockwise |
mehatfie | 16:2482d226cf4d | 170 | else if (dir.compare("C") == 0 || dir.compare("c") == 0) |
mehatfie | 16:2482d226cf4d | 171 | dirValue = 1; //Turn CounterClockwise |
mehatfie | 16:2482d226cf4d | 172 | |
mehatfie | 16:2482d226cf4d | 173 | else{ |
mehatfie | 16:2482d226cf4d | 174 | ErrorOut("Direction Value must be C or CC", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 175 | return -1; |
mehatfie | 16:2482d226cf4d | 176 | } |
mehatfie | 16:2482d226cf4d | 177 | |
mehatfie | 16:2482d226cf4d | 178 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality |
mehatfie | 16:2482d226cf4d | 179 | if (DummyMode) |
mehatfie | 16:2482d226cf4d | 180 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 16:2482d226cf4d | 181 | |
mehatfie | 16:2482d226cf4d | 182 | bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor |
mehatfie | 16:2482d226cf4d | 183 | } |
mehatfie | 16:2482d226cf4d | 184 | |
mehatfie | 16:2482d226cf4d | 185 | |
mehatfie | 16:2482d226cf4d | 186 | /******************************************************************************/ |
mehatfie | 16:2482d226cf4d | 187 | /**** <Func: off> ****/ |
mehatfie | 16:2482d226cf4d | 188 | /******************************************************************************/ |
mehatfie | 16:2482d226cf4d | 189 | else if (func.compare("off") == 0){ |
mehatfie | 16:2482d226cf4d | 190 | |
mehatfie | 16:2482d226cf4d | 191 | if (lineData.numWords != 2){ |
mehatfie | 16:2482d226cf4d | 192 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 193 | return -1; |
mehatfie | 16:2482d226cf4d | 194 | } |
mehatfie | 16:2482d226cf4d | 195 | |
mehatfie | 16:2482d226cf4d | 196 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality |
mehatfie | 16:2482d226cf4d | 197 | if (DummyMode) |
mehatfie | 16:2482d226cf4d | 198 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 16:2482d226cf4d | 199 | |
mehatfie | 16:2482d226cf4d | 200 | off(); |
mehatfie | 16:2482d226cf4d | 201 | } |
mehatfie | 16:2482d226cf4d | 202 | |
mehatfie | 16:2482d226cf4d | 203 | else { |
mehatfie | 16:2482d226cf4d | 204 | ErrorOut("Unknown Command for Motor Class", lineData.lineNumber); |
mehatfie | 16:2482d226cf4d | 205 | return -1; |
mehatfie | 16:2482d226cf4d | 206 | } |
mehatfie | 16:2482d226cf4d | 207 | |
mehatfie | 16:2482d226cf4d | 208 | return 0; //Return success as 0 since no condition had to be met |
mehatfie | 16:2482d226cf4d | 209 | } |
mehatfie | 16:2482d226cf4d | 210 | |
mehatfie | 16:2482d226cf4d | 211 | |
mehatfie | 16:2482d226cf4d | 212 | |
mehatfie | 16:2482d226cf4d | 213 | //For stopping the entire system if an error occurs, can be called from main |
mehatfie | 16:2482d226cf4d | 214 | int CAN_Device::off(void){ |
mehatfie | 16:2482d226cf4d | 215 | bridges.drive(getMotor(), 0, 0); //Turn off the Motor |
mehatfie | 16:2482d226cf4d | 216 | return 0; |
mehatfie | 16:2482d226cf4d | 217 | } |