health.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. }
  35. bms_health_t *bms_health(){
  36. return &_health;
  37. }
  38. static void init_detect_timer(void){
  39. _load_detect_timer._timer.handler = load_delect_handler;
  40. _load_detect_timer.max_count = 100;
  41. _load_detect_timer.interval = 10;
  42. _charger_detect_timer._timer.handler = charger_detect_handler;
  43. _charger_detect_timer.max_count = 100;
  44. _charger_detect_timer.interval = 10;
  45. }
  46. static void load_delect_handler(shark_timer_t *timer){
  47. if (ml5238_is_load_disconnect()){
  48. _load_detect_timer.count ++;
  49. }else {
  50. _load_detect_timer.count = 0;
  51. }
  52. if (_load_detect_timer.count >= _load_detect_timer.max_count) {
  53. _health.load_current_short = 0; //负载移除,clear load current short
  54. ml5238_enable_load_detect(0);
  55. health_debug("load disconnect\n");
  56. }else {
  57. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  58. }
  59. }
  60. static void charger_detect_handler(shark_timer_t *timer){
  61. if (!io_state()->charger_detect) {
  62. _charger_detect_timer.count ++;
  63. }else {
  64. _charger_detect_timer.count = 0;
  65. }
  66. if (_charger_detect_timer.count >= _charger_detect_timer.max_count){
  67. _health.charger_over_current = 0;
  68. }else {
  69. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  70. }
  71. }
  72. static void check_ml5238_state(int event){
  73. health_warning("ml5238 event=0x%x\n", event);
  74. if (event == ML5238_Event_Charger_Over_Current){
  75. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  76. }else if (event == ML5238_Event_Short_Current) { //ml5238触发短路保护,充放电mos全部关闭
  77. _health.load_current_short = 1;
  78. ml5238_enable_load_detect(1); //打开负载检测
  79. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  80. }else if (event == ML5238_Event_Load_Disconnect) {
  81. shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
  82. }
  83. }
  84. static void debug_health(void){
  85. uint32_t *value = (uint32_t *)&_health;
  86. if (*value != 0){
  87. health_error("health value = 0x%x\n", *value);
  88. }
  89. }
  90. /* 检测电流情况,看是否过流等 */
  91. static debounce_t _charger_over_current = {
  92. .count = 0,
  93. .max_count = 10
  94. };
  95. void check_current_state(void){
  96. //判断是否过流充电,放电过流通过5238来检测
  97. if (bms_state_get()->charging && !_health.charger_over_current) {
  98. float current = measure_value()->load_current;
  99. if (current > MAX_CURRENT_FOR_CHARGER) {
  100. _charger_over_current.count ++;
  101. }else {
  102. _charger_over_current.count = 0;
  103. }
  104. if (_charger_over_current.count >= _charger_over_current.max_count){
  105. _health.charger_over_current = 1;
  106. _charger_over_current.count = 0;
  107. shark_timer_post(&_charger_detect_timer._timer, _charger_detect_timer.interval);
  108. }
  109. }
  110. debug_health();
  111. }
  112. /* 检测pack电压,cell电压,pack电压过低触发powerdown*/
  113. static debounce_t _discharger_lower_voltage = {.count = 0, .max_count = 10};
  114. static debounce_t _discharger_low_normal_voltage = {.count = 0, .max_count = 10};
  115. static debounce_t _power_down_voltage = {.count = 0, .max_count = 10};
  116. static debounce_t _sigle_cell_discharger_lower_vol = {.count = 5, .max_count = 10};
  117. static debounce_t _sigle_cell_charger_max_vol = {.count = 15, .max_count = 30};
  118. void check_voltage_state(void) {
  119. if (bms_state_get()->charging){
  120. if (bms_state_get()->cell_max_vol>= SIGLE_CELL_MAX_CHARGER_VOLTAGE){
  121. debounce_inc(_sigle_cell_charger_max_vol);
  122. }else {
  123. debounce_dec(_sigle_cell_charger_max_vol);
  124. }
  125. if (debounce_reach_max(_sigle_cell_charger_max_vol)){
  126. _health.sigle_cell_over_voltage = 1;
  127. }else if (debounce_reach_zero(_sigle_cell_charger_max_vol)){
  128. _health.sigle_cell_over_voltage = 0;
  129. }
  130. }else if (bms_state_get()->discharging){
  131. if (bms_state_get()->cell_min_vol <= SIGLE_CELL_LOWER_DISCHARGER_VOLTAGE){
  132. debounce_inc(_sigle_cell_discharger_lower_vol);
  133. }else {
  134. debounce_dec(_sigle_cell_discharger_lower_vol);
  135. }
  136. if (debounce_reach_max(_sigle_cell_discharger_lower_vol)){
  137. _health.sigle_cell_lower_voltage = 1;
  138. }else if (debounce_reach_zero(_sigle_cell_discharger_lower_vol)){
  139. _health.sigle_cell_lower_voltage = 0;
  140. }
  141. }
  142. if (!_health.discharger_lower_voltage){ //check for low pack voltage for close discharger
  143. if (bms_state_get()->pack_voltage <= MIN_VOLTAGE_FOR_DISCHARGER){
  144. debounce_inc(_discharger_lower_voltage);
  145. }else {
  146. debounce_reset(_discharger_lower_voltage);
  147. }
  148. if (debounce_reach_max(_discharger_lower_voltage)){
  149. _health.discharger_lower_voltage = 1;
  150. debounce_reset(_discharger_lower_voltage);
  151. }
  152. }else { //check for discharger recovery
  153. if (bms_state_get()->pack_voltage >= MIN_VOLTAGE_FOR_RECOVERY_DISCHARGER){
  154. debounce_inc(_discharger_low_normal_voltage);
  155. }else {
  156. debounce_reset(_discharger_low_normal_voltage);
  157. }
  158. if (debounce_reach_max(_discharger_low_normal_voltage)){
  159. _health.discharger_lower_voltage = 0;
  160. debounce_reset(_discharger_low_normal_voltage);
  161. }
  162. }
  163. /* check for power down */
  164. if (!io_state()->charger_detect && !bms_state_get()->charging && !_health.powerdown_lower_voltage){
  165. if (bms_state_get()->pack_voltage >= MIN_VOLTAGE_FOR_POWER_DOWN){
  166. debounce_inc(_power_down_voltage);
  167. }else {
  168. debounce_reset(_power_down_voltage);
  169. }
  170. if (debounce_reach_max(_power_down_voltage)){
  171. /*
  172. * no need to clear powerdown(bms is shutdown), when charger insert,
  173. * system will power on with powerdown_lower_voltage cleared
  174. */
  175. _health.powerdown_lower_voltage = 1;
  176. debounce_reset(_power_down_voltage);
  177. }
  178. }
  179. debug_health();
  180. }
  181. /* 检测温度情况,看是否过高温,或者过低温 */
  182. static debounce_t _charger_over_temp = {.count = 0, .max_count = 10};
  183. static debounce_t _charger_lower_temp = {.count = 0, .max_count = 10};
  184. static debounce_t _charger_normal_temp = {.count = 0, .max_count = 10};
  185. static debounce_t _discharger_over_temp = {.count = 0, .max_count = 10};
  186. static debounce_t _discharger_lower_temp = {.count = 0, .max_count = 10};
  187. static debounce_t _discharger_normal_temp = {.count = 0, .max_count = 10};
  188. static int _is_over_temp(int8_t *temps){
  189. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  190. if (measure_value()->pack_temp[i] >= temps[i]){
  191. return 1;
  192. }
  193. }
  194. return 0;
  195. }
  196. static int _is_low_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. void check_temp_state(void){
  205. if (bms_state_get()->charging){
  206. if (!_health.charger_over_temp){
  207. if (_is_over_temp(charger_higher_high_temp)) {//超过允许的最高温度
  208. debounce_inc(_charger_over_temp);
  209. }else {
  210. debounce_reset(_charger_over_temp);
  211. }
  212. if (debounce_reach_max(_charger_over_temp)){
  213. _health.charger_over_temp = 1;
  214. debounce_reset(_charger_over_temp);
  215. }
  216. }
  217. if (!_health.charger_lower_temp){
  218. if (_is_low_temp(charger_lower_low_temp)) {//低于允许的最低温度
  219. debounce_inc(_charger_lower_temp);
  220. }else {
  221. debounce_reset(_charger_lower_temp);
  222. }
  223. if (debounce_reach_max(_charger_lower_temp)) {
  224. _health.charger_lower_temp = 1;
  225. debounce_reset(_charger_lower_temp);
  226. }
  227. }
  228. if (_health.charger_over_temp || _health.charger_lower_temp) {
  229. if (!_is_over_temp(charger_normal_high_temp) && !_is_low_temp(charger_normal_low_temp)){
  230. debounce_inc(_charger_normal_temp);
  231. }else {
  232. debounce_reset(_charger_normal_temp);
  233. }
  234. if (debounce_reach_max(_charger_normal_temp)){
  235. _health.charger_over_temp = 0;
  236. _health.charger_lower_temp = 0;
  237. debounce_reset(_charger_normal_temp);
  238. }
  239. }
  240. }else {
  241. if (!_health.discharger_over_temp){
  242. if (_is_over_temp(discharger_higher_high_temp)) {//超过允许的最高温度
  243. debounce_inc(_discharger_over_temp);
  244. }else {
  245. debounce_reset(_discharger_over_temp);
  246. }
  247. if (debounce_reach_max(_discharger_over_temp)){
  248. _health.discharger_over_temp = 1;
  249. debounce_reset(_discharger_over_temp);
  250. }
  251. }
  252. if (!_health.discharger_lower_temp){
  253. if (_is_over_temp(discharger_lower_low_temp)) {//超过允许的最高温度
  254. debounce_inc(_discharger_lower_temp);
  255. }else {
  256. debounce_reset(_discharger_lower_temp);
  257. }
  258. if (debounce_reach_max(_discharger_lower_temp)) {
  259. _health.discharger_lower_temp = 1;
  260. debounce_reset(_discharger_lower_temp);
  261. }
  262. }
  263. if (_health.discharger_over_temp || _health.discharger_lower_temp) {
  264. if (!_is_over_temp(discharger_normal_high_temp) && !_is_low_temp(discharger_normal_low_temp)){
  265. debounce_inc(_discharger_normal_temp);
  266. }else {
  267. debounce_reset(_discharger_normal_temp);
  268. }
  269. if (debounce_reach_max(_discharger_normal_temp)){
  270. _health.charger_over_temp = 0;
  271. _health.charger_lower_temp = 0;
  272. debounce_reset(_discharger_normal_temp);
  273. }
  274. }
  275. }
  276. debug_health();
  277. }