health.c 20 KB

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