measure_task.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "bsp/gpio.h"
  2. #include "app/sox/measure.h"
  3. #include "app/sox/health.h"
  4. #include "libs/shark_task.h"
  5. #include "libs/logger.h"
  6. #include "measure_task.h"
  7. #include "state.h"
  8. static measure_value_t _measure_value;
  9. static measure_notify _current_notify;
  10. static measure_notify _voltage_notify;
  11. static measure_notify _temperature_notify;
  12. /* 测量task,用来定时测量电流,电压,温度等adc数据 */
  13. struct means_task {
  14. shark_task_t _task;
  15. u32 delay;
  16. u8 index;
  17. };
  18. static void init_current_voltage_task(void);
  19. static void init_temp_task(void);
  20. void measure_task_init(measure_notify cn, measure_notify vn, measure_notify tn){
  21. _current_notify = cn;
  22. _voltage_notify = vn;
  23. _temperature_notify = tn;
  24. measure_adc_init();
  25. init_current_voltage_task();
  26. init_temp_task();
  27. set_log_level(MOD_MEASURE, L_debug);
  28. }
  29. measure_value_t * measure_value(void){
  30. return &_measure_value;
  31. }
  32. void measure_log(void){
  33. measure_debug("Current %.4f\n", (float)_measure_value.load_current/1000.0f);
  34. for (int i = 0; i < CELLS_NUM; i++){
  35. measure_debug("Cell[%d]: %.3fv\n", i, _measure_value.cell_vol[i]/1000.0f);
  36. }
  37. measure_debug("Gain:%f, Off %f\n", get_ml5238_gain(), get_ml5238_vos());
  38. }
  39. /*
  40. * 测量电芯电压,计算出总电压, 测量总电流,放电和充电
  41. */
  42. static struct means_task _current_voltage_task;
  43. static u32 current_voltage_task_handler(void);
  44. static void init_current_voltage_task(void){
  45. _current_voltage_task._task.handler = current_voltage_task_handler;
  46. _current_voltage_task.delay = 15;
  47. _current_voltage_task.index = 0;
  48. _measure_value.load_current = get_pack_current();
  49. for (int i = 0; i < CELLS_NUM; i++){
  50. _measure_value.cell_vol[i] = get_cell_voltage(i);
  51. }
  52. shark_task_add(&_current_voltage_task._task);
  53. }
  54. static u32 current_voltage_task_handler(void){
  55. if (bms_state_get()->pack_balancing){ //if balance, do'nt sample cell voltage
  56. return _current_voltage_task.delay;
  57. }
  58. /* 测量电流 */
  59. _measure_value.load_current = get_pack_current();
  60. _current_notify();//通知bms state 有新的电流数据
  61. //用内阻对cell的电压进行补偿,充电减,放电加
  62. _measure_value.cell_vol[_current_voltage_task.index] = get_cell_voltage(_current_voltage_task.index) - bms_health()->internal_resistance[_current_voltage_task.index] * _measure_value.load_current/1000;
  63. _current_voltage_task.index = (_current_voltage_task.index + 1) % CELLS_NUM;
  64. _voltage_notify();//通知bms state 有新的电压数据
  65. return _current_voltage_task.delay;
  66. }
  67. /*
  68. * 测量4个温度
  69. */
  70. static struct means_task _temp_task;
  71. static u32 temp_task_handler(void);
  72. static void init_temp_task(void){
  73. _temp_task._task.handler = temp_task_handler;
  74. _temp_task.delay = 1 * 1000;
  75. _temp_task.index = 0;
  76. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  77. _measure_value.pack_temp[i] = get_pack_temperature(i);
  78. }
  79. shark_task_add(&_temp_task._task);
  80. }
  81. static u32 temp_task_handler(void){
  82. _measure_value.pack_temp[_temp_task.index] = get_pack_temperature(_temp_task.index);
  83. _temp_task.index = (_temp_task.index + 1) % (PACK_TEMPS_NUM);
  84. _temperature_notify();//通知bms state 有新的温度数据
  85. return _temp_task.delay;
  86. }