Hummingbird Flight Software
Flight software for the Hummingbird FCU quadcopter flight controller. Designed to run on the Teensy 4.1. Developed with VSCode+PlatformIO.
lis3mdl_magnetometer.h
Go to the documentation of this file.
1 // LIS3MDL 3-AXIS MAGNETOMETER SENSOR LIBRARY
2 //
3 // Used to communicate with the STMicroelectronic's LIS3MDL magnetometer
4 // sensor over I2C.
5 //
6 // Code By: Michael Wrona
7 // Created: 17 Jan 2021
8 //
9 // LIS3MDL Datasheet: https://www.st.com/resource/en/datasheet/lis3mdl.pdf
10 
11 
12 
13 #pragma once
14 
15 #include <Arduino.h>
16 #include <Wire.h>
17 #include "debugging.h"
18 #include "hummingbird_config.h"
19 
20 
21 #ifdef DEBUG
22 #define LIS3MDL_DEBUG // For printing magnetometer debug messages
23 #endif
24 
25 
26 // ------------------------------------
27 // Control Registers
28 // ------------------------------------
29 
30 // Default I2C default slave address (0x1C). SDO/SA1 pin connected to GND.
31 // If you have an issue connecting to the sensor, try changing
32 // LIS3MDL_ADDR to 0x1C
33 #define LIS3MDL_ADDR 0x1E
34 #define LIS3MDL_WHOAMI 0x0F // ID byte
35 #define LIS3MDL_CTRL_REG1 0x20 // Control register 1
36 #define LIS3MDL_CTRL_REG2 0x21 // Control register 2
37 #define LIS3MDL_CTRL_REG3 0x22 // Control register 3
38 #define LIS3MDL_CTRL_REG4 0x23 // Control register 4
39 #define LIS3MDL_CTRL_REG5 0x24 // Control register 5
40 
41 
45 typedef enum {
52  LIS3MDL_OUT_TEMP_L = 0x2E, // Low-byte for temperature sensor. Stores as twos-compliment.
53  LIS3MDL_OUT_TEMP_H = 0x2F // High-byte for temperature sensor. Stores as twos-compliment.
55 
56 
60 typedef enum {
61  LIS3MDL_RANGE_4G = 4, // +/- 4 G range
62  LIS3MDL_RANGE_8G = 8, // +/- 8 G range
63  LIS3MDL_RANGE_12G = 12, // +/- 12G range
64  LIS3MDL_RANGE_16G = 16 // +/-16G range
66 
67 
72 {
73 public:
74  LIS3MDL_Mag(TwoWire *userWire = &SENSOR_I2C);
77  bool ReadSensor();
78  float GetMx();
79  float GetMy();
80  float GetMz();
81  float GetTemperature();
82  uint32_t prevMeasMicros;
83 protected:
84 private:
85  float _mx;
86  float _my;
87  float _mz;
88  TwoWire *_SensorWire;
90  void I2Cwrite8(uint8_t regOfInterest, uint8_t valToWrite);
91  uint8_t I2Cread8(uint8_t regOfInterest);
92 };
STMicroelectronics LIS3MDL magnetometer sensor class.
Definition: lis3mdl_magnetometer.h:72
uint8_t I2Cread8(uint8_t regOfInterest)
Read register value from I2C device.
Definition: lis3mdl_magnetometer.cpp:265
float GetMx()
Return X-magnetometer reading in [uT].
Definition: lis3mdl_magnetometer.cpp:189
LIS3MDL_Mag(TwoWire *userWire=&SENSOR_I2C)
I2C Sensor class for the LIS3MDL magnetometer.
Definition: lis3mdl_magnetometer.cpp:20
bool ReadSensor()
Read magnetometer registers and extract measurements.
Definition: lis3mdl_magnetometer.cpp:125
float _mx
x-magnetometer reading [uT]
Definition: lis3mdl_magnetometer.h:85
float GetMy()
Return Y-magnetometer reading in [uT].
Definition: lis3mdl_magnetometer.cpp:197
uint32_t prevMeasMicros
Previous measurement micros()
Definition: lis3mdl_magnetometer.h:82
float GetMz()
Return Z-magnetometer reading in [uT].
Definition: lis3mdl_magnetometer.cpp:205
float GetTemperature()
Read temperature from the magnetometer sensor.
Definition: lis3mdl_magnetometer.cpp:217
LIS3MDL_MeasRange_t _range
Sensor measurement range.
Definition: lis3mdl_magnetometer.h:89
TwoWire * _SensorWire
I2C/wire interface the sensor is on.
Definition: lis3mdl_magnetometer.h:88
~LIS3MDL_Mag()
Definition: lis3mdl_magnetometer.h:75
float _mz
z-magnetometer reading [uT]
Definition: lis3mdl_magnetometer.h:87
float _my
y-magnetometer reading [uT]
Definition: lis3mdl_magnetometer.h:86
void I2Cwrite8(uint8_t regOfInterest, uint8_t valToWrite)
Write to device register over I2C.
Definition: lis3mdl_magnetometer.cpp:248
bool Initialize(LIS3MDL_MeasRange_t measRange=LIS3MDL_RANGE_4G)
Initialize the LIS3MDL magnetometer and specify the measurement range.
Definition: lis3mdl_magnetometer.cpp:36
#define SENSOR_I2C
UBER-EXTREME CAUTION SHOULD BE USED CHANGING PARAMETERS IN THIS FILE.
Definition: hummingbird_config.h:26
LIS3MDL_DataReg_t
LIS3MDL data registers.
Definition: lis3mdl_magnetometer.h:45
@ LIS3MDL_OUT_TEMP_H
Definition: lis3mdl_magnetometer.h:53
@ LIS3MDL_OUT_X_H
Definition: lis3mdl_magnetometer.h:47
@ LIS3MDL_OUT_Y_L
Definition: lis3mdl_magnetometer.h:48
@ LIS3MDL_OUT_X_L
Definition: lis3mdl_magnetometer.h:46
@ LIS3MDL_OUT_Y_H
Definition: lis3mdl_magnetometer.h:49
@ LIS3MDL_OUT_Z_H
Definition: lis3mdl_magnetometer.h:51
@ LIS3MDL_OUT_TEMP_L
Definition: lis3mdl_magnetometer.h:52
@ LIS3MDL_OUT_Z_L
Definition: lis3mdl_magnetometer.h:50
LIS3MDL_MeasRange_t
LIS3MDL measurement ranges (gauss)
Definition: lis3mdl_magnetometer.h:60
@ LIS3MDL_RANGE_16G
Definition: lis3mdl_magnetometer.h:64
@ LIS3MDL_RANGE_4G
Definition: lis3mdl_magnetometer.h:61
@ LIS3MDL_RANGE_8G
Definition: lis3mdl_magnetometer.h:62
@ LIS3MDL_RANGE_12G
Definition: lis3mdl_magnetometer.h:63