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
Motor.cpp@0:22618cf06f45, 2014-09-16 (annotated)
- Committer:
- mehatfie
- Date:
- Tue Sep 16 15:28:59 2014 +0000
- Revision:
- 0:22618cf06f45
- Child:
- 2:3e7baa3e3fec
- Initial Commit; - Code is a mess (in mid transition of general code architecture); - Functionality work to drive motors in both direction and turn off; - Delay functionality working; - Able to read a program from a txt file and perform functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mehatfie | 0:22618cf06f45 | 1 | //#include "Motor.hpp" |
mehatfie | 0:22618cf06f45 | 2 | //#include "mbed.h" |
mehatfie | 0:22618cf06f45 | 3 | //#include "LocalPinNames.h" |
mehatfie | 0:22618cf06f45 | 4 | //#include "BridgeDriver.h" |
mehatfie | 0:22618cf06f45 | 5 | // |
mehatfie | 0:22618cf06f45 | 6 | ////Constructor |
mehatfie | 0:22618cf06f45 | 7 | //Motor::Motor(){ |
mehatfie | 0:22618cf06f45 | 8 | // /* |
mehatfie | 0:22618cf06f45 | 9 | // //Order of Line: Command, Local_Name, VOLTAGE_DRIVER, Channel(1,2,3,4,5,6,7,8) |
mehatfie | 0:22618cf06f45 | 10 | // if (lineData.numWords != 4){ |
mehatfie | 0:22618cf06f45 | 11 | // //Error Check, incorrect number of parameter, error out |
mehatfie | 0:22618cf06f45 | 12 | // //return 0; |
mehatfie | 0:22618cf06f45 | 13 | // }*/ |
mehatfie | 0:22618cf06f45 | 14 | // |
mehatfie | 0:22618cf06f45 | 15 | // char channel = *lineData.word[3]; //Parameter is a single character, so dereference the point to the word |
mehatfie | 0:22618cf06f45 | 16 | // |
mehatfie | 0:22618cf06f45 | 17 | // switch (channel){ |
mehatfie | 0:22618cf06f45 | 18 | // case 'A': |
mehatfie | 0:22618cf06f45 | 19 | // case 'a': |
mehatfie | 0:22618cf06f45 | 20 | // this->motor = BridgeDriver::MOTOR_A; |
mehatfie | 0:22618cf06f45 | 21 | // break; |
mehatfie | 0:22618cf06f45 | 22 | // case 'B': |
mehatfie | 0:22618cf06f45 | 23 | // case 'b': |
mehatfie | 0:22618cf06f45 | 24 | // this->motor = BridgeDriver::MOTOR_B; |
mehatfie | 0:22618cf06f45 | 25 | // break; |
mehatfie | 0:22618cf06f45 | 26 | // case 'C': |
mehatfie | 0:22618cf06f45 | 27 | // case 'c': |
mehatfie | 0:22618cf06f45 | 28 | // this->motor = BridgeDriver::MOTOR_C; |
mehatfie | 0:22618cf06f45 | 29 | // break; |
mehatfie | 0:22618cf06f45 | 30 | // case 'D': |
mehatfie | 0:22618cf06f45 | 31 | // case 'd': |
mehatfie | 0:22618cf06f45 | 32 | // this->motor = BridgeDriver::MOTOR_D; |
mehatfie | 0:22618cf06f45 | 33 | // break; |
mehatfie | 0:22618cf06f45 | 34 | // } |
mehatfie | 0:22618cf06f45 | 35 | //} |
mehatfie | 0:22618cf06f45 | 36 | // |
mehatfie | 0:22618cf06f45 | 37 | // |
mehatfie | 0:22618cf06f45 | 38 | //enum BridgeDriver::Motors Motor::getMotor(){ |
mehatfie | 0:22618cf06f45 | 39 | // return this->motor; |
mehatfie | 0:22618cf06f45 | 40 | //} |
mehatfie | 0:22618cf06f45 | 41 | // |
mehatfie | 0:22618cf06f45 | 42 | ////A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on] |
mehatfie | 0:22618cf06f45 | 43 | //int Motor::interpret(){ |
mehatfie | 0:22618cf06f45 | 44 | // |
mehatfie | 0:22618cf06f45 | 45 | // |
mehatfie | 0:22618cf06f45 | 46 | // //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,....... |
mehatfie | 0:22618cf06f45 | 47 | // char *localname = lineData.word[0]; //just for the sake of following the variable easily and understanding |
mehatfie | 0:22618cf06f45 | 48 | // char *func = lineData.word[1]; |
mehatfie | 0:22618cf06f45 | 49 | // |
mehatfie | 0:22618cf06f45 | 50 | // /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 51 | // /*** <Func: enableBrake> ***/ |
mehatfie | 0:22618cf06f45 | 52 | // /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 53 | // if (strncmp(func,"enableBrake", 11) == 0){ |
mehatfie | 0:22618cf06f45 | 54 | // |
mehatfie | 0:22618cf06f45 | 55 | // if (lineData.numWords != 4){ |
mehatfie | 0:22618cf06f45 | 56 | // //Error Check, incorrect number of parameter, error out |
mehatfie | 0:22618cf06f45 | 57 | // return 0; |
mehatfie | 0:22618cf06f45 | 58 | // } |
mehatfie | 0:22618cf06f45 | 59 | // |
mehatfie | 0:22618cf06f45 | 60 | // //Initialize and Convert Parameters |
mehatfie | 0:22618cf06f45 | 61 | // char *enable = lineData.word[2]; |
mehatfie | 0:22618cf06f45 | 62 | // int enableValue = atoi(enable); |
mehatfie | 0:22618cf06f45 | 63 | // |
mehatfie | 0:22618cf06f45 | 64 | // bridges.enableBraking(getMotor(), enableValue); |
mehatfie | 0:22618cf06f45 | 65 | // } |
mehatfie | 0:22618cf06f45 | 66 | // |
mehatfie | 0:22618cf06f45 | 67 | // /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 68 | // /*** <Func: forceBrake> ***/ |
mehatfie | 0:22618cf06f45 | 69 | // /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 70 | // else if (strncmp(func,"forceBrake", 10) == 0){ |
mehatfie | 0:22618cf06f45 | 71 | // |
mehatfie | 0:22618cf06f45 | 72 | // if (lineData.numWords != 3){ |
mehatfie | 0:22618cf06f45 | 73 | // //Error Check, incorrect number of parameter, error out |
mehatfie | 0:22618cf06f45 | 74 | // return 0; |
mehatfie | 0:22618cf06f45 | 75 | // } |
mehatfie | 0:22618cf06f45 | 76 | // |
mehatfie | 0:22618cf06f45 | 77 | // bridges.forceBrake(getMotor()); |
mehatfie | 0:22618cf06f45 | 78 | // } |
mehatfie | 0:22618cf06f45 | 79 | // |
mehatfie | 0:22618cf06f45 | 80 | // |
mehatfie | 0:22618cf06f45 | 81 | // /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 82 | // /*** <Func: drive> ***/ |
mehatfie | 0:22618cf06f45 | 83 | // /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 84 | // |
mehatfie | 0:22618cf06f45 | 85 | // |
mehatfie | 0:22618cf06f45 | 86 | // /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 87 | // /**** <Func: off> ****/ |
mehatfie | 0:22618cf06f45 | 88 | // /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 89 | // else if (strncmp(func,"off", 3) == 0){ |
mehatfie | 0:22618cf06f45 | 90 | // |
mehatfie | 0:22618cf06f45 | 91 | // if (lineData.numWords != 2){ |
mehatfie | 0:22618cf06f45 | 92 | // //Error Check, incorrect number of parameter, error out |
mehatfie | 0:22618cf06f45 | 93 | // return 0; |
mehatfie | 0:22618cf06f45 | 94 | // } |
mehatfie | 0:22618cf06f45 | 95 | // |
mehatfie | 0:22618cf06f45 | 96 | // bridges.drive(getMotor(), 0, 0); //Turn off the Motor |
mehatfie | 0:22618cf06f45 | 97 | // } |
mehatfie | 0:22618cf06f45 | 98 | // |
mehatfie | 0:22618cf06f45 | 99 | // else { |
mehatfie | 0:22618cf06f45 | 100 | // return 0; |
mehatfie | 0:22618cf06f45 | 101 | // |
mehatfie | 0:22618cf06f45 | 102 | // } |
mehatfie | 0:22618cf06f45 | 103 | // |
mehatfie | 0:22618cf06f45 | 104 | // |
mehatfie | 0:22618cf06f45 | 105 | // return 1; |
mehatfie | 0:22618cf06f45 | 106 | //} |
mehatfie | 0:22618cf06f45 | 107 | // |
mehatfie | 0:22618cf06f45 | 108 | // |
mehatfie | 0:22618cf06f45 | 109 | // |
mehatfie | 0:22618cf06f45 | 110 | // |
mehatfie | 0:22618cf06f45 | 111 | // |
mehatfie | 0:22618cf06f45 | 112 | // |
mehatfie | 0:22618cf06f45 | 113 | // |
mehatfie | 0:22618cf06f45 | 114 | ///* |
mehatfie | 0:22618cf06f45 | 115 | //int Motor::enableBrake(){ |
mehatfie | 0:22618cf06f45 | 116 | // |
mehatfie | 0:22618cf06f45 | 117 | // if (lineData.numWords != 4){ |
mehatfie | 0:22618cf06f45 | 118 | // //Error Check, incorrect number of parameter, error out |
mehatfie | 0:22618cf06f45 | 119 | // return 0; |
mehatfie | 0:22618cf06f45 | 120 | // } |
mehatfie | 0:22618cf06f45 | 121 | // |
mehatfie | 0:22618cf06f45 | 122 | // //Initialize and Convert Parameters |
mehatfie | 0:22618cf06f45 | 123 | // char *enable = lineData.word[2]; |
mehatfie | 0:22618cf06f45 | 124 | // int enableValue = atoi(enable); |
mehatfie | 0:22618cf06f45 | 125 | // |
mehatfie | 0:22618cf06f45 | 126 | // //If the atoi returned anything besides 0, it worked properly |
mehatfie | 0:22618cf06f45 | 127 | // if (enableValue) |
mehatfie | 0:22618cf06f45 | 128 | // bridges.enableBraking(getMotor(), enableValue); |
mehatfie | 0:22618cf06f45 | 129 | // else{ |
mehatfie | 0:22618cf06f45 | 130 | // //Error Check, incorrect number of parameter, error out |
mehatfie | 0:22618cf06f45 | 131 | // return 0; |
mehatfie | 0:22618cf06f45 | 132 | // } |
mehatfie | 0:22618cf06f45 | 133 | // |
mehatfie | 0:22618cf06f45 | 134 | // return 1; |
mehatfie | 0:22618cf06f45 | 135 | //} |
mehatfie | 0:22618cf06f45 | 136 | // |
mehatfie | 0:22618cf06f45 | 137 | //int Motor::forceBrake(){ |
mehatfie | 0:22618cf06f45 | 138 | // |
mehatfie | 0:22618cf06f45 | 139 | // if (lineData.numWords != 3){ |
mehatfie | 0:22618cf06f45 | 140 | // //Error Check, incorrect number of parameter, error out |
mehatfie | 0:22618cf06f45 | 141 | // return 0; |
mehatfie | 0:22618cf06f45 | 142 | // } |
mehatfie | 0:22618cf06f45 | 143 | // |
mehatfie | 0:22618cf06f45 | 144 | // bridges.forceBrake(getMotor()); |
mehatfie | 0:22618cf06f45 | 145 | //} |
mehatfie | 0:22618cf06f45 | 146 | // |
mehatfie | 0:22618cf06f45 | 147 | //int Motor::drive(){ |
mehatfie | 0:22618cf06f45 | 148 | // |
mehatfie | 0:22618cf06f45 | 149 | // if (lineData.numWords != 4){ |
mehatfie | 0:22618cf06f45 | 150 | // //Error Check, incorrect number of parameter, error out |
mehatfie | 0:22618cf06f45 | 151 | // return 0; |
mehatfie | 0:22618cf06f45 | 152 | // } |
mehatfie | 0:22618cf06f45 | 153 | // |
mehatfie | 0:22618cf06f45 | 154 | // //Initialize Parameters |
mehatfie | 0:22618cf06f45 | 155 | // char *speed = lineData.word[2]; |
mehatfie | 0:22618cf06f45 | 156 | // char *dir = lineData.word[3]; |
mehatfie | 0:22618cf06f45 | 157 | // |
mehatfie | 0:22618cf06f45 | 158 | // //Initialize Convertion Variables if needed |
mehatfie | 0:22618cf06f45 | 159 | // float speedValue; |
mehatfie | 0:22618cf06f45 | 160 | // int dirValue = 0; |
mehatfie | 0:22618cf06f45 | 161 | // |
mehatfie | 0:22618cf06f45 | 162 | // //Convert string to usable values |
mehatfie | 0:22618cf06f45 | 163 | // //NOTE both atof and atoi functions return 0 if no valid conversion could be performed |
mehatfie | 0:22618cf06f45 | 164 | // speedValue = atof(speed) / 100; |
mehatfie | 0:22618cf06f45 | 165 | // |
mehatfie | 0:22618cf06f45 | 166 | // if (speedValue <= 0) |
mehatfie | 0:22618cf06f45 | 167 | // return 0; //Error Out because a value gives no functionality or is wrong |
mehatfie | 0:22618cf06f45 | 168 | // |
mehatfie | 0:22618cf06f45 | 169 | // if (strncmp(dir,"CC", 2) == 0 || strncmp(dir,"cc", 2) == 0) |
mehatfie | 0:22618cf06f45 | 170 | // dirValue = -1; //Turn Clockwise |
mehatfie | 0:22618cf06f45 | 171 | // else if (strncmp(dir,"C", 1) == 0 || strncmp(dir,"c", 1) == 0) |
mehatfie | 0:22618cf06f45 | 172 | // dirValue = 1; //Turn CounterClockwise |
mehatfie | 0:22618cf06f45 | 173 | // else |
mehatfie | 0:22618cf06f45 | 174 | // return 0; //Error Out since the parameter is incorrect |
mehatfie | 0:22618cf06f45 | 175 | // |
mehatfie | 0:22618cf06f45 | 176 | // bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor |
mehatfie | 0:22618cf06f45 | 177 | //} |
mehatfie | 0:22618cf06f45 | 178 | //*/ |
mehatfie | 0:22618cf06f45 | 179 | // |
mehatfie | 0:22618cf06f45 | 180 | // |
mehatfie | 0:22618cf06f45 | 181 | // |