state.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "bsp/gpio.h"
  2. #include "bsp/ml5238.h"
  3. #include "app/sox/measure.h"
  4. #include "app/sox/measure_task.h"
  5. #include "libs/shark_task.h"
  6. #include "health.h"
  7. #include "state.h"
  8. #include "iostate.h"
  9. static bms_state_t _bms_state;
  10. static void _current_notify(void);
  11. static void _voltage_notify(void);
  12. static void _temperature_notify(void);
  13. void bms_state_init(void){
  14. measure_task_init(_current_notify, _voltage_notify, _temperature_notify);
  15. io_state_init();
  16. health_init();
  17. }
  18. bms_state_t *bms_state_get(void){
  19. return &_bms_state;
  20. }
  21. static debounce_t _charging_detect = {
  22. .count = 0,
  23. .max_count = 10
  24. };
  25. static void check_charging(){
  26. if (bms_health()->charger_over_current || bms_health()->load_current_short) {
  27. ml5238_enable_discharger_mosfet(0);
  28. ml5238_enable_charger_mosfet(0); //disable charger mosfet
  29. _bms_state.charging = 0;
  30. return;
  31. }
  32. if (!_bms_state.charging) {
  33. if (measure_value()->load_current >= MIN_START_CHARGER_CURRENT) {
  34. _charging_detect.count ++;
  35. }else {
  36. _charging_detect.count = 0;
  37. }
  38. if (debounce_reach(&_charging_detect)){
  39. _bms_state.charging = 1;
  40. _bms_state.discharging = 0;
  41. _charging_detect.count = 0;
  42. }
  43. }else {
  44. if (measure_value()->load_current <= MIN_START_LOADING_CURRENT) {
  45. _charging_detect.count ++;
  46. }else {
  47. _charging_detect.count = 0;
  48. }
  49. if (debounce_reach(&_charging_detect)){
  50. _bms_state.charging = 0;
  51. _bms_state.discharging = 1;
  52. _charging_detect.count = 0;
  53. }
  54. }
  55. }
  56. static void _current_notify(void){
  57. check_current_state(); //check health of current
  58. check_charging();
  59. }
  60. /* 充电电流小于一定值,认为充满的时候,需要检查电芯的电压,如果发现有电芯电压过低,需要开启被动均衡 */
  61. static void check_cell_balance(void){
  62. }
  63. static void _voltage_notify(void){
  64. uint16_t voltage = 0;
  65. for (int i = 0; i < CELLS_NUM; i++){
  66. voltage += measure_value()->cell_vol[i];
  67. }
  68. _bms_state.pack_voltage = voltage;
  69. check_voltage_state(); //check health of cell voltage
  70. check_cell_balance();
  71. }
  72. static void _temperature_notify(void){
  73. check_temp_state(); //check health of cell/pcb temperature
  74. }