Austin O'Connell / Mbed 2 deprecated Menu4180

Dependencies:   4DGL-uLCD-SE Motordriver PinDetect RemoteIR mbed-rtos mbed

Fork of Menu4180 by Brandon Lasater

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 #include "motordriver.h"
00004 #include "PinDetect.h"
00005 #include "rtos.h"
00006 #include "ReceiverIR.h"
00007 
00008 uLCD_4DGL lcd(p28, p27, p29); // LCD screen
00009 Mutex stdio_mutex; // Mutex for LCD
00010 
00011 DigitalIn pb(p7); //LCD menu Pushbuttons
00012 DigitalIn pb1(p10); 
00013 DigitalIn pb2(p13); 
00014 DigitalIn pb3(p16); 
00015 PinDetect pb8(p8); // Pushbutton to start motors
00016 
00017 AnalogIn IRout(p20); // IR Module (front sensor)
00018 
00019 Motor left(p21,p22,p23,1);
00020 Motor right(p26,p25,p24,1);
00021 
00022 ReceiverIR ir_rx(p15); // IR receiver
00023 RemoteIR::Format format;
00024 
00025 DigitalOut led1(LED1); //LED1 used for debugging purposes
00026 DigitalOut led2(LED2);
00027 //Timer t; //Timer used to calculate automated driving distance
00028 
00029 //Globals
00030 bool OnOff = false; //turn motors on/off
00031 uint8_t buf[32];
00032 int bitcount;
00033 uint8_t dat; //used to Identify button pressed on Universal remote
00034 
00035 // push button hit callback to start/stop motor immediately
00036 void pb8_hit_callback (void) { 
00037     OnOff = !OnOff;   
00038 }
00039 
00040 void display_binary(uint8_t *buf, uint8_t cnt){
00041     dat = *(buf + 2);
00042     printf("%02x",dat);
00043     printf("\n\r");
00044 }
00045 
00046 // IR module in front of robot, stop motors if object detected
00047 void IRmod(void const *args){
00048     while(1){
00049         if(IRout > 0.35f ){
00050             OnOff = false;
00051             led2 = 1;
00052         }   
00053         else{
00054             //OnOff = false;
00055             led2 = 0;
00056         }
00057     }
00058 }
00059 
00060 // Motor manual control using IR Receiver with Universal Remote
00061 void IRrec(void const *args){
00062     while(1){
00063         if(OnOff == false){
00064             if (ir_rx.getState() == ReceiverIR::Received) {
00065                 bitcount = ir_rx.getData(&format, buf, sizeof(buf) * 8);
00066                 dat = ((bitcount + 7) / 8);
00067                 dat = *(buf + 2);
00068                 printf("%02x\n\r",dat);
00069                 switch (dat){
00070                     case 0x01:
00071                         left.speed(0.8);
00072                         right.speed(0.8);
00073                         wait(0.001);
00074                         break;
00075                     case 0x03:
00076                         left.speed(0.8);
00077                         right.speed(-0.8);
00078                         wait(0.001);
00079                         break;
00080                     case 0x05:
00081                         left.speed(-0.8);
00082                         right.speed(0.8);
00083                         wait(0.001);
00084                         break;
00085                     case 0x07:
00086                         left.speed(-0.8);
00087                         right.speed(-0.8);
00088                         wait(0.001);
00089                         break;
00090                     case 0x00: // End manual override, return to automated route
00091                         left.stop(1);
00092                         right.stop(1);
00093                         OnOff = true;
00094                         break;
00095                 }
00096             }
00097             else if (ir_rx.getState() == ReceiverIR::Receiving){
00098                 switch (dat){
00099                     case 0x01:
00100                         left.speed(0.8);
00101                         right.speed(0.8);
00102                         wait(0.001);
00103                         break;
00104                     case 0x03:
00105                         left.speed(-0.6);
00106                         right.speed(0.6);
00107                         wait(0.001);
00108                         break;
00109                     case 0x05:
00110                         left.speed(0.6);
00111                         right.speed(-0.6);
00112                         wait(0.001);
00113                         break;
00114                     case 0x07:
00115                         left.speed(-0.8);
00116                         right.speed(-0.8);
00117                         wait(0.001);
00118                         break;
00119                     case 0x00: // End manual override, return to automated route
00120                         left.stop(1);
00121                         right.stop(1);
00122                         OnOff = true;
00123                         break;
00124                 }
00125             }
00126             else if (ir_rx.getState() == ReceiverIR::Idle) {
00127                 wait(0.1);
00128                 if(ir_rx.getState() == ReceiverIR::Idle){
00129                     left.stop(1);
00130                     right.stop(1);
00131                     wait(0.001);
00132                 }
00133             }
00134         }
00135     }   
00136 }
00137 
00138 void rightTurn(){  //Turn bot 90 degrees right
00139     left.speed(-0.6);
00140     right.speed(0.6);
00141     wait(0.5);
00142     left.stop(1);
00143     right.stop(1);
00144 }
00145 
00146 void moveAhead(float length){
00147     Timer t;
00148     t.start();
00149     while(t.read() < length){
00150         if(OnOff == true){
00151             left.speed(0.8);
00152             right.speed(0.8);
00153         }
00154         else{
00155             left.stop(1);
00156             right.stop(1);
00157         }
00158     }
00159     left.stop(1);
00160     right.stop(1);
00161     t.stop();   
00162 }
00163 
00164 int main() {
00165     led1 = 0;
00166     Thread thread1(IRmod); // Create IRmod thread
00167     Thread thread3(IRrec); // Create IR receiver thread for manual motor control
00168     
00169     lcd.baudrate(3000000); // Set LCD baud rate
00170     lcd.background_color(0);
00171     lcd.cls(); // clear LCD screen
00172     
00173     pb8.mode(PullUp); //internal pullup for push button 8
00174     wait(0.001); // initial delay for pullup
00175     pb8.attach_deasserted(&pb8_hit_callback); // Setup pb interrupt callback func
00176     pb8.setSampleFrequency(); //Start sampling pb input using interrupt
00177     
00178     pb.mode(PullUp); //Intitial Pusbutton Setup
00179     pb1.mode(PullUp);
00180     pb2.mode(PullUp);
00181     pb3.mode(PullUp);
00182     wait(.001);
00183     
00184     // Initial LCD menu setup
00185     /*
00186     stdio_mutex.lock();
00187     lcd.locate(1,2); 
00188     lcd.printf("Add Stop<");
00189     lcd.locate(1,4);
00190     lcd.printf("Remove Stop ");
00191     lcd.locate(1,6);
00192     lcd.printf("Exit ");
00193     stdio_mutex.unlock();
00194     */
00195     int stop = 1; // counter for which Bus Stop the bot is at
00196     bool running = 1; //alternates between moving or stopping
00197     
00198     //bool count = false;
00199     //int old_pb=1;
00200     //int new_pb;
00201     
00202     //int count1=0;
00203     //int old_pb1=1;
00204     //int new_pb1;
00205     
00206     //int old_pb2=1;
00207     //int new_pb2;
00208     
00209     //bool count3 = false; //whether enter has been pressed
00210     //bool count4 = true; //whether it is currently on add stops or remove stops
00211     //int old_pb3=1;
00212     //int new_pb3;
00213     //bool Menu = false;
00214     
00215  //Set up the booleans determining which stops are currently being serviced
00216    // bool stop1 = true;
00217    // bool stop2 = true;
00218     //bool stop3 = true;
00219     //bool stop4 = true;
00220     
00221     //bool running = true;
00222     //int stop = 1;
00223     
00224     while(1) {
00225         
00226         if(OnOff == true){ //Keeps Led1 in same state as OnOff (for debugging)
00227             led1 = 1;
00228         }
00229         else{
00230             led1 = 0;
00231         }
00232         
00233         if(OnOff == true){
00234             if(running && stop == 1){ // Moving to bus stop 1
00235                 stdio_mutex.lock();
00236                 lcd.cls();
00237                 lcd.locate(1,5);
00238                 lcd.printf("Moving to Stop 1");
00239                 stdio_mutex.unlock();
00240                 moveAhead(2); // Move forward 1.5 seconds
00241                 running = !running;
00242             }
00243             else if(!running && stop == 1){ // Stopped at bus stop 1
00244                 stdio_mutex.lock();
00245                 lcd.cls();
00246                 lcd.locate(1,5);
00247                 lcd.printf("Waiting at Stop 1");
00248                 stdio_mutex.unlock();
00249                 Thread::wait(5000); // Wait 5 seconds at Bus Stop
00250                 rightTurn(); // 90 degree right turn
00251                 running = !running;
00252                 stop++;
00253             }
00254             else if(running && stop == 2){ //moving to bus stop 2
00255                 stdio_mutex.lock();
00256                 lcd.cls();
00257                 lcd.locate(1,5);
00258                 lcd.printf("Moving to Stop 2");
00259                 stdio_mutex.unlock();
00260                 moveAhead(1.5); // Move forward for 1 second
00261                 running = !running;
00262             }
00263             else if(!running && stop == 2){ // Stopped at bus stop 2
00264                 stdio_mutex.lock();
00265                 lcd.cls();
00266                 lcd.locate(1,5);
00267                 lcd.printf("Waiting at Stop 2");
00268                 stdio_mutex.unlock();
00269                 Thread::wait(5000);
00270                 rightTurn();
00271                 running = !running;
00272                 stop++;
00273             }
00274             else if(running && stop == 3){ // moving to bus stop 3
00275                 stdio_mutex.lock();
00276                 lcd.cls();
00277                 lcd.locate(1,5);
00278                 lcd.printf("Moving to Stop 3");
00279                 stdio_mutex.unlock();
00280                 moveAhead(2);
00281                 running = !running;
00282             }
00283             else if(!running && stop == 3){ // stopped at bus stop 3
00284                 stdio_mutex.lock();
00285                 lcd.cls();
00286                 lcd.locate(1,5);
00287                 lcd.printf("Waiting at Stop 3");
00288                 stdio_mutex.unlock();
00289                 Thread::wait(5000);
00290                 rightTurn();
00291                 running = !running;
00292                 stop++;
00293             }
00294             else if(running && stop == 4){ // moving to bus stop 4
00295                 stdio_mutex.lock();
00296                 lcd.cls();
00297                 lcd.locate(1,5);
00298                 lcd.printf("Moving to Stop 4");
00299                 stdio_mutex.unlock();
00300                 moveAhead(1.5);
00301                 running = !running;
00302             }
00303             else if(!running && stop == 4){ // stopped at bus stop 4
00304                 stdio_mutex.lock();
00305                 lcd.cls();
00306                 lcd.locate(1,5);
00307                 lcd.printf("Waiting at Stop 4");
00308                 stdio_mutex.unlock();
00309                 Thread::wait(5000);
00310                 rightTurn();
00311                 running = !running;
00312                 stop = 1; // reset back to start
00313             }
00314         }
00315 
00316         //new_pb = pb;
00317         //new_pb1 = pb1;
00318         //new_pb2 = pb2;
00319         //new_pb3 = pb3;
00320         
00321         
00322         
00323         /* LCD Add/Remove Bus Stop Functionality
00324         // If NOT currently in "Menu" mode (motor running)
00325         if(!Menu && OnOff == true) {
00326             if(running && stop == 1){
00327                 stdio_mutex.lock();
00328                 lcd.locate(1,5);
00329                 lcd.printf("Moving to Stop1 ");  // move motors to stop 1  4 feet frwrd
00330                 stdio_mutex.unlock();
00331                 moveAhead(1); // move 4 seconds
00332                 running = !running;
00333             }
00334             else if(!running && stop == 1){
00335                 stdio_mutex.lock();
00336                 lcd.locate(1,5);
00337                 lcd.printf("Waiting at Stop1");   // stop motors 5 seconds
00338                 stdio_mutex.unlock();
00339                 //Thread::wait(5000); // 5 second wait
00340                 rightTurn();
00341                 running = !running;
00342                 stop++;
00343             }
00344             else if(running && stop == 2){
00345                 stdio_mutex.lock();
00346                 lcd.locate(1,5);
00347                 lcd.printf("Moving to Stop2 ");    // move to stop 2. 90 deg right turn + forward 3 feet
00348                 stdio_mutex.unlock();
00349             }
00350             else if(!running && stop == 2){
00351                 stdio_mutex.lock();
00352                 lcd.locate(1,5);
00353                 lcd.printf("Waiting at Stop2");   
00354                 stdio_mutex.unlock(); 
00355             }
00356             else if(running && stop == 3){
00357                 stdio_mutex.lock();
00358                 lcd.locate(1,5);
00359                 lcd.printf("Moving to Stop3");    
00360                 stdio_mutex.unlock();
00361             }
00362             else if(!running && stop == 3){
00363                 stdio_mutex.lock();
00364                 lcd.locate(1,5);
00365                 lcd.printf("Waiting at Stop3");   
00366                 stdio_mutex.unlock(); 
00367             }
00368             else if(running && stop == 4){
00369                 stdio_mutex.lock();
00370                 lcd.locate(1,5);
00371                 lcd.printf("Moving to Stop4 "); 
00372                 stdio_mutex.unlock();   
00373             }
00374             else if(!running && stop == 4){
00375                 stdio_mutex.lock();
00376                 lcd.locate(1,5);
00377                 lcd.printf("Waiting at Stop4");    
00378                 stdio_mutex.unlock();
00379             }
00380         }        
00381         else if(Menu) { //All of the below runs the menu interactions
00382         
00383             if ((new_pb==0) && (old_pb==1)) count = !count;
00384             if ((new_pb1==0) && (old_pb1==1) && count1 < 3) count1++;
00385             if ((new_pb2==0) && (old_pb2==1) && count1 > 0) count1--;
00386             if ((new_pb3==0) && (old_pb3==1)) {
00387                 count3 = !count3;
00388                 lcd.cls();
00389                 count1 = 0;
00390             }
00391         
00392             if(count && !count3){
00393                 lcd.cls();
00394                 count1 = 0;
00395                 count = false;
00396             }
00397         
00398             if(count && count3 && count4){
00399                 if(count1 == 0) stop1 = true;
00400                 if(count1 == 1) stop2 = true;
00401                 if(count1 == 2) stop3 = true;
00402                 if(count3 == 3) stop4 = true;
00403                 count = false;
00404             }
00405         
00406             if(count && count3 && count4){
00407                 if(count1 == 0) stop1 = false;
00408                 if(count1 == 1) stop2 = false;
00409                 if(count1 == 2) stop3 = false;
00410                 if(count3 == 3) stop4 = false;
00411                 count = false;
00412             }
00413         
00414             if(!count) {
00415             if(!count3) {
00416                 stdio_mutex.lock();
00417                 lcd.locate(1,1);
00418                 lcd.printf("             ");
00419                 stdio_mutex.unlock();
00420             }
00421 
00422             if(count3 && count4 && count1==0) {
00423                 stdio_mutex.lock();
00424                 lcd.locate(1,1);
00425                 lcd.printf("Add stops");
00426                 lcd.locate(1,2); //Initial Menu Setup for adding stops
00427                 lcd.printf("Stop 1<        ");
00428                 lcd.locate(1,4);
00429                 lcd.printf("Stop 2         ");
00430                 lcd.locate(1,6);
00431                 lcd.printf("Stop 3 ");
00432                 lcd.locate(1,8);
00433                 lcd.printf("Stop 4 ");
00434                 stdio_mutex.unlock();
00435             }
00436             if(count3 && count4 && count1==1) {
00437                 stdio_mutex.lock();
00438                 lcd.locate(1,1);
00439                 lcd.printf("Add stops");
00440                 lcd.locate(1,2); //Initial Menu Setup for adding stops
00441                 lcd.printf("Stop 1         ");
00442                 lcd.locate(1,4);
00443                 lcd.printf("Stop 2<        ");
00444                 lcd.locate(1,6);
00445                 lcd.printf("Stop 3 ");
00446                 lcd.locate(1,8);
00447                 lcd.printf("Stop 4 ");
00448                 stdio_mutex.unlock();
00449             }
00450             if(count3 && count4 && count1==2) {
00451                 stdio_mutex.lock();
00452                 lcd.locate(1,1);
00453                 lcd.printf("Add stops");
00454                 lcd.locate(1,2); //Initial Menu Setup for adding stops
00455                 lcd.printf("Stop 1         ");
00456                 lcd.locate(1,4);
00457                 lcd.printf("Stop 2         ");
00458                 lcd.locate(1,6);
00459                 lcd.printf("Stop 3<");
00460                 lcd.locate(1,8);
00461                 lcd.printf("Stop 4 ");
00462                 stdio_mutex.unlock();
00463             }
00464             if(count3 && count4 && count1==3) {
00465                 stdio_mutex.lock();
00466                 lcd.locate(1,1);
00467                 lcd.printf("Add stops");
00468                 lcd.locate(1,2); //Initial Menu Setup for adding stops
00469                 lcd.printf("Stop 1         ");
00470                 lcd.locate(1,4);
00471                 lcd.printf("Stop 2         ");
00472                 lcd.locate(1,6);
00473                 lcd.printf("Stop 3 ");
00474                 lcd.locate(1,8);
00475                 lcd.printf("Stop 4<");
00476                 stdio_mutex.unlock();
00477             }
00478             
00479             if(count3 && !count4 && count1 == 0) {
00480                 stdio_mutex.lock();
00481                 lcd.locate(1,1);
00482                 lcd.printf("Remove Stops");
00483                 lcd.locate(1,2); //Initial Menu Setup for removing
00484                 lcd.printf("Stop 1<        ");
00485                 lcd.locate(1,4);
00486                 lcd.printf("Stop 2         ");
00487                 lcd.locate(1,6);
00488                 lcd.printf("Stop 3 ");
00489                 lcd.locate(1,8);
00490                 lcd.printf("Stop 4 ");
00491                 stdio_mutex.unlock();
00492             }
00493             if(count3 && !count4 && count1 == 1) {
00494                 stdio_mutex.lock();
00495                 lcd.locate(1,1);
00496                 lcd.printf("Remove Stops");
00497                 lcd.locate(1,2); //Initial Menu Setup for removing
00498                 lcd.printf("Stop 1         ");
00499                 lcd.locate(1,4);
00500                 lcd.printf("Stop 2<        ");
00501                 lcd.locate(1,6);
00502                 lcd.printf("Stop 3 ");
00503                 lcd.locate(1,8);
00504                 lcd.printf("Stop 4 ");
00505                 stdio_mutex.unlock();
00506             }
00507             if(count3 && !count4 && count1 == 2) {
00508                 stdio_mutex.lock();
00509                 lcd.locate(1,1);
00510                 lcd.printf("Remove Stops");
00511                 lcd.locate(1,2); //Initial Menu Setup for removing
00512                 lcd.printf("Stop 1         ");
00513                 lcd.locate(1,4);
00514                 lcd.printf("Stop 2         ");
00515                 lcd.locate(1,6);
00516                 lcd.printf("Stop 3<");
00517                 lcd.locate(1,8);
00518                 lcd.printf("Stop 4 ");
00519                 stdio_mutex.unlock();
00520             }if(count3 && !count4 && count1 == 3) {
00521                 stdio_mutex.lock();
00522                 lcd.locate(1,1);
00523                 lcd.printf("Remove Stops");
00524                 lcd.locate(1,2); //Initial Menu Setup for removing
00525                 lcd.printf("Stop 1         ");
00526                 lcd.locate(1,4);
00527                 lcd.printf("Stop 2         ");
00528                 lcd.locate(1,6);
00529                 lcd.printf("Stop 3 ");
00530                 lcd.locate(1,8);
00531                 lcd.printf("Stop 4<");
00532                 stdio_mutex.unlock();
00533             }
00534             if(count1 == 0 && !count3) {
00535                 stdio_mutex.lock();
00536                 lcd.locate(1,2); //Initial Menu Setup
00537                 lcd.printf("Add Stop<"); 
00538                 stdio_mutex.unlock();
00539                 count4 = true;   
00540             } if(count1 != 0 && !count3) {
00541                 stdio_mutex.lock();
00542                 lcd.locate(1,2); //Initial Menu Setup
00543                 lcd.printf("Add Stop ");
00544                 stdio_mutex.unlock();
00545             }
00546             
00547             if(count1 == 1 && !count3) {
00548                 stdio_mutex.lock();
00549                 lcd.locate(1,4);
00550                 lcd.printf("Remove Stop<");  
00551                 stdio_mutex.unlock(); 
00552                 count4 = false; 
00553             } if(count1 != 1 && !count3) {
00554                 stdio_mutex.lock();
00555                 lcd.locate(1,4);
00556                 lcd.printf("Remove Stop ");
00557                 stdio_mutex.unlock();
00558             }
00559             
00560             if(count1 == 2 && !count3) {
00561                 stdio_mutex.lock();
00562                 lcd.locate(1,6);
00563                 lcd.printf("Exit<");   
00564                 stdio_mutex.unlock(); 
00565             } if(count1 != 2 && !count3) {
00566                 stdio_mutex.lock();
00567                 lcd.locate(1,6);
00568                 lcd.printf("Exit ");
00569                 stdio_mutex.unlock();
00570             }
00571             }
00572             }
00573         
00574         
00575         old_pb = new_pb;
00576         old_pb1 = new_pb1;
00577         old_pb2 = new_pb2;
00578         old_pb3 = new_pb3; */
00579     }
00580 }