Rdzleo 93f0e19d1d 初始化项目:精灵吊坠 RTC 语音助手 + VEML7700 石头同频匹配
ESP32-S3 吊坠设备固件,集成火山引擎 RTC 语音助手、蓝牙配网、
VEML7700 环境光传感器驱动及石头同频匹配交友功能。

VEML7700 驱动:
- 基于 ESP-IDF i2c_master API 实现,复用项目 I2cDevice 基类
- 支持 ALS + White 双通道、自动量程、Vishay 非线性校正
- 3 次采样取中位数过滤偶发异常

石头同频匹配算法(双维度):
- 维度1:光谱比值 ALS/White(石头固有光学特征,不随光照强度变化)
- 维度2:亮度等级(5级对数划分,排除极端环境差异)
- 比值阈值 15%,实测同石头姿势变化波动 1.6%~9.6%,安全余量充足

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 11:43:57 +08:00

136 lines
5.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "imu_sensor_thing.h"
#include "esp_log.h"
#include <cmath>
#include <cstring>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define TAG "ImuSensorThing"
namespace iot {
ImuSensorThing::ImuSensorThing(QMI8658A* sensor)
: Thing("ImuSensor", "姿态传感器"),
imu_sensor_(sensor),
motion_detected_(false),
motion_threshold_(1.5f) {
// 初始化数据
memset(&latest_data_, 0, sizeof(latest_data_));
// 定义属性:加速度计数据
properties_.AddNumberProperty("accel_x", "X轴加速度 (mg)", [this]() -> int {
return static_cast<int>(latest_data_.acc_x * 1000);
});
properties_.AddNumberProperty("accel_y", "Y轴加速度 (mg)", [this]() -> int {
return static_cast<int>(latest_data_.acc_y * 1000);
});
properties_.AddNumberProperty("accel_z", "Z轴加速度 (mg)", [this]() -> int {
return static_cast<int>(latest_data_.acc_z * 1000);
});
// 定义属性:陀螺仪数据
properties_.AddNumberProperty("gyro_x", "X轴角速度 (mdps)", [this]() -> int {
return static_cast<int>(latest_data_.gyro_x * 1000);
});
properties_.AddNumberProperty("gyro_y", "Y轴角速度 (mdps)", [this]() -> int {
return static_cast<int>(latest_data_.gyro_y * 1000);
});
properties_.AddNumberProperty("gyro_z", "Z轴角速度 (mdps)", [this]() -> int {
return static_cast<int>(latest_data_.gyro_z * 1000);
});
// 定义属性:运动检测状态
properties_.AddBooleanProperty("motion_detected", "是否检测到运动", [this]() -> bool {
return motion_detected_;
});
// 定义属性:传感器状态
properties_.AddBooleanProperty("sensor_available", "传感器是否可用", [this]() -> bool {
return imu_sensor_ != nullptr;
});
// 定义方法:校准传感器
methods_.AddMethod("Calibrate", "校准传感器", ParameterList(), [this](const ParameterList& parameters) {
if (imu_sensor_) {
ESP_LOGI(TAG, "开始校准IMU传感器");
imu_sensor_->StartBufferedReading(20);
imu_sensor_->StartCalibration(6000);
bool running = false;
float progress = 0.0f;
do {
imu_sensor_->GetCalibrationStatus(&running, &progress);
vTaskDelay(pdMS_TO_TICKS(200));
} while (running);
qmi8658a_calibration_t calib;
imu_sensor_->GetCalibrationData(&calib);
imu_sensor_->ApplyCalibration(&calib);
imu_sensor_->StopBufferedReading();
ESP_LOGI(TAG, "校准完成");
}
});
// 定义方法:设置运动检测阈值
methods_.AddMethod("SetMotionThreshold", "设置运动检测阈值", ParameterList({
Parameter("threshold", "运动检测阈值 (g)", kValueTypeNumber, true)
}), [this](const ParameterList& parameters) {
float threshold = static_cast<float>(parameters["threshold"].number()) / 1000.0f;
if (threshold > 0.1f && threshold < 10.0f) {
motion_threshold_ = threshold;
ESP_LOGI(TAG, "设置运动检测阈值为: %.3f g", motion_threshold_);
} else {
ESP_LOGW(TAG, "运动检测阈值超出范围 (0.1-10.0 g)");
}
});
// 定义方法:获取传感器信息
methods_.AddMethod("GetSensorInfo", "获取传感器信息", ParameterList(), [this](const ParameterList& parameters) {
if (imu_sensor_) {
ESP_LOGI(TAG, "IMU传感器: QMI8658A");
ESP_LOGI(TAG, "当前运动阈值: %.3f g", motion_threshold_);
}
});
methods_.AddMethod("DumpRegisters", "寄存器转储", ParameterList(), [this](const ParameterList& parameters) {
if (imu_sensor_) {
imu_sensor_->DumpRegisters();
}
});
methods_.AddMethod("BaselineDiagnostics", "静止基线诊断", ParameterList(), [this](const ParameterList& parameters) {
if (imu_sensor_) {
imu_sensor_->RunBaselineDiagnostics(200, 10);
}
});
}
void ImuSensorThing::UpdateData(const qmi8658a_data_t& data) {
latest_data_ = data;
// 计算加速度幅值来检测运动
float accel_magnitude = sqrt(data.acc_x * data.acc_x +
data.acc_y * data.acc_y +
data.acc_z * data.acc_z);
// 检测运动排除重力影响1g ≈ 9.8m/s²
float motion_level = fabs(accel_magnitude - 1.0f);
bool current_motion = motion_level > motion_threshold_;
if (current_motion != motion_detected_) {
motion_detected_ = current_motion;
ESP_LOGI(TAG, "运动状态变化: %s (幅值: %.3f g)",
motion_detected_ ? "检测到运动" : "静止", motion_level);
}
}
void ImuSensorThing::SetMotionDetected(bool detected) {
if (motion_detected_ != detected) {
motion_detected_ = detected;
ESP_LOGI(TAG, "运动检测状态更新: %s", detected ? "运动中" : "静止");
}
}
} // namespace iot