Skip to content

Commit

Permalink
Adjusted begin() to accept addresses and port, created beginSPI()
Browse files Browse the repository at this point in the history
-begin now accepts three arguments (accel/gyro address, mag address, i2c port). Note, this is used for I2C.
-created beginSPI(). Use this now to use SPI. Requires arguments for CS pins.
-removed Wire.begin() from inside library. It is now required that users call this in their Arduino sketches.
-adjusted all examples to use new functions and added Wire.begin().
-removed constructor with arguments for interface_mode and addresses. This is now handled in begin functions.
-FYI, I verified everything (including trying out a different wire port) on an artemis redboard.
  • Loading branch information
lewispg228 committed Nov 22, 2019
1 parent ec525f1 commit a606ab8
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 139 deletions.
129 changes: 60 additions & 69 deletions examples/LSM9DS1_Basic_I2C/LSM9DS1_Basic_I2C.ino
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
/*****************************************************************
LSM9DS1_Basic_I2C.ino
SFE_LSM9DS1 Library Simple Example Code - I2C Interface
Jim Lindblom @ SparkFun Electronics
Original Creation Date: April 30, 2015
https://github.com/sparkfun/LSM9DS1_Breakout
LSM9DS1_Basic_I2C.ino
SFE_LSM9DS1 Library Simple Example Code - I2C Interface
Jim Lindblom @ SparkFun Electronics
Original Creation Date: April 30, 2015
https://github.com/sparkfun/LSM9DS1_Breakout
The LSM9DS1 is a versatile 9DOF sensor. It has a built-in
accelerometer, gyroscope, and magnetometer. Very cool! Plus it
functions over either SPI or I2C.
The LSM9DS1 is a versatile 9DOF sensor. It has a built-in
accelerometer, gyroscope, and magnetometer. Very cool! Plus it
functions over either SPI or I2C.
This Arduino sketch is a demo of the simple side of the
SFE_LSM9DS1 library. It'll demo the following:
* How to create a LSM9DS1 object, using a constructor (global
This Arduino sketch is a demo of the simple side of the
SFE_LSM9DS1 library. It'll demo the following:
How to create a LSM9DS1 object, using a constructor (global
variables section).
* How to use the begin() function of the LSM9DS1 class.
* How to read the gyroscope, accelerometer, and magnetometer
using the readGryo(), readAccel(), readMag() functions and
How to use the begin() function of the LSM9DS1 class.
How to read the gyroscope, accelerometer, and magnetometer
using the readGryo(), readAccel(), readMag() functions and
the gx, gy, gz, ax, ay, az, mx, my, and mz variables.
* How to calculate actual acceleration, rotation speed,
magnetic field strength using the calcAccel(), calcGyro()
How to calculate actual acceleration, rotation speed,
magnetic field strength using the calcAccel(), calcGyro()
and calcMag() functions.
* How to use the data from the LSM9DS1 to calculate
How to use the data from the LSM9DS1 to calculate
orientation and heading.
Hardware setup: This library supports communicating with the
LSM9DS1 over either I2C or SPI. This example demonstrates how
to use I2C. The pin-out is as follows:
Hardware setup: This library supports communicating with the
LSM9DS1 over either I2C or SPI. This example demonstrates how
to use I2C. The pin-out is as follows:
LSM9DS1 --------- Arduino
SCL ---------- SCL (A5 on older 'Duinos')
SDA ---------- SDA (A4 on older 'Duinos')
VDD ------------- 3.3V
GND ------------- GND
(CSG, CSXM, SDOG, and SDOXM should all be pulled high.
Jumpers on the breakout board will do this for you.)
(CSG, CSXM, SDOG, and SDOXM should all be pulled high.
Jumpers on the breakout board will do this for you.)
The LSM9DS1 has a maximum voltage of 3.6V. Make sure you power it
off the 3.3V rail! I2C pins are open-drain, so you'll be
(mostly) safe connecting the LSM9DS1's SCL and SDA pins
directly to the Arduino.
The LSM9DS1 has a maximum voltage of 3.6V. Make sure you power it
off the 3.3V rail! I2C pins are open-drain, so you'll be
(mostly) safe connecting the LSM9DS1's SCL and SDA pins
directly to the Arduino.
Development environment specifics:
Development environment specifics:
IDE: Arduino 1.6.3
Hardware Platform: SparkFun Redboard
LSM9DS1 Breakout Version: 1.0
This code is beerware. If you see me (or any other SparkFun
employee) at the local, and you've found our code helpful,
please buy us a round!
This code is beerware. If you see me (or any other SparkFun
employee) at the local, and you've found our code helpful,
please buy us a round!
Distributed as-is; no warranty is given.
Distributed as-is; no warranty is given.
*****************************************************************/
// The SFE_LSM9DS1 library requires both Wire and SPI be
// included BEFORE including the 9DS1 library.
Expand All @@ -67,8 +67,8 @@ LSM9DS1 imu;
// Example I2C Setup //
///////////////////////
// SDO_XM and SDO_G are both pulled high, so our addresses are:
#define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
#define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
// #define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
// #define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW

////////////////////////////
// Sketch Output Settings //
Expand All @@ -78,42 +78,33 @@ LSM9DS1 imu;
#define PRINT_SPEED 250 // 250 ms between prints
static unsigned long lastPrint = 0; // Keep track of print time

// Earth's magnetic field varies by location. Add or subtract
// a declination to get a more accurate heading. Calculate
// Earth's magnetic field varies by location. Add or subtract
// a declination to get a more accurate heading. Calculate
// your's here:
// http://www.ngdc.noaa.gov/geomag-web/#declination
#define DECLINATION -8.58 // Declination (degrees) in Boulder, CO.

//Function definitions
void printGyro();
void printAccel();
void printMag();
void printGyro();
void printAccel();
void printMag();
void printAttitude(float ax, float ay, float az, float mx, float my, float mz);

void setup()
void setup()
{

Serial.begin(115200);

// Before initializing the IMU, there are a few settings
// we may need to adjust. Use the settings struct to set
// the device's communication mode and addresses:
imu.settings.device.commInterface = IMU_MODE_I2C;
imu.settings.device.mAddress = LSM9DS1_M;
imu.settings.device.agAddress = LSM9DS1_AG;
// The above lines will only take effect AFTER calling
// imu.begin(), which verifies communication with the IMU
// and turns it on.
if (!imu.begin())

Wire.begin();

if (!imu.begin()) // with no arguments, this uses default addresses (AG:0x6B, M:0x1E) and i2c port (Wire).
{
Serial.println("Failed to communicate with LSM9DS1.");
Serial.println("Double-check wiring.");
Serial.println("Default settings in this sketch will " \
"work for an out of the box LSM9DS1 " \
"Breakout, but may need to be modified " \
"if the board jumpers are.");
while (1)
;
"work for an out of the box LSM9DS1 " \
"Breakout, but may need to be modified " \
"if the board jumpers are.");
while (1);
}
}

Expand Down Expand Up @@ -141,7 +132,7 @@ void loop()
// mx, my, and mz variables with the most current data.
imu.readMag();
}

if ((lastPrint + PRINT_SPEED) < millis())
{
printGyro(); // Print "G: gx, gy, gz"
Expand All @@ -151,10 +142,10 @@ void loop()
// Call print attitude. The LSM9DS1's mag x and y
// axes are opposite to the accelerometer, so my, mx are
// substituted for each other.
printAttitude(imu.ax, imu.ay, imu.az,
-imu.my, -imu.mx, imu.mz);
printAttitude(imu.ax, imu.ay, imu.az,
-imu.my, -imu.mx, imu.mz);
Serial.println();

lastPrint = millis(); // Update lastPrint time
}
}
Expand Down Expand Up @@ -184,7 +175,7 @@ void printGyro()
}

void printAccel()
{
{
// Now we can use the ax, ay, and az variables as we please.
// Either print them as raw ADC values, or calculated in g's.
Serial.print("A: ");
Expand All @@ -198,7 +189,7 @@ void printAccel()
Serial.print(", ");
Serial.print(imu.calcAccel(imu.az), 2);
Serial.println(" g");
#elif defined PRINT_RAW
#elif defined PRINT_RAW
Serial.print(imu.ax);
Serial.print(", ");
Serial.print(imu.ay);
Expand All @@ -209,7 +200,7 @@ void printAccel()
}

void printMag()
{
{
// Now we can use the mx, my, and mz variables as we please.
// Either print them as raw ADC values, or calculated in Gauss.
Serial.print("M: ");
Expand Down Expand Up @@ -241,26 +232,26 @@ void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
{
float roll = atan2(ay, az);
float pitch = atan2(-ax, sqrt(ay * ay + az * az));

float heading;
if (my == 0)
heading = (mx < 0) ? PI : 0;
else
heading = atan2(mx, my);

heading -= DECLINATION * PI / 180;

if (heading > PI) heading -= (2 * PI);
else if (heading < -PI) heading += (2 * PI);

// Convert everything from radians to degrees:
heading *= 180.0 / PI;
pitch *= 180.0 / PI;
roll *= 180.0 / PI;

Serial.print("Pitch, Roll: ");
Serial.print(pitch, 2);
Serial.print(", ");
Serial.println(roll, 2);
Serial.print("Heading: "); Serial.println(heading, 2);
}
}
15 changes: 4 additions & 11 deletions examples/LSM9DS1_Basic_SPI/LSM9DS1_Basic_SPI.ino
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,10 @@ void printAttitude(float ax, float ay, float az, float mx, float my, float mz);
void setup()
{
Serial.begin(115200);

// Before initializing the IMU, there are a few settings
// we may need to adjust. Use the settings struct to set
// the device's communication mode and addresses:
imu.settings.device.commInterface = IMU_MODE_SPI;
imu.settings.device.mAddress = LSM9DS1_M_CS;
imu.settings.device.agAddress = LSM9DS1_AG_CS;
// The above lines will only take effect AFTER calling
// imu.begin(), which verifies communication with the IMU

// imu.beginSPI(), which verifies communication with the IMU
// and turns it on.
if (!imu.begin())
if (!imu.beginSPI(LSM9DS1_AG_CS, LSM9DS1_M_CS)) // note, we need to sent this our CS pins (defined above)
{
Serial.println("Failed to communicate with LSM9DS1.");
Serial.println("Double-check wiring.");
Expand Down Expand Up @@ -251,4 +244,4 @@ void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
Serial.print(", ");
Serial.println(roll, 2);
Serial.print("Heading: "); Serial.println(heading, 2);
}
}
13 changes: 5 additions & 8 deletions examples/LSM9DS1_Interrupts/LSM9DS1_Interrupts.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ to use I2C. The pin-out is as follows:
INT2 ------------- D4
INT1 ------------- D3
INTM ------------- D5
RDY -------------- D6
(CSG, CSXM, SDOG, and SDOXM should all be pulled high.
Jumpers on the breakout board will do this for you.)
Expand Down Expand Up @@ -86,11 +87,6 @@ void printStats();
// and sample rates.
uint16_t configureIMU()
{
// Set up Device Mode (I2C) and I2C addresses:
imu.settings.device.commInterface = IMU_MODE_I2C;
imu.settings.device.agAddress = LSM9DS1_AG_ADDR(1);
imu.settings.device.mAddress = LSM9DS1_M_ADDR(1);

// gyro.latchInterrupt controls the latching of the
// gyro and accelerometer interrupts (INT1 and INT2).
// false = no latching
Expand All @@ -106,10 +102,10 @@ uint16_t configureIMU()
imu.settings.mag.scale = 4;
// Set magnetometer sample rate to 0.625 Hz
imu.settings.mag.sampleRate = 0;

// Call imu.begin() to initialize the sensor and instill
// it with our new settings.
return imu.begin();
return imu.begin(LSM9DS1_AG_ADDR(1), LSM9DS1_M_ADDR(1), Wire); // set addresses and wire port
}

void configureLSM9DS1Interrupts()
Expand Down Expand Up @@ -194,6 +190,8 @@ void setup()
// It is active high and always turned on.
pinMode(RDYM_PIN, INPUT);

Wire.begin();

// Turn on the IMU with configureIMU() (defined above)
// check the return status of imu.begin() to make sure
// it's connected.
Expand Down Expand Up @@ -312,4 +310,3 @@ void printStats()
Serial.print(imu.my); Serial.print(", ");
Serial.println(imu.mz);
}

24 changes: 7 additions & 17 deletions examples/LSM9DS1_Settings/LSM9DS1_Settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Distributed as-is; no warranty is given.

LSM9DS1 imu; // Create an LSM9DS1 object

// SDO_XM and SDO_G are both pulled high, so our addresses are:
#define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
#define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW

// Global variables to keep track of update rates
unsigned long startTime;
unsigned int accelReadCounter = 0;
Expand All @@ -63,20 +67,6 @@ const unsigned int PRINT_RATE = 500;
//Function definitions
void printSensorReadings();

void setupDevice()
{
// [commInterface] determines whether we'll use I2C or SPI
// to communicate with the LSM9DS1.
// Use either IMU_MODE_I2C or IMU_MODE_SPI
imu.settings.device.commInterface = IMU_MODE_I2C;
// [mAddress] sets the I2C address or SPI CS pin of the
// LSM9DS1's magnetometer.
imu.settings.device.mAddress = 0x1E; // Use I2C addres 0x1E
// [agAddress] sets the I2C address or SPI CS pin of the
// LSM9DS1's accelerometer/gyroscope.
imu.settings.device.agAddress = 0x6B; // I2C address 0x6B
}

void setupGyro()
{
// [enabled] turns the gyro on or off.
Expand Down Expand Up @@ -192,18 +182,19 @@ void setupTemperature()

uint16_t initLSM9DS1()
{
setupDevice(); // Setup general device parameters
setupGyro(); // Set up gyroscope parameters
setupAccel(); // Set up accelerometer parameters
setupMag(); // Set up magnetometer parameters
setupTemperature(); // Set up temp sensor parameter

return imu.begin();
return imu.begin(LSM9DS1_AG, LSM9DS1_M, Wire); // for SPI use beginSPI()
}

void setup()
{
Serial.begin(115200);

Wire.begin();

Serial.println("Initializing the LSM9DS1");
uint16_t status = initLSM9DS1();
Expand Down Expand Up @@ -300,4 +291,3 @@ void printSensorReadings()
Serial.println(" Hz");
Serial.println();
}

1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gBiasRaw KEYWORD2
aBiasRaw KEYWORD2
mBiasRaw KEYWORD2
begin KEYWORD2
beginSPI KEYWORD2
calibrate KEYWORD2
calibrateMag KEYWORD2
magOffset KEYWORD2
Expand Down
Loading

0 comments on commit a606ab8

Please sign in to comment.