health.c 10.0 KB

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