calib_model.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * calib_model.c - 电容值到油水比例的标定模块
  3. *
  4. * 支持:
  5. * - 三阶多项式拟合 根据标定点做多项式拟合(如 y = a·x³ +b·x² + c·x + d),推导出油水百分比 y 与电容 x 的函数关系
  6. * - 电容变化非线性明显
  7. * - 6~8 个点足够
  8. */
  9. #include "Callback.h"
  10. #include "calib_model.h"
  11. #include "FLASH.h"
  12. #include "main.h"
  13. CalibCoeffs_t g_coeffs;
  14. // ========================
  15. // 默认三阶多项式系数
  16. // ========================
  17. float g_coeff_a = -0.0000116595f;
  18. float g_coeff_b = 0.0096538179f;
  19. float g_coeff_c = -2.3291849577f;
  20. float g_coeff_d = 181.7567479858f;
  21. // ========================
  22. // 设置系数
  23. // ========================
  24. void Set_Calib_Coeffs(float a, float b, float c, float d)
  25. {
  26. g_coeff_a = a;
  27. g_coeff_b = b;
  28. g_coeff_c = c;
  29. g_coeff_d = d;
  30. }
  31. // ========================
  32. // 估算百分比(带限幅)
  33. // ========================
  34. float Estimate_Percentage(float cap)
  35. {
  36. float percent = g_coeff_a * cap * cap * cap
  37. + g_coeff_b * cap * cap
  38. + g_coeff_c * cap
  39. + g_coeff_d;
  40. if (percent < 0.0f) percent = 0.0f;
  41. if (percent > 100.0f) percent = 100.0f;
  42. return percent;
  43. }
  44. // 启动时读取系数
  45. void Init_CalibModel_FromFlash(void)
  46. {
  47. Load_Calib_Coeffs_FromFlash(&g_coeffs);
  48. Set_Calib_Coeffs(g_coeffs.a, g_coeffs.b, g_coeffs.c, g_coeffs.d);
  49. }
  50. // 保存时调用
  51. void Save_Current_Calib_ToFlash(void)
  52. {
  53. g_coeffs.a = g_coeff_a;
  54. g_coeffs.b = g_coeff_b;
  55. g_coeffs.c = g_coeff_c;
  56. g_coeffs.d = g_coeff_d;
  57. Save_Calib_Coeffs_ToFlash(&g_coeffs);
  58. }
  59. void pack_CalibModel_a(void) // float g_coeff_a --> uint32_t uint_coeff_a
  60. {
  61. memcpy(&uint_coeff_a, &g_coeff_a, sizeof(float)); // float --> uint32_t
  62. }
  63. void pack_CalibModel_b(void) // float g_coeff_b --> uint32_t uint_coeff_b
  64. {
  65. memcpy(&uint_coeff_b, &g_coeff_b, sizeof(float)); // float --> uint32_t
  66. }
  67. void pack_CalibModel_c(void) // float g_coeff_c --> uint32_t uint_coeff_c
  68. {
  69. memcpy(&uint_coeff_c, &g_coeff_c, sizeof(float)); // float --> uint32_t
  70. }
  71. void pack_CalibModel_d(void) // float g_coeff_d --> uint32_t uint_coeff_d
  72. {
  73. memcpy(&uint_coeff_d, &g_coeff_d, sizeof(float)); // float --> uint32_t
  74. }