SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_advanced_features.ino
Go to the documentation of this file.
1
40#include <stdarg.h>
41#include <ImuDrv.hpp>
42#include "DevicesPins.h"
43
44// Select one interface.
45// #define USE_I2C_INTERFACE
46// #define USE_SPI_INTERFACE
47
48#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
49#define USE_I2C_INTERFACE
50#endif
51
52#if defined(USE_SPI_INTERFACE)
53
54#ifndef SPI_MOSI
55#define SPI_MOSI 35
56#endif
57
58#ifndef SPI_MISO
59#define SPI_MISO 37
60#endif
61
62#ifndef SPI_SCK
63#define SPI_SCK 36
64#endif
65
66#ifndef IMU_CS
67#define IMU_CS 34
68#endif
69
70#else
71
72#ifndef IMU_SDA
73#define IMU_SDA 17
74#endif
75
76#ifndef IMU_SCL
77#define IMU_SCL 18
78#endif
79
80#endif
81
82#ifndef IMU_IRQ
83#define IMU_IRQ 33
84#endif
85
86#ifdef ARDUINO_T_BEAM_S3_SUPREME
87#include <XPowersAXP2101.tpp>
88#endif
89
90static void serialPrintFmt(const char *fmt, ...)
91{
92 char buf[256];
93 va_list args;
94 va_start(args, fmt);
95 vsnprintf(buf, sizeof(buf), fmt, args);
96 va_end(args);
97 Serial.print(buf);
98}
99
101{
102#if defined(ARDUINO_T_BEAM_S3_SUPREME)
103 XPowersAXP2101 power;
104 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
105 power.disableALDO1();
106 power.disableALDO2();
107 delay(250);
108 power.setALDO1Voltage(3300);
109 power.enableALDO1();
110 power.setALDO2Voltage(3300);
111 power.enableALDO2();
112#endif
113}
114
116
117
118
119void setup()
120{
121 Serial.begin(115200);
122 while (!Serial);
123
124 setupPower();
125
126 Serial.println("QMI8658 Advanced Features Example");
127
128#ifdef USE_I2C_INTERFACE
129 if (!imu.begin(Wire, QMI8658_H_SLAVE_ADDRESS, IMU_SDA, IMU_SCL)) {
130 Serial.println("Failed to initialize QMI8658!");
131 while (1) {
132 delay(1000);
133 }
134 }
135#endif
136
137#ifdef USE_SPI_INTERFACE
138 // Using SPI interface
139 if (!imu.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
140 Serial.println("Failed to initialize QMI8658!");
141 while (1) {
142 delay(1000);
143 }
144 }
145#endif
146
147 Serial.println("\n=== Hardware Self-Test ===");
148 Serial.println("Running hardware self-test...");
149 if (imu.hardwareSelfTest(true, true)) {
150 Serial.println("Hardware self-test: PASSED");
151 float accel_result[3], gyro_result[3];
152 imu.getHardwareSelfTestResults(accel_result, gyro_result);
153 serialPrintFmt("Accel ST results: X=%.1f Y=%.1f Z=%.1f mg\n",
154 accel_result[0], accel_result[1], accel_result[2]);
155 serialPrintFmt("Gyro ST results: X=%.1f Y=%.1f Z=%.1f dps\n",
156 gyro_result[0], gyro_result[1], gyro_result[2]);
157 } else {
158 Serial.println("Hardware self-test: FAILED");
159 }
160
161 Serial.println("\n=== Helper Configuration Functions ===");
162 Serial.println("configMotionDetectDefault() - Auto-configure motion detection");
164 Serial.println("configTapDefault() - Auto-configure tap detection");
166 Serial.println("configPedometerDefault(125) - Auto-configure pedometer at 125Hz");
168 Serial.println("Helper functions configured successfully!\n");
169
170 Serial.println("\n=== Axis Layout Configuration ===");
171 Serial.println("Current layout: DEFAULT");
173 serialPrintFmt("Current layout value: %d\n", (int)imu.getAxisLayout());
174
175 Serial.println("\nSupported layouts:");
176 Serial.println(" 0 = DEFAULT (X=right, Y=forward, Z=up)");
177 Serial.println(" 1 = ROTATE_90 (X=forward, Y=left, Z=up)");
178 Serial.println(" 2 = ROTATE_180 (X=left, Y=backward, Z=up)");
179 Serial.println(" 3 = ROTATE_270 (X=backward, Y=right, Z=up)");
180 Serial.println(" 4 = FLIP_Z (X=right, Y=backward, Z=down)");
181 Serial.println(" 5 = FLIP_Z_90 (X=backward, Y=left, Z=down)");
182 Serial.println(" 6 = FLIP_Z_180 (X=left, Y=forward, Z=down)");
183 Serial.println(" 7 = FLIP_Z_270 (X=forward, Y=right, Z=down)");
184
185 Serial.println("\n=== Static Calibration ===");
186 Serial.println("Place sensor in stable position and keep it still!");
187 Serial.println("Static calibration will run for ~100 samples (blocking)...");
188
189 // Accelerometer: FS_2G(000), FS_4G(001), FS_8G(010), FS_16G(011)
190 // ODR: 1000, 500, 250, 125, 62.5, 31.25, 128, 21, 11, 3 Hz (6DOF: 448/224/112/56/28 Hz)
192 // Gyroscope: FS_125_DPS, FS_250_DPS, FS_500_DPS, FS_1000_DPS, FS_2000_DPS, FS_4000_DPS
193 // ODR: 7174/3587/1793/896/448/224/112/56/28 Hz
195 // When ACC and GYR are both enabled, effective ODR base
196 // is synchronized to gyroscope natural frequency.
198 imu.enableGyro();
199
202
203 uint32_t cali_start = millis();
204 uint32_t last_progress = 0;
206 if (imu.isDataReady()) {
211 }
212
213 uint32_t now = millis();
214 if (now - last_progress >= 250) {
215 Serial.print(".");
216 last_progress = now;
217 }
218
219 if (now - cali_start > 10000) {
220 Serial.println("\nStatic calibration timeout, continue with current offsets.");
221 break;
222 }
223 delay(2);
224 }
225
226 int16_t accel_offset[3], gyro_offset[3];
227 imu.getStaticCalibrationOffsets(accel_offset, gyro_offset);
228 Serial.println("\nStatic calibration done.");
229 serialPrintFmt("Static Cal Offsets - Accel: %d %d %d Gyro: %d %d %d\n",
230 accel_offset[0], accel_offset[1], accel_offset[2],
231 gyro_offset[0], gyro_offset[1], gyro_offset[2]);
232
233 Serial.println("\n=== Dynamic Gyroscope Calibration ===");
234 Serial.println("Dynamic gyro calibration enabled.");
235 Serial.println("Offsets will be updated when sensor is stationary.");
237
238 Serial.println("\n=== Data Output ===");
239 Serial.println("Accel(m/s2) | Gyro(dps) | Calibrated");
240 Serial.println("X Y Z | X Y Z | Accel Gyro");
241}
242
243void loop()
244{
245 if (imu.isDataReady()) {
250
251 serialPrintFmt("%7.3f %7.3f %7.3f | %7.3f %7.3f %7.3f | %s %s\n",
252 accel.mps2.x, accel.mps2.y, accel.mps2.z,
253 gyro.dps.x, gyro.dps.y, gyro.dps.z,
254 imu.isStaticCalibrationComplete() ? "Yes " : "No ",
255 imu.isDynamicGyroCalibrationEnabled() ? "Yes" : "No");
256
257 if (millis() % 5000 < 100 && imu.isStaticCalibrationComplete()) {
258 int16_t accel_offset[3], gyro_offset[3];
259 imu.getStaticCalibrationOffsets(accel_offset, gyro_offset);
260 serialPrintFmt("\nStatic Cal Offsets - Accel: %d %d %d Gyro: %d %d %d\n",
261 accel_offset[0], accel_offset[1], accel_offset[2],
262 gyro_offset[0], gyro_offset[1], gyro_offset[2]);
263
264 int16_t dyn_gyro_offset[3];
265 imu.getDynamicGyroCalibrationOffsets(dyn_gyro_offset);
266 serialPrintFmt("Dynamic Gyro Offsets: %d %d %d\n",
267 dyn_gyro_offset[0], dyn_gyro_offset[1], dyn_gyro_offset[2]);
268 }
269 }
270 delay(10);
271}
@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 readGyro(GyroscopeData &out) override
Read gyroscope data.
void getStaticCalibrationOffsets(int16_t accel_offset[3], int16_t gyro_offset[3])
Get static calibration offsets.
bool isDataReady(uint8_t mask=static_cast< uint8_t >(ImuBase::DataReadyMask::BOTH)) override
Check if requested IMU data is available.
bool configMotionDetectDefault(MotionType type)
Configure motion detection with default parameters.
bool enableAccel() override
Enable the accelerometer.
bool isStaticCalibrationComplete() const
Get the current static calibration status.
void enableStaticCalibration(bool enable)
Start static calibration.
bool readAccel(AccelerometerData &out) override
Read accelerometer data.
void getDynamicGyroCalibrationOffsets(int16_t gyro_offset[3])
Get dynamic gyroscope calibration offsets.
bool enableGyro() override
Enable the gyroscope.
void enableDynamicGyroCalibration(bool enable)
Enable dynamic gyroscope calibration.
Layout getAxisLayout() const
Get the current axis layout setting.
bool isDynamicGyroCalibrationEnabled() const
Check if dynamic gyro calibration is enabled.
@ ANY_MOTION
Any motion detection.
bool configAccel(AccelFullScaleRange range, float data_rate_hz, LpfMode lpf=LpfMode::MODE_0)
Configure the accelerometer with specified parameters.
void getHardwareSelfTestResults(float accel_result[3], float gyro_result[3])
Get hardware self-test results.
bool configTapDefault(TapPriority priority=TapPriority::X_GT_Y_GT_Z)
Configure tap detection with default parameters.
bool configPedometerDefault(float odr=125.0f)
Configure pedometer with default parameters.
bool hardwareSelfTest(bool includeAccel=true, bool includeGyro=true)
Perform hardware self-test on both sensors.
void setAxisLayout(Layout layout)
Set the sensor axis layout for coordinate transformation.
@ LAYOUT_DEFAULT
Default orientation: X=right, Y=forward, Z=up.
char buf[64]
#define IMU_SCL
SensorQMI8658 imu
#define IMU_SDA
IMUdata gyro[buffer_size]
IMUdata accel[buffer_size]
Structure representing accelerometer data.
Structure representing gyroscope data.