SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_motion_detection_deprecated.ino
Go to the documentation of this file.
1
30#include <Arduino.h>
31#include <Wire.h>
32#include <SPI.h>
33#include "SensorQMI8658.hpp"
34#ifdef ARDUINO_T_BEAM_S3_SUPREME
35#include <XPowersAXP2101.tpp> //PMU Library https://github.com/lewisxhe/XPowersLib.git
36#endif
37
38
39// #define USE_I2C //Using the I2C interface
40
41#ifdef USE_I2C
42#ifndef SENSOR_SDA
43#define SENSOR_SDA 17
44#endif
45
46#ifndef SENSOR_SCL
47#define SENSOR_SCL 18
48#endif
49
50#else /* SPI interface */
51
52#ifndef SPI_MOSI
53#define SPI_MOSI (35)
54#endif
55
56#ifndef SPI_SCK
57#define SPI_SCK (36)
58#endif
59
60#ifndef SPI_MISO
61#define SPI_MISO (37)
62#endif
63
64#ifndef IMU_CS
65#define IMU_CS 34 // IMU CS PIN
66#endif
67
68#endif /* USE_I2C*/
69
70
71#ifndef IMU_IRQ
72#define IMU_IRQ 33 // IMU INT PIN
73#endif
74
75#ifndef OLED_SDA
76#define OLED_SDA 22 // Display Wire SDA Pin
77#endif
78
79#ifndef OLED_SCL
80#define OLED_SCL 21 // Display Wire SCL Pin
81#endif
82
83
85
87{
88 // T_BEAM_S3_SUPREME The PMU voltage needs to be turned on to use the sensor
89#if defined(ARDUINO_T_BEAM_S3_SUPREME)
90 XPowersAXP2101 power;
91 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
92 power.disableALDO1();
93 power.disableALDO2();
94 delay(250);
95 power.setALDO1Voltage(3300); power.enableALDO1();
96 power.setALDO2Voltage(3300); power.enableALDO2();
97#endif
98}
99
100bool interruptFlag = false;
101
102void setFlag(void)
103{
104 interruptFlag = true;
105}
106
107void setup()
108{
109 Serial.begin(115200);
110 while (!Serial);
111
112 beginPower();
113
114 bool ret = false;
115#ifdef USE_I2C
116 ret = qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
117#else
118#if defined(SPI_MOSI) && defined(SPI_SCK) && defined(SPI_MISO)
119 ret = qmi.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK);
120#else
121 ret = qmi.begin(SPI, IMU_CS);
122#endif
123#endif
124
125 if (!ret) {
126 Serial.println("Failed to find QMI8658 - check your wiring!");
127 while (1) {
128 delay(1000);
129 }
130 }
131
132 /* Get chip id*/
133 Serial.print("Device ID:");
134 Serial.println(qmi.getChipID(), HEX);
135
136 //** The recommended output data rate for detection is higher than 500HZ
138
139 // Enable the accelerometer
141
142
143 //* Configure the motion detection axis direction
144 uint8_t modeCtrl = SensorQMI8658::ANY_MOTION_EN_X |
150
151 //* Define the slope threshold of the x-axis for arbitrary motion detection
152 float AnyMotionXThr = 100.0; // x-axis 100mg threshold
153 //* Define the slope threshold of the y-axis for arbitrary motion detection
154 float AnyMotionYThr = 100.0; // y-axis 100mg threshold
155 //* Define the slope threshold of the z-axis for arbitrary motion detection
156 float AnyMotionZThr = 1.0; // z-axis 1mg threshold
157 //* Defines the minimum number of consecutive samples (duration) that the absolute
158 //* of the slope of the enabled axis/axes data should keep higher than the threshold
159 uint8_t AnyMotionWindow = 1; // 1 samples
160
161
162 //TODO: No motion detection does not work
163 //* Defines the slope threshold of the x-axis for no motion detection
164 float NoMotionXThr = 0.1;
165 //* Defines the slope threshold of the y-axis for no motion detection
166 float NoMotionYThr = 0.1;
167 //* Defines the slope threshold of the z-axis for no motion detection
168 float NoMotionZThr = 0.1;
169
170 //* Defines the minimum number of consecutive samples (duration) that the absolute
171 //* of the slope of the enabled axis/axes data should keep lower than the threshold
172 uint8_t NoMotionWindow = 1; // 1 samples
173 //* Defines the wait window (idle time) starts from the first Any-Motion event until
174 //* starting to detecting another Any-Motion event form confirmation
175 uint16_t SigMotionWaitWindow = 1; // 1 samples
176 //* Defines the maximum duration for detecting the other Any-Motion
177 //* event to confirm Significant-Motion, starts from the first Any -Motion event
178 uint16_t SigMotionConfirmWindow = 1; // 1 samples
179
180 qmi.configMotion(modeCtrl,
181 AnyMotionXThr, AnyMotionYThr, AnyMotionZThr, AnyMotionWindow,
182 NoMotionXThr, NoMotionYThr, NoMotionZThr, NoMotionWindow,
183 SigMotionWaitWindow, SigMotionConfirmWindow);
184
185 // Enable the Motion Detection and enable the interrupt
187
188 /*
189 * When the QMI8658 is configured as Wom, the interrupt level is arbitrary,
190 * not absolute high or low, and it is in the jump transition state
191 */
192 attachInterrupt(IMU_IRQ, setFlag, CHANGE);
193}
194
195
196void loop()
197{
198 if (interruptFlag) {
199 interruptFlag = false;
200 uint8_t status = qmi.getStatusRegister();
201 Serial.print("STATUS:"); Serial.print(status);
202 Serial.print(" BIN:"); Serial.println(status, BIN);
203
205 Serial.println("Significant motion");
206 }
207 if (status & SensorQMI8658::EVENT_NO_MOTION) {
208 Serial.println("No Motion");
209 }
210 if (status & SensorQMI8658::EVENT_ANY_MOTION) {
211 Serial.println("Any Motion");
212 }
213 }
214 delay(300);
215}
#define SENSOR_SCL
#define SENSOR_SDA
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 configMotion(uint8_t modeCtrl, float AnyMotionXThr, float AnyMotionYThr, float AnyMotionZThr, uint8_t AnyMotionWindow, float NoMotionXThr, float NoMotionYThr, float NoMotionZThr, uint8_t NoMotionWindow, uint16_t SigMotionWaitWindow, uint16_t SigMotionConfirmWindow)
uint8_t getChipID() override
Get the chip ID / WHO_AM_I value.
bool configAccelerometer(AccelRange range, AccelODR odr, LpfMode lpfOdr=LPF_MODE_0)
Configure the accelerometer.
bool enableMotionDetect(IntPin pin=IntPin::PIN1)
Enable motion detection on specified pin.
int getStatusRegister()
Get the Pedometer counter.
bool enableAccelerometer()
Enable the accelerometer.