1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- /**
- * calib_model.c - 电容值到油水比例的标定模块
- *
- * 支持:
- * - 三阶多项式拟合 根据标定点做多项式拟合(如 y = a·x³ +b·x² + c·x + d),推导出油水百分比 y 与电容 x 的函数关系
- * - 电容变化非线性明显
- * - 6~8 个点足够
- */
- #include "Callback.h"
- #include "calib_model.h"
- #include "FLASH.h"
- #include "main.h"
- CalibCoeffs_t g_coeffs;
- // ========================
- // 默认三阶多项式系数
- // ========================
- float g_coeff_a = -0.0000116595f;
- float g_coeff_b = 0.0096538179f;
- float g_coeff_c = -2.3291849577f;
- float g_coeff_d = 181.7567479858f;
- // ========================
- // 设置系数
- // ========================
- void Set_Calib_Coeffs(float a, float b, float c, float d)
- {
- g_coeff_a = a;
- g_coeff_b = b;
- g_coeff_c = c;
- g_coeff_d = d;
- }
- // ========================
- // 估算百分比(带限幅)
- // ========================
- float Estimate_Percentage(float cap)
- {
- float percent = g_coeff_a * cap * cap * cap
- + g_coeff_b * cap * cap
- + g_coeff_c * cap
- + g_coeff_d;
- if (percent < 0.0f) percent = 0.0f;
- if (percent > 100.0f) percent = 100.0f;
- return percent;
- }
- // 启动时读取系数
- void Init_CalibModel_FromFlash(void)
- {
- Load_Calib_Coeffs_FromFlash(&g_coeffs);
- Set_Calib_Coeffs(g_coeffs.a, g_coeffs.b, g_coeffs.c, g_coeffs.d);
-
- }
- // 保存时调用
- void Save_Current_Calib_ToFlash(void)
- {
- g_coeffs.a = g_coeff_a;
- g_coeffs.b = g_coeff_b;
- g_coeffs.c = g_coeff_c;
- g_coeffs.d = g_coeff_d;
- Save_Calib_Coeffs_ToFlash(&g_coeffs);
- }
- void pack_CalibModel_a(void) // float g_coeff_a --> uint32_t uint_coeff_a
- {
- memcpy(&uint_coeff_a, &g_coeff_a, sizeof(float)); // float --> uint32_t
- }
- void pack_CalibModel_b(void) // float g_coeff_b --> uint32_t uint_coeff_b
- {
- memcpy(&uint_coeff_b, &g_coeff_b, sizeof(float)); // float --> uint32_t
- }
- void pack_CalibModel_c(void) // float g_coeff_c --> uint32_t uint_coeff_c
- {
- memcpy(&uint_coeff_c, &g_coeff_c, sizeof(float)); // float --> uint32_t
- }
- void pack_CalibModel_d(void) // float g_coeff_d --> uint32_t uint_coeff_d
- {
- memcpy(&uint_coeff_d, &g_coeff_d, sizeof(float)); // float --> uint32_t
- }
|