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