This program is a user interface to send gesture commands to the Magician Robot.

Dependencies:   4DGL-uLCD-SE APDS_9960 PinDetect SDFileSystem mbed wave_player

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 "glibr.h"
00004 #include "PinDetect.h"
00005 #include "string"
00006 #include "SDFileSystem.h"
00007 #include "wave_player.h"
00008 
00009 glibr GSensor(p9,p10);
00010 
00011 PinDetect pb2(p15);
00012 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00013 AnalogOut DACout(p18);
00014 wave_player waver(&DACout);
00015 DigitalOut myled(LED1);
00016 DigitalOut myled2(LED2);
00017 Serial xbee2(p13, p14);
00018 uLCD_4DGL lcd(p28, p27, p30);
00019 
00020 #define DEBUGLCD 0
00021 #ifdef DEBUGLCD
00022 Serial pc2(USBTX,USBRX);
00023 #endif
00024 //DigitalOut INT(p11);
00025 //uint16_t ambient_light = 0;
00026 //uint16_t red_light = 0;
00027 //uint16_t green_light = 0;
00028 //uint16_t blue_light = 0;
00029 //int volatile stop=0;
00030 //int volatile go=0;
00031 string str;
00032 
00033 
00034 void setup()
00035 {
00036 
00037     lcd.baudrate(3000000);
00038     lcd.background_color(0);
00039     lcd.cls();
00040     
00041     lcd.printf("Initializing...");
00042     wait(0.2);
00043     lcd.cls();
00044     lcd.color(RED); 
00045 }
00046 
00047 void printRIG(int column, int roww){
00048     lcd.cls();
00049     lcd.text_width(1);
00050     lcd.text_height(1);
00051     int col, row, r, c;    
00052 
00053     /* Draw staff of arrow */
00054     for(col=3; col<13; col++)
00055         for(row=5; row<10; row++)
00056             lcd.text_char('=', col, row, GREEN);
00057 
00058     /* Draw arrowhead */
00059     lcd.text_char('*', col-2, 4, GREEN); lcd.text_char('*', col-2, 3, GREEN); 
00060     lcd.text_char('*', col-1, 4, GREEN);         
00061     int a = 5, b=5;
00062     for(c = col; c<(col+3); c++){             
00063         for(r = a; r<(a+b); r++)
00064             lcd.text_char('*', c, r, GREEN);
00065         a++; b-=2;
00066     }       
00067     lcd.text_char('*', 11, row, GREEN); lcd.text_char('*', 11, row+1, GREEN); 
00068     lcd.text_char('*', 12, row, GREEN);
00069        
00070     /* Print Command String */        
00071     lcd.text_width(2);
00072     lcd.text_height(2);    
00073     lcd.text_string("RIG", column, roww, FONT_7X8, WHITE);
00074 }
00075 
00076 void printLEF(int column, int roww){
00077     lcd.cls();
00078     lcd.text_width(1);
00079     lcd.text_height(1);
00080     int col, row, r, c;    
00081 
00082     /* Draw staff of arrow */
00083     for(col=15; col>=6; col--)
00084         for(row=5; row<10; row++)
00085             lcd.text_char('=', col, row, GREEN);
00086     /* Draw arrowhead */
00087     lcd.text_char('*', col+2, 4, GREEN); lcd.text_char('*', col+2, 3, GREEN); lcd.text_char('*', col+1, 4, GREEN);                 
00088     lcd.text_char('*', col+2, row, GREEN); lcd.text_char('*', col+1, row, GREEN); lcd.text_char('*', col+2, row+1, GREEN);
00089     int a = 5, b=5;
00090     for(c = col; c>(col-3); c--){             
00091         for(r = a; r<(a+b); r++)
00092             lcd.text_char('*', c, r, GREEN);
00093         a++; b-=2;
00094     }
00095 
00096     /* Print Command String */        
00097     lcd.text_width(2);
00098     lcd.text_height(2);    
00099     lcd.text_string("LEF", column, roww, FONT_7X8, WHITE);
00100 }
00101 
00102 void printBWD(int column, int roww){
00103     lcd.cls();
00104     lcd.text_width(1);
00105     lcd.text_height(1);
00106     int col, row, r, c;
00107 
00108     /* Draw staff of arrow */
00109     for(row = 2; row<7; row++)
00110         for(col = 5; col<14; col++)
00111             lcd.text_char('|',col, row, GREEN);
00112 
00113     /* Draw arrowhead */
00114     lcd.text_char('_', col, row-1, GREEN); lcd.text_char('_', col+1, row-1, GREEN); 
00115     lcd.text_char('_', 4, row-1, GREEN); lcd.text_char('_', 3, row-1, GREEN); 
00116     int a = 5, b=8;
00117     for(r=row; r<=(row+5); r++){
00118         for(c=a; c<=(a+b); c++)
00119             lcd.text_char('*', c, r, GREEN);
00120         a++; b-=2;
00121     }
00122 
00123     /* Print Command String */        
00124     lcd.text_width(2);
00125     lcd.text_height(2);    
00126     lcd.text_string("BWD", column, roww, FONT_7X8, WHITE);        
00127 }
00128 
00129 void printFWD(int column, int roww){
00130     lcd.cls();
00131     lcd.text_width(1);
00132     lcd.text_height(1);
00133     int col, row, r, c;
00134  
00135     /* Draw staff of arrow */
00136     for(row = 10; row>=6; row--)
00137         for(col = 5; col<14; col++)
00138             lcd.text_char('|',col, row, GREEN);
00139  
00140     /* Draw arrowhead */
00141     lcd.text_char('_', col, row, GREEN); lcd.text_char('_', col+1, row, GREEN); 
00142     lcd.text_char('_', 4, row, GREEN); lcd.text_char('_', 3, row, GREEN); 
00143     int a = 5, b=8;
00144     for(r=row; r>=(row-5); r--){
00145         for(c=a; c<=(a+b); c++)
00146             lcd.text_char('*', c, r, GREEN);
00147         a++; b-=2;
00148     }
00149     
00150     /* Print Command String */        
00151         lcd.text_width(2);
00152         lcd.text_height(2);    
00153         lcd.text_string("FWD", column, roww, FONT_7X8, WHITE);  
00154 }
00155 
00156 void printERR(int column, int roww){
00157     lcd.cls();    
00158    // lcd.filled_circle(64 , 60 , 20, WHITE);          
00159     lcd.text_width(8);
00160     lcd.text_height(8);
00161     lcd.text_string("!!!", column, roww, FONT_7X8, RED);    
00162     lcd.text_string("!!!", column, roww+1, FONT_7X8, RED);
00163     lcd.text_width(2);
00164     lcd.text_height(2);
00165     lcd.text_string("ERR", 3, 7, FONT_7X8, WHITE);  
00166     
00167 }
00168 
00169 void printSTOP(int column, int roww){
00170     lcd.cls();    
00171     lcd.filled_circle(64 , 60 , 35, RED);        
00172     lcd.text_width(2);
00173     lcd.text_height(2);
00174     lcd.text_string("STP", column, roww, FONT_7X8, WHITE);    
00175 }
00176 
00177 
00178 
00179 
00180 void pb2_hit_callback (void) {
00181     myled2 =!myled2;
00182      str.append("S");
00183       #ifdef DEBUGLCD
00184         pc2.printf("%s\n", str);
00185       #endif
00186    xbee2.putc('S');
00187    printSTOP(3, 7); 
00188  //  FILE *Stop;
00189 //   Stop=fopen("/sd/Stop.wav","r");
00190 //   if(Stop!=NULL){
00191 //    waver.play(Stop);
00192 //    }else{
00193 //    pc2.printf("Stop not open");
00194 //   }
00195 //    fclose(Stop);
00196    str.clear();
00197 }
00198 
00199 int main()
00200 {
00201    
00202     bool gerror=false;
00203 
00204     if (GSensor.ginit()) {
00205         
00206         #ifdef DEBUGLCD
00207         pc2.printf("APDS-9960 initialization complete\n\r");
00208         #endif
00209     } else {
00210         #ifdef DEBUGLCD
00211         pc2.printf("Something went wrong during APDS-9960 init\n\r");
00212         #endif
00213         gerror=true;
00214     }
00215 
00216     // Start running the APDS-9960 gesture sensor engine
00217     if ( GSensor.enableGestureSensor(true) ) {
00218         #ifdef DEBUGLCD
00219         pc2.printf("Gesture sensor is now running\n\r");
00220         #endif
00221     } else {
00222         #ifdef DEBUGLCD
00223         pc2.printf("Something went wrong during gesture sensor init!\n\r");
00224         #endif
00225       gerror=true;
00226 
00227     }
00228 
00229 
00230   
00231     pb2.mode(PullUp);
00232     // Delay for initial pullup to take effect
00233     wait(.01);
00234     // Setup Interrupt callback function for a pb hit
00235     pb2.attach_deasserted(&pb2_hit_callback);
00236     // Start sampling pb input using interrupts
00237     pb2.setSampleFrequency();
00238     setup();
00239     
00240     while(!(GSensor.isGestureAvailable())){        
00241         lcd.text_width(2);
00242         lcd.text_height(2);  
00243         lcd.text_string("READY", 2, 2, FONT_12X16, WHITE);     
00244         lcd.text_string("FOR", 3, 3, FONT_12X16, WHITE);
00245         lcd.text_string("INPUT", 2, 4, FONT_12X16, WHITE);
00246         wait(0.5);
00247         lcd.cls();
00248         wait(0.5);
00249     }
00250     
00251     int temp;
00252     FILE *Forward;
00253     FILE *Backward;
00254     FILE *Left;
00255     FILE *Right;
00256     FILE *Error;
00257     FILE *Stop;
00258 
00259     while(gerror==false) {
00260         
00261         /*ret = GSensor.isGestureAvailable();
00262          pc2.printf("Is Gesture Available?: %d\n", ret);
00263          myled = ret;
00264          val = GSensor.readGesture();
00265          */
00266            
00267         
00268          if ( GSensor.isGestureAvailable()  || xbee2.readable()) {
00269               
00270             if(xbee2.readable()&& xbee2.getc()=='C'){
00271                 temp= 67;
00272             }else if(GSensor.isGestureAvailable()){
00273                 temp=GSensor.readGesture();
00274             }
00275             
00276             
00277             switch ( temp) {
00278                 case DIR_UP:
00279                 #ifdef DEBUGLCD
00280                     pc2.printf("Forward\n");
00281                     #endif
00282                      xbee2.putc('F');
00283                     printFWD(3, 7);
00284                 Forward=fopen("/sd/Forward.wav","r");
00285                     if(Forward!=NULL){
00286                      waver.play(Forward);
00287                     }else{pc2.printf("Forward not open");}
00288                      fclose(Forward);
00289                     str.append("F");
00290                     break;
00291                 case DIR_DOWN:
00292                     #ifdef DEBUGLCD
00293                     pc2.printf("Backward\n");
00294                     #endif
00295                     xbee2.putc('B');
00296                     printBWD(3, 7);
00297                    Backward=fopen("/sd/Backward.wav","r");
00298                     if(Backward!=NULL){
00299                     waver.play(Backward);
00300                     }else{pc2.printf("Backward not open");}
00301                       fclose(Backward);
00302                     str.append("B");
00303                     break;
00304                 case DIR_LEFT:
00305                     #ifdef DEBUGLCD
00306                     pc2.printf("LEFT\n");
00307                     #endif
00308                     xbee2.putc('L');
00309                    printLEF(3, 7);
00310                     Left=fopen("/sd/Left.wav","r");
00311                     if(Left!=NULL){
00312                     waver.play(Left);
00313                     }else{pc2.printf("Left not open");}
00314                        fclose(Left);
00315                     str.append("L");
00316                     break;
00317                 case DIR_RIGHT:
00318                     #ifdef DEBUGLCD
00319                     pc2.printf("RIGHT\n");
00320                     #endif
00321                     xbee2.putc('R');
00322                     printRIG(3, 7);
00323                     
00324                     Right=fopen("/sd/Right.wav","r");
00325                     if(Right!=NULL){
00326                     waver.play(Right);
00327                     }else{ pc2.printf("Right not open"); }
00328                     fclose(Right);
00329                      str.append("R");
00330                     break;
00331                 case 67: 
00332                     #ifdef DEBUGLCD
00333                     pc2.printf("Collision\n");
00334                     #endif
00335                     printSTOP(3, 7);
00336                      Stop=fopen("/sd/Stop.wav","r");
00337                     if(Stop!=NULL){
00338                     waver.play(Stop);
00339                     }else{ pc2.printf("Stop not open");}
00340                    fclose(Stop);
00341                     str.append("C");
00342                     break;
00343                 
00344                 case DIR_NEAR:
00345                     #ifdef DEBUGLCD
00346                     pc2.printf("NEAR\n");
00347                     #endif
00348                     break;
00349                 case DIR_FAR:
00350                      #ifdef DEBUGLCD
00351                      pc2.printf("FAR\n");
00352                      #endif
00353                      break;
00354                 default:
00355                     #ifdef DEBUGLCD
00356                     pc2.printf("NONE\n");
00357                     #endif
00358                   printERR(0, 0);
00359                    Error=fopen("/sd/R2D2a.wav","r");
00360                   if(Error!=NULL){
00361                     waver.play(Error);
00362                     }else{ pc2.printf("Error not open");}
00363                       fclose(Error);
00364                     break;
00365                     
00366             }
00367         }
00368         
00369 
00370 
00371         //   wait(1);
00372 
00373     }
00374     
00375 
00376 
00377 
00378 }
00379