Skyward boardcore
Loading...
Searching...
No Matches
MPU9250.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Alberto Nidasio
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 "MPU9250Data.h"
30
31namespace Boardcore
32{
33
37class MPU9250 : public Sensor<MPU9250Data>
38{
39public:
40 enum GyroFSR : uint8_t
41 {
45 GYRO_FSR_2000DPS = 0x18
46 };
47
48 enum AccelFSR : uint8_t
49 {
53 ACCEL_FSR_16G = 0x18
54 };
55
75
77 {
78 struct __attribute__((packed)) MPU9250RawDataBits
79 {
80 int16_t accelX;
81 int16_t accelY;
82 int16_t accelZ;
83 int16_t temp;
84 int16_t gyroX;
85 int16_t gyroY;
86 int16_t gyroZ;
87 uint8_t magSt1;
88 int16_t magX;
89 int16_t magY;
90 int16_t magZ;
92 int8_t bytes[21];
93 };
94
147
165
166 // Registers bits
167 static constexpr uint8_t REG_CONFIG_DLPF_CFG_1 = 0x1;
168 static constexpr uint8_t REG_USER_CTRL_I2C_MST_EN = 0x20;
169 static constexpr uint8_t REG_I2C_SLV_CTRL_EN = 0x80;
170 static constexpr uint8_t REG_PWR_MGMT_1_BIT_H_RESET = 0x80;
171 static constexpr uint8_t REG_PWR_MGMT_1_CLKSEL_AUTO = 0x1;
172 static constexpr uint8_t REG_WHO_AM_I_VAL = 0x71;
173
174 // AK8963 address and bits
175 static constexpr uint8_t AK8963_ADDR = 0xC;
176 static constexpr uint8_t AK8963_REG_WHO_AM_I_VAL = 0x48;
177 static constexpr uint8_t AK8963_REG_CNTL1_POWER_DOWN_MODE = 0x0;
178 static constexpr uint8_t AK8963_REG_CNTL1_SINGLE_MES_MODE = 0x11; // 16 bit
179 static constexpr uint8_t AK8963_REG_CNTL1_CONT_MES_MODE_1 = 0x12; // 16 bit
180 static constexpr uint8_t AK8963_REG_CNTL1_CONT_MES_MODE_2 = 0x16; // 16 bit
181 static constexpr uint8_t AK8963_REG_CNTL1_SELF_TEST_MODE = 0x8;
182 static constexpr uint8_t AK8963_REG_CNTL1_FUSE_ROM_ACCESS_MODE = 0xF;
183 static constexpr uint8_t AK8963_REG_CNTL1_BIT_16_BIT_OUT = 0xF;
184 static constexpr uint8_t AK8963_REG_CNTL2_BIT_SRST = 0x1;
185
186 const float ACCELERATION_FS_MAP[4] = {2.0, 4.0, 8.0, 16.0};
187 const float GYROSCOPE_FS_MAP[4] = {250, 500, 1000, 2000};
188
195 explicit MPU9250(SPIBusInterface& bus, miosix::GpioPin cs,
197 unsigned short samplingRate = 100,
198 GyroFSR gyroFsr = GYRO_FSR_250DPS,
199 AccelFSR accelFsr = ACCEL_FSR_2G);
200
207
214 bool init() override;
215
221 bool selfTest() override { return true; };
222
223protected:
224 MPU9250Data sampleImpl() override;
225
226private:
227 void resetDevice();
228
229 void selectAutoClock();
230
236 void setGyroFsr(GyroFSR fs);
237
243 void setAccelFsr(AccelFSR fs);
244
252 void setSampleRate(unsigned short rate);
253
254 void enableMpuI2CMasterInterface();
255
256 void setMpuI2CMasterInterfaceClock(I2CMasterInterfaceClock clk);
257
261 void setI2CMasterSlaveRead(uint8_t addr, uint8_t reg, uint8_t nBytes = 1,
262 uint8_t slave = 0);
263
267 void setI2CMasterSlaveWrite(uint8_t addr, uint8_t reg, uint8_t data,
268 uint8_t slave = 0);
269
273 void disableI2CMasterSlave(uint8_t slave = 0);
274
281 uint8_t readFromAk(uint8_t reg);
282
289 void writeToAk(uint8_t reg, uint8_t data);
290
296 bool initAk();
297
303 bool checkWhoAmI();
304
310 bool checkAkWhoAmI();
311
312 inline void writeSPIWithDelay(SPITransaction& transaction, uint8_t reg,
313 uint8_t data);
314
315 inline float normalizeAcceleration(int16_t rawValue);
316
317 inline float normalizeTemperature(int16_t rawValue);
318
319 inline float normalizeGyroscope(int16_t rawValue);
320
321 inline float normalizeMagnetometer(int16_t rawValue, float adjustmentCoeff);
322
323 SPISlave spiSlave;
324
325 const unsigned short samplingRate;
326
327 const GyroFSR gyroFsr;
328 const AccelFSR accelFsr;
329 float magSensAdjCoeff[3]; // Page 32 on datasheet
330
331 bool initialized = false; // Whether the sensor has been initialized
332
333 PrintLogger logger = Logging::getLogger("mpu9250");
334};
335
336} // namespace Boardcore
static PrintLogger getLogger(const string &name)
Driver class for MPU9250.
Definition MPU9250.h:38
const float GYROSCOPE_FS_MAP[4]
Definition MPU9250.h:187
static constexpr uint8_t AK8963_REG_CNTL1_SELF_TEST_MODE
Definition MPU9250.h:181
static constexpr uint8_t REG_WHO_AM_I_VAL
Definition MPU9250.h:172
static constexpr uint8_t AK8963_REG_CNTL1_SINGLE_MES_MODE
Definition MPU9250.h:178
bool selfTest() override
Self test.
Definition MPU9250.h:221
static constexpr uint8_t REG_I2C_SLV_CTRL_EN
Definition MPU9250.h:169
static constexpr uint8_t AK8963_REG_CNTL1_FUSE_ROM_ACCESS_MODE
Definition MPU9250.h:182
static constexpr uint8_t AK8963_REG_WHO_AM_I_VAL
Definition MPU9250.h:176
MPU9250(SPIBusInterface &bus, miosix::GpioPin cs, SPIBusConfig config=getDefaultSPIConfig(), unsigned short samplingRate=100, GyroFSR gyroFsr=GYRO_FSR_250DPS, AccelFSR accelFsr=ACCEL_FSR_2G)
Instantiates the driver.
Definition MPU9250.cpp:32
static constexpr uint8_t REG_USER_CTRL_I2C_MST_EN
Definition MPU9250.h:168
static constexpr uint8_t AK8963_REG_CNTL1_BIT_16_BIT_OUT
Definition MPU9250.h:183
static constexpr uint8_t REG_PWR_MGMT_1_BIT_H_RESET
Definition MPU9250.h:170
static constexpr uint8_t REG_CONFIG_DLPF_CFG_1
Definition MPU9250.h:167
static constexpr uint8_t AK8963_REG_CNTL1_CONT_MES_MODE_1
Definition MPU9250.h:179
static constexpr uint8_t AK8963_REG_CNTL1_CONT_MES_MODE_2
Definition MPU9250.h:180
bool init() override
Initialize the device.
Definition MPU9250.cpp:48
static constexpr uint8_t AK8963_REG_CNTL1_POWER_DOWN_MODE
Definition MPU9250.h:177
static constexpr uint8_t REG_PWR_MGMT_1_CLKSEL_AUTO
Definition MPU9250.h:171
const float ACCELERATION_FS_MAP[4]
Definition MPU9250.h:186
static constexpr uint8_t AK8963_ADDR
Definition MPU9250.h:175
MPU9250Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition MPU9250.cpp:100
static SPIBusConfig getDefaultSPIConfig()
Constructs the default config for SPI Bus.
Definition MPU9250.cpp:40
static constexpr uint8_t AK8963_REG_CNTL2_BIT_SRST
Definition MPU9250.h:184
Interface for low level access of a SPI bus as a master.
Provides high-level access to the SPI Bus for a single transaction.
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.
SPI Bus configuration for a specific slave.
Contains information about a single SPI slave device.
struct __attribute__((packed)) MPU9250RawDataBits
Definition MPU9250.h:78