Code for RFID Robot
Dependencies: DebounceIn HTTPClient ID12RFID SDFileSystem TextLCD WiflyInterface iniparser mbed
driver.cpp
00001 #include "driver.h" 00002 #include "wifiunit.h" 00003 00004 void processCommands(vector<robotCommand> &commands) { 00005 printLCD("Processing", NULL); 00006 forward(); 00007 stop(); 00008 for(int i=0; i<commands.size(); i++) { 00009 double mag = commands[i].magnitude; 00010 int angle; 00011 switch(commands[i].commandType) { 00012 case(Forward): 00013 printLCD("Forward", NULL); 00014 forward(); 00015 wait(mag*4); 00016 break; 00017 case(Reverse): 00018 printLCD("Reverse", NULL); 00019 reverse(); 00020 printf("%f\n", mag*4); 00021 wait(mag*4); 00022 break; 00023 case(Left): 00024 printLCD("Left", NULL); 00025 angle = (int) (mag*90); 00026 //create script 00027 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); 00028 device.printf("%c", DoScript); 00029 break; 00030 case(Right): 00031 printLCD("Right", NULL); 00032 angle = (int) (mag*90); 00033 //create script 00034 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); 00035 device.printf("%c", DoScript); 00036 break; 00037 case(PlaySound): 00038 printLCD("Sound", NULL); 00039 playsong((int) mag); 00040 break; 00041 } 00042 stop(); 00043 } 00044 } 00045 00046 void initialize() { 00047 initializeRobot(); 00048 forward(); 00049 wait(.5); 00050 stop(); 00051 reverse(); 00052 wait(.5); 00053 stop(); 00054 setupSDCard(); 00055 setupWiFi(); 00056 down.mode(PullUp); 00057 up.mode(PullUp); 00058 back.mode(PullUp); 00059 select.mode(PullUp); 00060 } 00061 00062 void cancel() { 00063 printLCD("Cancelled", NULL); 00064 wait(1.5); 00065 } 00066 00067 // Demo to move around using basic commands 00068 int main() { 00069 initialize(); 00070 const char* mainMenu[4] = {"Read From RFID", "Read From SD", "Reprogram", "WiFi Mode"}; 00071 const char* reprogramType[2] = {"Command", "Magnitude"}; 00072 const char* allCommands[5] = {"Forward", "Reverse", "Left", "Right", "Play Sound"}; 00073 const char* allMagnitudes[6] = {".5", "1", "1.5", "2", "2.5", "3"}; 00074 vector<int> tagValues; 00075 vector<robotCommand>* commands; 00076 00077 while(1) { 00078 wait(.2); 00079 int option = displayMenu("Main Menu", mainMenu, 4); 00080 wait(.2); 00081 switch(option) { 00082 case(0): { //read from RFID 00083 tagValues.clear(); 00084 int rt = readTagSequence(tagValues); 00085 if(!rt) { 00086 cancel(); 00087 break; 00088 } 00089 commands = translateTags(tagValues); 00090 processCommands(*commands); 00091 delete commands; 00092 } 00093 break; 00094 case(1): //read from SD 00095 commands = getProgrammedPath(1); 00096 processCommands(*commands); 00097 delete commands; 00098 break; 00099 case(2): //reprogram 00100 int type = displayMenu("Reprogram Type", reprogramType, 2); 00101 if(type == 0) { //reprogram command 00102 CommandType repCommand = (CommandType) displayMenu("Command Type", allCommands, 5); 00103 int tagID = readTag(); 00104 if(!tagID) { 00105 cancel(); 00106 break; 00107 } 00108 writeTagCommand(tagID, repCommand); 00109 } 00110 else { //reprogram magnitude 00111 double repMag = displayMenu("Mag Type", allMagnitudes, 6)*.5+.5; 00112 int tagID = readTag(); 00113 if(!tagID) { 00114 cancel(); 00115 break; 00116 } 00117 writeTagCommand(tagID, repMag); 00118 } 00119 break; 00120 00121 case(3): 00122 int ret = readTag(); 00123 commands = getTagCommands(ret%1000); 00124 processCommands(*commands); 00125 delete commands; 00126 break; 00127 } 00128 } 00129 }
Generated on Tue Jul 12 2022 18:24:35 by 1.7.2