measure.c 9.5 KB

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