health.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #include "bsp/gpio.h"
  2. #include "bsp/ml5238.h"
  3. #include "libs/logger.h"
  4. #include "state.h"
  5. #include "iostate.h"
  6. #include "measure.h"
  7. #include "measure_task.h"
  8. #include "health.h"
  9. #include "Least_Square.h"
  10. #if 0
  11. #define MIN_VOLTAGE_FOR_DISCHARGER (2.2f * CELLS_NUM * 1000) //允许能放电的最小电压
  12. #define MIN_VOLTAGE_FOR_RECOVERY_DISCHARGER (2.3f * CELLS_NUM * 1000) //恢复放电的最小电压
  13. #define MIN_VOLTAGE_FOR_POWER_DOWN (2.1f * CELLS_NUM* 1000)
  14. #define SIGLE_CELL_LOWER_DISCHARGER_VOLTAGE (1820) //最小允许的电芯放电电压 1.8v, 考虑到采样的误差取 1.82
  15. #endif
  16. static int8_t charger_normal_low_temp[PACK_TEMPS_NUM] = {0,0,0,-5}; //正常的充电最低温度
  17. static int8_t charger_normal_high_temp[PACK_TEMPS_NUM] = {50,50,50,55}; //正常的充电最高温度
  18. static int8_t charger_lower_low_temp[PACK_TEMPS_NUM] = {-1,-1,-1,-6}; //需要停止充电的最低温度
  19. static int8_t charger_higher_high_temp[PACK_TEMPS_NUM] = {55,55,55,60}; //需要停止充电的最高温度
  20. static int8_t discharger_normal_low_temp[PACK_TEMPS_NUM] = {-20,-20,-20,-25};//正常的放电最低温度
  21. static int8_t discharger_normal_high_temp[PACK_TEMPS_NUM] = {50,50,50,55};//正常的放电最高温度
  22. static int8_t discharger_lower_low_temp[PACK_TEMPS_NUM] = {-25,-25,-25,-30}; //需要停止放电的最低温度
  23. static int8_t discharger_higher_high_temp[PACK_TEMPS_NUM] = {55,55,55,60};//需要停止放电的最高温度
  24. static int8_t work_lower_temp[PACK_TEMPS_NUM - 1] = {0,0,0}; //pcb温度不用判断
  25. /*定义低温和正常温度下的电池保护参数, [0]低温参数, [1]常温参数 */
  26. /*能提供动力的最小电压*/
  27. static float min_discharger_power_vol[] = {32000, 38000}; //允许能提供动力的最小电压
  28. static float min_discharger_power_recovery_vol[] = {34000, 40000}; //恢复能提供动力的最小电压
  29. static float min_discharger_power_cell_vol[] = {2100, 2500}; //允许能提供动力的最小电芯电压
  30. static float min_discharger_power_recovery_cell_vol[] = {2200, 2600}; //恢复能提供动力的最小电芯电压
  31. /*能提供大电的最小电压*/
  32. static float min_discharger_vol[] = {30000, 36000};//允许能放电的最小电压
  33. static float min_discharger_recovery_vol[] = {32000, 40000};//恢复放电的最小电压
  34. static float min_discharger_cell_vol[] = {2000, 2500};//允许能放电的最小电芯电压
  35. static float min_discharger_cell_recovery_vol[] = {2100, 2600};//恢复放电的最小电芯电压
  36. /*电池PowerDown的最小电压 */
  37. static float min_discharger_pdown_vol[] = {28000, 34000}; //power down的最小电压
  38. static float min_discharger_pdown_cell_vol[] = {1900, 2200}; //power down的最小电芯电压
  39. #define MAX_TRY_FOR_AUX_SHORT 10
  40. /* health 模块,只检测状态,不做任何控制,如果有异常情况,控制中心会统一处理 */
  41. static void check_ml5238_state(int event);
  42. static void load_detect_handler(shark_timer_t *timer);
  43. static void clear_short_current_handler(shark_timer_t *timer);
  44. static void charger_detect_handler(shark_timer_t *timer);
  45. static void _aux_lock_timer_handler(shark_timer_t *t);
  46. static void _aux_unlock_timer_handler(shark_timer_t *t);
  47. void soft_current_init(void);
  48. int soft_current_push(float current_ma);
  49. static bms_health_t _health;
  50. static debounce_timer_t _load_detect_timer = {.max_count = 100, .interval = 10, ._timer.handler = load_detect_handler};
  51. static debounce_timer_t _charger_detect_timer = {.max_count = 500, .interval = 10, ._timer.handler = charger_detect_handler};
  52. static shark_timer_t _clear_short_current_timer = {.handler = clear_short_current_handler};
  53. static error_counts_t error_counts;
  54. void health_init(void){
  55. /* 5238如果有异常情况,比如短路,负载移除,通过这个handler上报 */
  56. ml5238_register_notify_handler(check_ml5238_state);
  57. soft_current_init();
  58. set_log_level(MOD_HEALTH, L_debug);
  59. for (int i = 0; i < CELLS_NUM; i++){
  60. _health.internal_resistance[i] = 1;//毫欧,暂时用一个固定数据,后期需要计算R0=(U2-U1)/(I1-I2) - R1(R1为电路上的等效电阻+采样电阻)
  61. }
  62. _health.is_work_temp_normal = 1;
  63. }
  64. void health_log(void){
  65. health_debug("soft short:%d\n", error_counts.soft_current_short);
  66. health_debug("hard short:%d\n", error_counts.hard_current_short);
  67. health_debug("work temp: %d\n", _health.is_work_temp_normal);
  68. health_debug("aux_short: %d, %d\n", error_counts.aux_short, error_counts.aux_real_short);
  69. }
  70. bms_health_t *bms_health(){
  71. return &_health;
  72. }
  73. uint32_t bms_health_pack_lower_voltage(void){
  74. return min_discharger_vol[_health.is_work_temp_normal];
  75. }
  76. uint32_t bms_health_cell_lower_voltage(void){
  77. return min_discharger_cell_vol[_health.is_work_temp_normal];
  78. }
  79. static void clear_short_current_handler(shark_timer_t *timer){
  80. _health.load_current_short = 0; //负载移除,clear load current short
  81. health_warning("clear load current short\n");
  82. }
  83. static void load_detect_handler(shark_timer_t *timer){
  84. if (ml5238_is_load_disconnect()){
  85. _load_detect_timer.count ++;
  86. }else {
  87. _load_detect_timer.count = 0;
  88. }
  89. if (_load_detect_timer.count >= _load_detect_timer.max_count) {
  90. ml5238_enable_load_detect(0);
  91. _load_detect_timer.count = 0;
  92. shark_timer_post(&_clear_short_current_timer, 60 * 1000); //负载移除1分钟后,清除current short flags, can open discharger again
  93. health_warning("load disconnect\n");
  94. }else {
  95. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  96. }
  97. }
  98. static void charger_detect_handler(shark_timer_t *timer){
  99. if (!io_state()->charger_detect || !bms_state_get()->charging) {
  100. _charger_detect_timer.count ++;
  101. }else {
  102. _charger_detect_timer.count = 0;
  103. }
  104. if (_charger_detect_timer.count >= _charger_detect_timer.max_count){
  105. _health.charger_over_current = 0;
  106. _charger_detect_timer.count = 0;
  107. health_warning("clear charger over current\n");
  108. }else {
  109. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  110. }
  111. }
  112. static void check_ml5238_state(int event){
  113. health_warning("ml5238 event=0x%x\n", event);
  114. if (event == ML5238_Event_Charger_Over_Current){
  115. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  116. }else if (event == ML5238_Event_Short_Current) { //ml5238触发短路保护,充放电mos全部关闭
  117. _health.load_current_short = 1;
  118. error_counts.hard_current_short ++;
  119. ml5238_enable_load_detect(1); //打开负载检测
  120. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  121. }else if (event == ML5238_Event_Load_Disconnect) {
  122. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  123. }
  124. }
  125. static void debug_health(void){
  126. uint32_t *value = (uint32_t *)&_health;
  127. if (*value != 0){
  128. //health_error("health value = 0x%x\n", *value);
  129. }
  130. }
  131. /* 检测电流情况,看是否过流等 */
  132. static debounce_t _charger_over_current = {
  133. .count = 0,
  134. .max_count = 70
  135. };
  136. /* 55 - 100A, 14 - I x I / 750 */
  137. void check_current_state(void){
  138. float current = measure_value()->load_current;
  139. if (bms_state_get()->charging) {
  140. //_discharger_over_current.count = 0;
  141. if (!_health.charger_over_current) {
  142. if (current > MAX_CURRENT_FOR_CHARGER) {
  143. _charger_over_current.count ++;
  144. }else {
  145. _charger_over_current.count = 0;
  146. }
  147. if (_charger_over_current.count >= _charger_over_current.max_count){
  148. _health.charger_over_current = 1;
  149. _charger_over_current.count = 0;
  150. health_warning("charger over current\n");
  151. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  152. }
  153. }
  154. }else{
  155. _charger_over_current.count = 0;
  156. if (!_health.load_current_short){
  157. if (soft_current_push(current)) {
  158. _health.load_current_short = 1;
  159. error_counts.soft_current_short ++;
  160. //_discharger_over_current.count = 0;
  161. ml5238_enable_load_detect(1); //打开负载检测
  162. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  163. soft_current_init();
  164. }
  165. }
  166. }
  167. }
  168. /* 检测pack电压,cell电压,pack电压过低触发powerdown*/
  169. static debounce_t _discharger_lower_voltage = {.count = 0, .max_count = 20, .init_count = 10};
  170. static debounce_t _power_down_voltage = {.count = 0, .max_count = 10, .init_count = 0};
  171. static debounce_t _sigle_cell_discharger_lower_vol = {.count = 0, .max_count = 10, .init_count = 5};
  172. static debounce_t _sigle_cell_charger_max_vol = {.count = 0, .max_count = 30, .init_count = 15};
  173. static debounce_t _shut_discharger_lower_voltage = {.count = 0, .max_count = 20,};
  174. static debounce_t _shut_discharger_cell_lower_voltage = {.count = 0, .max_count = 20,};
  175. static int judge_debounce(int input, debounce_t *d){
  176. if (input) {
  177. d->count ++;
  178. if (d->count >= d->max_count){
  179. d->count = 0;
  180. return 1;
  181. }
  182. return 0;
  183. }else {
  184. d->count = d->init_count;
  185. return 0;
  186. }
  187. }
  188. static int _can_powerdown(void){
  189. if (io_state()->charger_detect_irq || bms_state_get()->charging || !bms_work_is_normal()){
  190. return 0;
  191. }
  192. if ((bms_state_get()->pack_voltage <= min_discharger_pdown_vol[_health.is_work_temp_normal] ||
  193. bms_state_get()->cell_min_vol <= min_discharger_pdown_cell_vol[_health.is_work_temp_normal])){
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. void check_voltage_state(void) {
  199. if (bms_state_get()->charging){ //check sigle cell's voltage for charger
  200. _health.discharger_shutpower_voltage = 0;
  201. _health.sigle_cell_lower_voltage = 0;
  202. _health.discharger_lower_voltage = 0;
  203. _health.discharger_cell_shutpower_voltage = 0;
  204. if ((bms_state_get()->cell_max_vol>= SIGLE_CELL_MAX_CHARGER_VOLTAGE)){
  205. if (judge_debounce(!_health.sigle_cell_over_voltage, &_sigle_cell_charger_max_vol)){
  206. _health.sigle_cell_over_voltage = 1;
  207. sys_debug("sigle cell %d\n", bms_state_get()->cell_max_vol);
  208. }
  209. }else if ((bms_state_get()->cell_max_vol < SIGLE_CELL_MAX_CHARGER_VOLTAGE)){
  210. if (judge_debounce(_health.sigle_cell_over_voltage, &_sigle_cell_charger_max_vol)){
  211. _health.sigle_cell_over_voltage = 0;
  212. }
  213. }
  214. _health.charger_over_voltage = _health.sigle_cell_over_voltage;
  215. }else{
  216. //check sigle cell's voltage for discharger
  217. _health.charger_over_voltage = _health.sigle_cell_over_voltage = 0;
  218. if ((bms_state_get()->cell_min_vol <= min_discharger_cell_vol[_health.is_work_temp_normal])){
  219. if (judge_debounce(!_health.sigle_cell_lower_voltage, &_sigle_cell_discharger_lower_vol)){
  220. _health.sigle_cell_lower_voltage = 1;
  221. }
  222. }else if ((bms_state_get()->cell_min_vol >= min_discharger_cell_recovery_vol[_health.is_work_temp_normal])){
  223. if (judge_debounce(_health.sigle_cell_lower_voltage, &_sigle_cell_discharger_lower_vol)){
  224. _health.sigle_cell_lower_voltage = 0;
  225. }
  226. }
  227. //check sigle pack's voltage for discharger
  228. if (bms_state_get()->pack_voltage <= min_discharger_vol[_health.is_work_temp_normal]){
  229. if (judge_debounce(!_health.discharger_lower_voltage, &_discharger_lower_voltage)){
  230. _health.discharger_lower_voltage = 1;
  231. }
  232. }else if (bms_state_get()->pack_voltage >= min_discharger_recovery_vol[_health.is_work_temp_normal]){
  233. if (judge_debounce(_health.discharger_lower_voltage, &_discharger_lower_voltage)){
  234. _health.discharger_lower_voltage = 0;
  235. }
  236. }
  237. //check for shutdown power
  238. if ((bms_state_get()->cell_min_vol <= min_discharger_power_cell_vol[_health.is_work_temp_normal])){
  239. if (judge_debounce(!_health.discharger_cell_shutpower_voltage, &_shut_discharger_cell_lower_voltage)){
  240. _health.discharger_cell_shutpower_voltage = 1;
  241. }
  242. }else if ((bms_state_get()->cell_min_vol >= min_discharger_power_recovery_cell_vol[_health.is_work_temp_normal])){
  243. if (judge_debounce(_health.discharger_cell_shutpower_voltage, &_shut_discharger_cell_lower_voltage)){
  244. _health.discharger_cell_shutpower_voltage = 0;
  245. }
  246. }
  247. if ((bms_state_get()->cell_min_vol <= min_discharger_power_vol[_health.is_work_temp_normal])){
  248. if (judge_debounce(!_health.discharger_cell_shutpower_voltage, &_shut_discharger_lower_voltage)){
  249. _health.discharger_shutpower_voltage = 1;
  250. }
  251. }else if ((bms_state_get()->cell_min_vol >= min_discharger_power_recovery_vol[_health.is_work_temp_normal])){
  252. if (judge_debounce(_health.discharger_cell_shutpower_voltage, &_shut_discharger_lower_voltage)){
  253. _health.discharger_shutpower_voltage = 0;
  254. }
  255. }
  256. }
  257. /* check for power down */
  258. if (_can_powerdown()){
  259. if (judge_debounce(!_health.powerdown_lower_voltage, &_power_down_voltage)) {
  260. /*
  261. * no need to clear powerdown(bms is shutdown), when charger insert,
  262. * system will power on with powerdown_lower_voltage cleared
  263. */
  264. _health.powerdown_lower_voltage = 1;
  265. }
  266. }
  267. debug_health();
  268. }
  269. /* 检测温度情况,看是否过高温,或者过低温 */
  270. static debounce_t _charger_over_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  271. static debounce_t _charger_lower_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  272. static debounce_t _charger_normal_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  273. static debounce_t _discharger_over_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  274. static debounce_t _discharger_lower_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  275. static debounce_t _discharger_normal_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  276. static debounce_t _work_lower_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  277. static int _is_over_temp(int8_t *temps, int size){
  278. int count = 0;
  279. for (int i = 0; i < size; i++){
  280. if (measure_value()->pack_temp[i] >= temps[i]){
  281. count ++;
  282. }
  283. }
  284. return count;
  285. }
  286. static int _is_low_temp(int8_t *temps, int size){
  287. int count = 0;
  288. for (int i = 0; i < size; i++){
  289. if (measure_value()->pack_temp[i] < temps[i]){
  290. count ++;
  291. }
  292. }
  293. return count;
  294. }
  295. static uint8_t small_power_detect_count = 0;
  296. static shark_timer_t _aux_lock_timer = {.handler = _aux_lock_timer_handler};
  297. static shark_timer_t _aux_unlock_timer = {.handler = _aux_unlock_timer_handler};
  298. static void _aux_lock_timer_handler(shark_timer_t *t){
  299. AUX_VOL_OPEN(1);
  300. if (++small_power_detect_count >= MAX_TRY_FOR_AUX_SHORT){
  301. //端口电压小于阈值,判断为小电流短路
  302. int short_voltage = get_small_current_voltage()/1000;
  303. int pack_voltage = bms_state_get()->pack_voltage/1000;
  304. if (short_voltage >= (pack_voltage - AUX_SHORT_DIFF_VOLTAGE)) {
  305. _health.small_current_short = 1;
  306. error_counts.aux_short ++;
  307. AUX_VOL_OPEN(0);
  308. small_power_detect_count = 0;
  309. shark_timer_post( &_aux_lock_timer, 30 * 1000); //30s后再次尝试打开
  310. shark_timer_cancel(&_aux_unlock_timer);
  311. if (short_voltage >= (pack_voltage - AUX_SHORT_REAL_DIFF_VOLTAGE)){ //real short
  312. error_counts.aux_real_short ++;
  313. _health.small_current_real_short = 1;
  314. }
  315. health_debug("aux short, v:%d, and retry after 30s\n", short_voltage);
  316. }
  317. }else {
  318. health_debug("open aux[re-enable], %lld\n", shark_get_mseconds());
  319. shark_timer_post( &_aux_unlock_timer, 200);
  320. }
  321. }
  322. static void _aux_unlock_timer_handler(shark_timer_t *t){
  323. if (!io_state()->aux_lock_detect){
  324. health_debug("unlock aux detect\n");
  325. small_power_detect_count = 0;
  326. _health.small_current_short = 0;
  327. _health.small_current_real_short = 0;
  328. AUX_VOL_OPEN(1);
  329. }
  330. }
  331. void health_stop_aux_detect(void){
  332. shark_timer_cancel(&_aux_unlock_timer);
  333. shark_timer_cancel(&_aux_lock_timer);
  334. _health.small_current_short = 0;
  335. _health.small_current_real_short = 0;
  336. }
  337. void health_process_aux_lock(void){
  338. if (io_state()->aux_lock_detect) {
  339. if (AUX_VOL_IS_OPEN()){
  340. AUX_VOL_OPEN(0);
  341. health_debug("close aux[locked], %lld\n", shark_get_mseconds());
  342. shark_timer_post( &_aux_lock_timer, 1);
  343. shark_timer_cancel(&_aux_unlock_timer);
  344. }
  345. }else {
  346. if (AUX_VOL_IS_OPEN()) {
  347. shark_timer_post( &_aux_unlock_timer, 500);
  348. shark_timer_cancel(&_aux_lock_timer);
  349. }
  350. }
  351. }
  352. void check_temp_state(void){
  353. if (!_health.over_temp_deny_charger){
  354. if (_is_over_temp(charger_higher_high_temp, sizeof(charger_higher_high_temp))) {//超过允许的最高温度
  355. debounce_inc(_charger_over_temp_count);
  356. }else {
  357. debounce_reset(_charger_over_temp_count);
  358. }
  359. if (debounce_reach_max(_charger_over_temp_count)){
  360. _health.over_temp_deny_charger = 1;
  361. debounce_reset(_charger_over_temp_count);
  362. }
  363. }
  364. if (!_health.lower_temp_deny_charger){
  365. if (_is_low_temp(charger_lower_low_temp, sizeof(charger_lower_low_temp))) {//低于允许的最低温度
  366. debounce_inc(_charger_lower_temp_count);
  367. }else {
  368. debounce_reset(_charger_lower_temp_count);
  369. }
  370. if (debounce_reach_max(_charger_lower_temp_count)) {
  371. _health.lower_temp_deny_charger = 1;
  372. debounce_reset(_charger_lower_temp_count);
  373. }
  374. }
  375. if (_health.lower_temp_deny_charger || _health.over_temp_deny_charger) {
  376. if (!_is_over_temp(charger_normal_high_temp, sizeof(charger_normal_high_temp)) && !_is_low_temp(charger_normal_low_temp, sizeof(charger_normal_low_temp))){
  377. debounce_inc(_charger_normal_temp_count);
  378. }else {
  379. debounce_reset(_charger_normal_temp_count);
  380. }
  381. if (debounce_reach_max(_charger_normal_temp_count)){
  382. _health.over_temp_deny_charger = 0;
  383. _health.lower_temp_deny_charger = 0;
  384. debounce_reset(_charger_normal_temp_count);
  385. }
  386. }
  387. if (!_health.over_temp_deny_discharger){
  388. if (_is_over_temp(discharger_higher_high_temp, sizeof(discharger_higher_high_temp))) {//超过允许的最高温度
  389. debounce_inc(_discharger_over_temp_count);
  390. }else {
  391. debounce_reset(_discharger_over_temp_count);
  392. }
  393. if (debounce_reach_max(_discharger_over_temp_count)){
  394. _health.over_temp_deny_discharger = 1;
  395. debounce_reset(_discharger_over_temp_count);
  396. }
  397. }
  398. if (!_health.lower_temp_deny_discharger){
  399. if (_is_low_temp(discharger_lower_low_temp, sizeof(discharger_lower_low_temp))) {//低于允许的最低温度
  400. debounce_inc(_discharger_lower_temp_count);
  401. }else {
  402. debounce_reset(_discharger_lower_temp_count);
  403. }
  404. if (debounce_reach_max(_discharger_lower_temp_count)) {
  405. _health.lower_temp_deny_discharger = 1;
  406. debounce_reset(_discharger_lower_temp_count);
  407. }
  408. }
  409. if (_health.lower_temp_deny_discharger || _health.over_temp_deny_discharger) {
  410. if (!_is_over_temp(discharger_normal_high_temp, sizeof(discharger_lower_low_temp)) && !_is_low_temp(discharger_normal_low_temp, sizeof(discharger_normal_low_temp))){
  411. debounce_inc(_discharger_normal_temp_count);
  412. }else {
  413. debounce_reset(_discharger_normal_temp_count);
  414. }
  415. if (debounce_reach_max(_discharger_normal_temp_count)){
  416. _health.over_temp_deny_discharger = 0;
  417. _health.lower_temp_deny_discharger = 0;
  418. debounce_reset(_discharger_normal_temp_count);
  419. }
  420. }
  421. if (!_health.is_work_temp_normal){
  422. /* 3个电芯温度都正常才算正常 */
  423. if (_is_over_temp(work_lower_temp, sizeof(work_lower_temp)) == sizeof(work_lower_temp)){
  424. debounce_inc(_work_lower_temp_count);
  425. if (debounce_reach_max(_work_lower_temp_count)){
  426. _health.is_work_temp_normal = 1;
  427. debounce_reset(_work_lower_temp_count);
  428. }
  429. }else {
  430. debounce_reset(_work_lower_temp_count);
  431. }
  432. }else {
  433. if (_is_low_temp(work_lower_temp, sizeof(work_lower_temp))){
  434. debounce_inc(_work_lower_temp_count);
  435. if (debounce_reach_max(_work_lower_temp_count)){
  436. _health.is_work_temp_normal = 0;
  437. debounce_reset(_work_lower_temp_count);
  438. }
  439. }else {
  440. debounce_reset(_work_lower_temp_count);
  441. }
  442. }
  443. if (bms_state_get()->charging){
  444. _health.discharger_over_temp = 0;
  445. _health.discharger_lower_temp = 0;
  446. _health.charger_over_temp = _health.over_temp_deny_charger;
  447. _health.charger_lower_temp = _health.lower_temp_deny_charger;
  448. }else {
  449. _health.charger_over_temp = 0;
  450. _health.charger_lower_temp = 0;
  451. _health.discharger_over_temp = _health.over_temp_deny_discharger;
  452. _health.discharger_lower_temp = _health.lower_temp_deny_discharger;
  453. }
  454. debug_health();
  455. }