health.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 (3880)//最大允许充电电压,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. /* health 模块,只检测状态,不做任何控制,如果有异常情况,控制中心会统一处理 */
  24. static void check_ml5238_state(int event);
  25. static void init_detect_timer(void);
  26. static void load_delect_handler(shark_timer_t *timer);
  27. static void charger_detect_handler(shark_timer_t *timer);
  28. static bms_health_t _health;
  29. static debounce_timer_t _load_detect_timer;
  30. static debounce_timer_t _charger_detect_timer;
  31. void health_init(void){
  32. /* 5238如果有异常情况,比如短路,负载移除,通过这个handler上报 */
  33. ml5238_register_notify_handler(check_ml5238_state);
  34. init_detect_timer();
  35. set_log_level(MOD_HEALTH, L_debug);
  36. _health.internal_resistance = 20; //毫欧,暂时用一个固定数据,后期需要计算R0=(U2-U1)/(I1-I2) - R1(R1为电路上的等效电阻+采样电阻)
  37. }
  38. bms_health_t *bms_health(){
  39. return &_health;
  40. }
  41. static void init_detect_timer(void){
  42. _load_detect_timer._timer.handler = load_delect_handler;
  43. _load_detect_timer.max_count = 100;
  44. _load_detect_timer.interval = 10;
  45. _charger_detect_timer._timer.handler = charger_detect_handler;
  46. _charger_detect_timer.max_count = 100;
  47. _charger_detect_timer.interval = 10;
  48. }
  49. static void load_delect_handler(shark_timer_t *timer){
  50. if (ml5238_is_load_disconnect()){
  51. _load_detect_timer.count ++;
  52. }else {
  53. _load_detect_timer.count = 0;
  54. }
  55. if (_load_detect_timer.count >= _load_detect_timer.max_count) {
  56. _health.load_current_short = 0; //负载移除,clear load current short
  57. ml5238_enable_load_detect(0);
  58. health_debug("load disconnect\n");
  59. }else {
  60. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  61. }
  62. }
  63. static void charger_detect_handler(shark_timer_t *timer){
  64. if (!io_state()->charger_detect) {
  65. _charger_detect_timer.count ++;
  66. }else {
  67. _charger_detect_timer.count = 0;
  68. }
  69. if (_charger_detect_timer.count >= _charger_detect_timer.max_count){
  70. _health.charger_over_current = 0;
  71. }else {
  72. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  73. }
  74. }
  75. static void check_ml5238_state(int event){
  76. health_warning("ml5238 event=0x%x\n", event);
  77. if (event == ML5238_Event_Charger_Over_Current){
  78. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  79. }else if (event == ML5238_Event_Short_Current) { //ml5238触发短路保护,充放电mos全部关闭
  80. _health.load_current_short = 1;
  81. ml5238_enable_load_detect(1); //打开负载检测
  82. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  83. }else if (event == ML5238_Event_Load_Disconnect) {
  84. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  85. }
  86. }
  87. static void debug_health(void){
  88. uint32_t *value = (uint32_t *)&_health;
  89. if (*value != 0){
  90. health_error("health value = 0x%x\n", *value);
  91. }
  92. }
  93. /* 检测电流情况,看是否过流等 */
  94. static debounce_t _charger_over_current = {
  95. .count = 0,
  96. .max_count = 10
  97. };
  98. void check_current_state(void){
  99. //判断是否过流充电,放电过流通过5238来检测
  100. if (bms_state_get()->charging && !_health.charger_over_current) {
  101. float current = measure_value()->load_current;
  102. if (current > MAX_CURRENT_FOR_CHARGER) {
  103. _charger_over_current.count ++;
  104. }else {
  105. _charger_over_current.count = 0;
  106. }
  107. if (_charger_over_current.count >= _charger_over_current.max_count){
  108. _health.charger_over_current = 1;
  109. _charger_over_current.count = 0;
  110. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  111. }
  112. }
  113. debug_health();
  114. }
  115. /* 检测pack电压,cell电压,pack电压过低触发powerdown*/
  116. static debounce_t _discharger_lower_voltage = {.count = 10, .max_count = 20, .init_count = 10};
  117. static debounce_t _power_down_voltage = {.count = 0, .max_count = 10, .init_count = 0};
  118. static debounce_t _sigle_cell_discharger_lower_vol = {.count = 5, .max_count = 10, .init_count = 5};
  119. static debounce_t _sigle_cell_charger_max_vol = {.count = 15, .max_count = 30, .init_count = 15};
  120. void check_voltage_state(void) {
  121. if (bms_state_get()->charging){ //check sigle cell's voltage for charger
  122. if ((bms_state_get()->cell_max_vol>= SIGLE_CELL_MAX_CHARGER_VOLTAGE) && !_health.sigle_cell_over_voltage){
  123. debounce_inc(_sigle_cell_charger_max_vol);
  124. if (debounce_reach_max(_sigle_cell_charger_max_vol)){
  125. _health.sigle_cell_over_voltage = 1;
  126. debounce_reset(_sigle_cell_charger_max_vol);
  127. }
  128. }else if ((bms_state_get()->cell_max_vol < SIGLE_CELL_MAX_CHARGER_VOLTAGE) && _health.sigle_cell_over_voltage){
  129. debounce_dec(_sigle_cell_charger_max_vol);
  130. if (debounce_reach_zero(_sigle_cell_charger_max_vol)){
  131. _health.sigle_cell_over_voltage = 0;
  132. debounce_reset(_sigle_cell_charger_max_vol);
  133. }
  134. }
  135. }else{
  136. //check sigle cell's voltage for discharger
  137. if ((bms_state_get()->cell_min_vol <= SIGLE_CELL_LOWER_DISCHARGER_VOLTAGE) && !_health.sigle_cell_lower_voltage){
  138. debounce_inc(_sigle_cell_discharger_lower_vol);
  139. if (debounce_reach_max(_sigle_cell_discharger_lower_vol)){
  140. _health.sigle_cell_lower_voltage = 1;
  141. debounce_reset(_sigle_cell_discharger_lower_vol);
  142. }
  143. }else if ((bms_state_get()->cell_min_vol > SIGLE_CELL_LOWER_DISCHARGER_VOLTAGE) && _health.sigle_cell_lower_voltage){
  144. debounce_dec(_sigle_cell_discharger_lower_vol);
  145. if (debounce_reach_zero(_sigle_cell_discharger_lower_vol)){
  146. _health.sigle_cell_lower_voltage = 0;
  147. debounce_reset(_sigle_cell_discharger_lower_vol);
  148. }
  149. }
  150. //check sigle pack's voltage for discharger
  151. if (!_health.discharger_lower_voltage){ //check for low pack voltage for close discharger
  152. if (bms_state_get()->pack_voltage <= MIN_VOLTAGE_FOR_DISCHARGER){
  153. debounce_inc(_discharger_lower_voltage);
  154. }else {
  155. debounce_reset(_discharger_lower_voltage);
  156. }
  157. if (debounce_reach_max(_discharger_lower_voltage)){
  158. _health.discharger_lower_voltage = 1;
  159. debounce_reset(_discharger_lower_voltage);
  160. }
  161. }else { //check for discharger recovery
  162. if (bms_state_get()->pack_voltage >= MIN_VOLTAGE_FOR_RECOVERY_DISCHARGER){
  163. debounce_inc(_discharger_lower_voltage);
  164. }else {
  165. debounce_reset(_discharger_lower_voltage);
  166. }
  167. if (debounce_reach_max(_discharger_lower_voltage)){
  168. _health.discharger_lower_voltage = 0;
  169. debounce_reset(_discharger_lower_voltage);
  170. }
  171. }
  172. }
  173. /* check for power down */
  174. if (!io_state()->charger_detect && !bms_state_get()->charging && !_health.powerdown_lower_voltage){
  175. if (bms_state_get()->pack_voltage <= MIN_VOLTAGE_FOR_POWER_DOWN){
  176. debounce_inc(_power_down_voltage);
  177. }else {
  178. debounce_reset(_power_down_voltage);
  179. }
  180. if (debounce_reach_max(_power_down_voltage)){
  181. /*
  182. * no need to clear powerdown(bms is shutdown), when charger insert,
  183. * system will power on with powerdown_lower_voltage cleared
  184. */
  185. _health.powerdown_lower_voltage = 1;
  186. debounce_reset(_power_down_voltage);
  187. }
  188. }
  189. debug_health();
  190. }
  191. /* 检测温度情况,看是否过高温,或者过低温 */
  192. static debounce_t _charger_over_temp = {.count = 0, .max_count = 10, .init_count = 0};
  193. static debounce_t _charger_lower_temp = {.count = 0, .max_count = 10, .init_count = 0};
  194. static debounce_t _charger_normal_temp = {.count = 0, .max_count = 10, .init_count = 0};
  195. static debounce_t _discharger_over_temp = {.count = 0, .max_count = 10, .init_count = 0};
  196. static debounce_t _discharger_lower_temp = {.count = 0, .max_count = 10, .init_count = 0};
  197. static debounce_t _discharger_normal_temp = {.count = 0, .max_count = 10, .init_count = 0};
  198. static int _is_over_temp(int8_t *temps){
  199. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  200. if (measure_value()->pack_temp[i] >= temps[i]){
  201. return 1;
  202. }
  203. }
  204. return 0;
  205. }
  206. static int _is_low_temp(int8_t *temps){
  207. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  208. if (measure_value()->pack_temp[i] <= temps[i]){
  209. return 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. static uint8_t small_power_detect_count = 0;
  215. static void _aux_lock_timer_handler(shark_timer_t *t){
  216. //有可能这个时候,用户发了关小电的指令
  217. if (bms_state_get()->user_request & USER_REQUEST_SMALLCURRENT){
  218. AUX_VOL_OPEN(1);
  219. if (++small_power_detect_count >= 10){
  220. delay_us(1000);
  221. //端口电压小于阈值,判断为小电流短路
  222. if (get_small_current_voltage() < SMALL_CURRENT_MIN_VOLTAGE){//real short
  223. bms_health()->small_current_short = 1;
  224. AUX_VOL_OPEN(0);
  225. small_power_detect_count = 0;
  226. }
  227. }
  228. }
  229. }
  230. static shark_timer_t _aux_lock_timer = {.handler = _aux_lock_timer_handler};
  231. void health_process_aux_lock(void){
  232. if (io_state()->aux_lock_detect) {
  233. if (AUX_VOL_IS_OPEN()){
  234. AUX_VOL_OPEN(0);
  235. shark_timer_post( &_aux_lock_timer, 1);
  236. }
  237. }else {
  238. bms_health()->small_current_short = 0;
  239. small_power_detect_count = 0;
  240. shark_timer_cancel(&_aux_lock_timer);
  241. }
  242. }
  243. void check_temp_state(void){
  244. if (bms_state_get()->charging){
  245. if (!_health.charger_over_temp){
  246. if (_is_over_temp(charger_higher_high_temp)) {//超过允许的最高温度
  247. debounce_inc(_charger_over_temp);
  248. }else {
  249. debounce_reset(_charger_over_temp);
  250. }
  251. if (debounce_reach_max(_charger_over_temp)){
  252. _health.charger_over_temp = 1;
  253. debounce_reset(_charger_over_temp);
  254. }
  255. }
  256. if (!_health.charger_lower_temp){
  257. if (_is_low_temp(charger_lower_low_temp)) {//低于允许的最低温度
  258. debounce_inc(_charger_lower_temp);
  259. }else {
  260. debounce_reset(_charger_lower_temp);
  261. }
  262. if (debounce_reach_max(_charger_lower_temp)) {
  263. _health.charger_lower_temp = 1;
  264. debounce_reset(_charger_lower_temp);
  265. }
  266. }
  267. if (_health.charger_over_temp || _health.charger_lower_temp) {
  268. if (!_is_over_temp(charger_normal_high_temp) && !_is_low_temp(charger_normal_low_temp)){
  269. debounce_inc(_charger_normal_temp);
  270. }else {
  271. debounce_reset(_charger_normal_temp);
  272. }
  273. if (debounce_reach_max(_charger_normal_temp)){
  274. _health.charger_over_temp = 0;
  275. _health.charger_lower_temp = 0;
  276. debounce_reset(_charger_normal_temp);
  277. }
  278. }
  279. }else {
  280. if (!_health.discharger_over_temp){
  281. if (_is_over_temp(discharger_higher_high_temp)) {//超过允许的最高温度
  282. debounce_inc(_discharger_over_temp);
  283. }else {
  284. debounce_reset(_discharger_over_temp);
  285. }
  286. if (debounce_reach_max(_discharger_over_temp)){
  287. _health.discharger_over_temp = 1;
  288. debounce_reset(_discharger_over_temp);
  289. }
  290. }
  291. if (!_health.discharger_lower_temp){
  292. if (_is_over_temp(discharger_lower_low_temp)) {//超过允许的最高温度
  293. debounce_inc(_discharger_lower_temp);
  294. }else {
  295. debounce_reset(_discharger_lower_temp);
  296. }
  297. if (debounce_reach_max(_discharger_lower_temp)) {
  298. _health.discharger_lower_temp = 1;
  299. debounce_reset(_discharger_lower_temp);
  300. }
  301. }
  302. if (_health.discharger_over_temp || _health.discharger_lower_temp) {
  303. if (!_is_over_temp(discharger_normal_high_temp) && !_is_low_temp(discharger_normal_low_temp)){
  304. debounce_inc(_discharger_normal_temp);
  305. }else {
  306. debounce_reset(_discharger_normal_temp);
  307. }
  308. if (debounce_reach_max(_discharger_normal_temp)){
  309. _health.charger_over_temp = 0;
  310. _health.charger_lower_temp = 0;
  311. debounce_reset(_discharger_normal_temp);
  312. }
  313. }
  314. }
  315. debug_health();
  316. }