state.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "bsp/gpio.h"
  2. #include "bsp/ml5238.h"
  3. #include "bsp/mcu_power_sleep.h"
  4. #include "app/sox/measure.h"
  5. #include "app/sox/measure_task.h"
  6. #include "libs/shark_task.h"
  7. #include "libs/logger.h"
  8. #include "health.h"
  9. #include "soc.h"
  10. #include "state.h"
  11. #include "iostate.h"
  12. #define ALLOW_DEEP_SLEEP 0
  13. #define ALLOW_POWER_DOWN 1
  14. #define ALLOW_5238_BALANCE 1
  15. static void _current_notify(void);
  16. static void _voltage_notify(void);
  17. static void _temperature_notify(void);
  18. static u32 _bms_main_task_handler(void);
  19. static void _balance_timer_handler(shark_timer_t *t);
  20. static bms_state_t _bms_state;
  21. static shark_task_t _bms_main_task = {.handler = _bms_main_task_handler};
  22. static shark_timer_t _balance_timer = {.handler = _balance_timer_handler};
  23. void bms_state_init(void){
  24. _bms_state.cell_index_of_max_vol = 0xff;
  25. measure_task_init(_current_notify, _voltage_notify, _temperature_notify);
  26. io_state_init();
  27. health_init();
  28. soc_init();
  29. shark_task_add(&_bms_main_task);
  30. set_log_level(MOD_STATE, L_debug);
  31. state_debug("BMS System Starting......\n");
  32. }
  33. bms_state_t *bms_state_get(void){
  34. return &_bms_state;
  35. }
  36. /*
  37. 放电mos和充电mos的开关要小心:
  38. 1. 大部分的情况下,尽量能做到同时开关,主要是用来保护被关闭那路mos的体二极管(不能过大电流)
  39. 2. 充电过压的情况下,必须要关闭充电mos,但是这个时候放电mos可能是打开的,这样的情况下,
  40. 需要检测放电电流,超过4A必须强制打开充电mos,防止烧充电mos的体二极管
  41. 3. 收到打开大电的指令后,必须两个mos都要打开,然后再经过2的判断
  42. 。。。。。
  43. */
  44. static void discharger_open(int open){
  45. /* 打开大电前,先打开短路保护*/
  46. if (open) {
  47. ml5238_short_current_detect(SHORT_CURRENT_MODE_100A_200A);//SP600:100A, SP700:200A
  48. }else {
  49. ml5238_short_current_detect(SHORT_CURRENT_MODE_DISABLE);
  50. }
  51. ml5238_enable_discharger_mosfet(open);
  52. }
  53. static void charger_open(int open) {
  54. ml5238_enable_charger_mosfet(open);
  55. }
  56. #define Health_Success 0
  57. #define Health_Discharger_Failt 1
  58. #define Health_charger_Fault 2
  59. #define Health_aux_Fault 4
  60. static s32 _process_unheath(void){
  61. if (bms_health()->load_current_short) {//短路检测后,关闭充放电mos
  62. discharger_open(0);
  63. charger_open(0); //disable charger mosfet
  64. AUX_VOL_OPEN(0);
  65. _bms_state.charging = 0;
  66. return (Health_Discharger_Failt | Health_charger_Fault);
  67. }
  68. if (bms_health()->charger_over_current || bms_health()->charger_over_temp || bms_health()->charger_lower_temp){
  69. charger_open(0); //disable charger mosfet
  70. _bms_state.charging = 0;
  71. return Health_charger_Fault;
  72. }
  73. if (bms_health()->discharger_over_temp || bms_health()->discharger_lower_temp || bms_health()->discharger_lower_voltage){
  74. discharger_open(0); //disable charger mosfet
  75. return Health_Discharger_Failt;
  76. }
  77. if (io_state()->aux_lock_detect || bms_health()->small_current_short) {
  78. return Health_aux_Fault;
  79. }
  80. return Health_Success;
  81. }
  82. //处理PS100/310/320/360,充电底座,充电柜的指令或者bms自己发给自己的指令
  83. static void _process_user_request(s32 health){
  84. if (_bms_state.user_request & USER_REQUEST_PENDING){
  85. if ((health & Health_charger_Fault) == 0){
  86. charger_open(!!(_bms_state.user_request & USER_REQUEST_CHARGER));
  87. }
  88. if ((health & Health_Discharger_Failt) == 0){
  89. discharger_open(!!(_bms_state.user_request & USER_REQUEST_DISCHARGER));
  90. }
  91. if ((health & Health_aux_Fault) == 0){
  92. AUX_VOL_OPEN(!!(_bms_state.user_request & USER_REQUEST_SMALLCURRENT));
  93. }
  94. _bms_state.user_request &= ~USER_REQUEST_PENDING;//clear user request pending
  95. }
  96. }
  97. static void _process_power_down(void){
  98. #if (ALLOW_POWER_DOWN==1)
  99. if (bms_health()->powerdown_lower_voltage){
  100. ml5238_enable_charger_detect(AUX_VOL_IS_OPEN(), 1);
  101. delay_us(2* 1000);
  102. if (!ml5238_charger_is_disconnect(AUX_VOL_IS_OPEN())){//have charger, do'nt power down
  103. bms_health()->powerdown_lower_voltage = 0;
  104. return;
  105. }
  106. AUX_VOL_OPEN(0);
  107. CS1180_PWR_ENABLE(0);
  108. discharger_open(0);
  109. charger_open(0);
  110. ml5238_power_down();
  111. }
  112. #endif
  113. }
  114. static void _process_deepsleep(s32 health){
  115. #if (ALLOW_DEEP_SLEEP==1)
  116. if (health != Health_Success){
  117. return;
  118. }
  119. if (ml5238_is_charging() || ml5238_is_discharging() || IS_CHARGER_IN()){
  120. return;
  121. }
  122. if(io_state()->hall_detect){
  123. return;
  124. }
  125. /*if (红外或者485通信没有超时){
  126. return;
  127. }*/
  128. AUX_VOL_OPEN(1);
  129. delay_us(5000);//delay 5ms, to detect if short current
  130. if (io_state()->aux_lock_detect){
  131. return;
  132. }
  133. mcu_enter_deepsleep();
  134. #endif
  135. }
  136. static void _process_iostate_changed(void){
  137. if (!(io_state()->hall_detect)){
  138. if (ml5238_is_discharging()){
  139. discharger_open(0);
  140. }
  141. if (!AUX_VOL_IS_OPEN()){
  142. AUX_VOL_OPEN(1);
  143. }
  144. }
  145. if (IS_CHARGER_IN()) {
  146. if (!ml5238_is_charging()){
  147. charger_open(1);
  148. }
  149. }
  150. }
  151. static u32 _bms_main_task_handler(void){
  152. s32 unhealth = _process_unheath();
  153. _process_user_request(unhealth);
  154. _process_deepsleep(unhealth);
  155. _process_power_down();
  156. _process_iostate_changed();
  157. return 0;
  158. }
  159. static debounce_t _charging_detect = {.count = 10, .max_count = 20, .init_count = 10};
  160. static void check_charging(){
  161. if ((measure_value()->load_current >= MIN_START_CHARGER_CURRENT) && !_bms_state.charging) {
  162. debounce_inc(_charging_detect);
  163. if (debounce_reach_max(_charging_detect)){
  164. _bms_state.charging = 1;
  165. debounce_reset(_charging_detect);
  166. }
  167. }else if ((measure_value()->load_current < MIN_START_CHARGER_CURRENT) && _bms_state.charging){
  168. debounce_dec(_charging_detect);
  169. if (debounce_reach_zero(_charging_detect)){
  170. _bms_state.charging = 1;
  171. debounce_reset(_charging_detect);
  172. }
  173. }
  174. }
  175. static void _current_notify(void){
  176. check_current_state(); //check health of current
  177. check_charging();
  178. soc_update(); //计算soc
  179. }
  180. /* 需要检查电芯的电压,如果发现有电芯电压过高,需要开启被动均衡
  181. * 充电过程中考虑balance,主要是希望cell 电压扩散后,保证1. 单电芯不能过压, 2. 单电芯不能比平均电压过低,导致
  182. * 木桶效应,目标是电压最高的那个cell,尽量压制,不让电压再升高,或者升高的尽量慢一些
  183. */
  184. static debounce_t _cell_balance = {.count = 10, .max_count = 20};
  185. static void _balance_timer_handler(shark_timer_t *t){
  186. ml5238_cell_start_balance(0);
  187. _bms_state.pack_balancing = 0;
  188. }
  189. static void check_cell_balance(uint8_t current_max_index){
  190. if (!_bms_state.charging){ //not charging, need not do balance
  191. if (_bms_state.pack_balancing){
  192. _bms_state.pack_balancing = 0;
  193. _cell_balance.count = 10;
  194. ml5238_cell_start_balance(0);
  195. }
  196. return;
  197. }
  198. if (!_bms_state.pack_balancing && _bms_state.cell_min_vol < CELL_FUSION_VOLTAGE){
  199. return;
  200. }
  201. if (_bms_state.cell_max_vol - _bms_state.cell_min_vol >= MAX_DIFF_BETWEEN_MIN_MAX_CELL){
  202. debounce_inc(_cell_balance);
  203. }else {
  204. debounce_dec(_cell_balance);
  205. }
  206. if (!_bms_state.pack_balancing && debounce_reach_max(_cell_balance)){
  207. _bms_state.pack_balancing = 1;
  208. }else if (_bms_state.pack_balancing && debounce_reach_zero(_cell_balance)){
  209. _bms_state.pack_balancing = 0;
  210. ml5238_cell_start_balance(0);
  211. }
  212. if (_bms_state.pack_balancing && (current_max_index != _bms_state.cell_index_of_max_vol)){
  213. ml5238_cell_start_balance(BIT(current_max_index));
  214. shark_timer_post(&_balance_timer, 60 * 1000); //stop balance after 1 minute
  215. }
  216. _bms_state.cell_index_of_max_vol = current_max_index;
  217. }
  218. static uint8_t calc_cell_voltage(void){
  219. uint16_t voltage = 0;
  220. uint16_t max_cell = 0;
  221. uint16_t min_cell = 0xf000;
  222. uint8_t max_index = 0;
  223. for (int i = 0; i < CELLS_NUM; i++){
  224. voltage += measure_value()->cell_vol[i];
  225. if (max_cell > measure_value()->cell_vol[i]){
  226. max_cell = measure_value()->cell_vol[i];
  227. max_index = i;
  228. }
  229. if (min_cell < measure_value()->cell_vol[i]){
  230. min_cell = measure_value()->cell_vol[i];
  231. }
  232. }
  233. _bms_state.pack_voltage = voltage;
  234. _bms_state.cell_max_vol = max_cell;
  235. _bms_state.cell_min_vol = min_cell;
  236. return max_index;
  237. }
  238. static void _voltage_notify(void){
  239. uint8_t max_index = calc_cell_voltage();
  240. check_voltage_state(); //check health of cell voltage
  241. #if (ALLOW_5238_BALANCE==1)
  242. check_cell_balance(max_index);
  243. #endif
  244. }
  245. static void _temperature_notify(void){
  246. check_temp_state(); //check health of cell/pcb temperature
  247. }