|
| 1 | +## SparkFun LSM9DS1 Particle Library |
| 2 | + |
| 3 | +Firmware library SparkFun's Photon IMU Shield and the LSM9DS1 Breakout. |
| 4 | + |
| 5 | +About |
| 6 | +------------------- |
| 7 | + |
| 8 | +This is a firmware library for [SparkFun's Photon IMU Shield](https://www.sparkfun.com/products/13629). |
| 9 | + |
| 10 | +[](https://www.sparkfun.com/products/13629). |
| 11 | + |
| 12 | +The Photon IMU Shield connects the Photon WiFi development board up to an [ST LSM9DS1](http://www.st.com/web/catalog/sense_power/FM89/SC1448/PF259998) 9DOF IMU - providing it access to an accelerometer, gyroscope, and magnetometer. |
| 13 | + |
| 14 | +Repository Contents |
| 15 | +------------------- |
| 16 | + |
| 17 | +* **/doc** - Additional documentation for the user. These files are ignored by the IDE. |
| 18 | +* **/firmware** - Source files for the library (.cpp, .h). |
| 19 | +* **/firmware/examples** - Example sketches for the library (.cpp). Run these from the Particle IDE. |
| 20 | +* **spark.json** - General library properties for the Particel library manager. |
| 21 | + |
| 22 | +Example Usage |
| 23 | +------------------- |
| 24 | + |
| 25 | +#### Initializing the Library |
| 26 | + |
| 27 | +Include the library, declare an IMU object, and set it up with these snippets of code: |
| 28 | + |
| 29 | + #include "SparkFunLSM9DS1/SparkFunLSM9DS1.h" |
| 30 | + |
| 31 | + // Use the LSM9DS1 class to create an object. [imu] can be |
| 32 | + // named anything, we'll refer to that throught the sketch. |
| 33 | + LSM9DS1 imu; |
| 34 | + |
| 35 | + // SDO_XM and SDO_G are both pulled high, so our addresses are: |
| 36 | + #define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW |
| 37 | + #define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW |
| 38 | + |
| 39 | + void setup() |
| 40 | + { |
| 41 | + Serial.begin(115200); |
| 42 | + |
| 43 | + // Before initializing the IMU, there are a few settings |
| 44 | + // we may need to adjust. Use the settings struct to set |
| 45 | + // the device's communication mode and addresses: |
| 46 | + imu.settings.device.commInterface = IMU_MODE_I2C; |
| 47 | + imu.settings.device.mAddress = LSM9DS1_M; |
| 48 | + imu.settings.device.agAddress = LSM9DS1_AG; |
| 49 | + // The above lines will only take effect AFTER calling |
| 50 | + // imu.begin(), which verifies communication with the IMU |
| 51 | + // and turns it on. |
| 52 | + if (!imu.begin()) |
| 53 | + { |
| 54 | + Serial.println("Failed to communicate with LSM9DS1."); |
| 55 | + Serial.println("Double-check wiring."); |
| 56 | + Serial.println("Default settings in this sketch will " \ |
| 57 | + "work for an out of the box LSM9DS1 " \ |
| 58 | + "Breakout, but may need to be modified " \ |
| 59 | + "if the board jumpers are."); |
| 60 | + while (1) |
| 61 | + ; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +#### Reading Sensor Data |
| 66 | + |
| 67 | +To get data out of the IMU, call `imu.readAccel()`, `imu.readGyro()`, and `imu.readMag()`. Those functions will update the objects member variables: `imu.ax`, `imu.ay`, `imu.az`, `imu.gx`, `imu.gy`, `imu.gz`, `imu.mx`, `imu.my`, and`imu.mz`. Here, some example functions can probably make it more clear: |
| 68 | + |
| 69 | + void printAccel() |
| 70 | + { |
| 71 | + // To read from the accelerometer, you must first call the |
| 72 | + // readAccel() function. When this exits, it'll update the |
| 73 | + // ax, ay, and az variables with the most current data. |
| 74 | + imu.readAccel(); |
| 75 | + |
| 76 | + // Now we can use the ax, ay, and az variables as we please. |
| 77 | + Serial.print("A: "); |
| 78 | + Serial.print(imu.ax); |
| 79 | + Serial.print(", "); |
| 80 | + Serial.print(imu.ay); |
| 81 | + Serial.print(", "); |
| 82 | + Serial.println(imu.az); |
| 83 | + } |
| 84 | + |
| 85 | + void printGyro() |
| 86 | + { |
| 87 | + // To read from the gyroscope, you must first call the |
| 88 | + // readGyro() function. When this exits, it'll update the |
| 89 | + // gx, gy, and gz variables with the most current data. |
| 90 | + imu.readGyro(); |
| 91 | + |
| 92 | + // Now we can use the gx, gy, and gz variables as we please. |
| 93 | + Serial.print("G: "); |
| 94 | + Serial.print(imu.gx); |
| 95 | + Serial.print(", "); |
| 96 | + Serial.print(imu.gy); |
| 97 | + Serial.print(", "); |
| 98 | + Serial.println(imu.gz); |
| 99 | + } |
| 100 | + |
| 101 | + void printMag() |
| 102 | + { |
| 103 | + // To read from the magnetometer, you must first call the |
| 104 | + // readMag() function. When this exits, it'll update the |
| 105 | + // mx, my, and mz variables with the most current data. |
| 106 | + imu.readMag(); |
| 107 | + |
| 108 | + // Now we can use the mx, my, and mz variables as we please. |
| 109 | + Serial.print("M: "); |
| 110 | + Serial.print(imu.mx); |
| 111 | + Serial.print(", "); |
| 112 | + Serial.print(imu.my); |
| 113 | + Serial.print(", "); |
| 114 | + Serial.println(imu.mz); |
| 115 | + } |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +Check out the example files in the [examples directory](https://github.com/sparkfun/SparkFun_LSM9DS1_Particle_Library/tree/master/firmware/examples) for more guidance. |
| 120 | + |
| 121 | +Recommended Components |
| 122 | +------------------- |
| 123 | + |
| 124 | +* [Particle Photon](https://www.sparkfun.com/products/13345) |
| 125 | +* [SparkFun Photon IMU Shield](https://www.sparkfun.com/products/13629) |
| 126 | + |
| 127 | + |
| 128 | +License Information |
| 129 | +------------------- |
| 130 | + |
| 131 | +This product is _**open source**_! |
| 132 | + |
| 133 | +Please review the LICENSE.md file for license information. |
| 134 | + |
| 135 | +If you have any questions or concerns on licensing, please contact [email protected]. |
| 136 | + |
| 137 | +Distributed as-is; no warranty is given. |
| 138 | + |
| 139 | +- Your friends at SparkFun. |
0 commit comments