39 : slave(bus, pin, spiConfig), configuration(config)
56 LOG_ERR(logger,
"Attempted to initialized sensor twice but failed");
70 if (res != WHO_AM_I_VALUE)
73 "WHO_AM_I value differs from expectation: read 0x{:x} "
74 "but expected 0x{:x}",
89 LOG_ERR(logger,
"Invoked selfTest() but sensor was uninitialized");
99 static constexpr int NUM_SAMPLES = 50;
100 static constexpr int SLEEP_TIME = 10;
101 Vector3f avgPreTest = Vector3f::Zero();
102 Vector3f avgPostTest = Vector3f::Zero();
116 miosix::Thread::sleep(20);
120 for (
int i = 0; i < NUM_SAMPLES - 1; i++)
122 tmp << static_cast<MagnetometerData>(
sampleImpl());
125 miosix::Thread::sleep(SLEEP_TIME);
127 tmp << static_cast<MagnetometerData>(
sampleImpl());
131 avgPreTest /= NUM_SAMPLES;
142 miosix::Thread::sleep(60);
146 for (
int i = 0; i < NUM_SAMPLES - 1; i++)
148 tmp << static_cast<MagnetometerData>(
sampleImpl());
151 miosix::Thread::sleep(SLEEP_TIME);
153 tmp << static_cast<MagnetometerData>(
sampleImpl());
157 avgPostTest /= NUM_SAMPLES;
164 Vector3f deltas = (avgPostTest - avgPreTest).cwiseAbs();
168 static constexpr float ST_MIN = 0.015;
169 static constexpr float ST_MAX = 0.500;
172 (ST_MIN < deltas.array()).all() && (deltas.array() < ST_MAX).all();
179 LOG_ERR(logger,
"selfTest() failed");
209 LOG_ERR(logger,
"Invoked sampleImpl() but sensor was uninitialized");
224 newData.temperature = DEG_PER_LSB * outTemp;
225 newData.temperature += REFERENCE_TEMPERATURE;
235 int16_t outX = values[1] << 8 | values[0];
236 int16_t outY = values[3] << 8 | values[2];
237 int16_t outZ = values[5] << 8 | values[4];
240 newData.magneticFieldX = GAUSS_PER_LSB * outX;
241 newData.magneticFieldY = GAUSS_PER_LSB * outY;
242 newData.magneticFieldZ = GAUSS_PER_LSB * outZ;
#define LOG_ERR(logger,...)
LIS2MDL(SPIBusInterface &bus, miosix::GpioPin pin, SPIBusConfig spiConfig, Config config)
bool init() override
Initialize the sensor.
bool applyConfig(Config config)
Overwrites the sensor settings.
LIS2MDLData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
bool selfTest() override
Check if the sensor is working.
static SPIBusConfig getDefaultSPIConfig()
Interface for low level access of a SPI bus as a master.
Provides high-level access to the SPI Bus for a single transaction.
uint8_t readRegister(uint8_t reg)
Reads an 8 bit register.
void readRegisters(uint8_t reg, uint8_t *data, size_t size)
Reads multiple bytes starting from the specified register.
void writeRegister(uint8_t reg, uint8_t data)
Writes an 8 bit register.
uint16_t readRegister16(uint8_t reg)
Reads a 16 bit register.
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
unsigned temperatureDivider
Divide the temperature sampling rate.
SPI Bus configuration for a specific slave.
SPI::ClockDivider clockDivider
< Peripheral clock division