Craig Raslawski / Mbed 2 deprecated IMURoomba

Dependencies:   LSM9DS1_Library_cal mbed

Fork of 4180Lab2Part4 by James Plager

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 #define PI 3.14159
00004 // Earth's magnetic field varies by location. Add or subtract
00005 // a declination to get a more accurate heading. Calculate
00006 // your's here:
00007 // http://www.ngdc.noaa.gov/geomag-web/#declination
00008 #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.
00009 
00010 //Serial pc(USBTX, USBRX);
00011 RawSerial  pc(USBTX, USBRX);
00012 RawSerial  dev(p28,p27); //tx, rx
00013 DigitalOut myled(LED1);
00014 DigitalOut led2(LED2);
00015 DigitalOut led4(LED4);
00016 // Calculate pitch, roll, and heading.
00017 // Pitch/roll calculations taken from this app note:
00018 // http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1
00019 // Heading calculations taken from this app note:
00020 // http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/Magnetic__Literature_Application_notes-documents/AN203_Compass_Heading_Using_Magnetometers.pdf
00021 void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
00022 {
00023     float roll = atan2(ay, az);
00024     float pitch = atan2(-ax, sqrt(ay * ay + az * az));
00025 // touchy trig stuff to use arctan to get compass heading (scale is 0..360)
00026     mx = -mx;
00027     float heading;
00028     if (my == 0.0)
00029         heading = (mx < 0.0) ? 180.0 : 0.0;
00030     else
00031         heading = atan2(mx, my)*360.0/(2.0*PI);
00032     //pc.printf("heading atan=%f \n\r",heading);
00033     heading -= DECLINATION; //correct for geo location
00034     if(heading>180.0) heading = heading - 360.0;
00035     else if(heading<-180.0) heading = 360.0 + heading;
00036     else if(heading<0.0) heading = 360.0  + heading;
00037 
00038 
00039     // Convert everything from radians to degrees:
00040     //heading *= 180.0 / PI;
00041     pitch *= 180.0 / PI;
00042     roll  *= 180.0 / PI;
00043 
00044     //~pc.printf("Pitch: %f,    Roll: %f degress\n\r",pitch,roll);
00045     //~pc.printf("Magnetic Heading: %f degress\n\r",heading);
00046 }
00047 
00048 /*
00049 void dev_recv()
00050 {
00051     led2 = !led2;
00052     while(dev.readable()) {
00053         pc.putc(dev.getc());
00054     }
00055 }
00056 
00057 void pc_recv()
00058 {
00059     led4 = !led4;
00060     while(pc.readable()) {
00061         dev.putc(pc.getc());
00062     }
00063 }*/
00064 
00065 int main()
00066 {
00067     //IMU setup
00068     LSM9DS1 IMU(p9, p10, 0xd6, 0x3c);
00069     IMU.begin();
00070     if (!IMU.begin()) {
00071         pc.printf("Failed to communicate with LSM9DS1.\n");
00072     }
00073     IMU.calibrate(1);
00074     IMU.calibrateMag(0);
00075     
00076     //bluetooth setup
00077     pc.baud(9600);
00078     dev.baud(9600);
00079 
00080     /*pc.attach(&pc_recv, Serial::RxIrq);
00081     dev.attach(&dev_recv, Serial::RxIrq);*/
00082     
00083     while(1) {
00084     
00085         while(!IMU.magAvailable(X_AXIS));
00086         IMU.readMag();
00087         while(!IMU.accelAvailable());
00088         IMU.readAccel();
00089         while(!IMU.gyroAvailable());
00090         IMU.readGyro();
00091         pc.puts("        X axis    Y axis    Z axis\n\r");
00092         dev.puts("        X axis    Y axis    Z axis\n\r");
00093         //pc.printf("gyro:  %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz));
00094         //pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az));
00095         //pc.printf("mag:   %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00096         printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),
00097                       IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00098         myled = 1;
00099         wait(0.5);
00100         myled = 0;
00101         wait(0.5);
00102         
00103 
00104         //uLCD.filled_circle(floor(128*oldtempX), floor(128*oldtempY), 6, 0x000000);    // erase old bubble
00105 
00106         //draw filled circle based on info from IMU
00107         //draw new bubble
00108         //uLCD.filled_circle(floor(128*tempX), floor(128*tempY), 6, 0xFFFFFF);
00109         
00110     }
00111 }
00112