-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SensorQMI8658] Update pedometer and tap detection examples
- Loading branch information
Showing
2 changed files
with
306 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* | ||
* @license MIT License | ||
* | ||
* Copyright (c) 2022 lewis he | ||
* Copyright (c) 2024 lewis he | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
|
@@ -22,18 +22,20 @@ | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* @file QMI8658_GetDataExample.ino | ||
* @file QMI8658_PedometerExample.ino | ||
* @author Lewis He ([email protected]) | ||
* @date 2022-10-16 | ||
* @date 2024-09-26 | ||
* | ||
*/ | ||
#include <Arduino.h> | ||
#include <Wire.h> | ||
#include <SPI.h> | ||
#include "SensorQMI8658.hpp" | ||
|
||
#define USE_WIRE | ||
|
||
// #define USE_WIRE | ||
|
||
#if defined(USE_WIRE) | ||
#ifndef SENSOR_SDA | ||
#define SENSOR_SDA 17 | ||
#endif | ||
|
@@ -46,17 +48,56 @@ | |
#define SENSOR_IRQ -1 | ||
#endif | ||
|
||
#define IMU_CS 5 | ||
#else | ||
|
||
//USE SPI | ||
#ifndef SPI_MOSI | ||
#define SPI_MOSI (35) | ||
#endif | ||
|
||
#ifndef SPI_SCK | ||
#define SPI_SCK (36) | ||
#endif | ||
|
||
#ifndef SPI_MISO | ||
#define SPI_MISO (37) | ||
#endif | ||
|
||
#ifndef IMU_CS | ||
#define IMU_CS (34) | ||
#endif | ||
|
||
#ifndef IMU_INT | ||
#define IMU_INT (33) | ||
#endif | ||
|
||
#endif | ||
|
||
SensorQMI8658 qmi; | ||
|
||
bool interruptFlag = false; | ||
|
||
void setFlag(void) | ||
{ | ||
interruptFlag = true; | ||
} | ||
|
||
|
||
void pedometerEvent() | ||
{ | ||
uint32_t val = qmi.getPedometerCounter(); | ||
Serial.print("Detected Pedometer event : "); | ||
Serial.println(val); | ||
} | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial); | ||
|
||
|
||
|
||
#if IMU_INT > 0 | ||
qmi.setPins(IMU_INT); | ||
#endif | ||
|
||
#ifdef USE_WIRE | ||
//Using WIRE !! | ||
|
@@ -67,7 +108,7 @@ void setup() | |
} | ||
} | ||
#else | ||
if (!qmi.begin(IMU_CS)) { | ||
if (!qmi.begin(IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) { | ||
Serial.println("Failed to find QMI8658 - check your wiring!"); | ||
while (1) { | ||
delay(1000); | ||
|
@@ -79,122 +120,66 @@ void setup() | |
Serial.print("Device ID:"); | ||
Serial.println(qmi.getChipID(), HEX); | ||
|
||
qmi.configAccelerometer( | ||
/* | ||
* ACC_RANGE_2G | ||
* ACC_RANGE_4G | ||
* ACC_RANGE_8G | ||
* ACC_RANGE_16G | ||
* */ | ||
SensorQMI8658::ACC_RANGE_4G, | ||
/* | ||
* ACC_ODR_1000H | ||
* ACC_ODR_500Hz | ||
* ACC_ODR_250Hz | ||
* ACC_ODR_125Hz | ||
* ACC_ODR_62_5Hz | ||
* ACC_ODR_31_25Hz | ||
* ACC_ODR_LOWPOWER_128Hz | ||
* ACC_ODR_LOWPOWER_21Hz | ||
* ACC_ODR_LOWPOWER_11Hz | ||
* ACC_ODR_LOWPOWER_3H | ||
* */ | ||
SensorQMI8658::ACC_ODR_125Hz, | ||
/* | ||
* LPF_MODE_0 //2.66% of ODR | ||
* LPF_MODE_1 //3.63% of ODR | ||
* LPF_MODE_2 //5.39% of ODR | ||
* LPF_MODE_3 //13.37% of ODR | ||
* LPF_OFF // OFF Low-Pass Fitter | ||
* */ | ||
SensorQMI8658::LPF_MODE_0); | ||
|
||
|
||
|
||
|
||
qmi.configGyroscope( | ||
/* | ||
* GYR_RANGE_16DPS | ||
* GYR_RANGE_32DPS | ||
* GYR_RANGE_64DPS | ||
* GYR_RANGE_128DPS | ||
* GYR_RANGE_256DPS | ||
* GYR_RANGE_512DPS | ||
* GYR_RANGE_1024DPS | ||
* */ | ||
SensorQMI8658::GYR_RANGE_64DPS, | ||
/* | ||
* GYR_ODR_7174_4Hz | ||
* GYR_ODR_3587_2Hz | ||
* GYR_ODR_1793_6Hz | ||
* GYR_ODR_896_8Hz | ||
* GYR_ODR_448_4Hz | ||
* GYR_ODR_224_2Hz | ||
* GYR_ODR_112_1Hz | ||
* GYR_ODR_56_05Hz | ||
* GYR_ODR_28_025H | ||
* */ | ||
SensorQMI8658::GYR_ODR_112_1Hz, | ||
/* | ||
* LPF_MODE_0 //2.66% of ODR | ||
* LPF_MODE_1 //3.63% of ODR | ||
* LPF_MODE_2 //5.39% of ODR | ||
* LPF_MODE_3 //13.37% of ODR | ||
* LPF_OFF // OFF Low-Pass Fitter | ||
* */ | ||
SensorQMI8658::LPF_MODE_3); | ||
|
||
// Equipped with acceleration sensor, 2G, ORR62.5HZ | ||
qmi.configAccelerometer(SensorQMI8658::ACC_RANGE_2G, SensorQMI8658::ACC_ODR_62_5Hz); | ||
|
||
/* | ||
* If both the accelerometer and gyroscope sensors are turned on at the same time, | ||
* the output frequency will be based on the gyroscope output frequency. | ||
* The example configuration is 896.8HZ output frequency, | ||
* so the acceleration output frequency is also limited to 112.1HZ | ||
* */ | ||
qmi.enableGyroscope(); | ||
// Enable the accelerometer | ||
qmi.enableAccelerometer(); | ||
|
||
//* Indicates the count of sample batch/window for calculation | ||
uint16_t ped_sample_cnt = 50; //50 samples | ||
//* Indicates the threshold of the valid peak-to-peak detection | ||
uint16_t ped_fix_peak2peak = 200; //200mg | ||
//* Indicates the threshold of the peak detection comparing to average | ||
uint16_t ped_fix_peak = 100; //100mg | ||
//* Indicates the maximum duration (timeout window) for a step. | ||
//* Reset counting calculation if no peaks detected within this duration. | ||
uint16_t ped_time_up = 200; // 200 samples 4s | ||
//* Indicates the minimum duration for a step. | ||
//* The peaks detected within this duration (quiet time) is ignored. | ||
uint8_t ped_time_low = 20; //20 samples | ||
//* Indicates the minimum continuous steps to start the valid step counting. | ||
//* If the continuously detected steps is lower than this count and timeout,the steps will not be take into account; | ||
//* if yes, the detected steps will all be taken into account and counting is started to count every following step before timeout. | ||
//* This is useful to screen out the fake steps detected by non-step vibrations | ||
//* The timeout duration is defined by ped_time_up. | ||
uint8_t ped_time_cnt_entry = 10; //10 steps entry count | ||
//* Recommended 0 | ||
uint8_t ped_fix_precision = 0; | ||
//* The amount of steps when to update the pedometer output registers. | ||
uint8_t ped_sig_count = 4; //Every 4 valid steps is detected, update the registers once (added by 4). | ||
|
||
qmi.configPedometer(ped_sample_cnt, | ||
ped_fix_peak2peak, | ||
ped_fix_peak, | ||
ped_time_up, | ||
ped_time_low, | ||
ped_time_cnt_entry, | ||
ped_fix_precision, | ||
ped_sig_count); | ||
|
||
// Enable the step counter and enable the interrupt | ||
if (!qmi.enablePedometer(SensorQMI8658::INTERRUPT_PIN_1)) { | ||
Serial.println("Enable pedometer failed!"); | ||
while (1); | ||
} | ||
|
||
qmi.configPedometer(0x007D, 0x00CC, 0x0066, 0x00C8, 0x14, 0x0A, 0, 0x04); | ||
|
||
qmi.enablePedometer(); | ||
|
||
// Print register configuration information | ||
qmi.dumpCtrlRegister(); | ||
|
||
Serial.println("Read data now..."); | ||
// Set the step counter callback function | ||
qmi.setPedometerEventCallBack(pedometerEvent); | ||
|
||
/* | ||
* When the QMI8658 is configured as Wom, the interrupt level is arbitrary, | ||
* not absolute high or low, and it is in the jump transition state | ||
*/ | ||
attachInterrupt(IMU_INT, setFlag, CHANGE); | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
// Get IMU status | ||
uint8_t status = qmi.getStatusRegister(); | ||
Serial.printf("STATUS:0x%x BIN:", status); | ||
Serial.println(status, BIN); | ||
|
||
if (status & SensorQMI8658::EVENT_TAP_MOTION) { | ||
Serial.println("Detected TAP event"); | ||
qmi.getTapStatus(); | ||
} | ||
if (status & SensorQMI8658::EVENT_WOM_MOTION) { | ||
Serial.println("Detected Wom event"); | ||
} | ||
if (status & SensorQMI8658::EVENT_PEDOMETER_MOTION) { | ||
Serial.println("Detected Pedometer event"); | ||
uint32_t val = qmi.getPedometerCounter(); | ||
Serial.println(val); | ||
if (interruptFlag) { | ||
interruptFlag = false; | ||
qmi.update(); | ||
} | ||
if (status & SensorQMI8658::EVENT_ANY_MOTION) { | ||
Serial.println("Detected Any Motion event"); | ||
} | ||
if (status & SensorQMI8658::EVENT_NO_MOTION) { | ||
Serial.println("Detected No Motion event"); | ||
} | ||
if (status & SensorQMI8658::EVENT_SIGNIFICANT_MOTION) { | ||
Serial.println("Detected Significant Motion event"); | ||
} | ||
|
||
delay(500); | ||
} |
Oops, something went wrong.