11/* ****************************************************************
2- LSM9DS1_Basic_I2C.ino
3- SFE_LSM9DS1 Library Simple Example Code - I2C Interface
4- Jim Lindblom @ SparkFun Electronics
5- Original Creation Date: April 30, 2015
6- https://github.com/sparkfun/LSM9DS1_Breakout
2+ LSM9DS1_Basic_I2C.ino
3+ SFE_LSM9DS1 Library Simple Example Code - I2C Interface
4+ Jim Lindblom @ SparkFun Electronics
5+ Original Creation Date: April 30, 2015
6+ https://github.com/sparkfun/LSM9DS1_Breakout
77
8- The LSM9DS1 is a versatile 9DOF sensor. It has a built-in
9- accelerometer, gyroscope, and magnetometer. Very cool! Plus it
10- functions over either SPI or I2C.
8+ The LSM9DS1 is a versatile 9DOF sensor. It has a built-in
9+ accelerometer, gyroscope, and magnetometer. Very cool! Plus it
10+ functions over either SPI or I2C.
1111
12- This Arduino sketch is a demo of the simple side of the
13- SFE_LSM9DS1 library. It'll demo the following:
14- * How to create a LSM9DS1 object, using a constructor (global
12+ This Arduino sketch is a demo of the simple side of the
13+ SFE_LSM9DS1 library. It'll demo the following:
14+ How to create a LSM9DS1 object, using a constructor (global
1515 variables section).
16- * How to use the begin() function of the LSM9DS1 class.
17- * How to read the gyroscope, accelerometer, and magnetometer
18- using the readGryo(), readAccel(), readMag() functions and
16+ How to use the begin() function of the LSM9DS1 class.
17+ How to read the gyroscope, accelerometer, and magnetometer
18+ using the readGryo(), readAccel(), readMag() functions and
1919 the gx, gy, gz, ax, ay, az, mx, my, and mz variables.
20- * How to calculate actual acceleration, rotation speed,
21- magnetic field strength using the calcAccel(), calcGyro()
20+ How to calculate actual acceleration, rotation speed,
21+ magnetic field strength using the calcAccel(), calcGyro()
2222 and calcMag() functions.
23- * How to use the data from the LSM9DS1 to calculate
23+ How to use the data from the LSM9DS1 to calculate
2424 orientation and heading.
2525
26- Hardware setup: This library supports communicating with the
27- LSM9DS1 over either I2C or SPI. This example demonstrates how
28- to use I2C. The pin-out is as follows:
26+ Hardware setup: This library supports communicating with the
27+ LSM9DS1 over either I2C or SPI. This example demonstrates how
28+ to use I2C. The pin-out is as follows:
2929 LSM9DS1 --------- Arduino
3030 SCL ---------- SCL (A5 on older 'Duinos')
3131 SDA ---------- SDA (A4 on older 'Duinos')
3232 VDD ------------- 3.3V
3333 GND ------------- GND
34- (CSG, CSXM, SDOG, and SDOXM should all be pulled high.
35- Jumpers on the breakout board will do this for you.)
34+ (CSG, CSXM, SDOG, and SDOXM should all be pulled high.
35+ Jumpers on the breakout board will do this for you.)
3636
37- The LSM9DS1 has a maximum voltage of 3.6V. Make sure you power it
38- off the 3.3V rail! I2C pins are open-drain, so you'll be
39- (mostly) safe connecting the LSM9DS1's SCL and SDA pins
40- directly to the Arduino.
37+ The LSM9DS1 has a maximum voltage of 3.6V. Make sure you power it
38+ off the 3.3V rail! I2C pins are open-drain, so you'll be
39+ (mostly) safe connecting the LSM9DS1's SCL and SDA pins
40+ directly to the Arduino.
4141
42- Development environment specifics:
42+ Development environment specifics:
4343 IDE: Arduino 1.6.3
4444 Hardware Platform: SparkFun Redboard
4545 LSM9DS1 Breakout Version: 1.0
4646
47- This code is beerware. If you see me (or any other SparkFun
48- employee) at the local, and you've found our code helpful,
49- please buy us a round!
47+ This code is beerware. If you see me (or any other SparkFun
48+ employee) at the local, and you've found our code helpful,
49+ please buy us a round!
5050
51- Distributed as-is; no warranty is given.
51+ Distributed as-is; no warranty is given.
5252*****************************************************************/
5353// The SFE_LSM9DS1 library requires both Wire and SPI be
5454// included BEFORE including the 9DS1 library.
@@ -67,8 +67,8 @@ LSM9DS1 imu;
6767// Example I2C Setup //
6868// /////////////////////
6969// SDO_XM and SDO_G are both pulled high, so our addresses are:
70- #define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
71- #define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
70+ // #define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
71+ // #define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
7272
7373// //////////////////////////
7474// Sketch Output Settings //
@@ -78,42 +78,33 @@ LSM9DS1 imu;
7878#define PRINT_SPEED 250 // 250 ms between prints
7979static unsigned long lastPrint = 0 ; // Keep track of print time
8080
81- // Earth's magnetic field varies by location. Add or subtract
82- // a declination to get a more accurate heading. Calculate
81+ // Earth's magnetic field varies by location. Add or subtract
82+ // a declination to get a more accurate heading. Calculate
8383// your's here:
8484// http://www.ngdc.noaa.gov/geomag-web/#declination
8585#define DECLINATION -8.58 // Declination (degrees) in Boulder, CO.
8686
8787// Function definitions
88- void printGyro ();
89- void printAccel ();
90- void printMag ();
88+ void printGyro ();
89+ void printAccel ();
90+ void printMag ();
9191void printAttitude (float ax, float ay, float az, float mx, float my, float mz);
9292
93- void setup ()
93+ void setup ()
9494{
95-
9695 Serial.begin (115200 );
97-
98- // Before initializing the IMU, there are a few settings
99- // we may need to adjust. Use the settings struct to set
100- // the device's communication mode and addresses:
101- imu.settings .device .commInterface = IMU_MODE_I2C;
102- imu.settings .device .mAddress = LSM9DS1_M;
103- imu.settings .device .agAddress = LSM9DS1_AG;
104- // The above lines will only take effect AFTER calling
105- // imu.begin(), which verifies communication with the IMU
106- // and turns it on.
107- if (!imu.begin ())
96+
97+ Wire.begin ();
98+
99+ if (!imu.begin ()) // with no arguments, this uses default addresses (AG:0x6B, M:0x1E) and i2c port (Wire).
108100 {
109101 Serial.println (" Failed to communicate with LSM9DS1." );
110102 Serial.println (" Double-check wiring." );
111103 Serial.println (" Default settings in this sketch will " \
112- " work for an out of the box LSM9DS1 " \
113- " Breakout, but may need to be modified " \
114- " if the board jumpers are." );
115- while (1 )
116- ;
104+ " work for an out of the box LSM9DS1 " \
105+ " Breakout, but may need to be modified " \
106+ " if the board jumpers are." );
107+ while (1 );
117108 }
118109}
119110
@@ -141,7 +132,7 @@ void loop()
141132 // mx, my, and mz variables with the most current data.
142133 imu.readMag ();
143134 }
144-
135+
145136 if ((lastPrint + PRINT_SPEED) < millis ())
146137 {
147138 printGyro (); // Print "G: gx, gy, gz"
@@ -151,10 +142,10 @@ void loop()
151142 // Call print attitude. The LSM9DS1's mag x and y
152143 // axes are opposite to the accelerometer, so my, mx are
153144 // substituted for each other.
154- printAttitude (imu.ax , imu.ay , imu.az ,
155- -imu.my , -imu.mx , imu.mz );
145+ printAttitude (imu.ax , imu.ay , imu.az ,
146+ -imu.my , -imu.mx , imu.mz );
156147 Serial.println ();
157-
148+
158149 lastPrint = millis (); // Update lastPrint time
159150 }
160151}
@@ -184,7 +175,7 @@ void printGyro()
184175}
185176
186177void printAccel ()
187- {
178+ {
188179 // Now we can use the ax, ay, and az variables as we please.
189180 // Either print them as raw ADC values, or calculated in g's.
190181 Serial.print (" A: " );
@@ -198,7 +189,7 @@ void printAccel()
198189 Serial.print (" , " );
199190 Serial.print (imu.calcAccel (imu.az ), 2 );
200191 Serial.println (" g" );
201- #elif defined PRINT_RAW
192+ #elif defined PRINT_RAW
202193 Serial.print (imu.ax );
203194 Serial.print (" , " );
204195 Serial.print (imu.ay );
@@ -209,7 +200,7 @@ void printAccel()
209200}
210201
211202void printMag ()
212- {
203+ {
213204 // Now we can use the mx, my, and mz variables as we please.
214205 // Either print them as raw ADC values, or calculated in Gauss.
215206 Serial.print (" M: " );
@@ -241,26 +232,26 @@ void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
241232{
242233 float roll = atan2 (ay, az);
243234 float pitch = atan2 (-ax, sqrt (ay * ay + az * az));
244-
235+
245236 float heading;
246237 if (my == 0 )
247238 heading = (mx < 0 ) ? PI : 0 ;
248239 else
249240 heading = atan2 (mx, my);
250-
241+
251242 heading -= DECLINATION * PI / 180 ;
252-
243+
253244 if (heading > PI) heading -= (2 * PI);
254245 else if (heading < -PI) heading += (2 * PI);
255-
246+
256247 // Convert everything from radians to degrees:
257248 heading *= 180.0 / PI;
258249 pitch *= 180.0 / PI;
259250 roll *= 180.0 / PI;
260-
251+
261252 Serial.print (" Pitch, Roll: " );
262253 Serial.print (pitch, 2 );
263254 Serial.print (" , " );
264255 Serial.println (roll, 2 );
265256 Serial.print (" Heading: " ); Serial.println (heading, 2 );
266- }
257+ }
0 commit comments