health.c 12 KB

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