measure.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "measure.h"
  2. #include "bsp/ml5238.h"
  3. #include "bsp/cs1180.h"
  4. #include "bsp/gd32_adc.h"
  5. #include "bsp/clock.h"
  6. #include "bsp/gpio.h"
  7. #include "bsp/temp_lookup_tab.h"
  8. #include "bsp/shark_bsp.h"
  9. /* measure the temp & current & voltage for battery pack by using
  10. * ms5238 & cs1180(only used when bms is in small current loading)
  11. */
  12. /*
  13. 1. 小电流
  14. 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
  15. 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
  16. 2. 大电流
  17. cs1180(32x):1.861-2.082
  18. ml5238(50x):1.869-1.865-1.890
  19. */
  20. /* this is the inited gain set to the ms5238, but the really gain is calibrated
  21. * by measure_system_calibrate
  22. */
  23. static float imon_gain_10x = 10.0f;
  24. static float imon_gain_50x = 50.0f;
  25. static float imon_gain_now;
  26. static float vim0_10x = 0.0f;
  27. static float vim0_50x = 0.0f;
  28. static float vim0_now;
  29. #define gain_default_50x 1
  30. #define CS1180_MAX_CURRENT 4500 //MA, cs1180的最大电流,超过这个使用ML5238
  31. #define r_pcb_resistor 0.0f // pcb resistor
  32. static const float r_sense = r_resistor + r_pcb_resistor;
  33. static const float v_gd_ref = 3300.0f; //adc ref = 3.3v
  34. static const float max_gd_adc = 65535.0f;
  35. static const float v_cs1180_ref = 1235.0f;//cs1180 vref = 1.235v
  36. static const float max_cs1180_adc = 0x7FFFF;//
  37. static const float small_cur_r_sense = 360.0f;//欧姆
  38. static void __inline__ select_gain_10x(int select){
  39. if (select){
  40. ML5238_IMON_OUT_10X();
  41. imon_gain_now = imon_gain_10x;
  42. vim0_now = vim0_10x;
  43. }else {
  44. ML5238_IMON_OUT_50X();
  45. imon_gain_now = imon_gain_50x;
  46. vim0_now = vim0_50x;
  47. }
  48. }
  49. static int __inline__ _is_x10_gain(void){
  50. return imon_gain_now == imon_gain_10x;
  51. }
  52. static void current_10x_calibrate(void){
  53. /* calibrate the 10x gain */
  54. ML5238_IMON_OUT_ZERO_10X();
  55. vim0_10x = adc_sample_avg(ADC_CHAN_IMON, 10);
  56. ML5238_IMON_OUT_V2000_10X();
  57. float vim1 = adc_sample_avg(ADC_CHAN_IMON, 10);
  58. ML5238_IMON_OUT_V100_10X();
  59. float vr = adc_sample_avg(ADC_CHAN_IMON, 10);
  60. imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr);
  61. }
  62. static void current_50x_calibrate(void){
  63. /* calibrate the 50x gain */
  64. ML5238_IMON_OUT_ZERO_50X();
  65. vim0_50x = adc_sample_avg(ADC_CHAN_IMON, 10);
  66. ML5238_IMON_OUT_V2000_50X();
  67. float vim1 = adc_sample_avg(ADC_CHAN_IMON, 10);
  68. ML5238_IMON_OUT_V100_50X();
  69. float vr = adc_sample_avg(ADC_CHAN_IMON, 10);
  70. imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr);
  71. }
  72. /*calibrate when startup && temperature is changed more than 5? degree
  73. * calibrate the ms5238's IMON output voltage gain
  74. */
  75. void current_calibrate(void){
  76. #ifdef gain_default_50x
  77. current_50x_calibrate();
  78. select_gain_10x(0);
  79. #else
  80. current_10x_calibrate();
  81. select_gain_10x(1);
  82. #endif
  83. }
  84. void measure_adc_init(void){
  85. ml5238_init();
  86. adc_init();
  87. current_calibrate();
  88. cs1180_adc_init();
  89. }
  90. /* get battery pack's current (mA) */
  91. static float get_pack_current_by_gd(void){
  92. float adc = adc_sample_avg(ADC_CHAN_IMON, 10);
  93. if (adc >= 0xFFF0 && (!_is_x10_gain())){//overflow, use 10x gain
  94. current_10x_calibrate();
  95. select_gain_10x(1);
  96. adc = adc_sample_avg(ADC_CHAN_IMON, 10);
  97. }else if (adc <= 0x1F && (_is_x10_gain())){// is too small, select 50x gain
  98. current_50x_calibrate();
  99. select_gain_10x(0);
  100. adc = adc_sample_avg(ADC_CHAN_IMON, 10);
  101. }
  102. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  103. return (cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000;
  104. }
  105. static float get_pack_current_by_cs1180(void){
  106. float adc = cs1180_adc_sample();
  107. return (adc / max_cs1180_adc) * v_cs1180_ref / r_sense * 1000;
  108. }
  109. float get_pack_current(void){
  110. if (!cs1180_is_ready()){ //if cs1180 is not ready, just use gd adc
  111. return get_pack_current_by_gd();
  112. }
  113. float current = get_pack_current_by_cs1180();
  114. if (abs(current) >= CS1180_MAX_CURRENT){
  115. return get_pack_current_by_gd();
  116. }
  117. return current;
  118. }
  119. /* get cell's voltage (mV) */
  120. float get_cell_voltage(int cell){
  121. ML5238_SELECT_CELL(cell);
  122. delay_us(100);
  123. float adc = adc_sample_avg(ADC_CHAN_VMON, 10);
  124. return cell_real_vol((adc / max_gd_adc) * v_gd_ref);
  125. }
  126. /* get battery pack's aux current (MA) */
  127. float get_small_current(void){
  128. float adc = adc_sample_avg(ADC_CHAN_AUX_CURR, 10);
  129. return ((adc / max_gd_adc * v_gd_ref)) / small_cur_r_sense;
  130. }
  131. /* 用来判断小电流的情况下,电压小于某一个值认为小电流真正短路,比如16v*/
  132. float get_small_current_voltage(void){
  133. float s_current_a = get_small_current();//MA
  134. return s_current_a * (small_cur_r_sense + 28.0f);//28欧姆是mos的D极两个56的并联
  135. }
  136. int get_pcb_temperature(void){
  137. TEMP_OPEN(1);
  138. delay_us(100);
  139. uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_4, TRUE);
  140. TEMP_OPEN(0);
  141. return get_temp_by_adc(adc);
  142. }
  143. /*
  144. * index : 0...3, 3 indicat pcb temp
  145. */
  146. int get_pack_temperature(int index){
  147. TEMP_OPEN(1);
  148. delay_us(100);
  149. uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_1 + index, TRUE);
  150. TEMP_OPEN(0);
  151. return get_temp_by_adc(adc);
  152. }