Skip to content

Commit a1d66f6

Browse files
committed
Add LSM9DS1 I2C driver
1 parent 1d58b0a commit a1d66f6

File tree

6 files changed

+312
-1
lines changed

6 files changed

+312
-1
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit LIS3DH, Adafruit AS5600 Library, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA237 and INA238 Library, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit QMC5883P Library, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit LIS3DH, Adafruit LSM9DS1 Library, Adafruit AS5600 Library, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA237 and INA238 Library, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit QMC5883P Library, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ lib_deps =
4747
adafruit/Adafruit HTU21DF Library
4848
adafruit/Adafruit HTU31D Library
4949
adafruit/Adafruit LIS3DH
50+
adafruit/Adafruit LSM9DS1 Library
5051
adafruit/Adafruit LTR390 Library
5152
adafruit/Adafruit LTR329 and LTR303
5253
adafruit/Adafruit PCT2075

src/components/i2c/controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ static const std::map<std::string, FnCreateI2CSensorDriver> I2cFactorySensor = {
173173
const char *driver_name) -> drvBase * {
174174
return new drvLis3dh(i2c, addr, mux_channel, driver_name);
175175
}},
176+
{"lsm9ds1",
177+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
178+
const char *driver_name) -> drvBase * {
179+
return new drvLsm9ds1(i2c, addr, mux_channel, driver_name);
180+
}},
176181
{"lps3xhw",
177182
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
178183
const char *driver_name) -> drvBase * {

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "drivers/drvIna260.h"
4242
#include "drivers/drvLc709203f.h"
4343
#include "drivers/drvLis3dh.h"
44+
#include "drivers/drvLsm9ds1.h"
4445
#include "drivers/drvLps22hb.h"
4546
#include "drivers/drvLps25hb.h"
4647
#include "drivers/drvLps3xhw.h"
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*!
2+
* @file drvLsm9ds1.cpp
3+
*
4+
* Driver wrapper for the Adafruit LSM9DS1 9-DOF IMU.
5+
*
6+
*/
7+
8+
#include "drvLsm9ds1.h"
9+
10+
#include <Adafruit_LSM9DS1.h>
11+
#include <math.h>
12+
13+
/******************************************************************************/
14+
/*!
15+
@brief Destructor for a LSM9DS1 sensor.
16+
*/
17+
/******************************************************************************/
18+
drvLsm9ds1::~drvLsm9ds1() {
19+
if (_lsm) {
20+
delete _lsm;
21+
_lsm = nullptr;
22+
}
23+
}
24+
25+
/******************************************************************************/
26+
/*!
27+
@brief Initializes the LSM9DS1 sensor and begins I2C.
28+
@returns True if initialized successfully, False otherwise.
29+
*/
30+
/******************************************************************************/
31+
bool drvLsm9ds1::begin() {
32+
_lsm = new Adafruit_LSM9DS1(_i2c);
33+
// Consumes I2C Addresses 0x1E and 0x6B
34+
if (!_lsm->begin()) {
35+
WS_DEBUG_PRINTLN("LSM9DS1 failed to initialise!");
36+
return false;
37+
}
38+
39+
// Mirror the configuration used by the reference example
40+
_lsm->setupAccel(_lsm->LSM9DS1_ACCELRANGE_2G,
41+
_lsm->LSM9DS1_ACCELDATARATE_10HZ);
42+
_lsm->setupMag(_lsm->LSM9DS1_MAGGAIN_4GAUSS);
43+
_lsm->setupGyro(_lsm->LSM9DS1_GYROSCALE_245DPS);
44+
45+
return true;
46+
}
47+
48+
bool drvLsm9ds1::readAllEvents(sensors_event_t *accel,
49+
sensors_event_t *mag,
50+
sensors_event_t *gyro,
51+
sensors_event_t *temp) {
52+
if (!_lsm) {
53+
return false;
54+
}
55+
_lsm->read();
56+
_lsm->getEvent(accel, mag, gyro, temp);
57+
return true;
58+
}
59+
60+
/******************************************************************************/
61+
/*!
62+
@brief Gets the LSM9DS1's raw sensor event.
63+
@param rawEvent
64+
Pointer to the sensor event.
65+
@returns True if the sensor event was obtained successfully, False
66+
otherwise.
67+
*/
68+
/******************************************************************************/
69+
bool drvLsm9ds1::getEventRaw(sensors_event_t *rawEvent) {
70+
//TODO: Not yet, but eventually migrate to providing the temperatre data here
71+
WS_DEBUG_PRINTLN("[drvLsm9ds1] Getting raw event...");
72+
sensors_event_t accel, mag, gyro, temp;
73+
if (!readAllEvents(&accel, &mag, &gyro, &temp)) {
74+
return false;
75+
}
76+
77+
float mag_accel = sqrtf(accel.acceleration.x * accel.acceleration.x +
78+
accel.acceleration.y * accel.acceleration.y +
79+
accel.acceleration.z * accel.acceleration.z);
80+
rawEvent->data[0] = mag_accel;
81+
WS_DEBUG_PRINT("[drvLsm9ds1] Raw magnitude: ");
82+
WS_DEBUG_PRINTLN(mag_accel);
83+
return true;
84+
}
85+
86+
/******************************************************************************/
87+
/*!
88+
@brief Gets the LSM9DS1's boolean sensor event.
89+
@param booleanEvent
90+
Pointer to the sensor event.
91+
@returns True if the sensor event was obtained successfully, False
92+
otherwise.
93+
*/
94+
/******************************************************************************/
95+
bool drvLsm9ds1::getEventBoolean(sensors_event_t *booleanEvent) {
96+
WS_DEBUG_PRINTLN("[drvLsm9ds1] Checking for tap event...");
97+
sensors_event_t accel, mag, gyro, temp;
98+
if (!readAllEvents(&accel, &mag, &gyro, &temp)) {
99+
return false;
100+
}
101+
102+
float mag_accel = sqrtf(accel.acceleration.x * accel.acceleration.x +
103+
accel.acceleration.y * accel.acceleration.y +
104+
accel.acceleration.z * accel.acceleration.z);
105+
106+
bool tap_detected = (mag_accel > LSM9DS1_TAP_THRESHOLD_MSS);
107+
booleanEvent->data[0] = tap_detected ? 1.0f : 0.0f;
108+
109+
if (tap_detected) {
110+
WS_DEBUG_PRINTLN("[drvLsm9ds1] Tap event detected!");
111+
}
112+
113+
return true;
114+
}
115+
116+
/******************************************************************************/
117+
/*!
118+
@brief Gets the LSM9DS1's accelerometer sensor event (x,y,z in m/s^2).
119+
@param accelEvent
120+
Pointer to the accelerometer sensor event.
121+
@returns True if the sensor event was obtained successfully, False
122+
otherwise.
123+
*/
124+
/******************************************************************************/
125+
bool drvLsm9ds1::getEventAccelerometer(sensors_event_t *accelEvent) {
126+
WS_DEBUG_PRINTLN("[drvLsm9ds1] Getting accelerometer event...");
127+
sensors_event_t mag, gyro, temp;
128+
if (!readAllEvents(accelEvent, &mag, &gyro, &temp)) {
129+
return false;
130+
}
131+
return true;
132+
}
133+
134+
/******************************************************************************/
135+
/*!
136+
@brief Gets the LSM9DS1's gyroscope sensor event (x,y,z in rad/s).
137+
@param gyroEvent
138+
Pointer to the gyroscope sensor event.
139+
@returns True if the sensor event was obtained successfully, False
140+
otherwise.
141+
*/
142+
/******************************************************************************/
143+
bool drvLsm9ds1::getEventGyroscope(sensors_event_t *gyroEvent) {
144+
WS_DEBUG_PRINTLN("[drvLsm9ds1] Getting gyroscope event...");
145+
sensors_event_t accel, mag, temp;
146+
if (!readAllEvents(&accel, &mag, gyroEvent, &temp)) {
147+
return false;
148+
}
149+
return true;
150+
}
151+
152+
/******************************************************************************/
153+
/*!
154+
@brief Gets the LSM9DS1's magnetometer sensor event (x,y,z in uT).
155+
@param magEvent
156+
Pointer to the magnetometer sensor event.
157+
@returns True if the sensor event was obtained successfully, False
158+
otherwise.
159+
*/
160+
/******************************************************************************/
161+
bool drvLsm9ds1::getEventMagneticField(sensors_event_t *magEvent) {
162+
WS_DEBUG_PRINTLN("[drvLsm9ds1] Getting magnetometer event...");
163+
sensors_event_t accel, gyro, temp;
164+
if (!readAllEvents(&accel, magEvent, &gyro, &temp)) {
165+
return false;
166+
}
167+
return true;
168+
}
169+
170+
void drvLsm9ds1::ConfigureDefaultSensorTypes() {
171+
_default_sensor_types_count = 1;
172+
_default_sensor_types[0] =
173+
wippersnapper_sensor_SensorType_SENSOR_TYPE_ACCELEROMETER;
174+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*!
2+
* @file drvLsm9ds1.h
3+
*
4+
* Driver wrapper for the Adafruit LSM9DS1 9-DOF IMU.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef DRV_LSM9DS1_H
16+
#define DRV_LSM9DS1_H
17+
18+
#include "Wippersnapper_V2.h"
19+
#include "drvBase.h"
20+
#include <Adafruit_LSM9DS1.h>
21+
22+
class Adafruit_LSM9DS1; // forward
23+
24+
// Approximate acceleration magnitude (m/s^2) that triggers a tap event
25+
#define LSM9DS1_TAP_THRESHOLD_MSS 15.0f
26+
27+
/**************************************************************************/
28+
/*!
29+
@brief Class that provides a driver interface for a LSM9DS1 IMU.
30+
*/
31+
/**************************************************************************/
32+
class drvLsm9ds1 : public drvBase {
33+
public:
34+
/*******************************************************************************/
35+
/*!
36+
@brief Constructor for a LSM9DS1 sensor.
37+
@param i2c
38+
The I2C interface.
39+
@param sensorAddress
40+
7-bit device address.
41+
@param mux_channel
42+
The I2C multiplexer channel.
43+
@param driver_name
44+
The name of the driver.
45+
*/
46+
/*******************************************************************************/
47+
drvLsm9ds1(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
48+
const char *driver_name)
49+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {}
50+
51+
/*******************************************************************************/
52+
/*!
53+
@brief Destructor for a LSM9DS1 sensor.
54+
*/
55+
/*******************************************************************************/
56+
~drvLsm9ds1();
57+
58+
/*******************************************************************************/
59+
/*!
60+
@brief Initializes the LSM9DS1 sensor and begins I2C.
61+
@returns True if initialized successfully, False otherwise.
62+
*/
63+
/*******************************************************************************/
64+
bool begin() override;
65+
66+
/******************************************************************************/
67+
/*!
68+
@brief Gets the LSM9DS1's boolean sensor event.
69+
@param booleanEvent
70+
Pointer to the sensor event.
71+
@returns True if the sensor event was obtained successfully, False
72+
otherwise.
73+
*/
74+
/******************************************************************************/
75+
bool getEventBoolean(sensors_event_t *booleanEvent) override;
76+
77+
/*******************************************************************************/
78+
/*!
79+
@brief Gets the LSM9DS1's raw sensor event (magnitude stored in
80+
event->data[0]).
81+
@param rawEvent
82+
Pointer to the sensor event to fill.
83+
@returns True if the event was obtained successfully, False otherwise.
84+
*/
85+
/*******************************************************************************/
86+
bool getEventRaw(sensors_event_t *rawEvent) override;
87+
88+
/*******************************************************************************/
89+
/*!
90+
@brief Gets the LSM9DS1's accelerometer sensor event (x,y,z in m/s^2).
91+
@param accelEvent
92+
Pointer to the accelerometer sensor event.
93+
@returns True if the sensor event was obtained successfully, False
94+
otherwise.
95+
*/
96+
/*******************************************************************************/
97+
bool getEventAccelerometer(sensors_event_t *accelEvent) override;
98+
99+
/*******************************************************************************/
100+
/*!
101+
@brief Gets the LSM9DS1's gyroscope sensor event (x,y,z in rad/s).
102+
@param gyroEvent
103+
Pointer to the gyroscope sensor event.
104+
@returns True if the sensor event was obtained successfully, False
105+
otherwise.
106+
*/
107+
/*******************************************************************************/
108+
bool getEventGyroscope(sensors_event_t *gyroEvent) override;
109+
110+
/*******************************************************************************/
111+
/*!
112+
@brief Gets the LSM9DS1's magnetometer sensor event (x,y,z in uT).
113+
@param magEvent
114+
Pointer to the magnetometer sensor event.
115+
@returns True if the sensor event was obtained successfully, False
116+
otherwise.
117+
*/
118+
/*******************************************************************************/
119+
bool getEventMagneticField(sensors_event_t *magEvent) override;
120+
121+
void ConfigureDefaultSensorTypes() override;
122+
123+
protected:
124+
Adafruit_LSM9DS1 *_lsm = nullptr; ///< Pointer to LSM9DS1 sensor object
125+
126+
bool readAllEvents(sensors_event_t *accel, sensors_event_t *mag,
127+
sensors_event_t *gyro, sensors_event_t *temp);
128+
};
129+
130+
#endif // DRV_LSM9DS1_H

0 commit comments

Comments
 (0)