Lab2_part2&3&4
Dependencies: 4DGL-uLCD-SE LSM9DS0 mbed
Fork of 4180_LSM9DS0_lab by
main.cpp@8:1fae20911dbc, 2015-11-28 (annotated)
- Committer:
- ldeng31
- Date:
- Sat Nov 28 00:41:51 2015 +0000
- Revision:
- 8:1fae20911dbc
- Parent:
- 7:8c458123a665
dfasd
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 |
ldeng31 | 8:1fae20911dbc | 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 | 8:1fae20911dbc | 16 | #define REFRESH_TIME_MS 500 |
aswild | 0:29ab304ca8ce | 17 | |
aswild | 0:29ab304ca8ce | 18 | // Verify that the pin assignments below match your breadboard |
ldeng31 | 8:1fae20911dbc | 19 | LSM9DS0 imu(p28, p27, LSM9DS0_G_ADDR, LSM9DS0_XM_ADDR); |
aswild | 1:ae1cefe9aa38 | 20 | Serial pc(USBTX, USBRX); |
aswild | 1:ae1cefe9aa38 | 21 | |
ldeng31 | 8:1fae20911dbc | 22 | //#ifdef PART_4 |
ldeng31 | 8:1fae20911dbc | 23 | //uLCD_4DGL lcd(p28, p27, p30); |
ldeng31 | 8:1fae20911dbc | 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 | 8:1fae20911dbc | 49 | //lcd.cls(); |
ldeng31 | 8:1fae20911dbc | 50 | |
ldeng31 | 8:1fae20911dbc | 51 | //float fx = 64.0, fy = 64.0; |
ldeng31 | 8:1fae20911dbc | 52 | //int x = 64, y = 64, radius = 4; |
ldeng31 | 8:1fae20911dbc | 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 |
ldeng31 | 8:1fae20911dbc | 66 | //imu.readGyro(); |
ldeng31 | 8:1fae20911dbc | 67 | //imu.readMag(); |
ycai47 | 6:87468d3886ea | 68 | imu.readAccel(); |
ldeng31 | 8:1fae20911dbc | 69 | //float heading = imu.calcHeading(); //calculate compass heading |
ldeng31 | 8:1fae20911dbc | 70 | //imu.readTemp(); |
ycai47 | 6:87468d3886ea | 71 | //pc.printf("Compass heading: %2f\n", heading); |
ldeng31 | 8:1fae20911dbc | 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 | 8:1fae20911dbc | 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 | 8:1fae20911dbc | 82 | /*lcd.filled_circle(x, y, radius, RED); |
ldeng31 | 7:8c458123a665 | 83 | wait(0.5); |
ldeng31 | 8:1fae20911dbc | 84 | vx = imu.ax * 64+60; |
ldeng31 | 8:1fae20911dbc | 85 | vy = imu.ay * (-64)+60; |
ldeng31 | 7:8c458123a665 | 86 | lcd.filled_circle(x, y, radius, BLACK); |
ldeng31 | 8:1fae20911dbc | 87 | //fx = 64 - vx; |
ldeng31 | 8:1fae20911dbc | 88 | //fy = 64 + vy; |
ldeng31 | 8:1fae20911dbc | 89 | fx = vx; |
ldeng31 | 8:1fae20911dbc | 90 | fy = vy; |
ldeng31 | 7:8c458123a665 | 91 | x = int(fx); |
ldeng31 | 7:8c458123a665 | 92 | y = int(fy); |
ldeng31 | 7:8c458123a665 | 93 | |
ldeng31 | 8:1fae20911dbc | 94 | lcd.circle(64, 64, 6, WHITE);*/ |
ldeng31 | 7:8c458123a665 | 95 | // |
ldeng31 | 7:8c458123a665 | 96 | |
aswild | 0:29ab304ca8ce | 97 | wait_ms(REFRESH_TIME_MS); |
aswild | 0:29ab304ca8ce | 98 | } |
aswild | 0:29ab304ca8ce | 99 | } |