health.c 17 KB

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