Roomba that displays room data to a C# GUI
Dependencies: LSM9DS1_Library_cal mbed
Fork of 4180Lab2Part4 by
main.cpp@0:e693d5bf0a25, 2016-02-03 (annotated)
- Committer:
- 4180_1
- Date:
- Wed Feb 03 18:47:07 2016 +0000
- Revision:
- 0:e693d5bf0a25
- Child:
- 1:aeb42bbdac27
ver 0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:e693d5bf0a25 | 1 | #include "mbed.h" |
4180_1 | 0:e693d5bf0a25 | 2 | #include "LSM9DS1.h" |
4180_1 | 0:e693d5bf0a25 | 3 | #define PI 3.14159 |
4180_1 | 0:e693d5bf0a25 | 4 | // Earth's magnetic field varies by location. Add or subtract |
4180_1 | 0:e693d5bf0a25 | 5 | // a declination to get a more accurate heading. Calculate |
4180_1 | 0:e693d5bf0a25 | 6 | // your's here: |
4180_1 | 0:e693d5bf0a25 | 7 | // http://www.ngdc.noaa.gov/geomag-web/#declination |
4180_1 | 0:e693d5bf0a25 | 8 | #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA. |
4180_1 | 0:e693d5bf0a25 | 9 | |
4180_1 | 0:e693d5bf0a25 | 10 | DigitalOut myled(LED1); |
4180_1 | 0:e693d5bf0a25 | 11 | Serial pc(USBTX, USBRX); |
4180_1 | 0:e693d5bf0a25 | 12 | // Calculate pitch, roll, and heading. |
4180_1 | 0:e693d5bf0a25 | 13 | // Pitch/roll calculations taken from this app note: |
4180_1 | 0:e693d5bf0a25 | 14 | // http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1 |
4180_1 | 0:e693d5bf0a25 | 15 | // Heading calculations taken from this app note: |
4180_1 | 0:e693d5bf0a25 | 16 | // 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 | 17 | void printAttitude(float ax, float ay, float az, float mx, float my, float mz) |
4180_1 | 0:e693d5bf0a25 | 18 | { |
4180_1 | 0:e693d5bf0a25 | 19 | float roll = atan2(ay, az); |
4180_1 | 0:e693d5bf0a25 | 20 | float pitch = atan2(-ax, sqrt(ay * ay + az * az)); |
4180_1 | 0:e693d5bf0a25 | 21 | // touchy trig stuff to use arctan to get compass heading (scale is 0..360) |
4180_1 | 0:e693d5bf0a25 | 22 | mx = -mx; |
4180_1 | 0:e693d5bf0a25 | 23 | float heading; |
4180_1 | 0:e693d5bf0a25 | 24 | if (my == 0.0) |
4180_1 | 0:e693d5bf0a25 | 25 | heading = (mx < 0.0) ? 180.0 : 0.0; |
4180_1 | 0:e693d5bf0a25 | 26 | else |
4180_1 | 0:e693d5bf0a25 | 27 | heading = atan2(mx, my)*360.0/(2.0*PI); |
4180_1 | 0:e693d5bf0a25 | 28 | //pc.printf("heading atan=%f \n\r",heading); |
4180_1 | 0:e693d5bf0a25 | 29 | heading -= DECLINATION; //correct for geo location |
4180_1 | 0:e693d5bf0a25 | 30 | if(heading>180.0) heading = heading - 360.0; |
4180_1 | 0:e693d5bf0a25 | 31 | else if(heading<-180.0) heading = 360.0 + heading; |
4180_1 | 0:e693d5bf0a25 | 32 | else if(heading<0.0) heading = 360.0 + heading; |
4180_1 | 0:e693d5bf0a25 | 33 | |
4180_1 | 0:e693d5bf0a25 | 34 | |
4180_1 | 0:e693d5bf0a25 | 35 | // Convert everything from radians to degrees: |
4180_1 | 0:e693d5bf0a25 | 36 | //heading *= 180.0 / PI; |
4180_1 | 0:e693d5bf0a25 | 37 | pitch *= 180.0 / PI; |
4180_1 | 0:e693d5bf0a25 | 38 | roll *= 180.0 / PI; |
4180_1 | 0:e693d5bf0a25 | 39 | |
4180_1 | 0:e693d5bf0a25 | 40 | pc.printf("Pitch: %f, Roll: %f degress\n\r",pitch,roll); |
4180_1 | 0:e693d5bf0a25 | 41 | pc.printf("Magnetic Heading: %f degress\n\r",heading); |
4180_1 | 0:e693d5bf0a25 | 42 | } |
4180_1 | 0:e693d5bf0a25 | 43 | |
4180_1 | 0:e693d5bf0a25 | 44 | |
4180_1 | 0:e693d5bf0a25 | 45 | |
4180_1 | 0:e693d5bf0a25 | 46 | |
4180_1 | 0:e693d5bf0a25 | 47 | int main() |
4180_1 | 0:e693d5bf0a25 | 48 | { |
4180_1 | 0:e693d5bf0a25 | 49 | //LSM9DS1 lol(p9, p10, 0x6B, 0x1E); |
4180_1 | 0:e693d5bf0a25 | 50 | LSM9DS1 IMU(p28, p27, 0xD6, 0x3C); |
4180_1 | 0:e693d5bf0a25 | 51 | IMU.begin(); |
4180_1 | 0:e693d5bf0a25 | 52 | if (!IMU.begin()) { |
4180_1 | 0:e693d5bf0a25 | 53 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
4180_1 | 0:e693d5bf0a25 | 54 | } |
4180_1 | 0:e693d5bf0a25 | 55 | IMU.calibrate(1); |
4180_1 | 0:e693d5bf0a25 | 56 | IMU.calibrateMag(0); |
4180_1 | 0:e693d5bf0a25 | 57 | while(1) { |
4180_1 | 0:e693d5bf0a25 | 58 | while(!IMU.tempAvailable()); |
4180_1 | 0:e693d5bf0a25 | 59 | IMU.readTemp(); |
4180_1 | 0:e693d5bf0a25 | 60 | while(!IMU.magAvailable(X_AXIS)); |
4180_1 | 0:e693d5bf0a25 | 61 | IMU.readMag(); |
4180_1 | 0:e693d5bf0a25 | 62 | while(!IMU.accelAvailable()); |
4180_1 | 0:e693d5bf0a25 | 63 | IMU.readAccel(); |
4180_1 | 0:e693d5bf0a25 | 64 | while(!IMU.gyroAvailable()); |
4180_1 | 0:e693d5bf0a25 | 65 | IMU.readGyro(); |
4180_1 | 0:e693d5bf0a25 | 66 | pc.printf("\nIMU Temperature = %f C\n\r",25.0 + IMU.temperature/16.0); |
4180_1 | 0:e693d5bf0a25 | 67 | pc.printf(" X axis Y axis Z axis\n\r"); |
4180_1 | 0:e693d5bf0a25 | 68 | 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 | 69 | 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 | 70 | 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 | 71 | printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx), |
4180_1 | 0:e693d5bf0a25 | 72 | IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz)); |
4180_1 | 0:e693d5bf0a25 | 73 | myled = 1; |
4180_1 | 0:e693d5bf0a25 | 74 | wait(0.5); |
4180_1 | 0:e693d5bf0a25 | 75 | myled = 0; |
4180_1 | 0:e693d5bf0a25 | 76 | wait(0.5); |
4180_1 | 0:e693d5bf0a25 | 77 | } |
4180_1 | 0:e693d5bf0a25 | 78 | } |
4180_1 | 0:e693d5bf0a25 | 79 |