ECE 4180 / Mbed 2 deprecated 4180-L2-P3

Dependencies:   mbed 4DGL-uLCD-SE LSM9DS1_Library_cal

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 "uLCD_4DGL.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 DigitalOut myled(LED1);
00012 Serial pc(USBTX, USBRX);
00013 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
00014 
00015 // Calculate pitch, roll, and heading.
00016 // Pitch/roll calculations taken from this app note:
00017 // http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1
00018 // Heading calculations taken from this app note:
00019 // http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/Magnetic__Literature_Application_notes-documents/AN203_Compass_Heading_Using_Magnetometers.pdf
00020 void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
00021 {
00022     float roll = atan2(ay, az);
00023     float pitch = atan2(-ax, sqrt(ay * ay + az * az));
00024 // touchy trig stuff to use arctan to get compass heading (scale is 0..360)
00025     mx = -mx;
00026     float heading;
00027     if (my == 0.0)
00028         heading = (mx < 0.0) ? 180.0 : 0.0;
00029     else
00030         heading = atan2(mx, my)*360.0/(2.0*PI);
00031     //pc.printf("heading atan=%f \n\r",heading);
00032     heading -= DECLINATION; //correct for geo location
00033     if(heading>180.0) heading = heading - 360.0;
00034     else if(heading<-180.0) heading = 360.0 + heading;
00035     else if(heading<0.0) heading = 360.0  + heading;
00036 
00037 
00038     // Convert everything from radians to degrees:
00039     //heading *= 180.0 / PI;
00040     pitch *= 180.0 / PI;
00041     roll  *= 180.0 / PI;
00042 
00043     pc.printf("Pitch: %f,    Roll: %f degress\n\r",pitch,roll);
00044     pc.printf("Magnetic Heading: %f degress\n\r",heading);
00045 }
00046 
00047 
00048 
00049 
00050 int main()
00051 {
00052     //LSM9DS1 lol(p9, p10, 0x6B, 0x1E);
00053     LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
00054     uLCD.baudrate(3000000);
00055     IMU.begin();
00056     if (!IMU.begin()) {
00057         pc.printf("Failed to communicate with LSM9DS1.\n");
00058     }
00059     IMU.calibrate(1);
00060     IMU.calibrateMag(0);
00061     
00062     uLCD.circle(64,64,62,WHITE);
00063     
00064     while(1)
00065     {
00066         while(!IMU.accelAvailable());
00067         IMU.readAccel();
00068         uLCD.filled_circle(64 + 64*IMU.calcAccel(IMU.ax),64 + 64*IMU.calcAccel(IMU.ay),3,RED);
00069         wait(0.1);
00070         uLCD.filled_circle(64 + 64*IMU.calcAccel(IMU.ax),64 + 64*IMU.calcAccel(IMU.ay),3,BLACK);
00071         
00072         while(!IMU.tempAvailable());
00073         IMU.readTemp();
00074         while(!IMU.magAvailable(X_AXIS));
00075         IMU.readMag();
00076         while(!IMU.accelAvailable());
00077         IMU.readAccel();
00078         while(!IMU.gyroAvailable());
00079         IMU.readGyro();
00080         pc.printf("\nIMU Temperature = %f C\n\r",25.0 + IMU.temperature/16.0);
00081         pc.printf("        X axis    Y axis    Z axis\n\r");
00082         pc.printf("gyro:  %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz));
00083         pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az));
00084         pc.printf("mag:   %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00085         printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),
00086                       IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00087         myled = 1;
00088         wait(0.5);
00089         myled = 0;
00090         wait(0.5);
00091         
00092     }
00093     
00094 //    while(1) {
00095 //        while(!IMU.tempAvailable());
00096 //        IMU.readTemp();
00097 //        while(!IMU.magAvailable(X_AXIS));
00098 //        IMU.readMag();
00099 //        while(!IMU.accelAvailable());
00100 //        IMU.readAccel();
00101 //        while(!IMU.gyroAvailable());
00102 //        IMU.readGyro();
00103 //        pc.printf("\nIMU Temperature = %f C\n\r",25.0 + IMU.temperature/16.0);
00104 //        pc.printf("        X axis    Y axis    Z axis\n\r");
00105 //        pc.printf("gyro:  %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz));
00106 //        pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az));
00107 //        pc.printf("mag:   %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00108 //        printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),
00109 //                      IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00110 //        myled = 1;
00111 //        wait(0.5);
00112 //        myled = 0;
00113 //        wait(0.5);
00114 //    }
00115 }
00116