measure.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #include "libs/logger.h"
  10. #include "Least_Square.h"
  11. #include "app/sox/state.h"
  12. /* measure the temp & current & voltage for battery pack by using
  13. * ms5238 & cs1180(only used when bms is in small current loading)
  14. */
  15. /*
  16. 1. 小电流
  17. 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
  18. 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
  19. 2. 大电流
  20. cs1180(32x):1.861-2.082
  21. ml5238(50x):1.869-1.865-1.890
  22. */
  23. /* this is the inited gain set to the ms5238, but the really gain is calibrated
  24. * by measure_system_calibrate
  25. */
  26. static float imon_gain_10x = 10.0f;
  27. static float imon_gain_50x = 50.0f;
  28. static float imon_gain_now;
  29. static float vim0_10x = 0.0f;
  30. static float vim0_50x = 0.0f;
  31. static float vim0_now;
  32. static linear_ceoff_t ml5238_10x_ceoff = {.Ka = 1.0f, .Cb = 0.0f};
  33. static linear_ceoff_t ml5238_50x_ceoff = {.Ka = 1.0f, .Cb = 0.0f};
  34. static linear_ceoff_t ml5238_now_ceoff;
  35. static linear_ceoff_t cs1180_ceoff = {.Ka = 1.0f, .Cb = 0.0f};
  36. #define gain_default_50x 1
  37. #define CS1180_MAX_CURRENT 4500 //MA, cs1180的最大电流,超过这个使用ML5238
  38. #define r_pcb_resistor 0.0f // pcb resistor
  39. static const float r_sense = r_resistor + r_pcb_resistor;
  40. static const float v_gd_ref = 3300.0f; //adc ref = 3.3v
  41. static const float max_gd_adc = 4095.0f;//65536.0f;
  42. static const float v_cs1180_ref = 1235.0f;//cs1180 vref = 1.235v
  43. static const float max_cs1180_adc = 0x7FFFF;//
  44. static const float small_cur_r_sense = 0.360f;//欧姆
  45. static least_square_t adc_cali[2]; // y = ax + b
  46. #define GD32_ADC_READ_TIMES 128
  47. static void __inline__ select_gain_10x(int select){
  48. if (select){
  49. ML5238_IMON_OUT_10X();
  50. imon_gain_now = imon_gain_10x;
  51. vim0_now = vim0_10x;
  52. ml5238_now_ceoff = ml5238_10x_ceoff;
  53. }else {
  54. ML5238_IMON_OUT_50X();
  55. imon_gain_now = imon_gain_50x;
  56. vim0_now = vim0_50x;
  57. ml5238_now_ceoff = ml5238_50x_ceoff;
  58. }
  59. }
  60. static int __inline__ _is_x10_gain(void){
  61. return imon_gain_now == imon_gain_10x;
  62. }
  63. float get_ml5238_gain(void){
  64. return imon_gain_now;
  65. }
  66. float get_ml5238_vos(void){
  67. return vim0_now;
  68. }
  69. static void current_10x_calibrate(void){
  70. /* calibrate the 10x gain */
  71. ML5238_IMON_OUT_ZERO_10X();
  72. vim0_10x = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  73. #if 0
  74. ML5238_IMON_OUT_V2000_10X();
  75. float vim1 = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  76. ML5238_IMON_OUT_V100_10X();
  77. float vr = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  78. imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr);
  79. #else
  80. imon_gain_10x = 10.0f;
  81. #endif
  82. }
  83. static void current_50x_calibrate(void){
  84. /* calibrate the 50x gain */
  85. ML5238_IMON_OUT_ZERO_50X();
  86. vim0_50x = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  87. #if 0
  88. ML5238_IMON_OUT_V2000_50X();
  89. float vim1 = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  90. ML5238_IMON_OUT_V20_50X();
  91. float vr = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  92. imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr) * 0.948f;
  93. #else
  94. imon_gain_50x = 50.0f;
  95. #endif
  96. }
  97. /*calibrate when startup && temperature is changed more than 5? degree
  98. * calibrate the ms5238's IMON output voltage gain
  99. */
  100. void current_calibrate(void){
  101. current_10x_calibrate();
  102. current_50x_calibrate();
  103. #ifdef gain_default_50x
  104. select_gain_10x(0);
  105. #else
  106. select_gain_10x(1);
  107. #endif
  108. }
  109. void measure_adc_init(void){
  110. ml5238_init();
  111. gd32_adc_init();
  112. current_calibrate();
  113. cs1180_adc_init();
  114. }
  115. static float get_current_by_ml5238(void){
  116. float adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  117. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  118. return (int)((cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000);
  119. }
  120. /* get battery pack's current (mA) */
  121. static float get_pack_current_by_gd(void){
  122. float adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  123. if (adc >= (max_gd_adc - 255.0f) && (!_is_x10_gain())){//overflow, use 10x gain
  124. select_gain_10x(1);
  125. adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  126. }else if (abs(adc - vim0_now) <= 255.0f && (_is_x10_gain())){// is too small, select 50x gain
  127. select_gain_10x(0);
  128. adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  129. }
  130. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  131. return (int)((cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000);
  132. }
  133. static float get_pack_current_by_cs1180(int *valid){
  134. float adc = cs1180_adc_sample(valid);
  135. float vol = (adc / max_cs1180_adc) * v_cs1180_ref;
  136. return (vol / r_sense) * 1000 - 5.0f;//板子固定5MA,cs1180无法测量到
  137. }
  138. static __inline__ float get_caliberated_current(linear_ceoff_t *cof, float x) {
  139. return (x * cof->Ka) + cof->Cb;
  140. }
  141. float get_pack_current(int *current_5238){
  142. float current = get_pack_current_by_gd();
  143. current = get_caliberated_current(&ml5238_now_ceoff, current);
  144. if (current_5238 != NULL){
  145. *current_5238 = (int)current;
  146. }
  147. if (cs1180_change_gain(current) == 1) {
  148. int valid = 1;
  149. float cs1180_current = get_pack_current_by_cs1180(&valid);
  150. if (valid == 1) {
  151. current = get_caliberated_current(&cs1180_ceoff, cs1180_current);
  152. }
  153. }
  154. return current;
  155. }
  156. /* get cell's voltage (mV) */
  157. float get_cell_voltage(int cell){
  158. ML5238_SELECT_CELL(cell);
  159. delay_us(100);
  160. float adc = adc_sample_avg(ADC_CHAN_VMON, GD32_ADC_READ_TIMES);
  161. return cell_real_vol((adc / max_gd_adc) * v_gd_ref);
  162. }
  163. /* get battery pack's aux current (MA) */
  164. float get_small_current(void){
  165. float adc = adc_sample_avg(ADC_CHAN_AUX_CURR, 16);
  166. return ((adc / max_gd_adc * v_gd_ref)) / small_cur_r_sense;
  167. }
  168. /* 用来判断小电流的情况下,电压小于某一个值认为小电流真正短路,比如16v*/
  169. float get_small_current_voltage(void){
  170. float s_current_a = get_small_current();//MA
  171. return s_current_a * (small_cur_r_sense + SMALL_CURRENT_R);//28欧姆是mos的D极两个56的并联
  172. }
  173. int get_pcb_temperature(void){
  174. TEMP_OPEN(1);
  175. delay_us(100);
  176. uint16_t adc = adc_sample_avg(ADC_CHAN_TEMPERATURE_4, 1);
  177. TEMP_OPEN(0);
  178. return get_temp_by_adc(adc);
  179. }
  180. /*
  181. * index : 0...3, 3 indicat pcb temp
  182. */
  183. int get_pack_temperature(int index){
  184. TEMP_OPEN(1);
  185. delay_us(100);
  186. uint16_t adc = adc_sample_avg(ADC_CHAN_TEMPERATURE_1 + index, 1);
  187. TEMP_OPEN(0);
  188. return get_temp_by_adc((adc<<4)&0xFFFF);
  189. }
  190. int measure_start_cali(uint8_t adc, uint8_t gain, uint8_t samples){
  191. bms_work_mode_set(WORK_MODE_CALIBRATE, 1);
  192. least_square_init(&adc_cali[adc], samples);
  193. sys_debug("start cali %d, %d, %d\n", adc, gain, samples);
  194. if (adc == GD32_ADC) {
  195. if (gain == 10) {
  196. select_gain_10x(1);
  197. }else if (gain == 50) {
  198. select_gain_10x(0);
  199. }else {
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. return 1;
  205. }
  206. int measure_continue_cali(uint8_t adc, uint16_t voltage, int16_t current) {
  207. float x = 0;
  208. float y = current;
  209. sys_debug("continue cali %d, %d, %d\n", adc, voltage, current);
  210. if (adc == GD32_ADC) {
  211. x = get_current_by_ml5238();
  212. }else {
  213. x = get_pack_current_by_cs1180(NULL);
  214. }
  215. least_square_put(&adc_cali[adc], x, y);
  216. return 1;
  217. }
  218. int measure_stop_cali(uint8_t adc, uint8_t gain){
  219. bms_work_mode_set(WORK_MODE_CALIBRATE, 0);
  220. sys_debug("continue stop %d, %d\n", adc, gain);
  221. if (adc_cali[adc].finished ) {
  222. if (adc == GD32_ADC) {
  223. if (gain == 10) {
  224. ml5238_10x_ceoff = adc_cali[adc].coeff;
  225. }else if (gain == 50) {
  226. ml5238_50x_ceoff = adc_cali[adc].coeff;
  227. }
  228. }else {
  229. cs1180_ceoff = adc_cali[adc].coeff;
  230. }
  231. sys_debug("stop %f, %f\n", adc_cali[adc].coeff.Ka, adc_cali[adc].coeff.Cb);
  232. }
  233. select_gain_10x(0);
  234. return adc_cali[adc].finished;
  235. }