soc.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "soc.h"
  2. #include "app/sox/measure.h"
  3. #include "app/sox/measure_task.h"
  4. #include "health.h"
  5. #include "state.h"
  6. static soc_t _soc;
  7. static uint8_t charing = 0;
  8. static u64 time_ms = 0;
  9. static float _charger_coefficient = 1.0f;
  10. static float _discharger_coefficient = 1.0f;
  11. void soc_init(void){
  12. time_ms = shark_get_mseconds();
  13. _soc.coulomb_min = 0;
  14. _soc.coulomb_max = 30.0f * 3600.0f; //30HA,这个值最总需要health模块给
  15. }
  16. static __inline__ float _delta_time(void){
  17. u32 delta = shark_get_mseconds() - time_ms;
  18. time_ms = shark_get_mseconds();
  19. return (float)delta / (1000.0f); //秒
  20. }
  21. void soc_update(void){
  22. if (!charing && bms_state_get()->charging){
  23. _soc.charger_coulomb = 0;//clear charing
  24. charing = 1;
  25. }else if (charing && !bms_state_get()->charging){
  26. charing = 0;
  27. _soc.dischrger_coulomb = 0; //clear discharger
  28. }
  29. float current = measure_value()->load_current / 1000.0f; //A
  30. float delta_q = current * _delta_time();
  31. if (charing){
  32. delta_q = delta_q * _charger_coefficient;
  33. _soc.charger_coulomb += abs(delta_q);
  34. }else {
  35. delta_q = delta_q * _discharger_coefficient;
  36. _soc.dischrger_coulomb += abs(delta_q); //转为正数
  37. }
  38. _soc.coulomb_now += delta_q; //充电加, 放电减
  39. _soc.capacity = (_soc.coulomb_now - _soc.coulomb_min)/(_soc.coulomb_max - _soc.coulomb_min) * 100;
  40. }
  41. soc_t *get_soc(void){
  42. return &_soc;
  43. }