| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #include "measure.h"
- #include "bsp/ml5238.h"
- #include "bsp/cs1180.h"
- #include "bsp/gd32_adc.h"
- #include "bsp/clock.h"
- #include "bsp/gpio.h"
- #include "bsp/temp_lookup_tab.h"
- #include "bsp/shark_bsp.h"
- #include "libs/logger.h"
- /* measure the temp & current & voltage for battery pack by using
- * ms5238 & cs1180(only used when bms is in small current loading)
- */
- /*
- 1. 小电流
- cs1180(32x):0.305-0.315, 0.277-0.286, 0.255-0.264, 0.220-0.228, 0.184-0.190, 0.137-0.142, 0.062-0.064, 0.011-0.009:0.012
- ml5289(50x):0.307-0.313:0.330,0.277-0.275:0.290,0.254-0.251:0.271,0.221-0.208:0.253,0.183-0.170:0.196,0.136-0.121:0.155,0.061-0.072:0.096, 0.011-0.033:0.060
- 2. 大电流
- cs1180(32x):1.861-2.082
- ml5238(50x):1.869-1.865-1.890
- */
- /* this is the inited gain set to the ms5238, but the really gain is calibrated
- * by measure_system_calibrate
- */
- static float imon_gain_10x = 10.0f;
- static float imon_gain_50x = 50.0f;
- static float imon_gain_now;
- static float vim0_10x = 0.0f;
- static float vim0_50x = 0.0f;
- static float vim0_now;
- #define gain_default_50x 1
- #define CS1180_MAX_CURRENT 4500 //MA, cs1180的最大电流,超过这个使用ML5238
- #define r_pcb_resistor 0.0f // pcb resistor
- static const float r_sense = r_resistor + r_pcb_resistor;
- static const float v_gd_ref = 3300.0f; //adc ref = 3.3v
- static const float max_gd_adc = 65535.0f;
- static const float v_cs1180_ref = 1235.0f;//cs1180 vref = 1.235v
- static const float max_cs1180_adc = 0x7FFFF;//
- static const float small_cur_r_sense = 360.0f;//欧姆
- static void __inline__ select_gain_10x(int select){
- if (select){
- ML5238_IMON_OUT_10X();
- imon_gain_now = imon_gain_10x;
- vim0_now = vim0_10x;
- }else {
- ML5238_IMON_OUT_50X();
- imon_gain_now = imon_gain_50x;
- vim0_now = vim0_50x;
- }
- }
- static int __inline__ _is_x10_gain(void){
- return imon_gain_now == imon_gain_10x;
- }
- static void current_10x_calibrate(void){
- /* calibrate the 10x gain */
- ML5238_IMON_OUT_ZERO_10X();
- vim0_10x = adc_sample_avg(ADC_CHAN_IMON, 10);
- ML5238_IMON_OUT_V2000_10X();
- float vim1 = adc_sample_avg(ADC_CHAN_IMON, 10);
- ML5238_IMON_OUT_V100_10X();
- float vr = adc_sample_avg(ADC_CHAN_IMON, 10);
- imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr);
- }
- static void current_50x_calibrate(void){
- /* calibrate the 50x gain */
- ML5238_IMON_OUT_ZERO_50X();
- vim0_50x = adc_sample_avg(ADC_CHAN_IMON, 10);
- ML5238_IMON_OUT_V2000_50X();
- float vim1 = adc_sample_avg(ADC_CHAN_IMON, 10);
- ML5238_IMON_OUT_V100_50X();
- float vr = adc_sample_avg(ADC_CHAN_IMON, 10);
- imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr);
- }
- /*calibrate when startup && temperature is changed more than 5? degree
- * calibrate the ms5238's IMON output voltage gain
- */
- void current_calibrate(void){
- #ifdef gain_default_50x
- current_50x_calibrate();
- select_gain_10x(0);
- #else
- current_10x_calibrate();
- select_gain_10x(1);
- #endif
- }
- void measure_adc_init(void){
- ml5238_init();
- gd32_adc_init();
- current_calibrate();
- cs1180_adc_init();
- set_log_level(MOD_SYSTEM, L_debug);
- }
- /* get battery pack's current (mA) */
- static float get_pack_current_by_gd(void){
- float adc = adc_sample_avg(ADC_CHAN_IMON, 10);
- if (adc >= 0xFFF0 && (!_is_x10_gain())){//overflow, use 10x gain
- current_10x_calibrate();
- select_gain_10x(1);
- adc = adc_sample_avg(ADC_CHAN_IMON, 10);
- }else if (adc <= 0x1F && (_is_x10_gain())){// is too small, select 50x gain
- current_50x_calibrate();
- select_gain_10x(0);
- adc = adc_sample_avg(ADC_CHAN_IMON, 10);
- }
- float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
- return (int)((cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000);
- }
- static float get_pack_current_by_cs1180(void){
- float adc = cs1180_adc_sample();
- return (adc / max_cs1180_adc) * v_cs1180_ref / r_sense * 1000;
- }
- float get_pack_current(void){
- if (!cs1180_is_ready()){ //if cs1180 is not ready, just use gd adc
- return get_pack_current_by_gd();
- }
- float current = get_pack_current_by_cs1180();
- if (abs(current) >= CS1180_MAX_CURRENT){
- return get_pack_current_by_gd();
- }
- sys_debug("%f, %f\n", current, get_pack_current_by_gd());
- return current;
- }
- /* get cell's voltage (mV) */
- float get_cell_voltage(int cell){
- ML5238_SELECT_CELL(cell);
- delay_us(100);
- float adc = adc_sample_avg(ADC_CHAN_VMON, 10);
- return cell_real_vol((adc / max_gd_adc) * v_gd_ref);
- }
- /* get battery pack's aux current (MA) */
- float get_small_current(void){
- float adc = adc_sample_avg(ADC_CHAN_AUX_CURR, 10);
- return ((adc / max_gd_adc * v_gd_ref)) / small_cur_r_sense;
- }
- /* 用来判断小电流的情况下,电压小于某一个值认为小电流真正短路,比如16v*/
- float get_small_current_voltage(void){
- float s_current_a = get_small_current();//MA
- return s_current_a * (small_cur_r_sense + 28.0f);//28欧姆是mos的D极两个56的并联
- }
- int get_pcb_temperature(void){
- TEMP_OPEN(1);
- delay_us(100);
- uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_4, TRUE);
- TEMP_OPEN(0);
- return get_temp_by_adc(adc);
- }
- /*
- * index : 0...3, 3 indicat pcb temp
- */
- int get_pack_temperature(int index){
- TEMP_OPEN(1);
- delay_us(100);
- uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_1 + index, TRUE);
- TEMP_OPEN(0);
- return get_temp_by_adc(adc);
- }
|