handle master side communication of openIMU300ZI module

Dependencies:   mbed

Dependents:   VDU_2021

Committer:
Arithemetica
Date:
Wed Dec 18 06:53:12 2019 +0000
Revision:
19:3ee54f9a56f3
Parent:
17:629b2f317d0a
Child:
20:19f5c94f8660
add operator for ImuExtiConfig enum

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Arithemetica 7:224ffdacb240 1 /**
Arithemetica 7:224ffdacb240 2 * @file imu_driver.hpp
Arithemetica 10:d8223969c541 3 * @brief ImuDriver class and other data storage struct definition. The C language version
Arithemetica 10:d8223969c541 4 * can be found at <A HREF="https://github.com/osjacky430/imu_driver">this github website</A>
Arithemetica 7:224ffdacb240 5 */
Arithemetica 7:224ffdacb240 6
Arithemetica 1:8e31413068af 7 #ifndef IMU_DRIVER_HPP_
Arithemetica 1:8e31413068af 8 #define IMU_DRIVER_HPP_
Arithemetica 1:8e31413068af 9
Arithemetica 0:8c01a98a2812 10 #include <mbed.h>
Arithemetica 0:8c01a98a2812 11 #include <cstdint>
Arithemetica 0:8c01a98a2812 12
Arithemetica 14:6c31a7ab45f0 13 typedef enum ImuExtiConfig {
Arithemetica 14:6c31a7ab45f0 14 ImuExtiRcvNotUsed = 0b00,
Arithemetica 14:6c31a7ab45f0 15 ImuExtiRcvNormalMsg = 0b01,
Arithemetica 14:6c31a7ab45f0 16 ImuExtiRcvAhrsMsg = 0b10
Arithemetica 14:6c31a7ab45f0 17 } ImuExtiConfig;
Arithemetica 14:6c31a7ab45f0 18
Arithemetica 19:3ee54f9a56f3 19 inline ImuExtiConfig operator |(ImuExtiConfig const& left, ImuExtiConfig const& right)
Arithemetica 19:3ee54f9a56f3 20 {
Arithemetica 19:3ee54f9a56f3 21 return ImuExtiConfig(left | right);
Arithemetica 19:3ee54f9a56f3 22 }
Arithemetica 19:3ee54f9a56f3 23
Arithemetica 17:629b2f317d0a 24 /**
Arithemetica 17:629b2f317d0a 25 * @brief ImuDriverStatus
Arithemetica 17:629b2f317d0a 26 */
Arithemetica 12:bb490978e153 27 typedef enum ImuDriverStatus {
Arithemetica 14:6c31a7ab45f0 28 ImuDriverStatusOK,
Arithemetica 14:6c31a7ab45f0 29 ImuDriverStatusDataNotReady,
Arithemetica 14:6c31a7ab45f0 30 ImuDriverStatusInvalidCall
Arithemetica 14:6c31a7ab45f0 31 } ImuDriverStatus;
Arithemetica 12:bb490978e153 32
Arithemetica 0:8c01a98a2812 33 typedef enum ImuDriverRegister {
Arithemetica 0:8c01a98a2812 34 ReadAhrsBurstDataRegister = 0x3D,
Arithemetica 0:8c01a98a2812 35 ReadBurstDataRegister = 0x3E,
Arithemetica 0:8c01a98a2812 36 ReadExtBurstDataRegister = 0x3F
Arithemetica 0:8c01a98a2812 37 } ImuDriverRegister;
Arithemetica 0:8c01a98a2812 38
Arithemetica 0:8c01a98a2812 39 typedef struct AhrsRawData {
Arithemetica 0:8c01a98a2812 40 std::uint16_t status;
Arithemetica 0:8c01a98a2812 41 std::int16_t attitude[3];
Arithemetica 0:8c01a98a2812 42 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 43 } AhrsRawData;
Arithemetica 0:8c01a98a2812 44
Arithemetica 10:d8223969c541 45 /**
Arithemetica 12:bb490978e153 46 * @brief AhrsProcessedData is data storage class fpr processed AHRS data
Arithemetica 14:6c31a7ab45f0 47 *
Arithemetica 11:866dd73e4ab3 48 * The data storage struct consists of:
Arithemetica 11:866dd73e4ab3 49 *
Arithemetica 10:d8223969c541 50 * - attitude: pitch, roll, yaw in deg, range from -180 ~ +180 deg
Arithemetica 10:d8223969c541 51 */
Arithemetica 0:8c01a98a2812 52 typedef struct AhrsProcessedData {
Arithemetica 0:8c01a98a2812 53 float attitude[3];
Arithemetica 0:8c01a98a2812 54 } AhrsProcessedData;
Arithemetica 0:8c01a98a2812 55
Arithemetica 0:8c01a98a2812 56 typedef struct ImuRawData {
Arithemetica 0:8c01a98a2812 57 std::uint16_t status;
Arithemetica 0:8c01a98a2812 58 std::int16_t rate[3];
Arithemetica 0:8c01a98a2812 59 std::int16_t accel[3];
Arithemetica 0:8c01a98a2812 60 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 61 } ImuRawData;
Arithemetica 0:8c01a98a2812 62
Arithemetica 10:d8223969c541 63 /**
Arithemetica 11:866dd73e4ab3 64 * @brief ImuProcessedData is data storage class for processed imu data
Arithemetica 10:d8223969c541 65 *
Arithemetica 11:866dd73e4ab3 66 * This data storage class consists of:
Arithemetica 14:6c31a7ab45f0 67 *
Arithemetica 10:d8223969c541 68 * - status: if status = 0x0010 (over range error), status = 0 (no error)
Arithemetica 10:d8223969c541 69 * - gyroscope: deg/s, range from -300 deg/s ~ 300 deg/s
Arithemetica 10:d8223969c541 70 * - accelerometer: g, range from -4.5g ~ 4.5g
Arithemetica 10:d8223969c541 71 * - temperature: Celcius
Arithemetica 10:d8223969c541 72 */
Arithemetica 0:8c01a98a2812 73 typedef struct ImuProcessedData {
Arithemetica 0:8c01a98a2812 74 std::uint16_t status;
Arithemetica 0:8c01a98a2812 75
Arithemetica 0:8c01a98a2812 76 float rate[3];
Arithemetica 0:8c01a98a2812 77 float accel[3];
Arithemetica 0:8c01a98a2812 78
Arithemetica 0:8c01a98a2812 79 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 80 } ImuProcessedData;
Arithemetica 0:8c01a98a2812 81
Arithemetica 0:8c01a98a2812 82 /**
Arithemetica 12:bb490978e153 83 * @brief ImuDriver is used to handle the master side of SPI communication of openIMU300ZI module, the
Arithemetica 2:f3a7def7a7e1 84 * default setups are:
Arithemetica 0:8c01a98a2812 85 *
Arithemetica 2:f3a7def7a7e1 86 * - Data frame: 16 bits
Arithemetica 2:f3a7def7a7e1 87 * - CPOL: High (1)
Arithemetica 2:f3a7def7a7e1 88 * - CPHA: 2 Edge (1)
Arithemetica 17:629b2f317d0a 89 * - Frequency: 1 MHz (Default frequency of spi)
Arithemetica 2:f3a7def7a7e1 90 * - Endian: MSB first (Mbed only support MSB AFAIK)
Arithemetica 5:e71931fcae33 91 *
Arithemetica 10:d8223969c541 92 * According to <A HREF="https://openimu.readthedocs.io/en/latest/software/SPImessaging.html">openIMU300ZI SPI framework</A>:
Arithemetica 5:e71931fcae33 93 *
Arithemetica 3:8552e26cd162 94 * - Data transferred in 16-bit word-length and MSB-first
Arithemetica 5:e71931fcae33 95 * - fCLK ≤ 2.0 MHz
Arithemetica 3:8552e26cd162 96 * - CPOL = 1 (clock polarity) and CPHA = 1 (clock phase)
Arithemetica 0:8c01a98a2812 97 *
Arithemetica 2:f3a7def7a7e1 98 * @tparam spi SPI comm instance, ImuDriver currently support only SPI framework
Arithemetica 2:f3a7def7a7e1 99 * @tparam rst Reset pin name, this is used to reset the openIMU300ZI module
Arithemetica 2:f3a7def7a7e1 100 * @tparam drdy Data ready pin name, this is used as indication of data readiness
Arithemetica 2:f3a7def7a7e1 101 * @tparam ss Slave select pin name, this is used as slave select, pull low to initiate
Arithemetica 2:f3a7def7a7e1 102 * communication process.
Arithemetica 5:e71931fcae33 103 *
Arithemetica 7:224ffdacb240 104 * @todo Attach to exti in the future, and make user able to choose
Arithemetica 7:224ffdacb240 105 *
Arithemetica 0:8c01a98a2812 106 * Example of using ImuDriver:
Arithemetica 0:8c01a98a2812 107 *
Arithemetica 0:8c01a98a2812 108 * @code
Arithemetica 0:8c01a98a2812 109 * #include "imu_driver.hpp"
Arithemetica 0:8c01a98a2812 110 *
Arithemetica 0:8c01a98a2812 111 * SPI spi3(PB_5, PB_6, PB_3); // declare SPI instance globally
Arithemetica 0:8c01a98a2812 112 * Serial pc(USBTX, USBRX, 115200); // print debug message
Arithemetica 0:8c01a98a2812 113 *
Arithemetica 0:8c01a98a2812 114 * int main()
Arithemetica 0:8c01a98a2812 115 * {
Arithemetica 5:e71931fcae33 116 * // SPI instance, reset, data ready, slave select
Arithemetica 17:629b2f317d0a 117 * ImuDriver<spi3, PA_10, PA_8, PA_9> imu(ImuExtiRcvNormalMsg);
Arithemetica 0:8c01a98a2812 118 *
Arithemetica 0:8c01a98a2812 119 * while(true)
Arithemetica 0:8c01a98a2812 120 * {
Arithemetica 17:629b2f317d0a 121 * pc.printf("%.3f, %.3f, %.3f\n\r", imu.imuProcessedData.rate[0], imu.imuProcessedData.rate[1], imu.imuProcessedData.rate[2]);
Arithemetica 0:8c01a98a2812 122 * }
Arithemetica 0:8c01a98a2812 123 * }
Arithemetica 5:e71931fcae33 124 *
Arithemetica 0:8c01a98a2812 125 * @endcode
Arithemetica 0:8c01a98a2812 126 */
Arithemetica 2:f3a7def7a7e1 127 template <SPI& Spi, PinName rst, PinName drdy, PinName ss>
Arithemetica 0:8c01a98a2812 128 class ImuDriver
Arithemetica 0:8c01a98a2812 129 {
Arithemetica 0:8c01a98a2812 130 private:
Arithemetica 14:6c31a7ab45f0 131 ImuExtiConfig m_extiConfig;
Arithemetica 14:6c31a7ab45f0 132
Arithemetica 0:8c01a98a2812 133 SPI& m_spi;
Arithemetica 0:8c01a98a2812 134 DigitalOut m_rst, m_ss;
Arithemetica 0:8c01a98a2812 135 DigitalIn m_drdy;
Arithemetica 2:f3a7def7a7e1 136 InterruptIn m_drdyExti;
Arithemetica 5:e71931fcae33 137
Arithemetica 0:8c01a98a2812 138 float m_gyroScaler[3];
Arithemetica 0:8c01a98a2812 139 float m_accelScaler[3];
Arithemetica 0:8c01a98a2812 140 float m_ahrsScaler[3];
Arithemetica 0:8c01a98a2812 141 private:
Arithemetica 0:8c01a98a2812 142 std::uint16_t m_imuSpiWrite(const std::uint16_t val) const
Arithemetica 0:8c01a98a2812 143 {
Arithemetica 0:8c01a98a2812 144 // RAII
Arithemetica 0:8c01a98a2812 145 class SpiLock
Arithemetica 0:8c01a98a2812 146 {
Arithemetica 0:8c01a98a2812 147 private:
Arithemetica 0:8c01a98a2812 148 SPI& m_spi;
Arithemetica 0:8c01a98a2812 149 public:
Arithemetica 0:8c01a98a2812 150 SpiLock(SPI& t_spi) : m_spi(t_spi)
Arithemetica 0:8c01a98a2812 151 {
Arithemetica 0:8c01a98a2812 152 m_spi.lock();
Arithemetica 0:8c01a98a2812 153 }
Arithemetica 0:8c01a98a2812 154
Arithemetica 0:8c01a98a2812 155 ~SpiLock()
Arithemetica 0:8c01a98a2812 156 {
Arithemetica 0:8c01a98a2812 157 m_spi.unlock();
Arithemetica 0:8c01a98a2812 158 }
Arithemetica 0:8c01a98a2812 159 };
Arithemetica 0:8c01a98a2812 160
Arithemetica 0:8c01a98a2812 161 SpiLock lock(m_spi);
Arithemetica 0:8c01a98a2812 162 return m_spi.write(val);
Arithemetica 0:8c01a98a2812 163 }
Arithemetica 0:8c01a98a2812 164
Arithemetica 0:8c01a98a2812 165 inline std::uint16_t m_spiGenerateReadCmd(const std::uint8_t t_val) const
Arithemetica 0:8c01a98a2812 166 {
Arithemetica 0:8c01a98a2812 167 return t_val << 8U;
Arithemetica 0:8c01a98a2812 168 }
Arithemetica 0:8c01a98a2812 169
Arithemetica 0:8c01a98a2812 170 inline bool m_spiIsDataReady()
Arithemetica 0:8c01a98a2812 171 {
Arithemetica 0:8c01a98a2812 172 return m_drdy.read() == 0;
Arithemetica 0:8c01a98a2812 173 }
Arithemetica 0:8c01a98a2812 174
Arithemetica 0:8c01a98a2812 175 inline void m_processImuRawData()
Arithemetica 0:8c01a98a2812 176 {
Arithemetica 0:8c01a98a2812 177 for (int i = 0; i < 3; ++i) {
Arithemetica 0:8c01a98a2812 178 imuProcessedData.accel[i] = imuRawData.accel[i] / m_accelScaler[i];
Arithemetica 0:8c01a98a2812 179 imuProcessedData.rate[i] = imuRawData.rate[i] / m_gyroScaler[i];
Arithemetica 0:8c01a98a2812 180 }
Arithemetica 0:8c01a98a2812 181 }
Arithemetica 0:8c01a98a2812 182
Arithemetica 0:8c01a98a2812 183 inline void m_processAhrsRawData()
Arithemetica 0:8c01a98a2812 184 {
Arithemetica 0:8c01a98a2812 185 for (int i = 0; i < 3; ++i) {
Arithemetica 0:8c01a98a2812 186 ahrsProcessedData.attitude[i] = ahrsRawData.attitude[i] / m_ahrsScaler[i];
Arithemetica 0:8c01a98a2812 187 }
Arithemetica 0:8c01a98a2812 188 }
Arithemetica 5:e71931fcae33 189
Arithemetica 14:6c31a7ab45f0 190 void m_dataReadyExtiCallback()
Arithemetica 2:f3a7def7a7e1 191 {
Arithemetica 14:6c31a7ab45f0 192 if (m_extiConfig & ImuExtiRcvNormalMsg) {
Arithemetica 14:6c31a7ab45f0 193 receiveBurstMsgImpl();
Arithemetica 14:6c31a7ab45f0 194 }
Arithemetica 15:ca49fdec90fc 195
Arithemetica 14:6c31a7ab45f0 196 if (m_extiConfig & ImuExtiRcvAhrsMsg) {
Arithemetica 16:6583ad08d0d0 197 receiveAhrsMsgImpl();
Arithemetica 14:6c31a7ab45f0 198 }
Arithemetica 2:f3a7def7a7e1 199 }
Arithemetica 14:6c31a7ab45f0 200 private:
Arithemetica 16:6583ad08d0d0 201 ImuDriver(); // cannot initialize via default constructor
Arithemetica 16:6583ad08d0d0 202
Arithemetica 14:6c31a7ab45f0 203 ImuDriverStatus receiveBurstMsgImpl(bool t_extended = false)
Arithemetica 15:ca49fdec90fc 204 {
Arithemetica 0:8c01a98a2812 205 if (m_spiIsDataReady()) {
Arithemetica 0:8c01a98a2812 206 std::uint16_t max_data = 0U;
Arithemetica 0:8c01a98a2812 207 std::uint16_t burst_reg = 0U;
Arithemetica 0:8c01a98a2812 208
Arithemetica 0:8c01a98a2812 209 if (!t_extended) {
Arithemetica 0:8c01a98a2812 210 max_data = 8U;
Arithemetica 0:8c01a98a2812 211 burst_reg = m_spiGenerateReadCmd(ReadBurstDataRegister);
Arithemetica 0:8c01a98a2812 212 } else {
Arithemetica 0:8c01a98a2812 213 max_data = 11U;
Arithemetica 0:8c01a98a2812 214 burst_reg = m_spiGenerateReadCmd(ReadExtBurstDataRegister);
Arithemetica 0:8c01a98a2812 215 }
Arithemetica 0:8c01a98a2812 216
Arithemetica 0:8c01a98a2812 217 m_ss.write(0); // start transfer
Arithemetica 0:8c01a98a2812 218 m_imuSpiWrite(burst_reg);
Arithemetica 0:8c01a98a2812 219
Arithemetica 0:8c01a98a2812 220 static std::uint8_t data_rcved = 0U;
Arithemetica 0:8c01a98a2812 221
Arithemetica 0:8c01a98a2812 222 while(data_rcved < max_data) {
Arithemetica 0:8c01a98a2812 223 const std::uint16_t spi_data = m_imuSpiWrite(0x0000);
Arithemetica 0:8c01a98a2812 224 switch(data_rcved) {
Arithemetica 0:8c01a98a2812 225 case 0:
Arithemetica 0:8c01a98a2812 226 imuRawData.status = spi_data;
Arithemetica 0:8c01a98a2812 227 break;
Arithemetica 0:8c01a98a2812 228 case 1:
Arithemetica 0:8c01a98a2812 229 case 2:
Arithemetica 0:8c01a98a2812 230 case 3:
Arithemetica 0:8c01a98a2812 231 imuRawData.rate[data_rcved - 1] = spi_data;
Arithemetica 0:8c01a98a2812 232 break;
Arithemetica 0:8c01a98a2812 233 case 4:
Arithemetica 0:8c01a98a2812 234 case 5:
Arithemetica 0:8c01a98a2812 235 case 6:
Arithemetica 0:8c01a98a2812 236 imuRawData.accel[data_rcved - 4] = spi_data;
Arithemetica 0:8c01a98a2812 237 break;
Arithemetica 0:8c01a98a2812 238 case 7:
Arithemetica 0:8c01a98a2812 239 imuRawData.temperature = spi_data;
Arithemetica 0:8c01a98a2812 240 break;
Arithemetica 0:8c01a98a2812 241 default:
Arithemetica 0:8c01a98a2812 242 break;
Arithemetica 0:8c01a98a2812 243 }
Arithemetica 0:8c01a98a2812 244
Arithemetica 0:8c01a98a2812 245 ++data_rcved;
Arithemetica 0:8c01a98a2812 246 }
Arithemetica 0:8c01a98a2812 247
Arithemetica 0:8c01a98a2812 248 data_rcved = 0U;
Arithemetica 0:8c01a98a2812 249 m_ss.write(1);
Arithemetica 0:8c01a98a2812 250
Arithemetica 0:8c01a98a2812 251 m_processImuRawData();
Arithemetica 0:8c01a98a2812 252 return ImuDriverStatusOK;
Arithemetica 0:8c01a98a2812 253 } else {
Arithemetica 0:8c01a98a2812 254 return ImuDriverStatusDataNotReady;
Arithemetica 0:8c01a98a2812 255 }
Arithemetica 0:8c01a98a2812 256 }
Arithemetica 15:ca49fdec90fc 257
Arithemetica 15:ca49fdec90fc 258 ImuDriverStatus receiveAhrsMsgImpl()
Arithemetica 15:ca49fdec90fc 259 {
Arithemetica 15:ca49fdec90fc 260 if (m_spiIsDataReady()) {
Arithemetica 15:ca49fdec90fc 261 static std::uint8_t data_rcved = 0U;
Arithemetica 15:ca49fdec90fc 262 const std::uint8_t max_data = 5U;
Arithemetica 15:ca49fdec90fc 263 const std::uint16_t ahrs_reg = m_spiGenerateReadCmd(ReadAhrsBurstDataRegister);
Arithemetica 15:ca49fdec90fc 264
Arithemetica 15:ca49fdec90fc 265 m_ss.write(0);
Arithemetica 15:ca49fdec90fc 266 m_imuSpiWrite(ahrs_reg);
Arithemetica 15:ca49fdec90fc 267
Arithemetica 15:ca49fdec90fc 268 while(data_rcved < max_data) {
Arithemetica 15:ca49fdec90fc 269 const std::uint16_t spi_data = m_imuSpiWrite(0x0000);
Arithemetica 15:ca49fdec90fc 270 switch(data_rcved) {
Arithemetica 15:ca49fdec90fc 271 case 0:
Arithemetica 15:ca49fdec90fc 272 ahrsRawData.status = spi_data;
Arithemetica 15:ca49fdec90fc 273 break;
Arithemetica 15:ca49fdec90fc 274 case 1:
Arithemetica 15:ca49fdec90fc 275 case 2:
Arithemetica 15:ca49fdec90fc 276 case 3:
Arithemetica 15:ca49fdec90fc 277 ahrsRawData.attitude[data_rcved - 1] = spi_data;
Arithemetica 15:ca49fdec90fc 278 break;
Arithemetica 15:ca49fdec90fc 279 case 4:
Arithemetica 15:ca49fdec90fc 280 ahrsRawData.temperature = spi_data;
Arithemetica 15:ca49fdec90fc 281 break;
Arithemetica 15:ca49fdec90fc 282 default:
Arithemetica 15:ca49fdec90fc 283 break;
Arithemetica 15:ca49fdec90fc 284 }
Arithemetica 15:ca49fdec90fc 285
Arithemetica 15:ca49fdec90fc 286 ++data_rcved;
Arithemetica 17:629b2f317d0a 287
Arithemetica 15:ca49fdec90fc 288 }
Arithemetica 15:ca49fdec90fc 289
Arithemetica 15:ca49fdec90fc 290 data_rcved = 0U;
Arithemetica 15:ca49fdec90fc 291 m_ss.write(1);
Arithemetica 15:ca49fdec90fc 292
Arithemetica 15:ca49fdec90fc 293 m_processAhrsRawData();
Arithemetica 15:ca49fdec90fc 294 return ImuDriverStatusOK;
Arithemetica 15:ca49fdec90fc 295 } else {
Arithemetica 15:ca49fdec90fc 296 return ImuDriverStatusDataNotReady;
Arithemetica 15:ca49fdec90fc 297 }
Arithemetica 15:ca49fdec90fc 298 }
Arithemetica 14:6c31a7ab45f0 299 public:
Arithemetica 14:6c31a7ab45f0 300 /**
Arithemetica 14:6c31a7ab45f0 301 * imuRawData contains raw data received from @ref receiveBurstMsg(bool t_extended)
Arithemetica 14:6c31a7ab45f0 302 */
Arithemetica 14:6c31a7ab45f0 303 ImuRawData imuRawData;
Arithemetica 14:6c31a7ab45f0 304
Arithemetica 14:6c31a7ab45f0 305 /**
Arithemetica 14:6c31a7ab45f0 306 * imuProcessedData contains processed data, which is merely scaling operation
Arithemetica 14:6c31a7ab45f0 307 */
Arithemetica 14:6c31a7ab45f0 308 ImuProcessedData imuProcessedData;
Arithemetica 14:6c31a7ab45f0 309
Arithemetica 14:6c31a7ab45f0 310 /**
Arithemetica 14:6c31a7ab45f0 311 * ahrsRawaData contains raw data received from @ref receiveAhrsMsg()
Arithemetica 14:6c31a7ab45f0 312 */
Arithemetica 14:6c31a7ab45f0 313 AhrsRawData ahrsRawData;
Arithemetica 14:6c31a7ab45f0 314
Arithemetica 14:6c31a7ab45f0 315 /**
Arithemetica 14:6c31a7ab45f0 316 * ahrsProcessedData contains processed data, which is merely scaling operation
Arithemetica 14:6c31a7ab45f0 317 */
Arithemetica 14:6c31a7ab45f0 318 AhrsProcessedData ahrsProcessedData;
Arithemetica 14:6c31a7ab45f0 319
Arithemetica 14:6c31a7ab45f0 320 static const int DEFAULT_IMU_ACCEL_SCALER = 4000;
Arithemetica 14:6c31a7ab45f0 321 static const int DEFAULT_IMU_GYRO_SCALER = 100;
Arithemetica 14:6c31a7ab45f0 322 static const int DEFAULT_AHRS_ATTITUDE_SCALER = 90;
Arithemetica 14:6c31a7ab45f0 323 public:
Arithemetica 14:6c31a7ab45f0 324 explicit ImuDriver(const ImuExtiConfig t_exti_config) : m_extiConfig(t_exti_config), m_spi(Spi), m_rst(rst), m_ss(ss), m_drdy(drdy), m_drdyExti(drdy)
Arithemetica 14:6c31a7ab45f0 325 {
Arithemetica 14:6c31a7ab45f0 326 for (int i = 0; i < 3; ++i) {
Arithemetica 14:6c31a7ab45f0 327 m_gyroScaler[i] = static_cast<float>(DEFAULT_IMU_GYRO_SCALER); // what an idiotic way of initialization LUL
Arithemetica 14:6c31a7ab45f0 328 m_accelScaler[i] = static_cast<float>(DEFAULT_IMU_ACCEL_SCALER); // Oh, I forgot that mbed os2 still stucks at c++98,
Arithemetica 14:6c31a7ab45f0 329 m_ahrsScaler[i] = static_cast<float>(DEFAULT_AHRS_ATTITUDE_SCALER); // how unfortunate LUL
Arithemetica 14:6c31a7ab45f0 330 }
Arithemetica 14:6c31a7ab45f0 331
Arithemetica 14:6c31a7ab45f0 332 if (t_exti_config != ImuExtiRcvNotUsed) {
Arithemetica 14:6c31a7ab45f0 333 m_drdyExti.fall(this, &ImuDriver<Spi, rst, drdy, ss>::m_dataReadyExtiCallback);
Arithemetica 17:629b2f317d0a 334 m_drdyExti.enable_irq();
Arithemetica 14:6c31a7ab45f0 335 }
Arithemetica 14:6c31a7ab45f0 336
Arithemetica 14:6c31a7ab45f0 337 m_spi.format(16, 3);
Arithemetica 17:629b2f317d0a 338 m_spi.frequency();
Arithemetica 14:6c31a7ab45f0 339 }
Arithemetica 14:6c31a7ab45f0 340
Arithemetica 14:6c31a7ab45f0 341 /**
Arithemetica 14:6c31a7ab45f0 342 * @brief This function handles the receiving of burst message function, burst message
Arithemetica 14:6c31a7ab45f0 343 * contains accelerometer (g), gyroscope (deg/s), status and temperature data (Celcius)
Arithemetica 14:6c31a7ab45f0 344 *
Arithemetica 14:6c31a7ab45f0 345 * @param t_extended bool to indicate the message type is extended or not, default false
Arithemetica 14:6c31a7ab45f0 346 *
Arithemetica 14:6c31a7ab45f0 347 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 14:6c31a7ab45f0 348 */
Arithemetica 15:ca49fdec90fc 349 ImuDriverStatus receiveBurstMsg(bool t_extended = false)
Arithemetica 15:ca49fdec90fc 350 {
Arithemetica 15:ca49fdec90fc 351 return (m_extiConfig & ImuExtiRcvNormalMsg ? ImuDriverStatusInvalidCall : receiveBurstMsgImpl(t_extended));
Arithemetica 14:6c31a7ab45f0 352 }
Arithemetica 0:8c01a98a2812 353
Arithemetica 0:8c01a98a2812 354 /**
Arithemetica 5:e71931fcae33 355 * @brief This function handles the receiving of AHRS burst message function, AHRS
Arithemetica 10:d8223969c541 356 * burst message contains the attitude in euler angle (deg).
Arithemetica 0:8c01a98a2812 357 *
Arithemetica 0:8c01a98a2812 358 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 0:8c01a98a2812 359 *
Arithemetica 0:8c01a98a2812 360 * @note This is only suitable for AHRS app, and additional procedure needs to be done
Arithemetica 0:8c01a98a2812 361 * in order to make it work
Arithemetica 0:8c01a98a2812 362 */
Arithemetica 0:8c01a98a2812 363 ImuDriverStatus receiveAhrsMsg()
Arithemetica 0:8c01a98a2812 364 {
Arithemetica 15:ca49fdec90fc 365 return (m_extiConfig & ImuExtiRcvAhrsMsg ? ImuDriverStatusInvalidCall : receiveAhrsMsgImpl());
Arithemetica 15:ca49fdec90fc 366 }
Arithemetica 0:8c01a98a2812 367
Arithemetica 17:629b2f317d0a 368 /**
Arithemetica 17:629b2f317d0a 369 * @brief This function handles the external interrupt option, whether receive AHRS
Arithemetica 17:629b2f317d0a 370 * burst message or normal imu message.
Arithemetica 17:629b2f317d0a 371 *
Arithemetica 17:629b2f317d0a 372 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 17:629b2f317d0a 373 *
Arithemetica 17:629b2f317d0a 374 * @note Never try receiving two messages at the same time
Arithemetica 17:629b2f317d0a 375 */
Arithemetica 17:629b2f317d0a 376 ImuDriverStatus configExti(const ImuExtiConfig t_exti_config) const
Arithemetica 15:ca49fdec90fc 377 {
Arithemetica 14:6c31a7ab45f0 378 m_extiConfig = t_exti_config;
Arithemetica 17:629b2f317d0a 379
Arithemetica 17:629b2f317d0a 380 if (m_extoConfig == ImuExtiRcvNotUsed) {
Arithemetica 17:629b2f317d0a 381 m_drdyExti.disable_irq();
Arithemetica 17:629b2f317d0a 382 } else {
Arithemetica 17:629b2f317d0a 383 m_drdyExti.enable_irq();
Arithemetica 17:629b2f317d0a 384 }
Arithemetica 17:629b2f317d0a 385
Arithemetica 17:629b2f317d0a 386 return ImuDriverStatusOK;
Arithemetica 14:6c31a7ab45f0 387 }
Arithemetica 1:8e31413068af 388 };
Arithemetica 1:8e31413068af 389
Arithemetica 1:8e31413068af 390 #endif // IMU_DRIVER_HPP_