James Plager / Mbed 2 deprecated 4180Lab2Part4

Dependencies:   4DGL-uLCD-SE 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 "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,p29);
00014 // Calculate pitch, roll, and heading.
00015 // Pitch/roll calculations taken from this app note:
00016 // http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1
00017 // Heading calculations taken from this app note:
00018 // http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/Magnetic__Literature_Application_notes-documents/AN203_Compass_Heading_Using_Magnetometers.pdf
00019 void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
00020 {
00021     float roll = atan2(ay, az);
00022     float pitch = atan2(-ax, sqrt(ay * ay + az * az));
00023 // touchy trig stuff to use arctan to get compass heading (scale is 0..360)
00024     mx = -mx;
00025     float heading;
00026     if (my == 0.0)
00027         heading = (mx < 0.0) ? 180.0 : 0.0;
00028     else
00029         heading = atan2(mx, my)*360.0/(2.0*PI);
00030     //pc.printf("heading atan=%f \n\r",heading);
00031     heading -= DECLINATION; //correct for geo location
00032     if(heading>180.0) heading = heading - 360.0;
00033     else if(heading<-180.0) heading = 360.0 + heading;
00034     else if(heading<0.0) heading = 360.0  + heading;
00035 
00036 
00037     // Convert everything from radians to degrees:
00038     //heading *= 180.0 / PI;
00039     pitch *= 180.0 / PI;
00040     roll  *= 180.0 / PI;
00041 
00042     pc.printf("Pitch: %f,    Roll: %f degress\n\r",pitch,roll);
00043     pc.printf("Magnetic Heading: %f degress\n\r",heading);
00044 }
00045 
00046 float tempX = 0.0;
00047 float tempY = 0.0;
00048 float oldtempX;
00049 float oldtempY;
00050 
00051 int main()
00052 {
00053     LSM9DS1 IMU(p9, p10, 0xd6, 0x3c);
00054     //LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
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     while(1) {
00062         
00063         uLCD.locate(25,25);
00064     //uLCD.color(WHITE);
00065     uLCD.circle(64,64,60,0xFFFFFF);
00066     
00067         while(!IMU.tempAvailable());
00068         IMU.readTemp();
00069         while(!IMU.magAvailable(X_AXIS));
00070         IMU.readMag();
00071         while(!IMU.accelAvailable());
00072         IMU.readAccel();
00073         while(!IMU.gyroAvailable());
00074         IMU.readGyro();
00075         pc.printf("\nIMU Temperature = %f C\n\r",25.0 + IMU.temperature/16.0);
00076         pc.printf("        X axis    Y axis    Z axis\n\r");
00077         pc.printf("gyro:  %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz));
00078         pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az));
00079         pc.printf("mag:   %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00080         printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),
00081                       IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
00082         myled = 1;
00083         wait(0.5);
00084         myled = 0;
00085         wait(0.5);
00086         
00087         oldtempX = tempX;
00088         oldtempY = tempY;
00089         uLCD.filled_circle(floor(128*oldtempX), floor(128*oldtempY), 6, 0x000000);    // erase old bubble
00090         tempX = IMU.calcAccel(IMU.ax);
00091         tempX = (tempX+1.0)/2.0;
00092         tempY = IMU.calcAccel(IMU.ay);
00093         tempY = (tempY+1.0)/2.0;
00094         //draw filled circle based on info from IMU
00095         //draw new bubble
00096         uLCD.filled_circle(floor(128*tempX), floor(128*tempY), 6, 0xFFFFFF);
00097         
00098     }
00099 }
00100