SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_no_motion.ino
Go to the documentation of this file.
1
45#include <stdarg.h>
46#include <ImuDrv.hpp>
47#include "DevicesPins.h"
48
49// Select one interface.
50// #define USE_I2C_INTERFACE
51// #define USE_SPI_INTERFACE
52
53#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
54#define USE_I2C_INTERFACE
55#endif
56
57#if defined(USE_SPI_INTERFACE)
58#ifndef SPI_MOSI
59#define SPI_MOSI 35
60#endif
61#ifndef SPI_MISO
62#define SPI_MISO 37
63#endif
64#ifndef SPI_SCK
65#define SPI_SCK 36
66#endif
67#ifndef IMU_CS
68#define IMU_CS 34
69#endif
70#else
71#ifndef IMU_SDA
72#define IMU_SDA 17
73#endif
74#ifndef IMU_SCL
75#define IMU_SCL 18
76#endif
77#endif
78
79#ifndef IMU_IRQ
80#define IMU_IRQ 33
81#endif
82
83#ifdef ARDUINO_T_BEAM_S3_SUPREME
84#include <XPowersAXP2101.tpp>
85#endif
86
87static void serialPrintFmt(const char *fmt, ...)
88{
89 char buf[256];
90 va_list args;
91 va_start(args, fmt);
92 vsnprintf(buf, sizeof(buf), fmt, args);
93 va_end(args);
94 Serial.print(buf);
95}
96
98{
99#if defined(ARDUINO_T_BEAM_S3_SUPREME)
100 XPowersAXP2101 power;
101 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
102 power.disableALDO1();
103 power.disableALDO2();
104 delay(250);
105 power.setALDO1Voltage(3300);
106 power.enableALDO1();
107 power.setALDO2Voltage(3300);
108 power.enableALDO2();
109#endif
110}
111
113volatile bool irqTriggered = false;
114
115constexpr float kAccelOdrHz = 224.0f;
116// Any-motion companion parameters (used to leave no-motion state when movement starts):
117// - thresholdX/Y/Z (mg): larger value means less sensitive to movement.
118// - window (samples): consecutive above-threshold samples required.
119constexpr float kAnyThresholdMg = 62.5f;
120constexpr uint8_t kAnyWindow = 3;
121// No-motion parameters:
122// - thresholdX/Y/Z (mg): acceleration must stay below this value.
123// - window (samples): consecutive below-threshold samples required to assert no-motion.
124constexpr float kNoThresholdMg = 15.625f;
125constexpr uint8_t kNoWindow = 30;
126// Fallback poll period when no IRQ arrives (ms).
127constexpr uint32_t kPollIntervalMs = 50;
128
129void setup()
130{
131 Serial.begin(115200);
132 while (!Serial);
133
134 setupPower();
135 Serial.println("QMI8658 No-Motion Example");
136
137#ifdef USE_I2C_INTERFACE
138 if (!imu.begin(Wire, QMI8658_H_SLAVE_ADDRESS, IMU_SDA, IMU_SCL)) {
139 Serial.println("Failed to initialize QMI8658!");
140 while (1) delay(1000);
141 }
142#endif
143
144#ifdef USE_SPI_INTERFACE
145 if (!imu.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
146 Serial.println("Failed to initialize QMI8658!");
147 while (1) delay(1000);
148 }
149#endif
150
152 // Accelerometer: FS_2G(000), FS_4G(001), FS_8G(010), FS_16G(011)
153 // ODR: 1000, 500, 250, 125, 62.5, 31.25, 128, 21, 11, 3 Hz (6DOF: 448/224/112/56/28 Hz)
156
157 // Keep Any-Motion engine configured so movement can drive state away from No-Motion.
158 // We only report No-Motion in this example.
161 kAnyWindow);
164 kNoWindow);
165 bool en = imu.enableMotionDetect(SensorQMI8658::IntPin::PIN1);
166
167 int irq = digitalPinToInterrupt(IMU_IRQ);
168 if (irq >= 0) {
169 attachInterrupt(irq, +[]() {
170 irqTriggered = true;
171 }, CHANGE);
172 }
173
174 serialPrintFmt("No-Motion config any=%s no=%s enable=%s\n",
175 cfgAny ? "OK" : "FAIL",
176 cfg ? "OK" : "FAIL",
177 en ? "OK" : "FAIL");
178 Serial.println("Keep sensor still to trigger No-Motion.");
179
180
182 Serial.println("[EVENT] No motion detected!");
183 });
184}
185
186void loop()
187{
188 static uint32_t lastPollMs = 0;
189
190 bool doUpdate = false;
191 if (irqTriggered) {
192 irqTriggered = false;
193 doUpdate = true;
194 }
195
196 uint32_t now = millis();
197 if (!doUpdate && (now - lastPollMs >= kPollIntervalMs)) {
198 doUpdate = true;
199 }
200
201 if (doUpdate) {
202 lastPollMs = now;
203 imu.update();
204 }
205 delay(10);
206}
@license MIT License
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 enableAccel() override
Enable the accelerometer.
void setNoMotionCallback(MotionCallback callback)
Set callback for no-motion event.
bool enableMotionDetect(IntPin pin=IntPin::PIN1)
Enable motion detection on specified pin.
@ ANY_MOTION
Any motion detection.
@ NO_MOTION
No motion detection.
bool configAccel(AccelFullScaleRange range, float data_rate_hz, LpfMode lpf=LpfMode::MODE_0)
Configure the accelerometer with specified parameters.
uint16_t update()
Update sensor status and process events.
void setPins(int pin)
Set interrupt pin.
bool configMotionDetect(MotionType type, float threshold_x, float threshold_y, float threshold_z, uint8_t duration)
Configure motion detection parameters.
char buf[64]
constexpr uint8_t kNoWindow
constexpr float kAccelOdrHz
void setupPower()
void setup()
constexpr float kNoThresholdMg
#define IMU_IRQ
#define IMU_SCL
constexpr uint8_t kAnyWindow
volatile bool irqTriggered
SensorQMI8658 imu
constexpr uint32_t kPollIntervalMs
#define IMU_SDA
void loop()
constexpr float kAnyThresholdMg
uint32_t lastPollMs