Skip to content

Commit 08d773e

Browse files
committed
Add LIS3MDL magnetometer driver
1 parent c3a0dcd commit 08d773e

File tree

6 files changed

+202
-1
lines changed

6 files changed

+202
-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 LSM6DS Library, 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
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit LIS3DH, Adafruit LIS3MDL, Adafruit LSM6DS Library, Adafruit LSM9DS1 Library, Adafruit LSM303 Accel, Adafruit LSM303DLH Mag, Adafruit LIS2MDL, 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ lib_deps =
4747
adafruit/Adafruit HTU21DF Library
4848
adafruit/Adafruit HTU31D Library
4949
adafruit/Adafruit LIS3DH
50+
adafruit/Adafruit LIS3MDL
51+
adafruit/Adafruit LIS2MDL
5052
adafruit/Adafruit LSM6DS
5153
adafruit/Adafruit LSM9DS1 Library
54+
adafruit/Adafruit LSM303 Accel
55+
adafruit/Adafruit LSM303DLH Mag
5256
adafruit/Adafruit LTR390 Library
5357
adafruit/Adafruit LTR329 and LTR303
5458
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+
{"lis3mdl",
177+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
178+
const char *driver_name) -> drvBase * {
179+
return new drvLis3mdl(i2c, addr, mux_channel, driver_name);
180+
}},
176181
{"ism330dlc",
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
@@ -42,6 +42,7 @@
4242
#include "drivers/drvLc709203f.h"
4343
#include "drivers/drvLis3dh.h"
4444
#include "drivers/drvIsm330dhcx.h"
45+
#include "drivers/drvLis3mdl.h"
4546
#include "drivers/drvIsm330dlc.h"
4647
#include "drivers/drvLsm9ds1.h"
4748
#include "drivers/drvLps22hb.h"
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*!
2+
* @file drvLis3mdl.cpp
3+
*
4+
* Driver wrapper for the Adafruit LIS3MDL 3-axis magnetometer.
5+
*/
6+
7+
#include "drvLis3mdl.h"
8+
9+
#include <math.h>
10+
11+
/******************************************************************************/
12+
/*!
13+
@brief Destructor for a LIS3MDL sensor.
14+
*/
15+
/******************************************************************************/
16+
drvLis3mdl::~drvLis3mdl() {
17+
if (_mag) {
18+
delete _mag;
19+
_mag = nullptr;
20+
}
21+
}
22+
23+
/******************************************************************************/
24+
/*!
25+
@brief Initializes the LIS3MDL sensor and begins I2C.
26+
@returns True if initialized successfully, False otherwise.
27+
*/
28+
/******************************************************************************/
29+
bool drvLis3mdl::begin() {
30+
WS_DEBUG_PRINTLN("[drvLis3mdl] Initialising sensor");
31+
_mag = new Adafruit_LIS3MDL();
32+
if (!_mag)
33+
return false;
34+
35+
if (!_mag->begin_I2C(_address, _i2c)) {
36+
WS_DEBUG_PRINTLN("[drvLis3mdl] Failed to initialise sensor");
37+
delete _mag;
38+
_mag = nullptr;
39+
return false;
40+
}
41+
42+
_mag->setPerformanceMode(LIS3MDL_ULTRAHIGHMODE);
43+
_mag->setOperationMode(LIS3MDL_CONTINUOUSMODE);
44+
_mag->setDataRate(LIS3MDL_DATARATE_80_HZ);
45+
_mag->setRange(LIS3MDL_RANGE_4_GAUSS);
46+
_mag->setIntThreshold(500);
47+
_mag->configInterrupt(true, true, true, true, false, true);
48+
49+
WS_DEBUG_PRINTLN("[drvLis3mdl] Sensor initialised successfully");
50+
return true;
51+
}
52+
53+
/******************************************************************************/
54+
/*!
55+
@brief Reads the LIS3MDL's magnetometer event.
56+
@param event
57+
Pointer to the magnetometer event to populate.
58+
@returns True if the event was obtained successfully, False otherwise.
59+
*/
60+
/******************************************************************************/
61+
bool drvLis3mdl::readMagEvent(sensors_event_t *event) {
62+
if (!_mag)
63+
return false;
64+
return _mag->getEvent(event);
65+
}
66+
67+
/******************************************************************************/
68+
/*!
69+
@brief Computes the vector magnitude of a magnetometer reading.
70+
@param event
71+
Magnetometer event to evaluate.
72+
@param magnitude
73+
Reference to store the computed magnitude (micro Tesla).
74+
@returns True if the magnitude was computed successfully.
75+
*/
76+
/******************************************************************************/
77+
bool drvLis3mdl::computeMagnitude(const sensors_event_t &event,
78+
float &magnitude) {
79+
magnitude =
80+
sqrtf(event.magnetic.x * event.magnetic.x +
81+
event.magnetic.y * event.magnetic.y +
82+
event.magnetic.z * event.magnetic.z);
83+
return true;
84+
}
85+
86+
/******************************************************************************/
87+
/*!
88+
@brief Gets the LIS3MDL's raw sensor event (magnitude stored in data[0]).
89+
@param rawEvent
90+
Pointer to the sensor event.
91+
@returns True if the sensor event was obtained successfully, False
92+
otherwise.
93+
*/
94+
/******************************************************************************/
95+
bool drvLis3mdl::getEventRaw(sensors_event_t *rawEvent) {
96+
sensors_event_t magEvent;
97+
if (!readMagEvent(&magEvent)) {
98+
return false;
99+
}
100+
101+
float magnitude = 0;
102+
computeMagnitude(magEvent, magnitude);
103+
rawEvent->data[0] = magnitude;
104+
return true;
105+
}
106+
107+
/******************************************************************************/
108+
/*!
109+
@brief Gets the LIS3MDL's boolean sensor event.
110+
@param booleanEvent
111+
Pointer to the sensor event.
112+
@returns True if the sensor event was obtained successfully, False
113+
otherwise.
114+
*/
115+
/******************************************************************************/
116+
bool drvLis3mdl::getEventBoolean(sensors_event_t *booleanEvent) {
117+
// TODO(#offline-imu-compass): Reuse the raw magnitude once compass headings
118+
// are supported. For now, magnetometers do not emit boolean events.
119+
booleanEvent->data[0] = 0.0f;
120+
return true;
121+
}
122+
123+
/******************************************************************************/
124+
/*!
125+
@brief Gets the LIS3MDL's magnetometer sensor event (x,y,z in uTesla).
126+
@param magEvent
127+
Pointer to the magnetometer sensor event.
128+
@returns True if the sensor event was obtained successfully, False
129+
otherwise.
130+
*/
131+
/******************************************************************************/
132+
bool drvLis3mdl::getEventMagneticField(sensors_event_t *magEvent) {
133+
return readMagEvent(magEvent);
134+
}
135+
136+
void drvLis3mdl::ConfigureDefaultSensorTypes() {
137+
_default_sensor_types_count = 1;
138+
_default_sensor_types[0] =
139+
wippersnapper_sensor_SensorType_SENSOR_TYPE_MAGNETIC_FIELD;
140+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*!
2+
* @file drvLis3mdl.h
3+
*
4+
* Driver wrapper for the Adafruit LIS3MDL 3-axis magnetometer.
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+
* MIT license, all text here must be included in any redistribution.
11+
*/
12+
#ifndef DRV_LIS3MDL_H
13+
#define DRV_LIS3MDL_H
14+
15+
#include "Wippersnapper_V2.h"
16+
#include "drvBase.h"
17+
#include <Adafruit_LIS3MDL.h>
18+
19+
20+
/**************************************************************************/
21+
/*!
22+
@brief Class that provides a driver interface for a LIS3MDL magnetometer.
23+
*/
24+
/**************************************************************************/
25+
class drvLis3mdl : public drvBase {
26+
public:
27+
drvLis3mdl(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
28+
const char *driver_name)
29+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {}
30+
31+
~drvLis3mdl();
32+
33+
bool begin() override;
34+
35+
bool getEventRaw(sensors_event_t *rawEvent) override;
36+
37+
bool getEventBoolean(sensors_event_t *booleanEvent) override;
38+
39+
bool getEventMagneticField(sensors_event_t *magEvent) override;
40+
41+
protected:
42+
void ConfigureDefaultSensorTypes() override;
43+
44+
private:
45+
bool readMagEvent(sensors_event_t *event);
46+
bool computeMagnitude(const sensors_event_t &event, float &magnitude);
47+
48+
Adafruit_LIS3MDL *_mag = nullptr;
49+
};
50+
51+
#endif // DRV_LIS3MDL_H

0 commit comments

Comments
 (0)