界面实现,然后称重还有问题,灯改为手动控制

This commit is contained in:
Wang Beihong
2026-04-18 02:03:01 +08:00
parent 990abc210c
commit 98ea274985
22 changed files with 39154 additions and 73 deletions

View File

@@ -0,0 +1,69 @@
#include "scale_controller.h"
#include "esp_log.h"
static const char *TAG = "scale_controller";
#define FILTER_SIZE 10 // 均值滤波窗口大小
static int32_t s_filter_buf[FILTER_SIZE] = {0};
static int s_filter_idx = 0;
static bool s_buf_filled = false;
static int32_t s_tare_offset = 0;
static float s_calib_factor = 1.0f;
// 重置滤波器缓冲区
void scale_controller_filter_reset(void)
{
for (int i = 0; i < FILTER_SIZE; i++) s_filter_buf[i] = 0;
s_filter_idx = 0;
s_buf_filled = false;
}
// 初始化函数,接受配置参数
void scale_controller_init(const scale_config_t *config)
{
if (config != NULL) {
s_tare_offset = config->tare_offset; // 设置初始皮重偏移
s_calib_factor = config->calibration_factor; // 设置校准系数
scale_controller_filter_reset();
ESP_LOGI(TAG, "Initialized with offset: %ld, factor: %f", (long)s_tare_offset, s_calib_factor);
}
}
// 更新皮重偏移(去皮)
void scale_controller_set_tare(int32_t raw_avg)
{
s_tare_offset = raw_avg; // 更新皮重偏移为当前测量的平均原始值
ESP_LOGI(TAG, "Tare updated to offset: %ld", (long)s_tare_offset);
}
// 原始值转换为重量 (带 10 点滑动均值滤波)
float scale_controller_raw_to_weight(int16_t raw_val)
{
// 1. 更新滑动滤波缓冲区
s_filter_buf[s_filter_idx] = (int32_t)raw_val;
s_filter_idx = (s_filter_idx + 1) % FILTER_SIZE;
if (s_filter_idx == 0) s_buf_filled = true;
// 2. 计算缓冲区内的平均值
int32_t sum = 0;
int count = s_buf_filled ? FILTER_SIZE : s_filter_idx;
if (count == 0) return 0.0f;
for (int i = 0; i < count; i++) {
sum += s_filter_buf[i];
}
float avg_raw = (float)sum / count;
// 3. 应用去皮和校准系数
float diff = avg_raw - (float)s_tare_offset;
return diff * s_calib_factor;
}
// 设置校准系数 (外部调用以调整称重精度)
void scale_controller_set_factor(float factor)
{
s_calib_factor = factor; // 更新校准系数
ESP_LOGI(TAG, "Calibration factor updated: %f", factor);
}