health.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 (34 * 1000) //允许能放电的最小电压
  9. #define MIN_VOLTAGE_FOR_RECOVERY_DISCHARGER (40 * 1000) //恢复放电的最小电压
  10. #define MIN_VOLTAGE_FOR_POWER_DOWN (32 * 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 = 0, .max_count = 10};
  115. static debounce_t _discharger_low_normal_voltage = {.count = 0, .max_count = 10};
  116. static debounce_t _power_down_voltage = {.count = 0, .max_count = 10};
  117. static debounce_t _sigle_cell_discharger_lower_vol = {.count = 5, .max_count = 10};
  118. static debounce_t _sigle_cell_charger_max_vol = {.count = 15, .max_count = 30};
  119. void check_voltage_state(void) {
  120. if (bms_state_get()->charging){
  121. if (bms_state_get()->cell_max_vol>= SIGLE_CELL_MAX_CHARGER_VOLTAGE){
  122. debounce_inc(_sigle_cell_charger_max_vol);
  123. }else {
  124. debounce_dec(_sigle_cell_charger_max_vol);
  125. }
  126. if (debounce_reach_max(_sigle_cell_charger_max_vol)){
  127. _health.sigle_cell_over_voltage = 1;
  128. }else if (debounce_reach_zero(_sigle_cell_charger_max_vol)){
  129. _health.sigle_cell_over_voltage = 0;
  130. }
  131. }else if (bms_state_get()->discharging){
  132. if (bms_state_get()->cell_min_vol <= SIGLE_CELL_LOWER_DISCHARGER_VOLTAGE){
  133. debounce_inc(_sigle_cell_discharger_lower_vol);
  134. }else {
  135. debounce_dec(_sigle_cell_discharger_lower_vol);
  136. }
  137. if (debounce_reach_max(_sigle_cell_discharger_lower_vol)){
  138. _health.sigle_cell_lower_voltage = 1;
  139. }else if (debounce_reach_zero(_sigle_cell_discharger_lower_vol)){
  140. _health.sigle_cell_lower_voltage = 0;
  141. }
  142. }
  143. if (!_health.discharger_lower_voltage){ //check for low pack voltage for close discharger
  144. if (bms_state_get()->pack_voltage <= MIN_VOLTAGE_FOR_DISCHARGER){
  145. debounce_inc(_discharger_lower_voltage);
  146. }else {
  147. debounce_reset(_discharger_lower_voltage);
  148. }
  149. if (debounce_reach_max(_discharger_lower_voltage)){
  150. _health.discharger_lower_voltage = 1;
  151. debounce_reset(_discharger_lower_voltage);
  152. }
  153. }else { //check for discharger recovery
  154. if (bms_state_get()->pack_voltage >= MIN_VOLTAGE_FOR_RECOVERY_DISCHARGER){
  155. debounce_inc(_discharger_low_normal_voltage);
  156. }else {
  157. debounce_reset(_discharger_low_normal_voltage);
  158. }
  159. if (debounce_reach_max(_discharger_low_normal_voltage)){
  160. _health.discharger_lower_voltage = 0;
  161. debounce_reset(_discharger_low_normal_voltage);
  162. }
  163. }
  164. /* check for power down */
  165. if (!io_state()->charger_detect && !bms_state_get()->charging && !_health.powerdown_lower_voltage){
  166. if (bms_state_get()->pack_voltage >= MIN_VOLTAGE_FOR_POWER_DOWN){
  167. debounce_inc(_power_down_voltage);
  168. }else {
  169. debounce_reset(_power_down_voltage);
  170. }
  171. if (debounce_reach_max(_power_down_voltage)){
  172. /*
  173. * no need to clear powerdown(bms is shutdown), when charger insert,
  174. * system will power on with powerdown_lower_voltage cleared
  175. */
  176. _health.powerdown_lower_voltage = 1;
  177. debounce_reset(_power_down_voltage);
  178. }
  179. }
  180. debug_health();
  181. }
  182. /* 检测温度情况,看是否过高温,或者过低温 */
  183. static debounce_t _charger_over_temp = {.count = 0, .max_count = 10};
  184. static debounce_t _charger_lower_temp = {.count = 0, .max_count = 10};
  185. static debounce_t _charger_normal_temp = {.count = 0, .max_count = 10};
  186. static debounce_t _discharger_over_temp = {.count = 0, .max_count = 10};
  187. static debounce_t _discharger_lower_temp = {.count = 0, .max_count = 10};
  188. static debounce_t _discharger_normal_temp = {.count = 0, .max_count = 10};
  189. static int _is_over_temp(int8_t *temps){
  190. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  191. if (measure_value()->pack_temp[i] >= temps[i]){
  192. return 1;
  193. }
  194. }
  195. return 0;
  196. }
  197. static int _is_low_temp(int8_t *temps){
  198. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  199. if (measure_value()->pack_temp[i] <= temps[i]){
  200. return 1;
  201. }
  202. }
  203. return 0;
  204. }
  205. void check_temp_state(void){
  206. if (bms_state_get()->charging){
  207. if (!_health.charger_over_temp){
  208. if (_is_over_temp(charger_higher_high_temp)) {//超过允许的最高温度
  209. debounce_inc(_charger_over_temp);
  210. }else {
  211. debounce_reset(_charger_over_temp);
  212. }
  213. if (debounce_reach_max(_charger_over_temp)){
  214. _health.charger_over_temp = 1;
  215. debounce_reset(_charger_over_temp);
  216. }
  217. }
  218. if (!_health.charger_lower_temp){
  219. if (_is_low_temp(charger_lower_low_temp)) {//低于允许的最低温度
  220. debounce_inc(_charger_lower_temp);
  221. }else {
  222. debounce_reset(_charger_lower_temp);
  223. }
  224. if (debounce_reach_max(_charger_lower_temp)) {
  225. _health.charger_lower_temp = 1;
  226. debounce_reset(_charger_lower_temp);
  227. }
  228. }
  229. if (_health.charger_over_temp || _health.charger_lower_temp) {
  230. if (!_is_over_temp(charger_normal_high_temp) && !_is_low_temp(charger_normal_low_temp)){
  231. debounce_inc(_charger_normal_temp);
  232. }else {
  233. debounce_reset(_charger_normal_temp);
  234. }
  235. if (debounce_reach_max(_charger_normal_temp)){
  236. _health.charger_over_temp = 0;
  237. _health.charger_lower_temp = 0;
  238. debounce_reset(_charger_normal_temp);
  239. }
  240. }
  241. }else {
  242. if (!_health.discharger_over_temp){
  243. if (_is_over_temp(discharger_higher_high_temp)) {//超过允许的最高温度
  244. debounce_inc(_discharger_over_temp);
  245. }else {
  246. debounce_reset(_discharger_over_temp);
  247. }
  248. if (debounce_reach_max(_discharger_over_temp)){
  249. _health.discharger_over_temp = 1;
  250. debounce_reset(_discharger_over_temp);
  251. }
  252. }
  253. if (!_health.discharger_lower_temp){
  254. if (_is_over_temp(discharger_lower_low_temp)) {//超过允许的最高温度
  255. debounce_inc(_discharger_lower_temp);
  256. }else {
  257. debounce_reset(_discharger_lower_temp);
  258. }
  259. if (debounce_reach_max(_discharger_lower_temp)) {
  260. _health.discharger_lower_temp = 1;
  261. debounce_reset(_discharger_lower_temp);
  262. }
  263. }
  264. if (_health.discharger_over_temp || _health.discharger_lower_temp) {
  265. if (!_is_over_temp(discharger_normal_high_temp) && !_is_low_temp(discharger_normal_low_temp)){
  266. debounce_inc(_discharger_normal_temp);
  267. }else {
  268. debounce_reset(_discharger_normal_temp);
  269. }
  270. if (debounce_reach_max(_discharger_normal_temp)){
  271. _health.charger_over_temp = 0;
  272. _health.charger_lower_temp = 0;
  273. debounce_reset(_discharger_normal_temp);
  274. }
  275. }
  276. }
  277. debug_health();
  278. }