Refactor project structure and update display logic
Reorganized source files into new directories for config, drivers, sensors, emotion engine, and UI. Removed legacy assets and code related to previous eye animation implementation. Added new display and touch drivers, emotion manager, shake detector, and UI components. Updated CMake project name to 'irobot_display'.
This commit is contained in:
213
main/sensors/shake_detector.cpp
Normal file
213
main/sensors/shake_detector.cpp
Normal file
@@ -0,0 +1,213 @@
|
||||
/**
|
||||
* @file shake_detector.cpp
|
||||
* @brief Shake detection implementation using QMI8658 IMU sensor
|
||||
*
|
||||
* This module implements shake detection using the SensorLib QMI8658 driver.
|
||||
* It performs baseline calibration at startup and detects acceleration
|
||||
* deviations that indicate device movement or shaking.
|
||||
*/
|
||||
|
||||
#include "sensors/shake_detector.h"
|
||||
#include "SensorQMI8658.hpp"
|
||||
#include "config/hardware_config.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
static const char *TAG = "shake_detector";
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Private State
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static SensorQMI8658 qmiSensor;
|
||||
|
||||
static uint32_t lastTriggerTime = 0;
|
||||
static float lastAccelX = 0, lastAccelY = 0, lastAccelZ = 0;
|
||||
static float baselineX = 0, baselineY = 0, baselineZ = 0;
|
||||
static bool baselineReady = false;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Helper Functions
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static uint32_t getMillis() { return (uint32_t)(esp_timer_get_time() / 1000); }
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Public API Implementation
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
extern "C" bool shakeDetectorInit(void) {
|
||||
ESP_LOGI(TAG, "Initializing shake detector (QMI8658)");
|
||||
|
||||
// Initialize sensor (I2C driver should already be installed by display
|
||||
// driver)
|
||||
if (!qmiSensor.begin(QMI8658_I2C_PORT, QMI8658_I2C_ADDR, QMI8658_PIN_SDA,
|
||||
QMI8658_PIN_SCL)) {
|
||||
ESP_LOGE(TAG, "Failed to initialize QMI8658");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t chipId = qmiSensor.getChipID();
|
||||
ESP_LOGI(TAG, "Sensor initialized. Chip ID: 0x%02x", chipId);
|
||||
|
||||
// Configure accelerometer (4G range, 1000Hz ODR)
|
||||
ESP_LOGI(TAG, "Configuring accelerometer...");
|
||||
int retAccel = qmiSensor.configAccelerometer(SensorQMI8658::ACC_RANGE_4G,
|
||||
SensorQMI8658::ACC_ODR_1000Hz,
|
||||
SensorQMI8658::LPF_MODE_0, true);
|
||||
if (retAccel != 0) {
|
||||
ESP_LOGE(TAG, "Accelerometer config failed: %d", retAccel);
|
||||
return false;
|
||||
}
|
||||
ESP_LOGI(TAG, "Accelerometer configured");
|
||||
|
||||
// Configure gyroscope (64DPS range, 896.8Hz ODR)
|
||||
ESP_LOGI(TAG, "Configuring gyroscope...");
|
||||
int retGyro = qmiSensor.configGyroscope(SensorQMI8658::GYR_RANGE_64DPS,
|
||||
SensorQMI8658::GYR_ODR_896_8Hz,
|
||||
SensorQMI8658::LPF_MODE_3, true);
|
||||
if (retGyro != 0) {
|
||||
ESP_LOGW(TAG, "Gyroscope config failed: %d (continuing)", retGyro);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Gyroscope configured");
|
||||
}
|
||||
|
||||
// Enable sensors
|
||||
qmiSensor.enableGyroscope();
|
||||
qmiSensor.enableAccelerometer();
|
||||
ESP_LOGI(TAG, "Sensors enabled");
|
||||
|
||||
// Verify sensor is responding
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
if (qmiSensor.getDataReady()) {
|
||||
float testX, testY, testZ;
|
||||
if (qmiSensor.getAccelerometer(testX, testY, testZ)) {
|
||||
ESP_LOGI(TAG, "First reading: x=%.3f y=%.3f z=%.3f", testX, testY, testZ);
|
||||
}
|
||||
}
|
||||
|
||||
baselineReady = false;
|
||||
ESP_LOGI(TAG, "Shake detector ready. Calibrating baseline...");
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" ShakeEvent shakeDetectorUpdate(void) {
|
||||
uint32_t now = getMillis();
|
||||
|
||||
if (!qmiSensor.getDataReady()) {
|
||||
static uint32_t lastNotReadyLog = 0;
|
||||
if (now - lastNotReadyLog > 3000) {
|
||||
ESP_LOGW(TAG, "Sensor data not ready");
|
||||
lastNotReadyLog = now;
|
||||
}
|
||||
return SHAKE_NONE;
|
||||
}
|
||||
|
||||
float x, y, z;
|
||||
if (!qmiSensor.getAccelerometer(x, y, z)) {
|
||||
static uint32_t lastReadFailLog = 0;
|
||||
if (now - lastReadFailLog > 3000) {
|
||||
ESP_LOGW(TAG, "Failed to read accelerometer");
|
||||
lastReadFailLog = now;
|
||||
}
|
||||
return SHAKE_NONE;
|
||||
}
|
||||
|
||||
lastAccelX = x;
|
||||
lastAccelY = y;
|
||||
lastAccelZ = z;
|
||||
|
||||
// Baseline calibration phase
|
||||
if (!baselineReady) {
|
||||
static uint32_t baselineCount = 0;
|
||||
static float baselineSumX = 0, baselineSumY = 0, baselineSumZ = 0;
|
||||
|
||||
baselineSumX += x;
|
||||
baselineSumY += y;
|
||||
baselineSumZ += z;
|
||||
baselineCount++;
|
||||
|
||||
ESP_LOGI(TAG, "[%2lu/%d] x=%+.3f y=%+.3f z=%+.3f",
|
||||
(unsigned long)baselineCount, SHAKE_BASELINE_SAMPLES, x, y, z);
|
||||
|
||||
if (baselineCount >= SHAKE_BASELINE_SAMPLES) {
|
||||
baselineX = baselineSumX / SHAKE_BASELINE_SAMPLES;
|
||||
baselineY = baselineSumY / SHAKE_BASELINE_SAMPLES;
|
||||
baselineZ = baselineSumZ / SHAKE_BASELINE_SAMPLES;
|
||||
baselineReady = true;
|
||||
|
||||
// Reset for potential future recalibration
|
||||
baselineCount = 0;
|
||||
baselineSumX = baselineSumY = baselineSumZ = 0;
|
||||
|
||||
float magnitude = sqrtf(baselineX * baselineX + baselineY * baselineY +
|
||||
baselineZ * baselineZ);
|
||||
ESP_LOGI(TAG, "Baseline calibrated: x=%+.3f y=%+.3f z=%+.3f (mag: %.3f)",
|
||||
baselineX, baselineY, baselineZ, magnitude);
|
||||
}
|
||||
return SHAKE_NONE;
|
||||
}
|
||||
|
||||
// Calculate deviation from baseline
|
||||
float deltaX = x - baselineX;
|
||||
float deltaY = y - baselineY;
|
||||
float deltaZ = z - baselineZ;
|
||||
float deviation = sqrtf(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
|
||||
|
||||
// Periodic logging for monitoring
|
||||
static uint32_t lastLogTime = 0;
|
||||
if (now - lastLogTime > 1000) {
|
||||
ESP_LOGI(TAG, "ACCEL: x=%+.3f y=%+.3f z=%+.3f | deviation=%.3f", x, y, z,
|
||||
deviation);
|
||||
lastLogTime = now;
|
||||
}
|
||||
|
||||
// Debounce check
|
||||
if (now - lastTriggerTime < SHAKE_DEBOUNCE_MS) {
|
||||
return SHAKE_NONE;
|
||||
}
|
||||
|
||||
// Shake detection
|
||||
if (deviation > SHAKE_ACCEL_THRESHOLD) {
|
||||
ESP_LOGI(TAG, "SHAKE DETECTED! deviation=%.3f > threshold=%.3f", deviation,
|
||||
SHAKE_ACCEL_THRESHOLD);
|
||||
|
||||
lastTriggerTime = now;
|
||||
|
||||
// Random emotion selection
|
||||
int r = rand() % 3;
|
||||
switch (r) {
|
||||
case 0:
|
||||
ESP_LOGI(TAG, "Emotion: SURPRISED");
|
||||
return SHAKE_SURPRISED;
|
||||
case 1:
|
||||
ESP_LOGI(TAG, "Emotion: SAD");
|
||||
return SHAKE_SAD;
|
||||
case 2:
|
||||
ESP_LOGI(TAG, "Emotion: ANGRY");
|
||||
return SHAKE_ANGRY;
|
||||
default:
|
||||
return SHAKE_SURPRISED;
|
||||
}
|
||||
}
|
||||
|
||||
return SHAKE_NONE;
|
||||
}
|
||||
|
||||
extern "C" bool shakeDetectorGetRawAccel(float *x, float *y, float *z) {
|
||||
if (!qmiSensor.getDataReady()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!qmiSensor.getAccelerometer(*x, *y, *z)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
lastAccelX = *x;
|
||||
lastAccelY = *y;
|
||||
lastAccelZ = *z;
|
||||
return true;
|
||||
}
|
||||
86
main/sensors/shake_detector.h
Normal file
86
main/sensors/shake_detector.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* @file shake_detector.h
|
||||
* @brief Shake detection interface using QMI8658 IMU sensor
|
||||
*
|
||||
* This module provides motion detection capabilities using the QMI8658
|
||||
* accelerometer/gyroscope. It detects shake gestures and can trigger
|
||||
* emotional responses in the display system.
|
||||
*/
|
||||
|
||||
#ifndef SHAKE_DETECTOR_H
|
||||
#define SHAKE_DETECTOR_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Configuration
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Acceleration deviation threshold to detect shake (in G)
|
||||
#define SHAKE_ACCEL_THRESHOLD 0.5f
|
||||
|
||||
// Debounce time between shake detections (milliseconds)
|
||||
#define SHAKE_DEBOUNCE_MS 2000
|
||||
|
||||
// Number of samples to average for baseline calibration
|
||||
#define SHAKE_BASELINE_SAMPLES 10
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Type Definitions
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Shake event types with associated emotions
|
||||
*/
|
||||
typedef enum {
|
||||
SHAKE_NONE = 0, // No shake detected
|
||||
SHAKE_SURPRISED, // Quick shake - surprised emotion
|
||||
SHAKE_SAD, // Shake detected - sad emotion
|
||||
SHAKE_ANGRY // Shake detected - angry emotion
|
||||
} ShakeEvent;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Public API
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Initialize the shake detector and IMU sensor
|
||||
*
|
||||
* Configures the QMI8658 accelerometer and gyroscope for shake detection.
|
||||
* The I2C bus must be initialized before calling this function.
|
||||
*
|
||||
* @return true on successful initialization, false on failure
|
||||
*/
|
||||
bool shakeDetectorInit(void);
|
||||
|
||||
/**
|
||||
* @brief Poll the shake detector for events
|
||||
*
|
||||
* Should be called periodically (e.g., every 50ms) to check for shakes.
|
||||
* Performs baseline calibration on first several calls.
|
||||
*
|
||||
* @return The detected shake event, or SHAKE_NONE if no shake
|
||||
*/
|
||||
ShakeEvent shakeDetectorUpdate(void);
|
||||
|
||||
/**
|
||||
* @brief Get raw accelerometer readings
|
||||
*
|
||||
* Retrieves the latest accelerometer values in G units.
|
||||
*
|
||||
* @param x Pointer to store X-axis acceleration
|
||||
* @param y Pointer to store Y-axis acceleration
|
||||
* @param z Pointer to store Z-axis acceleration
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool shakeDetectorGetRawAccel(float *x, float *y, float *z);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // SHAKE_DETECTOR_H
|
||||
Reference in New Issue
Block a user