Code for RFID Robot
Dependencies: DebounceIn HTTPClient ID12RFID SDFileSystem TextLCD WiflyInterface iniparser mbed
driver.cpp@0:9fd64882c5aa, 2013-12-10 (annotated)
- Committer:
- 4180skrw
- Date:
- Tue Dec 10 02:17:48 2013 +0000
- Revision:
- 0:9fd64882c5aa
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180skrw | 0:9fd64882c5aa | 1 | #include "driver.h" |
4180skrw | 0:9fd64882c5aa | 2 | #include "wifiunit.h" |
4180skrw | 0:9fd64882c5aa | 3 | |
4180skrw | 0:9fd64882c5aa | 4 | void processCommands(vector<robotCommand> &commands) { |
4180skrw | 0:9fd64882c5aa | 5 | printLCD("Processing", NULL); |
4180skrw | 0:9fd64882c5aa | 6 | forward(); |
4180skrw | 0:9fd64882c5aa | 7 | stop(); |
4180skrw | 0:9fd64882c5aa | 8 | for(int i=0; i<commands.size(); i++) { |
4180skrw | 0:9fd64882c5aa | 9 | double mag = commands[i].magnitude; |
4180skrw | 0:9fd64882c5aa | 10 | int angle; |
4180skrw | 0:9fd64882c5aa | 11 | switch(commands[i].commandType) { |
4180skrw | 0:9fd64882c5aa | 12 | case(Forward): |
4180skrw | 0:9fd64882c5aa | 13 | printLCD("Forward", NULL); |
4180skrw | 0:9fd64882c5aa | 14 | forward(); |
4180skrw | 0:9fd64882c5aa | 15 | wait(mag*4); |
4180skrw | 0:9fd64882c5aa | 16 | break; |
4180skrw | 0:9fd64882c5aa | 17 | case(Reverse): |
4180skrw | 0:9fd64882c5aa | 18 | printLCD("Reverse", NULL); |
4180skrw | 0:9fd64882c5aa | 19 | reverse(); |
4180skrw | 0:9fd64882c5aa | 20 | printf("%f\n", mag*4); |
4180skrw | 0:9fd64882c5aa | 21 | wait(mag*4); |
4180skrw | 0:9fd64882c5aa | 22 | break; |
4180skrw | 0:9fd64882c5aa | 23 | case(Left): |
4180skrw | 0:9fd64882c5aa | 24 | printLCD("Left", NULL); |
4180skrw | 0:9fd64882c5aa | 25 | angle = (int) (mag*90); |
4180skrw | 0:9fd64882c5aa | 26 | //create script |
4180skrw | 0:9fd64882c5aa | 27 | device.printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", NewScript, 13, Drive, char((speed>>8)&0xFF), char(speed&0xFF), char(((1)>>8)&0xFF), char((1)&0xFF), WaitAngle, char((angle>>8)&0xFF), char(angle&0xFF), Drive, 0, 0, 0, 0); |
4180skrw | 0:9fd64882c5aa | 28 | device.printf("%c", DoScript); |
4180skrw | 0:9fd64882c5aa | 29 | break; |
4180skrw | 0:9fd64882c5aa | 30 | case(Right): |
4180skrw | 0:9fd64882c5aa | 31 | printLCD("Right", NULL); |
4180skrw | 0:9fd64882c5aa | 32 | angle = (int) (mag*90); |
4180skrw | 0:9fd64882c5aa | 33 | //create script |
4180skrw | 0:9fd64882c5aa | 34 | device.printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", NewScript, 13, Drive, char((speed>>8)&0xFF), char(speed&0xFF), char(((-1)>>8)&0xFF), char((-1)&0xFF), WaitAngle, char(((-angle)>>8)&0xFF), char((-angle)&0xFF), Drive, 0, 0, 0, 0); |
4180skrw | 0:9fd64882c5aa | 35 | device.printf("%c", DoScript); |
4180skrw | 0:9fd64882c5aa | 36 | break; |
4180skrw | 0:9fd64882c5aa | 37 | case(PlaySound): |
4180skrw | 0:9fd64882c5aa | 38 | printLCD("Sound", NULL); |
4180skrw | 0:9fd64882c5aa | 39 | playsong((int) mag); |
4180skrw | 0:9fd64882c5aa | 40 | break; |
4180skrw | 0:9fd64882c5aa | 41 | } |
4180skrw | 0:9fd64882c5aa | 42 | stop(); |
4180skrw | 0:9fd64882c5aa | 43 | } |
4180skrw | 0:9fd64882c5aa | 44 | } |
4180skrw | 0:9fd64882c5aa | 45 | |
4180skrw | 0:9fd64882c5aa | 46 | void initialize() { |
4180skrw | 0:9fd64882c5aa | 47 | initializeRobot(); |
4180skrw | 0:9fd64882c5aa | 48 | forward(); |
4180skrw | 0:9fd64882c5aa | 49 | wait(.5); |
4180skrw | 0:9fd64882c5aa | 50 | stop(); |
4180skrw | 0:9fd64882c5aa | 51 | reverse(); |
4180skrw | 0:9fd64882c5aa | 52 | wait(.5); |
4180skrw | 0:9fd64882c5aa | 53 | stop(); |
4180skrw | 0:9fd64882c5aa | 54 | setupSDCard(); |
4180skrw | 0:9fd64882c5aa | 55 | setupWiFi(); |
4180skrw | 0:9fd64882c5aa | 56 | down.mode(PullUp); |
4180skrw | 0:9fd64882c5aa | 57 | up.mode(PullUp); |
4180skrw | 0:9fd64882c5aa | 58 | back.mode(PullUp); |
4180skrw | 0:9fd64882c5aa | 59 | select.mode(PullUp); |
4180skrw | 0:9fd64882c5aa | 60 | } |
4180skrw | 0:9fd64882c5aa | 61 | |
4180skrw | 0:9fd64882c5aa | 62 | void cancel() { |
4180skrw | 0:9fd64882c5aa | 63 | printLCD("Cancelled", NULL); |
4180skrw | 0:9fd64882c5aa | 64 | wait(1.5); |
4180skrw | 0:9fd64882c5aa | 65 | } |
4180skrw | 0:9fd64882c5aa | 66 | |
4180skrw | 0:9fd64882c5aa | 67 | // Demo to move around using basic commands |
4180skrw | 0:9fd64882c5aa | 68 | int main() { |
4180skrw | 0:9fd64882c5aa | 69 | initialize(); |
4180skrw | 0:9fd64882c5aa | 70 | const char* mainMenu[4] = {"Read From RFID", "Read From SD", "Reprogram", "WiFi Mode"}; |
4180skrw | 0:9fd64882c5aa | 71 | const char* reprogramType[2] = {"Command", "Magnitude"}; |
4180skrw | 0:9fd64882c5aa | 72 | const char* allCommands[5] = {"Forward", "Reverse", "Left", "Right", "Play Sound"}; |
4180skrw | 0:9fd64882c5aa | 73 | const char* allMagnitudes[6] = {".5", "1", "1.5", "2", "2.5", "3"}; |
4180skrw | 0:9fd64882c5aa | 74 | vector<int> tagValues; |
4180skrw | 0:9fd64882c5aa | 75 | vector<robotCommand>* commands; |
4180skrw | 0:9fd64882c5aa | 76 | |
4180skrw | 0:9fd64882c5aa | 77 | while(1) { |
4180skrw | 0:9fd64882c5aa | 78 | wait(.2); |
4180skrw | 0:9fd64882c5aa | 79 | int option = displayMenu("Main Menu", mainMenu, 4); |
4180skrw | 0:9fd64882c5aa | 80 | wait(.2); |
4180skrw | 0:9fd64882c5aa | 81 | switch(option) { |
4180skrw | 0:9fd64882c5aa | 82 | case(0): { //read from RFID |
4180skrw | 0:9fd64882c5aa | 83 | tagValues.clear(); |
4180skrw | 0:9fd64882c5aa | 84 | int rt = readTagSequence(tagValues); |
4180skrw | 0:9fd64882c5aa | 85 | if(!rt) { |
4180skrw | 0:9fd64882c5aa | 86 | cancel(); |
4180skrw | 0:9fd64882c5aa | 87 | break; |
4180skrw | 0:9fd64882c5aa | 88 | } |
4180skrw | 0:9fd64882c5aa | 89 | commands = translateTags(tagValues); |
4180skrw | 0:9fd64882c5aa | 90 | processCommands(*commands); |
4180skrw | 0:9fd64882c5aa | 91 | delete commands; |
4180skrw | 0:9fd64882c5aa | 92 | } |
4180skrw | 0:9fd64882c5aa | 93 | break; |
4180skrw | 0:9fd64882c5aa | 94 | case(1): //read from SD |
4180skrw | 0:9fd64882c5aa | 95 | commands = getProgrammedPath(1); |
4180skrw | 0:9fd64882c5aa | 96 | processCommands(*commands); |
4180skrw | 0:9fd64882c5aa | 97 | delete commands; |
4180skrw | 0:9fd64882c5aa | 98 | break; |
4180skrw | 0:9fd64882c5aa | 99 | case(2): //reprogram |
4180skrw | 0:9fd64882c5aa | 100 | int type = displayMenu("Reprogram Type", reprogramType, 2); |
4180skrw | 0:9fd64882c5aa | 101 | if(type == 0) { //reprogram command |
4180skrw | 0:9fd64882c5aa | 102 | CommandType repCommand = (CommandType) displayMenu("Command Type", allCommands, 5); |
4180skrw | 0:9fd64882c5aa | 103 | int tagID = readTag(); |
4180skrw | 0:9fd64882c5aa | 104 | if(!tagID) { |
4180skrw | 0:9fd64882c5aa | 105 | cancel(); |
4180skrw | 0:9fd64882c5aa | 106 | break; |
4180skrw | 0:9fd64882c5aa | 107 | } |
4180skrw | 0:9fd64882c5aa | 108 | writeTagCommand(tagID, repCommand); |
4180skrw | 0:9fd64882c5aa | 109 | } |
4180skrw | 0:9fd64882c5aa | 110 | else { //reprogram magnitude |
4180skrw | 0:9fd64882c5aa | 111 | double repMag = displayMenu("Mag Type", allMagnitudes, 6)*.5+.5; |
4180skrw | 0:9fd64882c5aa | 112 | int tagID = readTag(); |
4180skrw | 0:9fd64882c5aa | 113 | if(!tagID) { |
4180skrw | 0:9fd64882c5aa | 114 | cancel(); |
4180skrw | 0:9fd64882c5aa | 115 | break; |
4180skrw | 0:9fd64882c5aa | 116 | } |
4180skrw | 0:9fd64882c5aa | 117 | writeTagCommand(tagID, repMag); |
4180skrw | 0:9fd64882c5aa | 118 | } |
4180skrw | 0:9fd64882c5aa | 119 | break; |
4180skrw | 0:9fd64882c5aa | 120 | |
4180skrw | 0:9fd64882c5aa | 121 | case(3): |
4180skrw | 0:9fd64882c5aa | 122 | int ret = readTag(); |
4180skrw | 0:9fd64882c5aa | 123 | commands = getTagCommands(ret%1000); |
4180skrw | 0:9fd64882c5aa | 124 | processCommands(*commands); |
4180skrw | 0:9fd64882c5aa | 125 | delete commands; |
4180skrw | 0:9fd64882c5aa | 126 | break; |
4180skrw | 0:9fd64882c5aa | 127 | } |
4180skrw | 0:9fd64882c5aa | 128 | } |
4180skrw | 0:9fd64882c5aa | 129 | } |