Roomba that displays room data to a C# GUI

Dependencies:   LSM9DS1_Library_cal mbed

Fork of 4180Lab2Part4 by James Plager

Committer:
jplager3
Date:
Wed Mar 01 06:01:10 2017 +0000
Revision:
1:aeb42bbdac27
Parent:
0:e693d5bf0a25
Child:
2:516dd8a72972
Code functioning;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:e693d5bf0a25 1 #include "mbed.h"
4180_1 0:e693d5bf0a25 2 #include "LSM9DS1.h"
jplager3 1:aeb42bbdac27 3 #include "uLCD_4DGL.h"
4180_1 0:e693d5bf0a25 4 #define PI 3.14159
4180_1 0:e693d5bf0a25 5 // Earth's magnetic field varies by location. Add or subtract
4180_1 0:e693d5bf0a25 6 // a declination to get a more accurate heading. Calculate
4180_1 0:e693d5bf0a25 7 // your's here:
4180_1 0:e693d5bf0a25 8 // http://www.ngdc.noaa.gov/geomag-web/#declination
4180_1 0:e693d5bf0a25 9 #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.
4180_1 0:e693d5bf0a25 10
4180_1 0:e693d5bf0a25 11 DigitalOut myled(LED1);
4180_1 0:e693d5bf0a25 12 Serial pc(USBTX, USBRX);
jplager3 1:aeb42bbdac27 13 uLCD_4DGL uLCD(p28,p27,p29);
4180_1 0:e693d5bf0a25 14 // Calculate pitch, roll, and heading.
4180_1 0:e693d5bf0a25 15 // Pitch/roll calculations taken from this app note:
4180_1 0:e693d5bf0a25 16 // http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1
4180_1 0:e693d5bf0a25 17 // Heading calculations taken from this app note:
4180_1 0:e693d5bf0a25 18 // http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/Magnetic__Literature_Application_notes-documents/AN203_Compass_Heading_Using_Magnetometers.pdf
4180_1 0:e693d5bf0a25 19 void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
4180_1 0:e693d5bf0a25 20 {
4180_1 0:e693d5bf0a25 21 float roll = atan2(ay, az);
4180_1 0:e693d5bf0a25 22 float pitch = atan2(-ax, sqrt(ay * ay + az * az));
4180_1 0:e693d5bf0a25 23 // touchy trig stuff to use arctan to get compass heading (scale is 0..360)
4180_1 0:e693d5bf0a25 24 mx = -mx;
4180_1 0:e693d5bf0a25 25 float heading;
4180_1 0:e693d5bf0a25 26 if (my == 0.0)
4180_1 0:e693d5bf0a25 27 heading = (mx < 0.0) ? 180.0 : 0.0;
4180_1 0:e693d5bf0a25 28 else
4180_1 0:e693d5bf0a25 29 heading = atan2(mx, my)*360.0/(2.0*PI);
4180_1 0:e693d5bf0a25 30 //pc.printf("heading atan=%f \n\r",heading);
4180_1 0:e693d5bf0a25 31 heading -= DECLINATION; //correct for geo location
4180_1 0:e693d5bf0a25 32 if(heading>180.0) heading = heading - 360.0;
4180_1 0:e693d5bf0a25 33 else if(heading<-180.0) heading = 360.0 + heading;
4180_1 0:e693d5bf0a25 34 else if(heading<0.0) heading = 360.0 + heading;
4180_1 0:e693d5bf0a25 35
4180_1 0:e693d5bf0a25 36
4180_1 0:e693d5bf0a25 37 // Convert everything from radians to degrees:
4180_1 0:e693d5bf0a25 38 //heading *= 180.0 / PI;
4180_1 0:e693d5bf0a25 39 pitch *= 180.0 / PI;
4180_1 0:e693d5bf0a25 40 roll *= 180.0 / PI;
4180_1 0:e693d5bf0a25 41
4180_1 0:e693d5bf0a25 42 pc.printf("Pitch: %f, Roll: %f degress\n\r",pitch,roll);
4180_1 0:e693d5bf0a25 43 pc.printf("Magnetic Heading: %f degress\n\r",heading);
4180_1 0:e693d5bf0a25 44 }
4180_1 0:e693d5bf0a25 45
jplager3 1:aeb42bbdac27 46 float tempX = 0.0;
jplager3 1:aeb42bbdac27 47 float tempY = 0.0;
jplager3 1:aeb42bbdac27 48 float oldtempX;
jplager3 1:aeb42bbdac27 49 float oldtempY;
4180_1 0:e693d5bf0a25 50
4180_1 0:e693d5bf0a25 51 int main()
4180_1 0:e693d5bf0a25 52 {
jplager3 1:aeb42bbdac27 53 LSM9DS1 IMU(p9, p10, 0xd6, 0x3c);
jplager3 1:aeb42bbdac27 54 //LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
4180_1 0:e693d5bf0a25 55 IMU.begin();
4180_1 0:e693d5bf0a25 56 if (!IMU.begin()) {
4180_1 0:e693d5bf0a25 57 pc.printf("Failed to communicate with LSM9DS1.\n");
4180_1 0:e693d5bf0a25 58 }
4180_1 0:e693d5bf0a25 59 IMU.calibrate(1);
4180_1 0:e693d5bf0a25 60 IMU.calibrateMag(0);
4180_1 0:e693d5bf0a25 61 while(1) {
jplager3 1:aeb42bbdac27 62
jplager3 1:aeb42bbdac27 63 uLCD.locate(25,25);
jplager3 1:aeb42bbdac27 64 //uLCD.color(WHITE);
jplager3 1:aeb42bbdac27 65 uLCD.circle(64,64,60,0xFFFFFF);
jplager3 1:aeb42bbdac27 66
4180_1 0:e693d5bf0a25 67 while(!IMU.tempAvailable());
4180_1 0:e693d5bf0a25 68 IMU.readTemp();
4180_1 0:e693d5bf0a25 69 while(!IMU.magAvailable(X_AXIS));
4180_1 0:e693d5bf0a25 70 IMU.readMag();
4180_1 0:e693d5bf0a25 71 while(!IMU.accelAvailable());
4180_1 0:e693d5bf0a25 72 IMU.readAccel();
4180_1 0:e693d5bf0a25 73 while(!IMU.gyroAvailable());
4180_1 0:e693d5bf0a25 74 IMU.readGyro();
4180_1 0:e693d5bf0a25 75 pc.printf("\nIMU Temperature = %f C\n\r",25.0 + IMU.temperature/16.0);
4180_1 0:e693d5bf0a25 76 pc.printf(" X axis Y axis Z axis\n\r");
4180_1 0:e693d5bf0a25 77 pc.printf("gyro: %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz));
4180_1 0:e693d5bf0a25 78 pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az));
4180_1 0:e693d5bf0a25 79 pc.printf("mag: %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
4180_1 0:e693d5bf0a25 80 printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),
4180_1 0:e693d5bf0a25 81 IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
4180_1 0:e693d5bf0a25 82 myled = 1;
4180_1 0:e693d5bf0a25 83 wait(0.5);
4180_1 0:e693d5bf0a25 84 myled = 0;
4180_1 0:e693d5bf0a25 85 wait(0.5);
jplager3 1:aeb42bbdac27 86
jplager3 1:aeb42bbdac27 87 oldtempX = tempX;
jplager3 1:aeb42bbdac27 88 oldtempY = tempY;
jplager3 1:aeb42bbdac27 89 uLCD.filled_circle(floor(128*oldtempX), floor(128*oldtempY), 6, 0x000000); // erase old bubble
jplager3 1:aeb42bbdac27 90 tempX = IMU.calcAccel(IMU.ax);
jplager3 1:aeb42bbdac27 91 tempX = (tempX+1.0)/2.0;
jplager3 1:aeb42bbdac27 92 tempY = IMU.calcAccel(IMU.ay);
jplager3 1:aeb42bbdac27 93 tempY = (tempY+1.0)/2.0;
jplager3 1:aeb42bbdac27 94 //draw filled circle based on info from IMU
jplager3 1:aeb42bbdac27 95 //draw new bubble
jplager3 1:aeb42bbdac27 96 uLCD.filled_circle(floor(128*tempX), floor(128*tempY), 6, 0xFFFFFF);
jplager3 1:aeb42bbdac27 97
4180_1 0:e693d5bf0a25 98 }
4180_1 0:e693d5bf0a25 99 }
4180_1 0:e693d5bf0a25 100