#include "soc.h" #include "app/sox/measure.h" #include "app/sox/measure_task.h" #include "health.h" #include "state.h" static soc_t _soc; static uint8_t charing = 0; static u64 time_ms = 0; static float _charger_coefficient = 1.0f; static float _discharger_coefficient = 1.0f; void soc_init(void){ time_ms = shark_get_mseconds(); _soc.coulomb_min = 0; _soc.coulomb_max = 30.0f * 3600.0f; //30HA,这个值最总需要health模块给 } static __inline__ float _delta_time(void){ u32 delta = shark_get_mseconds() - time_ms; time_ms = shark_get_mseconds(); return (float)delta / (1000.0f); //秒 } void soc_update(void){ if (!charing && bms_state_get()->charging){ _soc.charger_coulomb = 0;//clear charing charing = 1; }else if (charing && !bms_state_get()->charging){ charing = 0; _soc.dischrger_coulomb = 0; //clear discharger } float current = measure_value()->load_current / 1000.0f; //A float delta_q = current * _delta_time(); if (charing){ delta_q = delta_q * _charger_coefficient; _soc.charger_coulomb += abs(delta_q); }else { delta_q = delta_q * _discharger_coefficient; _soc.dischrger_coulomb += abs(delta_q); //转为正数 } _soc.coulomb_now += delta_q; //充电加, 放电减 _soc.capacity = (_soc.coulomb_now - _soc.coulomb_min)/(_soc.coulomb_max - _soc.coulomb_min) * 100; } soc_t *get_soc(void){ return &_soc; }