health.c 15 KB

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