measure.c 9.0 KB

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