Skyward boardcore
Loading...
Searching...
No Matches
LIS3MDL.h
Go to the documentation of this file.
1/* Copyright (c) 2020 Skyward Experimental Rocketry
2 * Author: Riccardo Musso
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
27#include <sensors/Sensor.h>
28
29#include "LIS3MDLData.h"
30
31namespace Boardcore
32{
33
37class LIS3MDL : public Sensor<LIS3MDLData>
38{
39public:
47 enum ODR : uint8_t
48 {
49 ODR_0_625_HZ = 0x00,
50 ODR_1_25_HZ = 0x04,
51 ODR_2_5_HZ = 0x08,
52 ODR_5_HZ = 0x0c,
53 ODR_10_HZ = 0x10,
54 ODR_20_HZ = 0x14,
55 ODR_40_HZ = 0x18,
56 ODR_80_HZ = 0x1c,
57
58 ODR_155_HZ = 0x62,
59 ODR_300_HZ = 0x42,
60 ODR_560_HZ = 0x22,
61 ODR_1000_HZ = 0x02,
62
68 };
69
70 enum FullScale : uint8_t
71 {
72 FS_4_GAUSS = 0x00,
73 FS_8_GAUSS = 0x20,
74 FS_12_GAUSS = 0x40,
75 FS_16_GAUSS = 0x60,
76 };
77
84 enum OperativeMode : uint8_t
85 {
90 };
91
124
125 LIS3MDL(SPIBusInterface& bus, miosix::GpioPin pin,
126 SPIBusConfig spiConfig = {}, Config config = {});
127
128 bool init() override;
129
130 bool selfTest() override;
131
146 bool applyConfig(Config config);
147
148protected:
149 LIS3MDLData sampleImpl() override;
150
151private:
152 void updateUnit(FullScale fs);
153
154 SPISlave slave;
155 Config configuration;
156
157 unsigned tempCounter = 0;
158 bool isInitialized = false;
159 float currentUnit = 0;
160
161 enum Registers : uint8_t
162 {
163 WHO_AM_I = 0x0f,
164
165 CTRL_REG1 = 0x20,
166 CTRL_REG2 = 0x21,
167 CTRL_REG3 = 0x22,
168 CTRL_REG4 = 0x23,
169 CTRL_REG5 = 0x24,
170
171 STATUS_REG = 0x27,
172 OUT_X_L = 0x28,
173 OUT_X_H = 0x29,
174 OUT_Y_L = 0x2a,
175 OUT_Y_H = 0x2b,
176 OUT_Z_L = 0x2c,
177 OUT_Z_H = 0x2d,
178
179 TEMP_OUT_L = 0x2e,
180 TEMP_OUT_H = 0x2f,
181
182 INT_CFG = 0x30,
183 INT_THS_L = 0x32,
184 INT_THS_H = 0x33,
185 };
186
187 static constexpr uint32_t WHO_AM_I_VALUE = 0x3d;
188 static constexpr uint32_t CONTINUOS_CONVERSION = 0x0;
189
190 static constexpr uint32_t REFERENCE_TEMPERATURE = 25;
191 static constexpr float DEG_PER_LSB = 0.125;
192
193 static constexpr float GAUSS_PER_LSB_FS_4 = 0.000146156;
194 static constexpr float GAUSS_PER_LSB_FS_8 = 0.000292312;
195 static constexpr float GAUSS_PER_LSB_FS_12 = 0.000438404;
196 static constexpr float GAUSS_PER_LSB_FS_16 = 0.000584454;
197
198 static constexpr uint32_t ENABLE_TEMPERATURE = 1 << 7;
199 static constexpr uint32_t ENABLE_SELF_TEST = 1 << 0;
200 static constexpr uint32_t ENABLE_BDU = 1 << 6;
201
202 static constexpr uint32_t ENABLE_INT_PIN = 1 << 0;
203 static constexpr uint32_t ENABLE_INT_X = 1 << 7;
204 static constexpr uint32_t ENABLE_INT_Y = 1 << 6;
205 static constexpr uint32_t ENABLE_INT_Z = 1 << 5;
206
212 static constexpr uint8_t INCREMENT_REG_FLAG = 0x40;
213
214 PrintLogger logger = Logging::getLogger("lis3mdl");
215};
216
217} // namespace Boardcore
Driver for LIS3MDL, a three-axis magnetic sensor.
Definition LIS3MDL.h:38
bool selfTest() override
Check if the sensor is working.
Definition LIS3MDL.cpp:66
LIS3MDL(SPIBusInterface &bus, miosix::GpioPin pin, SPIBusConfig spiConfig={}, Config config={})
Definition LIS3MDL.cpp:30
LIS3MDLData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition LIS3MDL.cpp:204
bool init() override
Initialize the sensor.
Definition LIS3MDL.cpp:38
@ FS_16_GAUSS
+/- 16 gauss
Definition LIS3MDL.h:75
@ FS_12_GAUSS
+/- 12 gauss
Definition LIS3MDL.h:74
@ FS_4_GAUSS
+/- 4 gauss
Definition LIS3MDL.h:72
@ FS_8_GAUSS
+/- 8 gauss
Definition LIS3MDL.h:73
OperativeMode
Operative mode constants.
Definition LIS3MDL.h:85
ODR
Constants for Output Data Rate configuration.
Definition LIS3MDL.h:48
@ ODR_300_HZ
300 Hz
Definition LIS3MDL.h:59
@ ODR_155_HZ
155 Hz
Definition LIS3MDL.h:58
@ ODR_0_625_HZ
0.625 Hz
Definition LIS3MDL.h:49
@ ODR_560_HZ
560 Hz
Definition LIS3MDL.h:60
@ ODR_1000_HZ
1000 Hz
Definition LIS3MDL.h:61
@ ODR_1_25_HZ
1.25 Hz
Definition LIS3MDL.h:50
@ ODR_2_5_HZ
2.5 Hz
Definition LIS3MDL.h:51
bool applyConfig(Config config)
Overrides the sensor settings.
Definition LIS3MDL.cpp:149
static PrintLogger getLogger(const string &name)
Interface for low level access of a SPI bus as a master.
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
This file includes all the types the logdecoder script will decode.
Sensor configuration.
Definition LIS3MDL.h:100
bool doBlockDataUpdate
BDU setting.
Definition LIS3MDL.h:122
unsigned temperatureDivider
Divide the temperature sampling rate.
Definition LIS3MDL.h:114
SPI Bus configuration for a specific slave.