health.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. #include "event_record.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] = {5,5,5}; //pcb温度不用判断
  26. static int8_t work_lower_temp_recovry[PACK_TEMPS_NUM - 1] = {10,10,10}; //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, 2400}; //允许能提供动力的最小电芯电压
  32. static float min_discharger_power_recovery_cell_vol[] = {2200, 2500}; //恢复能提供动力的最小电芯电压
  33. /*能提供大电的最小电压*/
  34. static float min_discharger_vol[] = {27000, 33000};//允许能放电的最小电压
  35. static float min_discharger_recovery_vol[] = {30000, 36000};//恢复放电的最小电压
  36. static float min_discharger_cell_vol[] = {1800, 2200};//允许能放电的最小电芯电压
  37. static float min_discharger_cell_recovery_vol[] = {1900, 2300};//恢复放电的最小电芯电压
  38. /*电池PowerDown的最小电压 */
  39. static float min_discharger_pdown_vol[] = {26000, 30000}; //power down的最小电压
  40. static float min_discharger_pdown_cell_vol[] = {1600, 1800}; //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. void health_init(void){
  57. /* 5238如果有异常情况,比如短路,负载移除,通过这个handler上报 */
  58. ml5238_register_notify_handler(check_ml5238_state);
  59. soft_current_init();
  60. set_log_level(MOD_HEALTH, L_debug);
  61. for (int i = 0; i < CELLS_NUM; i++){
  62. _health.internal_resistance[i] = 1;//毫欧,暂时用一个固定数据,后期需要计算R0=(U2-U1)/(I1-I2) - R1(R1为电路上的等效电阻+采样电阻)
  63. }
  64. _health.is_work_temp_normal = 1;
  65. }
  66. void health_log(void){
  67. health_debug("soft short:%d\n", error_counts.soft_current_short);
  68. health_debug("hard short:%d\n", error_counts.hard_current_short);
  69. health_debug("work temp: %d\n", _health.is_work_temp_normal);
  70. health_debug("aux_short: %d, %d\n", error_counts.aux_short, error_counts.aux_real_short);
  71. health_debug("lower voltage: %d, %d\n", error_counts.cell_under_voltage, error_counts.pack_under_voltage);
  72. health_debug("uart error %d, %d, %d\n", error_counts.uart_crc_error, error_counts.uart_len_error, error_counts.uart_dir_error);
  73. 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);
  74. }
  75. bms_health_t *bms_health(){
  76. return &_health;
  77. }
  78. void health_add_uart_error(uint32_t c, uint32_t l, uint32_t d) {
  79. error_counts.uart_crc_error += c;
  80. error_counts.uart_len_error += l;
  81. error_counts.uart_dir_error += d;
  82. }
  83. uint32_t bms_health_pack_lower_voltage(void){
  84. return min_discharger_vol[_health.is_work_temp_normal];
  85. }
  86. uint32_t bms_health_cell_lower_voltage(void){
  87. return min_discharger_cell_vol[_health.is_work_temp_normal];
  88. }
  89. static void clear_short_current_handler(shark_timer_t *timer){
  90. _health.load_current_short = 0; //负载移除,clear load current short
  91. health_warning("clear load current short\n");
  92. }
  93. static void load_detect_handler(shark_timer_t *timer){
  94. if (ml5238_is_load_disconnect()){
  95. _load_detect_timer.count ++;
  96. }else {
  97. _load_detect_timer.count = 0;
  98. }
  99. if (_load_detect_timer.count >= _load_detect_timer.max_count) {
  100. ml5238_enable_load_detect(0);
  101. _load_detect_timer.count = 0;
  102. shark_timer_post(&_clear_short_current_timer, 60 * 1000); //负载移除1分钟后,清除current short flags, can open discharger again
  103. health_warning("load disconnect\n");
  104. }else {
  105. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  106. }
  107. }
  108. static void charger_detect_handler(shark_timer_t *timer){
  109. if (!io_state()->charger_detect || !bms_state_get()->charging) {
  110. _charger_detect_timer.count ++;
  111. }else {
  112. _charger_detect_timer.count = 0;
  113. }
  114. if (_charger_detect_timer.count >= _charger_detect_timer.max_count){
  115. _health.charger_over_current = 0;
  116. _charger_detect_timer.count = 0;
  117. health_warning("clear charger over current\n");
  118. }else {
  119. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  120. }
  121. }
  122. static void check_ml5238_state(int event){
  123. health_warning("ml5238 event=0x%x\n", event);
  124. if (event == ML5238_Event_Charger_Over_Current){
  125. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  126. }else if (event == ML5238_Event_Short_Current) { //ml5238触发短路保护,充放电mos全部关闭
  127. _health.load_current_short = 1;
  128. push_event(Current_Short, 1);
  129. error_counts.hard_current_short ++;
  130. ml5238_enable_load_detect(1); //打开负载检测
  131. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  132. }else if (event == ML5238_Event_Load_Disconnect) {
  133. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  134. }
  135. }
  136. static void debug_health(void){
  137. uint32_t *value = (uint32_t *)&_health;
  138. if (*value != 0){
  139. //health_error("health value = 0x%x\n", *value);
  140. }
  141. }
  142. /* 检测电流情况,看是否过流等 */
  143. static debounce_t _charger_over_current = {
  144. .count = 0,
  145. .max_count = 70
  146. };
  147. /* 55 - 100A, 14 - I x I / 750 */
  148. void check_current_state(void){
  149. float current = measure_value()->load_current;
  150. if (bms_state_get()->charging) {
  151. //_discharger_over_current.count = 0;
  152. if (!_health.charger_over_current) {
  153. if (current > MAX_CURRENT_FOR_CHARGER) {
  154. _charger_over_current.count ++;
  155. }else {
  156. _charger_over_current.count = 0;
  157. }
  158. if (_charger_over_current.count >= _charger_over_current.max_count){
  159. _health.charger_over_current = 1;
  160. _charger_over_current.count = 0;
  161. health_warning("charger over current\n");
  162. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  163. }
  164. }
  165. }else{
  166. _charger_over_current.count = 0;
  167. if (!_health.load_current_short){
  168. if (soft_current_push(current)) {
  169. _health.load_current_short = 1;
  170. push_event(Current_Short, 0);
  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 = 200, .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 = 200, .init_count = 0};
  184. static debounce_t _sigle_cell_charger_max_vol = {.count = 0, .max_count = 50, .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){
  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. static void _single_low_judge_current(bool set) {
  211. if (!set){
  212. bms_health()->b_flags &= ~(B_FLAGS_SINGLE_LOW_CURRENT | B_FLAGS_SINGLE_MID_CURRENT | B_FLAGS_SINGLE_BIG_CURRENT | B_FLAGS_SINGLE_LARGER_CURRENT);
  213. return;
  214. }
  215. int load_current = abs(measure_value()->load_current);
  216. if (load_current < MAX_HA*1000/2) { // 0.5C ???
  217. bms_health()->b_flags |= B_FLAGS_SINGLE_LOW_CURRENT;
  218. }else if (load_current < MAX_HA*1000) { // 1C ???
  219. bms_health()->b_flags |= B_FLAGS_SINGLE_MID_CURRENT;
  220. }else if (load_current < MAX_HA * 1000 * 3/2) { // 1.5C ???
  221. bms_health()->b_flags |= B_FLAGS_SINGLE_BIG_CURRENT;
  222. }else {
  223. bms_health()->b_flags |= B_FLAGS_SINGLE_LARGER_CURRENT;
  224. }
  225. }
  226. static void _pack_low_judge_current(bool set) {
  227. if (!set){
  228. bms_health()->b_flags &= ~(B_FLAGS_PACK_LOW_CURRENT | B_FLAGS_PACK_MID_CURRENT | B_FLAGS_PACK_BIG_CURRENT | B_FLAGS_PACK_LARGER_CURRENT);
  229. return;
  230. }
  231. int load_current = abs(measure_value()->load_current);
  232. if (load_current < MAX_HA*1000/2) {
  233. bms_health()->b_flags |= B_FLAGS_PACK_LOW_CURRENT;
  234. }else if (load_current < MAX_HA*1000) {
  235. bms_health()->b_flags |= B_FLAGS_PACK_MID_CURRENT;
  236. }else if (load_current < MAX_HA * 1000 * 3/2){
  237. bms_health()->b_flags |= B_FLAGS_PACK_BIG_CURRENT;
  238. }else {
  239. bms_health()->b_flags |= B_FLAGS_PACK_LARGER_CURRENT;
  240. }
  241. }
  242. void check_voltage_state(void) {
  243. static uint16_t _charging = 0xFFFF;
  244. if (_charging != bms_state_get()->charging) {
  245. if (bms_state_get()->charging) {
  246. }else {
  247. error_counts.cell_under_voltage = 0;
  248. }
  249. _charging = bms_state_get()->charging;
  250. }
  251. if (bms_state_get()->charging){ //check sigle cell's voltage for charger
  252. _health.discharger_shutpower_voltage = 0;
  253. _health.sigle_cell_lower_voltage = 0;
  254. _health.discharger_lower_voltage = 0;
  255. _health.discharger_cell_shutpower_voltage = 0;
  256. if ((bms_state_get()->cell_max_vol>= SIGLE_CELL_MAX_CHARGER_VOLTAGE)){
  257. if (judge_debounce(!_health.sigle_cell_over_voltage, &_sigle_cell_charger_max_vol)){
  258. _health.sigle_cell_over_voltage = 1;
  259. push_event(Cell_Over_Vol, bms_state_get()->cell_max_vol);
  260. sys_debug("sigle cell %d\n", bms_state_get()->cell_max_vol);
  261. }
  262. }else if ((bms_state_get()->cell_max_vol < SIGLE_CELL_MAX_CHARGER_VOLTAGE)){
  263. if (judge_debounce(_health.sigle_cell_over_voltage, &_sigle_cell_charger_max_vol)){
  264. _health.sigle_cell_over_voltage = 0;
  265. }
  266. }
  267. _health.charger_over_voltage = _health.sigle_cell_over_voltage;
  268. }else{
  269. //check sigle cell's voltage for discharger
  270. _health.charger_over_voltage = _health.sigle_cell_over_voltage = 0;
  271. if ((bms_state_get()->cell_min_vol <= min_discharger_cell_vol[_health.is_work_temp_normal])){
  272. if (judge_debounce(!_health.sigle_cell_lower_voltage, &_sigle_cell_discharger_lower_vol)){
  273. _health.sigle_cell_lower_voltage = 1;
  274. push_event(Cell_Under_Vol, bms_state_get()->cell_min_vol);
  275. _single_low_judge_current(true);
  276. error_counts.cell_under_voltage++;
  277. }
  278. }else if ((bms_state_get()->cell_min_vol >= min_discharger_cell_recovery_vol[_health.is_work_temp_normal])){
  279. _single_low_judge_current(false);
  280. if (judge_debounce(_health.sigle_cell_lower_voltage, &_sigle_cell_discharger_lower_vol)){
  281. _health.sigle_cell_lower_voltage = 0;
  282. }
  283. }else {
  284. debounce_reset(_sigle_cell_discharger_lower_vol);
  285. }
  286. //check sigle pack's voltage for discharger
  287. if (bms_state_get()->pack_voltage <= min_discharger_vol[_health.is_work_temp_normal]){
  288. if (judge_debounce(!_health.discharger_lower_voltage, &_discharger_lower_voltage)){
  289. _health.discharger_lower_voltage = 1;
  290. push_event(Pack_Under_Vol, bms_state_get()->pack_voltage);
  291. _pack_low_judge_current(true);
  292. error_counts.pack_under_voltage++;
  293. }
  294. }else if (bms_state_get()->pack_voltage >= min_discharger_recovery_vol[_health.is_work_temp_normal]){
  295. _pack_low_judge_current(false);
  296. if (judge_debounce(_health.discharger_lower_voltage, &_discharger_lower_voltage)){
  297. _health.discharger_lower_voltage = 0;
  298. }
  299. }else {
  300. debounce_reset(_discharger_lower_voltage);
  301. }
  302. //check for shutdown power
  303. if ((bms_state_get()->cell_min_vol <= min_discharger_power_cell_vol[_health.is_work_temp_normal])){
  304. if (judge_debounce(!_health.discharger_cell_shutpower_voltage, &_shut_discharger_cell_lower_voltage)){
  305. _health.discharger_cell_shutpower_voltage = 1;
  306. }
  307. }else if ((bms_state_get()->cell_min_vol >= min_discharger_power_recovery_cell_vol[_health.is_work_temp_normal])){
  308. if (judge_debounce(_health.discharger_cell_shutpower_voltage, &_shut_discharger_cell_lower_voltage)){
  309. _health.discharger_cell_shutpower_voltage = 0;
  310. }
  311. }else {
  312. debounce_reset(_shut_discharger_cell_lower_voltage);
  313. }
  314. if ((bms_state_get()->pack_voltage <= min_discharger_power_vol[_health.is_work_temp_normal])){
  315. if (judge_debounce(!_health.discharger_shutpower_voltage, &_shut_discharger_lower_voltage)){
  316. _health.discharger_shutpower_voltage = 1;
  317. }
  318. }else if ((bms_state_get()->pack_voltage >= min_discharger_power_recovery_vol[_health.is_work_temp_normal])){
  319. if (judge_debounce(_health.discharger_shutpower_voltage, &_shut_discharger_lower_voltage)){
  320. _health.discharger_shutpower_voltage = 0;
  321. }
  322. }else {
  323. debounce_reset(_shut_discharger_lower_voltage);
  324. }
  325. }
  326. /* check for power down */
  327. if (_can_powerdown()){
  328. if (judge_debounce(!_health.powerdown_lower_voltage, &_power_down_voltage)) {
  329. /*
  330. * no need to clear powerdown(bms is shutdown), when charger insert,
  331. * system will power on with powerdown_lower_voltage cleared
  332. */
  333. _health.powerdown_lower_voltage = 1;
  334. _health.sigle_cell_lower_voltage = 1;
  335. _health.pd_time = shark_get_seconds();
  336. }
  337. }else {
  338. _health.powerdown_lower_voltage = 0;
  339. _power_down_voltage.count = _power_down_voltage.init_count;
  340. _health.pd_time = shark_get_seconds();
  341. }
  342. debug_health();
  343. }
  344. /* 检测温度情况,看是否过高温,或者过低温 */
  345. static debounce_t _charger_over_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  346. static debounce_t _charger_lower_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  347. static debounce_t _charger_normal_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  348. static debounce_t _discharger_over_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  349. static debounce_t _discharger_lower_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  350. static debounce_t _discharger_normal_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  351. static debounce_t _work_lower_temp_count = {.count = 0, .max_count = 8, .init_count = 0};
  352. static int _get_max_temp(int size){
  353. int max = -1000;
  354. for (int i = 0; i < size; i++){
  355. if (measure_value()->pack_temp[i] >= max){
  356. max = measure_value()->pack_temp[i];
  357. }
  358. }
  359. return max;
  360. }
  361. static int _get_min_temp(int size){
  362. int min = 1000;
  363. for (int i = 0; i < size; i++){
  364. if (measure_value()->pack_temp[i] < min){
  365. min = measure_value()->pack_temp[i];
  366. }
  367. }
  368. return min;
  369. }
  370. static int _is_over_temp(int8_t *temps, int size){
  371. int count = 0;
  372. for (int i = 0; i < size; i++){
  373. if (measure_value()->pack_temp[i] >= temps[i]){
  374. count ++;
  375. }
  376. }
  377. return count;
  378. }
  379. static int _is_low_temp(int8_t *temps, int size){
  380. int count = 0;
  381. for (int i = 0; i < size; i++){
  382. if (measure_value()->pack_temp[i] < temps[i]){
  383. count ++;
  384. }
  385. }
  386. return count;
  387. }
  388. static uint8_t small_power_detect_count = 0;
  389. static shark_timer_t _aux_lock_timer = {.handler = _aux_lock_timer_handler};
  390. static shark_timer_t _aux_unlock_timer = {.handler = _aux_unlock_timer_handler};
  391. u32 _aux_unlock_delay(float voltage){
  392. float aux_current = voltage / SMALL_CURRENT_R;
  393. return aux_current * 10 * 1000; //ms
  394. }
  395. static void _aux_lock_timer_handler(shark_timer_t *t){
  396. AUX_VOL_OPEN(1);
  397. if (++small_power_detect_count >= MAX_TRY_FOR_AUX_SHORT){
  398. //端口电压小于阈值,判断为小电流短路
  399. int short_voltage = get_small_current_voltage()/1000;
  400. int pack_voltage = bms_state_get()->pack_voltage/1000;
  401. if (short_voltage >= AUX_SHORT_DIFF_VOLTAGE) {
  402. if (_health.small_current_short == 0) {
  403. push_event(Aux_Current_Short, short_voltage);
  404. }
  405. _health.small_current_short = 1;
  406. error_counts.aux_short ++;
  407. AUX_VOL_OPEN(0);
  408. small_power_detect_count = 0;
  409. u32 delay_time = _aux_unlock_delay(short_voltage);
  410. if (short_voltage >= (pack_voltage - AUX_SHORT_REAL_DIFF_VOLTAGE)){ //real short
  411. error_counts.aux_real_short ++;
  412. _health.small_current_real_short = 1;
  413. delay_time = 30 * 1000;
  414. }
  415. shark_timer_post( &_aux_lock_timer, delay_time); //30s后再次尝试打开
  416. shark_timer_cancel(&_aux_unlock_timer);
  417. health_debug("aux short, v:%d, and retry after %ds\n", short_voltage, delay_time/1000);
  418. }
  419. }else {
  420. health_debug("open aux[re-enable], %lld\n", shark_get_mseconds());
  421. shark_timer_post( &_aux_unlock_timer, 200);
  422. }
  423. }
  424. static void _aux_unlock_timer_handler(shark_timer_t *t){
  425. if (!io_state()->aux_lock_detect){
  426. health_debug("unlock aux detect\n");
  427. small_power_detect_count = 0;
  428. _health.small_current_short = 0;
  429. _health.small_current_real_short = 0;
  430. AUX_VOL_OPEN(1);
  431. }
  432. }
  433. void health_stop_aux_detect(void){
  434. shark_timer_cancel(&_aux_unlock_timer);
  435. shark_timer_cancel(&_aux_lock_timer);
  436. _health.small_current_short = 0;
  437. _health.small_current_real_short = 0;
  438. }
  439. void health_process_aux_lock(void){
  440. if (io_state()->aux_lock_detect) {
  441. if (AUX_VOL_IS_OPEN()){
  442. AUX_VOL_OPEN(0);
  443. health_debug("close aux[locked], %lld\n", shark_get_mseconds());
  444. shark_timer_post( &_aux_lock_timer, 1);
  445. shark_timer_cancel(&_aux_unlock_timer);
  446. }
  447. }else {
  448. if (AUX_VOL_IS_OPEN()) {
  449. shark_timer_post( &_aux_unlock_timer, 500);
  450. shark_timer_cancel(&_aux_lock_timer);
  451. }
  452. }
  453. }
  454. void check_temp_state(void){
  455. if (!_health.over_temp_deny_charger){
  456. if (_is_over_temp(charger_higher_high_temp, sizeof(charger_higher_high_temp))) {//超过允许的最高温度
  457. debounce_inc(_charger_over_temp_count);
  458. }else {
  459. debounce_reset(_charger_over_temp_count);
  460. }
  461. if (debounce_reach_max(_charger_over_temp_count)){
  462. _health.over_temp_deny_charger = 1;
  463. push_event(Temp_High_Charger, _get_max_temp(4));
  464. error_counts.charger_high_temp ++;
  465. debounce_reset(_charger_over_temp_count);
  466. }
  467. }
  468. if (!_health.lower_temp_deny_charger){
  469. if (_is_low_temp(charger_lower_low_temp, sizeof(charger_lower_low_temp))) {//低于允许的最低温度
  470. debounce_inc(_charger_lower_temp_count);
  471. }else {
  472. debounce_reset(_charger_lower_temp_count);
  473. }
  474. if (debounce_reach_max(_charger_lower_temp_count)) {
  475. _health.lower_temp_deny_charger = 1;
  476. push_event(Temp_Low_Charger, _get_min_temp(4));
  477. error_counts.charger_lower_temp ++;
  478. debounce_reset(_charger_lower_temp_count);
  479. }
  480. }
  481. if (_health.lower_temp_deny_charger || _health.over_temp_deny_charger) {
  482. 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))){
  483. debounce_inc(_charger_normal_temp_count);
  484. }else {
  485. debounce_reset(_charger_normal_temp_count);
  486. }
  487. if (debounce_reach_max(_charger_normal_temp_count)){
  488. _health.over_temp_deny_charger = 0;
  489. _health.lower_temp_deny_charger = 0;
  490. debounce_reset(_charger_normal_temp_count);
  491. }
  492. }
  493. if (!_health.over_temp_deny_discharger){
  494. if (_is_over_temp(discharger_higher_high_temp, sizeof(discharger_higher_high_temp))) {//超过允许的最高温度
  495. debounce_inc(_discharger_over_temp_count);
  496. }else {
  497. debounce_reset(_discharger_over_temp_count);
  498. }
  499. if (debounce_reach_max(_discharger_over_temp_count)){
  500. _health.over_temp_deny_discharger = 1;
  501. push_event(Temp_High_Discharger, _get_max_temp(4));
  502. error_counts.discharger_high_temp ++;
  503. debounce_reset(_discharger_over_temp_count);
  504. }
  505. }
  506. if (!_health.lower_temp_deny_discharger){
  507. if (_is_low_temp(discharger_lower_low_temp, sizeof(discharger_lower_low_temp))) {//低于允许的最低温度
  508. debounce_inc(_discharger_lower_temp_count);
  509. }else {
  510. debounce_reset(_discharger_lower_temp_count);
  511. }
  512. if (debounce_reach_max(_discharger_lower_temp_count)) {
  513. _health.lower_temp_deny_discharger = 1;
  514. push_event(Temp_Low_Discharger, _get_min_temp(4));
  515. error_counts.discharger_lower_temp ++;
  516. debounce_reset(_discharger_lower_temp_count);
  517. }
  518. }
  519. if (_health.lower_temp_deny_discharger || _health.over_temp_deny_discharger) {
  520. 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))){
  521. debounce_inc(_discharger_normal_temp_count);
  522. }else {
  523. debounce_reset(_discharger_normal_temp_count);
  524. }
  525. if (debounce_reach_max(_discharger_normal_temp_count)){
  526. _health.over_temp_deny_discharger = 0;
  527. _health.lower_temp_deny_discharger = 0;
  528. debounce_reset(_discharger_normal_temp_count);
  529. }
  530. }
  531. if (!_health.is_work_temp_normal){
  532. /* 3个电芯温度都正常才算正常 */
  533. if (_is_over_temp(work_lower_temp_recovry, sizeof(work_lower_temp_recovry)) == sizeof(work_lower_temp_recovry)){
  534. debounce_inc(_work_lower_temp_count);
  535. if (debounce_reach_max(_work_lower_temp_count)){
  536. _health.is_work_temp_normal = 1;
  537. push_event(Temp_Changed, 1);
  538. debounce_reset(_work_lower_temp_count);
  539. }
  540. }else {
  541. debounce_reset(_work_lower_temp_count);
  542. }
  543. }else {
  544. if (_is_low_temp(work_lower_temp, sizeof(work_lower_temp))){
  545. debounce_inc(_work_lower_temp_count);
  546. if (debounce_reach_max(_work_lower_temp_count)){
  547. _health.is_work_temp_normal = 0;
  548. push_event(Temp_Changed, 0);
  549. debounce_reset(_work_lower_temp_count);
  550. }
  551. }else {
  552. debounce_reset(_work_lower_temp_count);
  553. }
  554. }
  555. if (bms_state_get()->charging){
  556. _health.discharger_over_temp = 0;
  557. _health.discharger_lower_temp = 0;
  558. _health.charger_over_temp = _health.over_temp_deny_charger;
  559. _health.charger_lower_temp = _health.lower_temp_deny_charger;
  560. }else {
  561. _health.charger_over_temp = 0;
  562. _health.charger_lower_temp = 0;
  563. _health.discharger_over_temp = _health.over_temp_deny_discharger;
  564. _health.discharger_lower_temp = _health.lower_temp_deny_discharger;
  565. }
  566. debug_health();
  567. }