measure_task.c 3.2 KB

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