health.c 11 KB

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