SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_fifo_read.ino
Go to the documentation of this file.
1
48#include <stdarg.h>
49#include <ImuDrv.hpp>
50#include "DevicesPins.h"
51
52
53// Select one interface.
54// #define USE_I2C_INTERFACE
55// #define USE_SPI_INTERFACE
56
57#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
58#define USE_I2C_INTERFACE
59#endif
60
61#if defined(USE_SPI_INTERFACE)
62
63#ifndef SPI_MOSI
64#define SPI_MOSI 35
65#endif
66
67#ifndef SPI_MISO
68#define SPI_MISO 37
69#endif
70
71#ifndef SPI_SCK
72#define SPI_SCK 36
73#endif
74
75#ifndef IMU_CS
76#define IMU_CS 34
77#endif
78
79#else
80
81#ifndef IMU_SDA
82#define IMU_SDA 17
83#endif
84
85#ifndef IMU_SCL
86#define IMU_SCL 18
87#endif
88
89#endif
90
91#ifndef IMU_IRQ
92#define IMU_IRQ 33
93#endif
94
95#ifdef ARDUINO_T_BEAM_S3_SUPREME
96#include <XPowersAXP2101.tpp>
97#endif
98
99static void serialPrintFmt(const char *fmt, ...)
100{
101 char buf[256];
102 va_list args;
103 va_start(args, fmt);
104 vsnprintf(buf, sizeof(buf), fmt, args);
105 va_end(args);
106 Serial.print(buf);
107}
108
110{
111#if defined(ARDUINO_T_BEAM_S3_SUPREME)
112 XPowersAXP2101 power;
113 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
114 power.disableALDO1();
115 power.disableALDO2();
116 delay(250);
117 power.setALDO1Voltage(3300);
118 power.enableALDO1();
119 power.setALDO2Voltage(3300);
120 power.enableALDO2();
121#endif
122}
123
124static constexpr uint16_t MAX_FIFO_SAMPLES = 32;
125static uint8_t fifoWarmupDiscard = 8;
126
128AccelerometerData accelFifo[MAX_FIFO_SAMPLES];
129GyroscopeData gyroFifo[MAX_FIFO_SAMPLES];
130
131
132
133void setup()
134{
135 Serial.begin(115200);
136 while (!Serial);
137
138 setupPower();
139
140 Serial.println("QMI8658 FIFO Read Example");
141
142#ifdef USE_I2C_INTERFACE
143 if (!imu.begin(Wire, QMI8658_H_SLAVE_ADDRESS, IMU_SDA, IMU_SCL)) {
144 Serial.println("Failed to initialize QMI8658!");
145 while (1) {
146 delay(1000);
147 }
148 }
149#endif
150
151#ifdef USE_SPI_INTERFACE
152 // Using SPI interface
153 if (!imu.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
154 Serial.println("Failed to initialize QMI8658!");
155 while (1) {
156 delay(1000);
157 }
158 }
159#endif
160
162
163 // Accelerometer: FS_2G(000), FS_4G(001), FS_8G(010), FS_16G(011)
164 // ODR: 1000, 500, 250, 125, 62.5, 31.25, 128, 21, 11, 3 Hz (6DOF: 448/224/112/56/28 Hz)
166 125.0f);
167
168 // Gyroscope: FS_125_DPS, FS_250_DPS, FS_500_DPS, FS_1000_DPS, FS_2000_DPS, FS_4000_DPS
169 // ODR: 7174/3587/1793/896/448/224/112/56/28 Hz
171 112.0f);
172
175 16);
176
177 // With ACC+GYR both enabled, synchronized ODR base follows
178 // gyroscope natural frequency.
180 imu.enableGyro();
181
182 // In Non-SyncSample mode, enabling FIFO (mode != BYPASS) disables DRDY.
183 // Do not enable data-ready interrupt in FIFO mode.
184
185 Serial.println("FIFO configured. Waiting for data...");
186 Serial.println("Note: FIFO mode disables DRDY; polling FIFO/STATUS is used here.");
187}
188
189void loop()
190{
191 // FIFO mode should not use DRDY to judge readiness.
192 // Poll FIFO status/content by calling readFromFifo directly.
193 uint16_t samples = imu.readFromFifo(accelFifo, MAX_FIFO_SAMPLES,
194 gyroFifo, MAX_FIFO_SAMPLES);
195
196 if (samples > 0) {
197 if (fifoWarmupDiscard > 0) {
198 fifoWarmupDiscard--;
199 return;
200 }
201
202 serialPrintFmt("Read %u samples from FIFO:\n", samples);
203
204 for (uint16_t i = 0; i < samples && i < 5; ++i) {
205 serialPrintFmt(" [%u] Accel: %7.3f %7.3f %7.3f | Gyro: %8.3f %8.3f %8.3f\n",
206 i,
207 accelFifo[i].mps2.x, accelFifo[i].mps2.y, accelFifo[i].mps2.z,
209 }
210 Serial.println();
211 }
212
213 delay(10);
214}
@license MIT License
@ FS_1000_DPS
±1000 °/s
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
QMI8658 IMU sensor driver class.
bool configGyro(GyroFullScaleRange range, float data_rate_hz, LpfMode lpf=LpfMode::MODE_0)
Configure the gyroscope with specified parameters.
bool enableAccel() override
Enable the accelerometer.
bool configFifo(FifoMode mode, FifoSamples samples=FifoSamples::SAMPLES_16, uint8_t watermark_samples=16)
Configure FIFO with specified mode.
@ STREAM
Stream mode (overwrites oldest)
bool enableGyro() override
Enable the gyroscope.
uint16_t readFromFifo(AccelerometerData *accel_data, uint16_t accel_count, GyroscopeData *gyro_data, uint16_t gyro_count) override
Read data from FIFO buffer.
bool configAccel(AccelFullScaleRange range, float data_rate_hz, LpfMode lpf=LpfMode::MODE_0)
Configure the accelerometer with specified parameters.
void setPins(int pin)
Set interrupt pin.
uint8_t i
char buf[64]
GyroscopeData gyroFifo[MAX_FIFO_SAMPLES]
AccelerometerData accelFifo[MAX_FIFO_SAMPLES]
void setupPower()
void setup()
#define IMU_IRQ
#define IMU_SCL
SensorQMI8658 imu
#define IMU_SDA
void loop()
Structure representing accelerometer data.
SensorVector mps2
Acceleration in meters per second squared (m/s²)
Structure representing gyroscope data.
SensorVector dps
Angular velocity in degrees per second (°/s)
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.