Lab2_part2&3&4
Dependencies: 4DGL-uLCD-SE LSM9DS0 mbed
Fork of 4180_LSM9DS0_lab by
main.cpp@6:87468d3886ea, 2015-09-05 (annotated)
- Committer:
- ycai47
- Date:
- Sat Sep 05 13:48:58 2015 +0000
- Revision:
- 6:87468d3886ea
- Parent:
- 4:a9e3007530a7
- Child:
- 7:8c458123a665
4180lab2_part2&3
Who changed what in which revision?
User | Revision | Line number | New 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 |
aswild | 1:ae1cefe9aa38 | 16 | #define REFRESH_TIME_MS 500 |
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 |
aswild | 1:ae1cefe9aa38 | 23 | uLCD_4DGL lcd(p28, p27, p30);X |
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 | |
aswild | 0:29ab304ca8ce | 34 | lcd.printf("Initializing..."); |
aswild | 1:ae1cefe9aa38 | 35 | #endif |
aswild | 1:ae1cefe9aa38 | 36 | |
aswild | 0:29ab304ca8ce | 37 | // Use the begin() function to initialize the LSM9DS0 library. |
aswild | 0:29ab304ca8ce | 38 | // You can either call it with no parameters (the easy way): |
aswild | 0:29ab304ca8ce | 39 | uint16_t status = imu.begin(); |
aswild | 0:29ab304ca8ce | 40 | |
aswild | 0:29ab304ca8ce | 41 | //Make sure communication is working |
aswild | 0:29ab304ca8ce | 42 | pc.printf("LSM9DS0 WHO_AM_I's returned: 0x%X\n", status); |
aswild | 0:29ab304ca8ce | 43 | pc.printf("Should be 0x49D4\n\n"); |
aswild | 0:29ab304ca8ce | 44 | } |
aswild | 0:29ab304ca8ce | 45 | |
aswild | 0:29ab304ca8ce | 46 | int main() |
aswild | 0:29ab304ca8ce | 47 | { |
aswild | 0:29ab304ca8ce | 48 | setup(); //Setup sensor and Serial |
aswild | 0:29ab304ca8ce | 49 | pc.printf("------ LSM0DS0 Demo -----------\n"); |
aswild | 0:29ab304ca8ce | 50 | |
aswild | 0:29ab304ca8ce | 51 | while (true) |
aswild | 0:29ab304ca8ce | 52 | { |
aswild | 0:29ab304ca8ce | 53 | // Compass Trigonometry tip: you can retrieve the compass heading (in degrees) directly from |
aswild | 0:29ab304ca8ce | 54 | // the IMU library. Example: |
aswild | 0:29ab304ca8ce | 55 | // imu.readMag(); |
aswild | 0:29ab304ca8ce | 56 | // float heading = imu.calcHeading(); |
aswild | 0:29ab304ca8ce | 57 | // Remember that x = length*cos(heading) and y = length*sin(heading) |
aswild | 0:29ab304ca8ce | 58 | // to convert from degrees to radians (for sin/cos functions), multiply by pi/180 |
ycai47 | 6:87468d3886ea | 59 | |
ycai47 | 6:87468d3886ea | 60 | // Part 2 read compass heading, accelerometer data, and temperature |
ycai47 | 6:87468d3886ea | 61 | // and print it to the USB serial port |
ycai47 | 6:87468d3886ea | 62 | imu.readGyro(); |
ycai47 | 6:87468d3886ea | 63 | imu.readMag(); |
ycai47 | 6:87468d3886ea | 64 | imu.readAccel(); |
ycai47 | 6:87468d3886ea | 65 | float heading = imu.calcHeading(); //calculate compass heading |
ycai47 | 6:87468d3886ea | 66 | imu.readTemp(); |
ycai47 | 6:87468d3886ea | 67 | //pc.printf("Compass heading: %2f\n", heading); |
ycai47 | 6:87468d3886ea | 68 | // pc.printf("Accelerometer data: %2f, %2f, %2f\n", imu.ax, imu.ay, imu.az); |
ycai47 | 6:87468d3886ea | 69 | // pc.printf("Temperature in Fahr: %3.2f\n", imu.temperature_f); |
ycai47 | 6:87468d3886ea | 70 | |
ycai47 | 6:87468d3886ea | 71 | //Part3 display it on the uLCD |
ycai47 | 6:87468d3886ea | 72 | pc.printf("C: %2f\n", heading); |
ycai47 | 6:87468d3886ea | 73 | pc.printf("A: %2f, %2f, %2f\n", imu.ax, imu.ay, imu.az); |
ycai47 | 6:87468d3886ea | 74 | pc.printf("T: %3.2f\n", imu.temperature_f); |
aswild | 0:29ab304ca8ce | 75 | |
aswild | 0:29ab304ca8ce | 76 | wait_ms(REFRESH_TIME_MS); |
aswild | 0:29ab304ca8ce | 77 | } |
aswild | 0:29ab304ca8ce | 78 | } |