Add shake emotion with persistent state and calibration

Introduces a new shake emotion animation with a shake-stop transition, including embedded Lottie animation data and a state machine for emotion transitions. Adds a non-volatile storage (NVS) driver for persistent state management, including first-boot detection, boot count, and IMU calibration storage. Updates emotion manager and related headers to support new shake emotion states, and integrates new UI components for calibration and loading animations.
This commit is contained in:
2026-01-24 20:02:04 +05:30
parent 8aeaed8a15
commit 4d68a788ad
22 changed files with 5255 additions and 93 deletions

View File

@@ -15,7 +15,6 @@
#include <cmath>
#include <cstdlib>
static const char *TAG = "shake_detector";
// ----------------------------------------------------------------------------
@@ -24,11 +23,14 @@ static const char *TAG = "shake_detector";
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;
// Continuous shake detection state
static uint32_t shakeCounter = 0;
static bool isShaking = false;
// ----------------------------------------------------------------------------
// Helper Functions
// ----------------------------------------------------------------------------
@@ -160,37 +162,38 @@ extern "C" ShakeEvent shakeDetectorUpdate(void) {
// 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);
ESP_LOGI(TAG, "ACCEL: dev=%.3f counter=%lu shaking=%d", deviation,
(unsigned long)shakeCounter, isShaking);
lastLogTime = now;
}
// Debounce check
if (now - lastTriggerTime < SHAKE_DEBOUNCE_MS) {
return SHAKE_NONE;
}
// Continuous shake detection - track consecutive readings above threshold
bool currentlyAboveThreshold = (deviation > SHAKE_ACCEL_THRESHOLD);
// Shake detection
if (deviation > SHAKE_ACCEL_THRESHOLD) {
ESP_LOGI(TAG, "SHAKE DETECTED! deviation=%.3f > threshold=%.3f", deviation,
SHAKE_ACCEL_THRESHOLD);
if (currentlyAboveThreshold) {
shakeCounter++;
lastTriggerTime = now;
// If we've hit the threshold for continuous shake, start shaking state
if (shakeCounter >= SHAKE_CONTINUOUS_THRESHOLD && !isShaking) {
isShaking = true;
ESP_LOGI(TAG, "SHAKE STARTED! (counter=%lu)",
(unsigned long)shakeCounter);
return SHAKE_ACTIVE;
}
// 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;
// If already shaking, continue reporting active
if (isShaking) {
return SHAKE_ACTIVE;
}
} else {
// Below threshold - reset counter
shakeCounter = 0;
// If we were shaking, now we've stopped
if (isShaking) {
isShaking = false;
ESP_LOGI(TAG, "SHAKE STOPPED!");
return SHAKE_STOPPED;
}
}
@@ -211,3 +214,77 @@ extern "C" bool shakeDetectorGetRawAccel(float *x, float *y, float *z) {
lastAccelZ = *z;
return true;
}
extern "C" bool shakeDetectorCalibrateBaseline(void) {
ESP_LOGI(TAG, "Starting explicit baseline calibration...");
float sumX = 0, sumY = 0, sumZ = 0;
int validSamples = 0;
// Collect samples for calibration
for (int i = 0; i < SHAKE_BASELINE_SAMPLES; i++) {
// Wait for data to be ready
int retries = 0;
while (!qmiSensor.getDataReady() && retries < 50) {
vTaskDelay(pdMS_TO_TICKS(10));
retries++;
}
if (retries >= 50) {
ESP_LOGW(TAG, "Timeout waiting for sensor data");
continue;
}
float x, y, z;
if (qmiSensor.getAccelerometer(x, y, z)) {
sumX += x;
sumY += y;
sumZ += z;
validSamples++;
ESP_LOGI(TAG, "[%d/%d] x=%+.3f y=%+.3f z=%+.3f", validSamples,
SHAKE_BASELINE_SAMPLES, x, y, z);
}
vTaskDelay(pdMS_TO_TICKS(50)); // 20Hz sampling
}
if (validSamples < SHAKE_BASELINE_SAMPLES / 2) {
ESP_LOGE(TAG, "Calibration failed: only %d valid samples", validSamples);
return false;
}
// Compute and set baseline
baselineX = sumX / validSamples;
baselineY = sumY / validSamples;
baselineZ = sumZ / validSamples;
baselineReady = true;
float magnitude = sqrtf(baselineX * baselineX + baselineY * baselineY +
baselineZ * baselineZ);
ESP_LOGI(TAG, "Calibration complete: x=%+.3f y=%+.3f z=%+.3f (mag: %.3f)",
baselineX, baselineY, baselineZ, magnitude);
return true;
}
extern "C" void shakeDetectorSetBaseline(float x, float y, float z) {
baselineX = x;
baselineY = y;
baselineZ = z;
baselineReady = true;
float magnitude = sqrtf(x * x + y * y + z * z);
ESP_LOGI(
TAG,
"Baseline set from stored values: x=%+.3f y=%+.3f z=%+.3f (mag: %.3f)", x,
y, z, magnitude);
}
extern "C" void shakeDetectorGetBaseline(float *x, float *y, float *z) {
if (x)
*x = baselineX;
if (y)
*y = baselineY;
if (z)
*z = baselineZ;
}

View File

@@ -21,10 +21,11 @@ extern "C" {
// ----------------------------------------------------------------------------
// Acceleration deviation threshold to detect shake (in G)
#define SHAKE_ACCEL_THRESHOLD 0.5f
// Set above 1G to account for gravity and require actual vigorous shaking
#define SHAKE_ACCEL_THRESHOLD 1.3f
// Debounce time between shake detections (milliseconds)
#define SHAKE_DEBOUNCE_MS 2000
// Number of consecutive readings above threshold to confirm shake
#define SHAKE_CONTINUOUS_THRESHOLD 10
// Number of samples to average for baseline calibration
#define SHAKE_BASELINE_SAMPLES 10
@@ -34,13 +35,12 @@ extern "C" {
// ----------------------------------------------------------------------------
/**
* @brief Shake event types with associated emotions
* @brief Shake event types
*/
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
SHAKE_NONE = 0, // No shake detected
SHAKE_ACTIVE, // Device is currently being shaken
SHAKE_STOPPED // Shaking just stopped
} ShakeEvent;
// ----------------------------------------------------------------------------
@@ -79,6 +79,36 @@ ShakeEvent shakeDetectorUpdate(void);
*/
bool shakeDetectorGetRawAccel(float *x, float *y, float *z);
/**
* @brief Perform immediate baseline calibration
*
* Collects multiple samples and computes the baseline average.
* Should be called when device is on a stable, flat surface.
*
* @return true on successful calibration, false on failure
*/
bool shakeDetectorCalibrateBaseline(void);
/**
* @brief Set baseline values directly
*
* Used to restore calibration from NVS storage.
*
* @param x X-axis baseline value
* @param y Y-axis baseline value
* @param z Z-axis baseline value
*/
void shakeDetectorSetBaseline(float x, float y, float z);
/**
* @brief Get current baseline values
*
* @param x Pointer to store X-axis baseline
* @param y Pointer to store Y-axis baseline
* @param z Pointer to store Z-axis baseline
*/
void shakeDetectorGetBaseline(float *x, float *y, float *z);
#ifdef __cplusplus
}
#endif