Jacky Tseng
/
imu_driver
handle master side communication of openIMU300ZI module
imu_driver.hpp@10:d8223969c541, 2019-11-18 (annotated)
- Committer:
- Arithemetica
- Date:
- Mon Nov 18 08:05:02 2019 +0000
- Revision:
- 10:d8223969c541
- Parent:
- 9:9591afda2394
- Child:
- 11:866dd73e4ab3
Update documentation
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:8c01a98a2812 | 13 | typedef enum ImuDriverRegister { |
Arithemetica | 0:8c01a98a2812 | 14 | ReadAhrsBurstDataRegister = 0x3D, |
Arithemetica | 0:8c01a98a2812 | 15 | ReadBurstDataRegister = 0x3E, |
Arithemetica | 0:8c01a98a2812 | 16 | ReadExtBurstDataRegister = 0x3F |
Arithemetica | 0:8c01a98a2812 | 17 | } ImuDriverRegister; |
Arithemetica | 0:8c01a98a2812 | 18 | |
Arithemetica | 0:8c01a98a2812 | 19 | typedef struct AhrsRawData { |
Arithemetica | 0:8c01a98a2812 | 20 | std::uint16_t status; |
Arithemetica | 0:8c01a98a2812 | 21 | std::int16_t attitude[3]; |
Arithemetica | 0:8c01a98a2812 | 22 | std::int16_t temperature; |
Arithemetica | 0:8c01a98a2812 | 23 | } AhrsRawData; |
Arithemetica | 0:8c01a98a2812 | 24 | |
Arithemetica | 10:d8223969c541 | 25 | /** |
Arithemetica | 10:d8223969c541 | 26 | * @brief AhrsProcessedData is data storage class, which consists of |
Arithemetica | 10:d8223969c541 | 27 | * |
Arithemetica | 10:d8223969c541 | 28 | * - attitude: pitch, roll, yaw in deg, range from -180 ~ +180 deg |
Arithemetica | 10:d8223969c541 | 29 | */ |
Arithemetica | 0:8c01a98a2812 | 30 | typedef struct AhrsProcessedData { |
Arithemetica | 0:8c01a98a2812 | 31 | float attitude[3]; |
Arithemetica | 0:8c01a98a2812 | 32 | } AhrsProcessedData; |
Arithemetica | 0:8c01a98a2812 | 33 | |
Arithemetica | 0:8c01a98a2812 | 34 | typedef struct ImuRawData { |
Arithemetica | 0:8c01a98a2812 | 35 | std::uint16_t status; |
Arithemetica | 0:8c01a98a2812 | 36 | std::int16_t rate[3]; |
Arithemetica | 0:8c01a98a2812 | 37 | std::int16_t accel[3]; |
Arithemetica | 0:8c01a98a2812 | 38 | std::int16_t temperature; |
Arithemetica | 0:8c01a98a2812 | 39 | } ImuRawData; |
Arithemetica | 0:8c01a98a2812 | 40 | |
Arithemetica | 10:d8223969c541 | 41 | /** |
Arithemetica | 10:d8223969c541 | 42 | * @brief ImuProcessedData is data storage class, which consists of: |
Arithemetica | 10:d8223969c541 | 43 | * |
Arithemetica | 10:d8223969c541 | 44 | * - status: if status = 0x0010 (over range error), status = 0 (no error) |
Arithemetica | 10:d8223969c541 | 45 | * - gyroscope: deg/s, range from -300 deg/s ~ 300 deg/s |
Arithemetica | 10:d8223969c541 | 46 | * - accelerometer: g, range from -4.5g ~ 4.5g |
Arithemetica | 10:d8223969c541 | 47 | * - temperature: Celcius |
Arithemetica | 10:d8223969c541 | 48 | */ |
Arithemetica | 0:8c01a98a2812 | 49 | typedef struct ImuProcessedData { |
Arithemetica | 0:8c01a98a2812 | 50 | std::uint16_t status; |
Arithemetica | 0:8c01a98a2812 | 51 | |
Arithemetica | 0:8c01a98a2812 | 52 | float rate[3]; |
Arithemetica | 0:8c01a98a2812 | 53 | float accel[3]; |
Arithemetica | 0:8c01a98a2812 | 54 | |
Arithemetica | 0:8c01a98a2812 | 55 | std::int16_t temperature; |
Arithemetica | 0:8c01a98a2812 | 56 | } ImuProcessedData; |
Arithemetica | 0:8c01a98a2812 | 57 | |
Arithemetica | 0:8c01a98a2812 | 58 | /** |
Arithemetica | 2:f3a7def7a7e1 | 59 | * @brief ImuDriver is used to handle the SPI master side of openIMU300ZI module, the |
Arithemetica | 2:f3a7def7a7e1 | 60 | * default setups are: |
Arithemetica | 0:8c01a98a2812 | 61 | * |
Arithemetica | 2:f3a7def7a7e1 | 62 | * - Data frame: 16 bits |
Arithemetica | 2:f3a7def7a7e1 | 63 | * - CPOL: High (1) |
Arithemetica | 2:f3a7def7a7e1 | 64 | * - CPHA: 2 Edge (1) |
Arithemetica | 2:f3a7def7a7e1 | 65 | * - Frequency: 1.40625 MHz (Baudrate prescaler 32 for NUCLEO-F446RE under 180MHz) |
Arithemetica | 2:f3a7def7a7e1 | 66 | * - Endian: MSB first (Mbed only support MSB AFAIK) |
Arithemetica | 5:e71931fcae33 | 67 | * |
Arithemetica | 10:d8223969c541 | 68 | * According to <A HREF="https://openimu.readthedocs.io/en/latest/software/SPImessaging.html">openIMU300ZI SPI framework</A>: |
Arithemetica | 5:e71931fcae33 | 69 | * |
Arithemetica | 3:8552e26cd162 | 70 | * - Data transferred in 16-bit word-length and MSB-first |
Arithemetica | 5:e71931fcae33 | 71 | * - fCLK ≤ 2.0 MHz |
Arithemetica | 3:8552e26cd162 | 72 | * - CPOL = 1 (clock polarity) and CPHA = 1 (clock phase) |
Arithemetica | 0:8c01a98a2812 | 73 | * |
Arithemetica | 2:f3a7def7a7e1 | 74 | * @tparam spi SPI comm instance, ImuDriver currently support only SPI framework |
Arithemetica | 2:f3a7def7a7e1 | 75 | * @tparam rst Reset pin name, this is used to reset the openIMU300ZI module |
Arithemetica | 2:f3a7def7a7e1 | 76 | * @tparam drdy Data ready pin name, this is used as indication of data readiness |
Arithemetica | 2:f3a7def7a7e1 | 77 | * @tparam ss Slave select pin name, this is used as slave select, pull low to initiate |
Arithemetica | 2:f3a7def7a7e1 | 78 | * communication process. |
Arithemetica | 5:e71931fcae33 | 79 | * |
Arithemetica | 7:224ffdacb240 | 80 | * @todo Attach to exti in the future, and make user able to choose |
Arithemetica | 7:224ffdacb240 | 81 | * |
Arithemetica | 0:8c01a98a2812 | 82 | * Example of using ImuDriver: |
Arithemetica | 0:8c01a98a2812 | 83 | * |
Arithemetica | 0:8c01a98a2812 | 84 | * @code |
Arithemetica | 0:8c01a98a2812 | 85 | * #include "imu_driver.hpp" |
Arithemetica | 0:8c01a98a2812 | 86 | * |
Arithemetica | 0:8c01a98a2812 | 87 | * SPI spi3(PB_5, PB_6, PB_3); // declare SPI instance globally |
Arithemetica | 0:8c01a98a2812 | 88 | * Serial pc(USBTX, USBRX, 115200); // print debug message |
Arithemetica | 0:8c01a98a2812 | 89 | * |
Arithemetica | 0:8c01a98a2812 | 90 | * int main() |
Arithemetica | 0:8c01a98a2812 | 91 | * { |
Arithemetica | 5:e71931fcae33 | 92 | * // SPI instance, reset, data ready, slave select |
Arithemetica | 0:8c01a98a2812 | 93 | * ImuDriver<spi3, PA_10, PA_8, PA_9> imu; |
Arithemetica | 0:8c01a98a2812 | 94 | * |
Arithemetica | 0:8c01a98a2812 | 95 | * while(true) |
Arithemetica | 0:8c01a98a2812 | 96 | * { |
Arithemetica | 0:8c01a98a2812 | 97 | * if (imu.receiveBurstMsg() == ImuDriverStatusOK) |
Arithemetica | 0:8c01a98a2812 | 98 | * { |
Arithemetica | 0:8c01a98a2812 | 99 | * pc.printf("%.3f, %.3f, %.3f\n\r", imu.imuProcessedData.rate[0], imu.imuProcessedData.rate[1], imu.imuProcessedData.rate[2]); |
Arithemetica | 0:8c01a98a2812 | 100 | * } |
Arithemetica | 0:8c01a98a2812 | 101 | * } |
Arithemetica | 0:8c01a98a2812 | 102 | * } |
Arithemetica | 5:e71931fcae33 | 103 | * |
Arithemetica | 0:8c01a98a2812 | 104 | * @endcode |
Arithemetica | 0:8c01a98a2812 | 105 | */ |
Arithemetica | 2:f3a7def7a7e1 | 106 | template <SPI& Spi, PinName rst, PinName drdy, PinName ss> |
Arithemetica | 0:8c01a98a2812 | 107 | class ImuDriver |
Arithemetica | 0:8c01a98a2812 | 108 | { |
Arithemetica | 0:8c01a98a2812 | 109 | private: |
Arithemetica | 0:8c01a98a2812 | 110 | SPI& m_spi; |
Arithemetica | 0:8c01a98a2812 | 111 | DigitalOut m_rst, m_ss; |
Arithemetica | 0:8c01a98a2812 | 112 | DigitalIn m_drdy; |
Arithemetica | 2:f3a7def7a7e1 | 113 | InterruptIn m_drdyExti; |
Arithemetica | 5:e71931fcae33 | 114 | |
Arithemetica | 0:8c01a98a2812 | 115 | float m_gyroScaler[3]; |
Arithemetica | 0:8c01a98a2812 | 116 | float m_accelScaler[3]; |
Arithemetica | 0:8c01a98a2812 | 117 | float m_ahrsScaler[3]; |
Arithemetica | 0:8c01a98a2812 | 118 | private: |
Arithemetica | 0:8c01a98a2812 | 119 | std::uint16_t m_imuSpiWrite(const std::uint16_t val) const |
Arithemetica | 0:8c01a98a2812 | 120 | { |
Arithemetica | 0:8c01a98a2812 | 121 | // RAII |
Arithemetica | 0:8c01a98a2812 | 122 | class SpiLock |
Arithemetica | 0:8c01a98a2812 | 123 | { |
Arithemetica | 0:8c01a98a2812 | 124 | private: |
Arithemetica | 0:8c01a98a2812 | 125 | SPI& m_spi; |
Arithemetica | 0:8c01a98a2812 | 126 | public: |
Arithemetica | 0:8c01a98a2812 | 127 | SpiLock(SPI& t_spi) : m_spi(t_spi) |
Arithemetica | 0:8c01a98a2812 | 128 | { |
Arithemetica | 0:8c01a98a2812 | 129 | m_spi.lock(); |
Arithemetica | 0:8c01a98a2812 | 130 | } |
Arithemetica | 0:8c01a98a2812 | 131 | |
Arithemetica | 0:8c01a98a2812 | 132 | ~SpiLock() |
Arithemetica | 0:8c01a98a2812 | 133 | { |
Arithemetica | 0:8c01a98a2812 | 134 | m_spi.unlock(); |
Arithemetica | 0:8c01a98a2812 | 135 | } |
Arithemetica | 0:8c01a98a2812 | 136 | }; |
Arithemetica | 0:8c01a98a2812 | 137 | |
Arithemetica | 0:8c01a98a2812 | 138 | SpiLock lock(m_spi); |
Arithemetica | 0:8c01a98a2812 | 139 | return m_spi.write(val); |
Arithemetica | 0:8c01a98a2812 | 140 | } |
Arithemetica | 0:8c01a98a2812 | 141 | |
Arithemetica | 0:8c01a98a2812 | 142 | inline std::uint16_t m_spiGenerateReadCmd(const std::uint8_t t_val) const |
Arithemetica | 0:8c01a98a2812 | 143 | { |
Arithemetica | 0:8c01a98a2812 | 144 | return t_val << 8U; |
Arithemetica | 0:8c01a98a2812 | 145 | } |
Arithemetica | 0:8c01a98a2812 | 146 | |
Arithemetica | 0:8c01a98a2812 | 147 | inline bool m_spiIsDataReady() |
Arithemetica | 0:8c01a98a2812 | 148 | { |
Arithemetica | 0:8c01a98a2812 | 149 | return m_drdy.read() == 0; |
Arithemetica | 0:8c01a98a2812 | 150 | } |
Arithemetica | 0:8c01a98a2812 | 151 | |
Arithemetica | 0:8c01a98a2812 | 152 | inline void m_processImuRawData() |
Arithemetica | 0:8c01a98a2812 | 153 | { |
Arithemetica | 0:8c01a98a2812 | 154 | for (int i = 0; i < 3; ++i) { |
Arithemetica | 0:8c01a98a2812 | 155 | imuProcessedData.accel[i] = imuRawData.accel[i] / m_accelScaler[i]; |
Arithemetica | 0:8c01a98a2812 | 156 | imuProcessedData.rate[i] = imuRawData.rate[i] / m_gyroScaler[i]; |
Arithemetica | 0:8c01a98a2812 | 157 | } |
Arithemetica | 0:8c01a98a2812 | 158 | } |
Arithemetica | 0:8c01a98a2812 | 159 | |
Arithemetica | 0:8c01a98a2812 | 160 | inline void m_processAhrsRawData() |
Arithemetica | 0:8c01a98a2812 | 161 | { |
Arithemetica | 0:8c01a98a2812 | 162 | for (int i = 0; i < 3; ++i) { |
Arithemetica | 0:8c01a98a2812 | 163 | ahrsProcessedData.attitude[i] = ahrsRawData.attitude[i] / m_ahrsScaler[i]; |
Arithemetica | 0:8c01a98a2812 | 164 | } |
Arithemetica | 0:8c01a98a2812 | 165 | } |
Arithemetica | 5:e71931fcae33 | 166 | |
Arithemetica | 2:f3a7def7a7e1 | 167 | void m_receiveAhrsCallback() |
Arithemetica | 2:f3a7def7a7e1 | 168 | { |
Arithemetica | 2:f3a7def7a7e1 | 169 | receiveAhrsMsg(); |
Arithemetica | 2:f3a7def7a7e1 | 170 | } |
Arithemetica | 0:8c01a98a2812 | 171 | public: |
Arithemetica | 10:d8223969c541 | 172 | typedef enum ImuDriverStatus { |
Arithemetica | 10:d8223969c541 | 173 | ImuDriverStatusOK, |
Arithemetica | 10:d8223969c541 | 174 | ImuDriverStatusDataNotReady |
Arithemetica | 10:d8223969c541 | 175 | } ImuDriverStatus; |
Arithemetica | 10:d8223969c541 | 176 | public: |
Arithemetica | 10:d8223969c541 | 177 | /** |
Arithemetica | 10:d8223969c541 | 178 | * imuRawData contains raw data received from @ref receiveBurstMsg(bool t_extended) |
Arithemetica | 10:d8223969c541 | 179 | */ |
Arithemetica | 0:8c01a98a2812 | 180 | ImuRawData imuRawData; |
Arithemetica | 10:d8223969c541 | 181 | |
Arithemetica | 10:d8223969c541 | 182 | /** |
Arithemetica | 10:d8223969c541 | 183 | * imuProcessedData contains processed data, which is merely scaling operation |
Arithemetica | 10:d8223969c541 | 184 | */ |
Arithemetica | 0:8c01a98a2812 | 185 | ImuProcessedData imuProcessedData; |
Arithemetica | 0:8c01a98a2812 | 186 | |
Arithemetica | 10:d8223969c541 | 187 | /** |
Arithemetica | 10:d8223969c541 | 188 | * ahrsRawaData contains raw data received from @ref receiveAhrsMsg() |
Arithemetica | 10:d8223969c541 | 189 | */ |
Arithemetica | 0:8c01a98a2812 | 190 | AhrsRawData ahrsRawData; |
Arithemetica | 10:d8223969c541 | 191 | |
Arithemetica | 10:d8223969c541 | 192 | /** |
Arithemetica | 10:d8223969c541 | 193 | * ahrsProcessedData contains processed data, which is merely scaling operation |
Arithemetica | 10:d8223969c541 | 194 | */ |
Arithemetica | 0:8c01a98a2812 | 195 | AhrsProcessedData ahrsProcessedData; |
Arithemetica | 0:8c01a98a2812 | 196 | |
Arithemetica | 0:8c01a98a2812 | 197 | static const int DEFAULT_IMU_ACCEL_SCALER = 4000; |
Arithemetica | 0:8c01a98a2812 | 198 | static const int DEFAULT_IMU_GYRO_SCALER = 200; |
Arithemetica | 0:8c01a98a2812 | 199 | static const int DEFAULT_AHRS_ATTITUDE_SCALER = 90; |
Arithemetica | 0:8c01a98a2812 | 200 | public: |
Arithemetica | 2:f3a7def7a7e1 | 201 | ImuDriver() : m_spi(Spi), m_rst(rst), m_ss(ss), m_drdy(drdy), m_drdyExti(drdy) |
Arithemetica | 0:8c01a98a2812 | 202 | { |
Arithemetica | 0:8c01a98a2812 | 203 | for (int i = 0; i < 3; ++i) { |
Arithemetica | 0:8c01a98a2812 | 204 | m_gyroScaler[i] = static_cast<float>(DEFAULT_IMU_GYRO_SCALER); // what an idiotic way of initialization LUL |
Arithemetica | 2:f3a7def7a7e1 | 205 | m_accelScaler[i] = static_cast<float>(DEFAULT_IMU_ACCEL_SCALER); // Oh, I forgot that mbed os2 still stucks at c++98, |
Arithemetica | 0:8c01a98a2812 | 206 | m_ahrsScaler[i] = static_cast<float>(DEFAULT_AHRS_ATTITUDE_SCALER); // how unfortunate LUL |
Arithemetica | 0:8c01a98a2812 | 207 | } |
Arithemetica | 0:8c01a98a2812 | 208 | |
Arithemetica | 2:f3a7def7a7e1 | 209 | // m_drdyExti.rise(this, &ImuDriver<Spi, rst, drdy, ss>::m_receiveAhrsCallback); |
Arithemetica | 0:8c01a98a2812 | 210 | m_spi.format(16, 3); |
Arithemetica | 0:8c01a98a2812 | 211 | m_spi.frequency(1406250); |
Arithemetica | 0:8c01a98a2812 | 212 | } |
Arithemetica | 0:8c01a98a2812 | 213 | |
Arithemetica | 0:8c01a98a2812 | 214 | /** |
Arithemetica | 5:e71931fcae33 | 215 | * @brief This function handles the receiving of burst message function, burst message |
Arithemetica | 10:d8223969c541 | 216 | * contains accelerometer (g), gyroscope (deg/s), status and temperature data (Celcius) |
Arithemetica | 0:8c01a98a2812 | 217 | * |
Arithemetica | 0:8c01a98a2812 | 218 | * @param t_extended bool to indicate the message type is extended or not, default false |
Arithemetica | 0:8c01a98a2812 | 219 | * |
Arithemetica | 0:8c01a98a2812 | 220 | * @return ImuDriverStatus to indicate the result of the receive operation |
Arithemetica | 0:8c01a98a2812 | 221 | */ |
Arithemetica | 0:8c01a98a2812 | 222 | ImuDriverStatus receiveBurstMsg(bool t_extended = false) |
Arithemetica | 0:8c01a98a2812 | 223 | { |
Arithemetica | 0:8c01a98a2812 | 224 | if (m_spiIsDataReady()) { |
Arithemetica | 0:8c01a98a2812 | 225 | std::uint16_t max_data = 0U; |
Arithemetica | 0:8c01a98a2812 | 226 | std::uint16_t burst_reg = 0U; |
Arithemetica | 0:8c01a98a2812 | 227 | |
Arithemetica | 0:8c01a98a2812 | 228 | if (!t_extended) { |
Arithemetica | 0:8c01a98a2812 | 229 | max_data = 8U; |
Arithemetica | 0:8c01a98a2812 | 230 | burst_reg = m_spiGenerateReadCmd(ReadBurstDataRegister); |
Arithemetica | 0:8c01a98a2812 | 231 | } else { |
Arithemetica | 0:8c01a98a2812 | 232 | max_data = 11U; |
Arithemetica | 0:8c01a98a2812 | 233 | burst_reg = m_spiGenerateReadCmd(ReadExtBurstDataRegister); |
Arithemetica | 0:8c01a98a2812 | 234 | } |
Arithemetica | 0:8c01a98a2812 | 235 | |
Arithemetica | 0:8c01a98a2812 | 236 | m_ss.write(0); // start transfer |
Arithemetica | 0:8c01a98a2812 | 237 | m_imuSpiWrite(burst_reg); |
Arithemetica | 0:8c01a98a2812 | 238 | |
Arithemetica | 0:8c01a98a2812 | 239 | static std::uint8_t data_rcved = 0U; |
Arithemetica | 0:8c01a98a2812 | 240 | |
Arithemetica | 0:8c01a98a2812 | 241 | while(data_rcved < max_data) { |
Arithemetica | 0:8c01a98a2812 | 242 | const std::uint16_t spi_data = m_imuSpiWrite(0x0000); |
Arithemetica | 0:8c01a98a2812 | 243 | switch(data_rcved) { |
Arithemetica | 0:8c01a98a2812 | 244 | case 0: |
Arithemetica | 0:8c01a98a2812 | 245 | imuRawData.status = spi_data; |
Arithemetica | 0:8c01a98a2812 | 246 | break; |
Arithemetica | 0:8c01a98a2812 | 247 | case 1: |
Arithemetica | 0:8c01a98a2812 | 248 | case 2: |
Arithemetica | 0:8c01a98a2812 | 249 | case 3: |
Arithemetica | 0:8c01a98a2812 | 250 | imuRawData.rate[data_rcved - 1] = spi_data; |
Arithemetica | 0:8c01a98a2812 | 251 | break; |
Arithemetica | 0:8c01a98a2812 | 252 | case 4: |
Arithemetica | 0:8c01a98a2812 | 253 | case 5: |
Arithemetica | 0:8c01a98a2812 | 254 | case 6: |
Arithemetica | 0:8c01a98a2812 | 255 | imuRawData.accel[data_rcved - 4] = spi_data; |
Arithemetica | 0:8c01a98a2812 | 256 | break; |
Arithemetica | 0:8c01a98a2812 | 257 | case 7: |
Arithemetica | 0:8c01a98a2812 | 258 | imuRawData.temperature = spi_data; |
Arithemetica | 0:8c01a98a2812 | 259 | break; |
Arithemetica | 0:8c01a98a2812 | 260 | default: |
Arithemetica | 0:8c01a98a2812 | 261 | break; |
Arithemetica | 0:8c01a98a2812 | 262 | } |
Arithemetica | 0:8c01a98a2812 | 263 | |
Arithemetica | 0:8c01a98a2812 | 264 | ++data_rcved; |
Arithemetica | 0:8c01a98a2812 | 265 | } |
Arithemetica | 0:8c01a98a2812 | 266 | |
Arithemetica | 0:8c01a98a2812 | 267 | data_rcved = 0U; |
Arithemetica | 0:8c01a98a2812 | 268 | m_ss.write(1); |
Arithemetica | 0:8c01a98a2812 | 269 | |
Arithemetica | 0:8c01a98a2812 | 270 | m_processImuRawData(); |
Arithemetica | 0:8c01a98a2812 | 271 | return ImuDriverStatusOK; |
Arithemetica | 0:8c01a98a2812 | 272 | } else { |
Arithemetica | 0:8c01a98a2812 | 273 | return ImuDriverStatusDataNotReady; |
Arithemetica | 0:8c01a98a2812 | 274 | } |
Arithemetica | 0:8c01a98a2812 | 275 | } |
Arithemetica | 0:8c01a98a2812 | 276 | |
Arithemetica | 0:8c01a98a2812 | 277 | /** |
Arithemetica | 5:e71931fcae33 | 278 | * @brief This function handles the receiving of AHRS burst message function, AHRS |
Arithemetica | 10:d8223969c541 | 279 | * burst message contains the attitude in euler angle (deg). |
Arithemetica | 0:8c01a98a2812 | 280 | * |
Arithemetica | 0:8c01a98a2812 | 281 | * @return ImuDriverStatus to indicate the result of the receive operation |
Arithemetica | 0:8c01a98a2812 | 282 | * |
Arithemetica | 0:8c01a98a2812 | 283 | * @note This is only suitable for AHRS app, and additional procedure needs to be done |
Arithemetica | 0:8c01a98a2812 | 284 | * in order to make it work |
Arithemetica | 0:8c01a98a2812 | 285 | */ |
Arithemetica | 0:8c01a98a2812 | 286 | ImuDriverStatus receiveAhrsMsg() |
Arithemetica | 0:8c01a98a2812 | 287 | { |
Arithemetica | 0:8c01a98a2812 | 288 | if (m_spiIsDataReady()) { |
Arithemetica | 0:8c01a98a2812 | 289 | static std::uint8_t data_rcved = 0U; |
Arithemetica | 0:8c01a98a2812 | 290 | const std::uint8_t max_data = 5U; |
Arithemetica | 0:8c01a98a2812 | 291 | const std::uint16_t ahrs_reg = m_spiGenerateReadCmd(ReadAhrsBurstDataRegister); |
Arithemetica | 0:8c01a98a2812 | 292 | |
Arithemetica | 0:8c01a98a2812 | 293 | m_ss.write(0); |
Arithemetica | 0:8c01a98a2812 | 294 | m_imuSpiWrite(ahrs_reg); |
Arithemetica | 0:8c01a98a2812 | 295 | |
Arithemetica | 0:8c01a98a2812 | 296 | while(data_rcved < max_data) { |
Arithemetica | 0:8c01a98a2812 | 297 | const std::uint16_t spi_data = m_imuSpiWrite(0x0000); |
Arithemetica | 0:8c01a98a2812 | 298 | switch(data_rcved) { |
Arithemetica | 0:8c01a98a2812 | 299 | case 0: |
Arithemetica | 0:8c01a98a2812 | 300 | ahrsRawData.status = spi_data; |
Arithemetica | 0:8c01a98a2812 | 301 | break; |
Arithemetica | 0:8c01a98a2812 | 302 | case 1: |
Arithemetica | 0:8c01a98a2812 | 303 | case 2: |
Arithemetica | 0:8c01a98a2812 | 304 | case 3: |
Arithemetica | 0:8c01a98a2812 | 305 | ahrsRawData.attitude[data_rcved - 1] = spi_data; |
Arithemetica | 0:8c01a98a2812 | 306 | break; |
Arithemetica | 0:8c01a98a2812 | 307 | case 4: |
Arithemetica | 0:8c01a98a2812 | 308 | ahrsRawData.temperature = spi_data; |
Arithemetica | 0:8c01a98a2812 | 309 | break; |
Arithemetica | 0:8c01a98a2812 | 310 | default: |
Arithemetica | 0:8c01a98a2812 | 311 | break; |
Arithemetica | 0:8c01a98a2812 | 312 | } |
Arithemetica | 0:8c01a98a2812 | 313 | |
Arithemetica | 0:8c01a98a2812 | 314 | ++data_rcved; |
Arithemetica | 0:8c01a98a2812 | 315 | } |
Arithemetica | 0:8c01a98a2812 | 316 | |
Arithemetica | 0:8c01a98a2812 | 317 | data_rcved = 0U; |
Arithemetica | 0:8c01a98a2812 | 318 | m_ss.write(1); |
Arithemetica | 0:8c01a98a2812 | 319 | |
Arithemetica | 0:8c01a98a2812 | 320 | m_processAhrsRawData(); |
Arithemetica | 0:8c01a98a2812 | 321 | return ImuDriverStatusOK; |
Arithemetica | 0:8c01a98a2812 | 322 | } else { |
Arithemetica | 0:8c01a98a2812 | 323 | return ImuDriverStatusDataNotReady; |
Arithemetica | 0:8c01a98a2812 | 324 | } |
Arithemetica | 0:8c01a98a2812 | 325 | } |
Arithemetica | 1:8e31413068af | 326 | }; |
Arithemetica | 1:8e31413068af | 327 | |
Arithemetica | 1:8e31413068af | 328 | #endif // IMU_DRIVER_HPP_ |