IMU for lab 2 part 2, part 3 and part 4.

Dependencies:   4DGL-uLCD-SE LSM9DS0 mbed

Fork of 4180_lab2_part2_LSM9DS0 by ECE4180

Committer:
ldeng31
Date:
Sat Sep 05 19:38:13 2015 +0000
Revision:
7:8c458123a665
Parent:
6:87468d3886ea
Child:
8:4927aa0ab07a
part 3  and part 4 done.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aswild 0:29ab304ca8ce 1 // LSM9DS90/uLCD Demo
aswild 0:29ab304ca8ce 2 // ECE 4180 Lab Code Template
aswild 0:29ab304ca8ce 3
aswild 0:29ab304ca8ce 4 #include "mbed.h"
aswild 0:29ab304ca8ce 5 #include "LSM9DS0.h"
aswild 0:29ab304ca8ce 6 #include "uLCD_4DGL.h"
aswild 0:29ab304ca8ce 7
aswild 4:a9e3007530a7 8 // uncomment this line to enable the uLCD for Part 4 of the lab
ycai47 6:87468d3886ea 9 #define PART_4
aswild 1:ae1cefe9aa38 10
aswild 0:29ab304ca8ce 11 // SDO_XM and SDO_G are pulled up, so our addresses are:
aswild 0:29ab304ca8ce 12 #define LSM9DS0_XM_ADDR 0x1D // Would be 0x1E if SDO_XM is LOW
aswild 0:29ab304ca8ce 13 #define LSM9DS0_G_ADDR 0x6B // Would be 0x6A if SDO_G is LOW
aswild 0:29ab304ca8ce 14
aswild 4:a9e3007530a7 15 // refresh time. set to 500 for part 2 and 50 for part 4
ldeng31 7:8c458123a665 16 #define REFRESH_TIME_MS 50
aswild 0:29ab304ca8ce 17
aswild 0:29ab304ca8ce 18 // Verify that the pin assignments below match your breadboard
aswild 0:29ab304ca8ce 19 LSM9DS0 imu(p9, p10, LSM9DS0_G_ADDR, LSM9DS0_XM_ADDR);
aswild 1:ae1cefe9aa38 20 Serial pc(USBTX, USBRX);
aswild 1:ae1cefe9aa38 21
aswild 4:a9e3007530a7 22 #ifdef PART_4
ldeng31 7:8c458123a665 23 uLCD_4DGL lcd(p28, p27, p30);
aswild 1:ae1cefe9aa38 24 #endif
aswild 0:29ab304ca8ce 25
aswild 0:29ab304ca8ce 26 //Init Serial port and LSM9DS0 chip
aswild 0:29ab304ca8ce 27 void setup()
aswild 0:29ab304ca8ce 28 {
aswild 4:a9e3007530a7 29 #ifdef PART_4
aswild 0:29ab304ca8ce 30 lcd.baudrate(3000000);
aswild 0:29ab304ca8ce 31 lcd.background_color(0);
aswild 0:29ab304ca8ce 32 lcd.cls();
aswild 0:29ab304ca8ce 33 lcd.printf("Initializing...");
aswild 1:ae1cefe9aa38 34 #endif
aswild 1:ae1cefe9aa38 35
aswild 0:29ab304ca8ce 36 // Use the begin() function to initialize the LSM9DS0 library.
aswild 0:29ab304ca8ce 37 // You can either call it with no parameters (the easy way):
aswild 0:29ab304ca8ce 38 uint16_t status = imu.begin();
aswild 0:29ab304ca8ce 39
aswild 0:29ab304ca8ce 40 //Make sure communication is working
aswild 0:29ab304ca8ce 41 pc.printf("LSM9DS0 WHO_AM_I's returned: 0x%X\n", status);
aswild 0:29ab304ca8ce 42 pc.printf("Should be 0x49D4\n\n");
aswild 0:29ab304ca8ce 43 }
aswild 0:29ab304ca8ce 44
aswild 0:29ab304ca8ce 45 int main()
aswild 0:29ab304ca8ce 46 {
aswild 0:29ab304ca8ce 47 setup(); //Setup sensor and Serial
aswild 0:29ab304ca8ce 48 pc.printf("------ LSM0DS0 Demo -----------\n");
ldeng31 7:8c458123a665 49 lcd.cls();
ldeng31 7:8c458123a665 50 lcd.circle(64, 64, 63, WHITE);
ldeng31 7:8c458123a665 51 float fx = 64.0, fy = 64.0;
ldeng31 7:8c458123a665 52 int x = 64, y = 64, radius = 4;
ldeng31 7:8c458123a665 53 float vx, vy;
ldeng31 7:8c458123a665 54
aswild 0:29ab304ca8ce 55 while (true)
aswild 0:29ab304ca8ce 56 {
aswild 0:29ab304ca8ce 57 // Compass Trigonometry tip: you can retrieve the compass heading (in degrees) directly from
aswild 0:29ab304ca8ce 58 // the IMU library. Example:
aswild 0:29ab304ca8ce 59 // imu.readMag();
aswild 0:29ab304ca8ce 60 // float heading = imu.calcHeading();
aswild 0:29ab304ca8ce 61 // Remember that x = length*cos(heading) and y = length*sin(heading)
aswild 0:29ab304ca8ce 62 // to convert from degrees to radians (for sin/cos functions), multiply by pi/180
ycai47 6:87468d3886ea 63
ycai47 6:87468d3886ea 64 // Part 2 read compass heading, accelerometer data, and temperature
ycai47 6:87468d3886ea 65 // and print it to the USB serial port
ycai47 6:87468d3886ea 66 imu.readGyro();
ycai47 6:87468d3886ea 67 imu.readMag();
ycai47 6:87468d3886ea 68 imu.readAccel();
ycai47 6:87468d3886ea 69 float heading = imu.calcHeading(); //calculate compass heading
ycai47 6:87468d3886ea 70 imu.readTemp();
ycai47 6:87468d3886ea 71 //pc.printf("Compass heading: %2f\n", heading);
ldeng31 7:8c458123a665 72 //pc.printf("Accelerometer data: %2f, %2f, %2f\n", imu.ax, imu.ay, imu.az);
ldeng31 7:8c458123a665 73 //pc.printf("Temperature in Fahr: %3.2f\n", imu.temperature_f);
ycai47 6:87468d3886ea 74
ldeng31 7:8c458123a665 75 //Part3 display through real serial COM
ldeng31 7:8c458123a665 76 //pc.printf("C: %2f\n", heading);
ldeng31 7:8c458123a665 77 //pc.printf("A: %2f, %2f, %2f\n", imu.ax, imu.ay, imu.az);
ldeng31 7:8c458123a665 78 //pc.printf("T: %3.2f\n", imu.temperature_f);
ldeng31 7:8c458123a665 79
ldeng31 7:8c458123a665 80 //Part4 display bubble level on the uLCD
aswild 0:29ab304ca8ce 81
ldeng31 7:8c458123a665 82 lcd.filled_circle(x, y, radius, RED);
ldeng31 7:8c458123a665 83 wait(0.5);
ldeng31 7:8c458123a665 84 vx = imu.ax * 58;
ldeng31 7:8c458123a665 85 vy = imu.ay * 58;
ldeng31 7:8c458123a665 86 lcd.filled_circle(x, y, radius, BLACK);
ldeng31 7:8c458123a665 87 fx = 64 - vx;
ldeng31 7:8c458123a665 88 fy = 64 + vy;
ldeng31 7:8c458123a665 89 x = int(fx);
ldeng31 7:8c458123a665 90 y = int(fy);
ldeng31 7:8c458123a665 91
ldeng31 7:8c458123a665 92 //
ldeng31 7:8c458123a665 93
aswild 0:29ab304ca8ce 94 wait_ms(REFRESH_TIME_MS);
aswild 0:29ab304ca8ce 95 }
aswild 0:29ab304ca8ce 96 }