measure.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. uint32_t check_gain_error = 0;
  46. static u64 check_gain_time = 0;
  47. static least_square_t adc_cali[2]; // y = ax + b
  48. #define GD32_ADC_READ_TIMES 128
  49. static int __inline__ _is_x10_gain(void){
  50. return imon_gain_now == imon_gain_10x;
  51. }
  52. static void __inline__ check_gain(void){
  53. int count = 5;
  54. while (_is_x10_gain() && !ML5238_IS_10X()){
  55. ML5238_IMON_OUT_10X();
  56. check_gain_error ++;
  57. if (count-- <= 0) {
  58. break;
  59. }
  60. }
  61. count = 5;
  62. while (!_is_x10_gain() && !ML5238_IS_50X()){
  63. ML5238_IMON_OUT_50X();
  64. check_gain_error ++;
  65. if (count-- <= 0) {
  66. break;
  67. }
  68. }
  69. }
  70. static void __inline__ select_gain_10x(int select){
  71. if (select){
  72. ML5238_IMON_OUT_10X();
  73. imon_gain_now = imon_gain_10x;
  74. vim0_now = vim0_10x;
  75. ml5238_now_ceoff = ml5238_10x_ceoff;
  76. }else {
  77. ML5238_IMON_OUT_50X();
  78. imon_gain_now = imon_gain_50x;
  79. vim0_now = vim0_50x;
  80. ml5238_now_ceoff = ml5238_50x_ceoff;
  81. }
  82. check_gain();
  83. }
  84. float get_ml5238_gain(void){
  85. return imon_gain_now;
  86. }
  87. float get_ml5238_vos(void){
  88. return vim0_now;
  89. }
  90. static void current_10x_calibrate(void){
  91. /* calibrate the 10x gain */
  92. ML5238_IMON_OUT_ZERO_10X();
  93. vim0_10x = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  94. #if 0
  95. ML5238_IMON_OUT_V2000_10X();
  96. float vim1 = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  97. ML5238_IMON_OUT_V100_10X();
  98. float vr = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  99. imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr);
  100. #else
  101. imon_gain_10x = 10.0f;
  102. #endif
  103. }
  104. static void current_50x_calibrate(void){
  105. /* calibrate the 50x gain */
  106. ML5238_IMON_OUT_ZERO_50X();
  107. vim0_50x = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  108. #if 0
  109. ML5238_IMON_OUT_V2000_50X();
  110. float vim1 = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  111. ML5238_IMON_OUT_V20_50X();
  112. float vr = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  113. imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr) * 0.948f;
  114. #else
  115. imon_gain_50x = 50.0f;
  116. #endif
  117. }
  118. /*calibrate when startup && temperature is changed more than 5? degree
  119. * calibrate the ms5238's IMON output voltage gain
  120. */
  121. void current_calibrate(void){
  122. current_10x_calibrate();
  123. current_50x_calibrate();
  124. #ifdef gain_default_50x
  125. select_gain_10x(0);
  126. #else
  127. select_gain_10x(1);
  128. #endif
  129. }
  130. void measure_adc_init(void){
  131. ml5238_init();
  132. gd32_adc_init();
  133. current_calibrate();
  134. cs1180_adc_init();
  135. }
  136. static float get_current_by_ml5238(void){
  137. float adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  138. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  139. return (int)((cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000);
  140. }
  141. /* get battery pack's current (mA) */
  142. static float get_pack_current_by_gd(void){
  143. u64 time_now = shark_get_mseconds();
  144. if (time_now - check_gain_time >= 100) {
  145. check_gain();
  146. check_gain_time = time_now;
  147. }
  148. float adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  149. if (adc >= (max_gd_adc - 255.0f) && (!_is_x10_gain())){//overflow, use 10x gain
  150. select_gain_10x(1);
  151. adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  152. }else if (abs(adc - vim0_now) <= 255.0f && (_is_x10_gain())){// is too small, select 50x gain
  153. select_gain_10x(0);
  154. adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  155. }
  156. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  157. return (int)((cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000);
  158. }
  159. static float get_pack_current_by_cs1180(int *valid){
  160. float adc = cs1180_adc_sample(valid);
  161. float vol = (adc / max_cs1180_adc) * v_cs1180_ref;
  162. return (vol / r_sense) * 1000 - 5.0f;//板子固定5MA,cs1180无法测量到
  163. }
  164. static __inline__ float get_caliberated_current(linear_ceoff_t *cof, float x) {
  165. return (x * cof->Ka) + cof->Cb;
  166. }
  167. float get_pack_current(int *current_5238){
  168. float current = get_pack_current_by_gd();
  169. current = get_caliberated_current(&ml5238_now_ceoff, current);
  170. if (current_5238 != NULL){
  171. *current_5238 = (int)current;
  172. }
  173. if (cs1180_change_gain(current) == 1) {
  174. int valid = 1;
  175. float cs1180_current = get_pack_current_by_cs1180(&valid);
  176. if (valid == 1) {
  177. current = get_caliberated_current(&cs1180_ceoff, cs1180_current);
  178. }
  179. }
  180. return current;
  181. }
  182. /* get cell's voltage (mV) */
  183. float get_cell_voltage(int cell){
  184. ML5238_SELECT_CELL(cell);
  185. delay_us(100);
  186. float adc = adc_sample_avg(ADC_CHAN_VMON, GD32_ADC_READ_TIMES);
  187. return cell_real_vol((adc / max_gd_adc) * v_gd_ref);
  188. }
  189. /* get battery pack's aux current (MA) */
  190. float get_small_current(void){
  191. float adc = adc_sample_avg(ADC_CHAN_AUX_CURR, 16);
  192. return ((adc / max_gd_adc * v_gd_ref)) / small_cur_r_sense;
  193. }
  194. /* 用来判断小电流的情况下,电压小于某一个值认为小电流真正短路,比如16v*/
  195. float get_small_current_voltage(void){
  196. float s_current_a = get_small_current();//MA
  197. return s_current_a * (small_cur_r_sense + SMALL_CURRENT_R);//28欧姆是mos的D极两个56的并联
  198. }
  199. int get_pcb_temperature(void){
  200. TEMP_OPEN(1);
  201. delay_us(100);
  202. uint16_t adc = adc_sample_avg(ADC_CHAN_TEMPERATURE_4, 8);
  203. TEMP_OPEN(0);
  204. return get_temp_by_adc(adc);
  205. }
  206. /*
  207. * index : 0...3, 3 indicat pcb temp
  208. */
  209. int get_pack_temperature(int index){
  210. TEMP_OPEN(1);
  211. delay_us(100);
  212. uint16_t adc = adc_sample_avg(ADC_CHAN_TEMPERATURE_1 + index, 8);
  213. TEMP_OPEN(0);
  214. return get_temp_by_adc((adc<<4)&0xFFFF);
  215. }
  216. int measure_start_cali(uint8_t adc, uint8_t gain, uint8_t samples){
  217. bms_work_mode_set(WORK_MODE_CALIBRATE, 1);
  218. least_square_init(&adc_cali[adc], samples);
  219. sys_debug("start cali %d, %d, %d\n", adc, gain, samples);
  220. if (adc == GD32_ADC) {
  221. if (gain == 10) {
  222. select_gain_10x(1);
  223. }else if (gain == 50) {
  224. select_gain_10x(0);
  225. }else {
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. return 1;
  231. }
  232. int measure_continue_cali(uint8_t adc, uint16_t voltage, int16_t current) {
  233. float x = 0;
  234. float y = current;
  235. sys_debug("continue cali %d, %d, %d\n", adc, voltage, current);
  236. if (adc == GD32_ADC) {
  237. x = get_current_by_ml5238();
  238. }else {
  239. x = get_pack_current_by_cs1180(NULL);
  240. }
  241. least_square_put(&adc_cali[adc], x, y);
  242. return 1;
  243. }
  244. int measure_stop_cali(uint8_t adc, uint8_t gain){
  245. bms_work_mode_set(WORK_MODE_CALIBRATE, 0);
  246. sys_debug("continue stop %d, %d\n", adc, gain);
  247. if (adc_cali[adc].finished ) {
  248. if (adc == GD32_ADC) {
  249. if (gain == 10) {
  250. ml5238_10x_ceoff = adc_cali[adc].coeff;
  251. }else if (gain == 50) {
  252. ml5238_50x_ceoff = adc_cali[adc].coeff;
  253. }
  254. }else {
  255. cs1180_ceoff = adc_cali[adc].coeff;
  256. }
  257. sys_debug("stop %f, %f\n", adc_cali[adc].coeff.Ka, adc_cali[adc].coeff.Cb);
  258. }
  259. select_gain_10x(0);
  260. return adc_cali[adc].finished;
  261. }