Donovan Kuhstoss / Mbed 2 deprecated Gesture_Glove

Dependencies:   LSM9DS1_Library_cal mbed

Fork of LSM9DS1_Demo_wCal by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LSM9DS1.h"
00003 #include "SMARTWAV.h"
00004 #define PI 3.14159
00005 // Earth's magnetic field varies by location. Add or subtract
00006 // a declination to get a more accurate heading. Calculate
00007 // your's here:
00008 // http://www.ngdc.noaa.gov/geomag-web/#declination
00009 #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.
00010 
00011 SMARTWAV sWav(p13,p14,p15);    //(TX,RX,Reset); Music Player
00012 LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); // IMU for gesture control
00013 DigitalOut myled1(LED1);          // Gesture feedback
00014 DigitalOut myled2(LED2);          // Gesture feedback
00015 DigitalOut myled3(LED3);          // Gesture feedback
00016 DigitalOut myled4(LED4);          // Gesture feedback
00017 Serial pc(USBTX, USBRX);          
00018 DigitalIn pb(p20);                // Read gesture input controller
00019 
00020 //define code for volume control
00021 AnalogIn IRSensor(p19);
00022 DigitalIn pushbutton(p8); //grounded - locks in volume control
00023 
00024 char level='9'; // Default volume level
00025 
00026 float Pitch;
00027 float Roll;
00028 float pitch;
00029 float roll;
00030 bool flag = true; // Reading in a base values
00031 bool start_music = true; 
00032 bool music_playing = false;
00033 bool flag2 = true;
00034 
00035 
00036 // Calculate pitch, roll, and heading.
00037 // Pitch/roll calculations taken from this app note:
00038 // http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1
00039 // Heading calculations taken from this app note:
00040 // http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/Magnetic__Literature_Application_notes-documents/AN203_Compass_Heading_Using_Magnetometers.pdf
00041 void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
00042 {
00043     roll = atan2(ay, az);
00044     pitch = atan2(-ax, sqrt(ay * ay + az * az));
00045 // touchy trig stuff to use arctan to get compass heading (scale is 0..360)
00046     mx = -mx;
00047     float heading;
00048     if (my == 0.0)
00049         heading = (mx < 0.0) ? 180.0 : 0.0;
00050     else
00051         heading = atan2(mx, my)*360.0/(2.0*PI);
00052     //pc.printf("heading atan=%f \n\r",heading);
00053     heading -= DECLINATION; //correct for geo location
00054     if(heading>180.0) heading = heading - 360.0;
00055     else if(heading<-180.0) heading = 360.0 + heading;
00056     else if(heading<0.0) heading = 360.0  + heading;
00057 
00058 
00059     // Convert everything from radians to degrees:
00060     //heading *= 180.0 / PI;
00061     pitch *= 180.0 / PI;
00062     roll  *= 180.0 / PI;
00063 
00064     //pc.printf("Pitch: %f,    Roll: %f degress\n\r",pitch, roll);
00065     //pc.printf("Magnetic Heading: %f degress\n\r",heading);
00066 }
00067 
00068 void controlvolume(){
00069     
00070     float a = IRSensor;// 1=4cm 0=30cm 
00071     if (pushbutton==0) //when the dipswitch is on change the volume; leds are defined to indicate volume level55yyh
00072     {             
00073              
00074                               if (a>0.9) //change to lowest volume
00075                             {
00076                                  sWav.volume(MIN); //all volume values are defined in smartwav.h file
00077                                  level = '0';
00078                               
00079                             }   
00080                              if (a>0.8 & a<=0.9)
00081                             {
00082                            sWav.volume(MIN0); 
00083                            level = '1';
00084                             }   
00085                             if (a>0.7 & a<=0.8)
00086                             {
00087                            sWav.volume(MIN1);
00088                              level = '2';
00089                             }
00090                             if (a>0.6 & a<=.7)
00091                          {
00092                           sWav.volume(MIN2);
00093                           level = '3';
00094                              
00095                             }
00096                             if (a>0.5 & a<=.6)
00097                          {
00098                           sWav.volume(MIN3);
00099                              level = '4';
00100                             }
00101                             if(a>.4 & a<=.5)
00102                             {
00103                           sWav.volume(MED1);
00104                           level = '5';  
00105                             }
00106                             if(a>.2 & a<=.3)
00107                             {
00108                           sWav.volume(MED2);
00109                                level = '6'; 
00110                             }
00111                             if(a>0.1 & a<=.2) 
00112                             {
00113                          sWav.volume(MED3);
00114                          level = '8';
00115                                     }
00116                             if(a<=0.1) //change to HIGHEST volume
00117                             {
00118                          sWav.volume(MAX);
00119                               level = '8';
00120                                     }
00121                                       if(level!='9'){pc.printf("v%c",level);}
00122     wait(.1);
00123     }
00124 }
00125 
00126 
00127 
00128 int main()
00129 {
00130     //char name[9]={0};
00131     //char prev[9]={0}; 
00132     sWav.reset();                //physically reset SMARTWAV
00133     myled1 = 1; myled2 = 1; myled3 = 1; myled4 = 1; // All 4 leds light up until calibration is finished
00134     pb.mode(PullUp); //pushbutton for controlling gesture input 
00135     pushbutton.mode(PullUp); //pushbutton for controlling volume
00136     LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); 
00137     IMU.begin();
00138     if (!IMU.begin()) {
00139         pc.printf("Failed to communicate with LSM9DS1.\n");
00140     }
00141     IMU.calibrate(1);
00142     IMU.calibrateMag(0);
00143     myled1 = 0; myled2 = 0; myled3 = 0; myled4 = 0; // All leds turn off to indicate calibration is finished
00144     sWav.continuousPlay(ENABLE);
00145 while(1) {
00146 controlvolume(); //initiate volume function using IR sensor
00147     // The pushbutton controls the reading of the IMU(gestures) if it is not presseed the gestures will be ignored
00148     // Pressing the pushbutton for the first time starts the music playing      
00149     if (pb == 0){
00150         // Starting music for the first time function
00151         if (start_music == true){
00152             pc.putc('P'); // Send 'P' to let the windows app that music has started to play
00153             sWav.playTracks(); // Play music
00154             start_music = false; // Keeps function from executing again
00155             music_playing = true; // Music is now playing
00156             }
00157         // Read IMU Values only the accel and mag values are being used to calculate the pitch and roll
00158         while(!IMU.tempAvailable());
00159         IMU.readTemp();
00160         while(!IMU.magAvailable(X_AXIS));
00161         IMU.readMag();
00162         while(!IMU.accelAvailable());
00163         IMU.readAccel();
00164         while(!IMU.gyroAvailable());
00165         IMU.readGyro();
00166         //pc.printf("\nIMU Temperature = %f C\n\r",25.0 + IMU.temperature/16.0);
00167         //pc.printf("        X axis    Y axis    Z axis\n\r");
00168         //pc.printf("gyro:  %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz));
00169         //pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az));
00170         //pc.printf("mag:   %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00171         printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),
00172                       IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00173                       
00174         // The gestures are read by setting base values once the pushbutton is press and once there is a certain deviation from those base values a specific gesture can be determined
00175         
00176         // Set Base Values
00177         if (flag == true){
00178             flag = false;
00179             Pitch = pitch;
00180             Roll = roll;
00181         }
00182         
00183         //Pause Track 
00184         if (Pitch-pitch >= 20){
00185             myled1 = 1; 
00186             if (music_playing == true){
00187                 pc.putc('p'); // pause character
00188                 sWav.pausePlay();
00189                 music_playing = false;
00190             }
00191             wait(.5);
00192         }
00193         else myled1 = 0;
00194         
00195         //Play Track 
00196         if (pitch-Pitch >= 20){
00197              myled2 = 1;
00198              if (music_playing == false){
00199                  pc.putc('P'); // Play character
00200                  sWav.pausePlay();
00201                  music_playing = true;
00202                 }
00203             wait(.5);
00204         }
00205         else myled2 = 0;
00206         
00207         // Restart Track
00208         if (Roll-roll >= 20){
00209             myled3 = 1;
00210             if (music_playing == true){
00211                 pc.putc('b'); // Restart Track character
00212                 sWav.rewindTrack();
00213             }
00214             wait(.5);
00215         }
00216         else myled3 = 0;
00217         
00218         //Next Track
00219         if (roll-Roll >= 20){
00220             myled4 = 1;
00221             wait(.5);
00222             if (music_playing == true){
00223                 pc.putc('n');
00224                 sWav.nextTrack();  
00225             }
00226             wait(.5);
00227         }
00228         else myled4 = 0;
00229         
00230     }
00231     else {
00232         flag = true; myled1 = 0; myled2 = 0; myled3 = 0; myled4 = 0;
00233     }
00234     
00235 }// end while loop
00236  
00237 }// end main
00238