measure_task.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  38. /*
  39. * 测量电芯电压,计算出总电压, 测量总电流,放电和充电
  40. */
  41. static struct means_task _current_voltage_task;
  42. static u32 current_voltage_task_handler(void);
  43. static void init_current_voltage_task(void){
  44. _current_voltage_task._task.handler = current_voltage_task_handler;
  45. _current_voltage_task.delay = 30;
  46. _current_voltage_task.index = 0;
  47. _measure_value.load_current = get_pack_current();
  48. for (int i = 0; i < CELLS_NUM; i++){
  49. _measure_value.cell_vol[i] = get_cell_voltage(i);
  50. }
  51. shark_task_add(&_current_voltage_task._task);
  52. }
  53. static u32 current_voltage_task_handler(void){
  54. if (bms_state_get()->pack_balancing){ //if balance, do'nt sample cell voltage
  55. return _current_voltage_task.delay;
  56. }
  57. /* 测量电流 */
  58. _measure_value.load_current = get_pack_current();
  59. _current_notify();//通知bms state 有新的电流数据
  60. //用内阻对cell的电压进行补偿,充电减,放电加
  61. _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;
  62. _current_voltage_task.index = (_current_voltage_task.index + 1) % CELLS_NUM;
  63. _voltage_notify();//通知bms state 有新的电压数据
  64. return _current_voltage_task.delay;
  65. }
  66. /*
  67. * 测量4个温度
  68. */
  69. static struct means_task _temp_task;
  70. static u32 temp_task_handler(void);
  71. static void init_temp_task(void){
  72. _temp_task._task.handler = temp_task_handler;
  73. _temp_task.delay = 1 * 1000;
  74. _temp_task.index = 0;
  75. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  76. _measure_value.pack_temp[i] = get_pack_temperature(i);
  77. }
  78. shark_task_add(&_temp_task._task);
  79. }
  80. static u32 temp_task_handler(void){
  81. _measure_value.pack_temp[_temp_task.index] = get_pack_temperature(_temp_task.index);
  82. _temp_task.index = (_temp_task.index + 1) % (PACK_TEMPS_NUM);
  83. _temperature_notify();//通知bms state 有新的温度数据
  84. return _temp_task.delay;
  85. }