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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user