mot_params_ind.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "foc/motor/motor.h"
  2. #include "foc/core/controller.h"
  3. #include "math/fast_math.h"
  4. #include "foc/motor/mot_params_ind.h"
  5. #include "libs/logger.h"
  6. #include "prot/can_foc_msg.h"
  7. /*
  8. 参考 MC_Simulink\modules\off_line_params_ind 仿真模型
  9. 必须空载测试
  10. */
  11. static void _rs_ind_timer_handler(shark_timer_t *);
  12. static shark_timer_t _rs_ind_timer = TIMER_INIT(_rs_ind_timer, _rs_ind_timer_handler);
  13. static void _ldq_ind_timer_handler(shark_timer_t *);
  14. static shark_timer_t _ldq_ind_timer = TIMER_INIT(_ldq_ind_timer, _ldq_ind_timer_handler);
  15. static void _flux_ind_timer_handler(shark_timer_t *);
  16. static shark_timer_t _flux_ind_timer = TIMER_INIT(_flux_ind_timer, _flux_ind_timer_handler);
  17. static float rs_id_max, rs_vd_max, rs_vd_now, rs_est_value;
  18. static s32 rs_meas_time;
  19. static bool b_rs_ind = false, b_rs_ested = false, b_ldq_ind = false, b_ld_ested = false, b_lq_ested = false, b_flux_ind = false, b_flux_ested = false;
  20. static u8 rs_ind_step = 0;
  21. void mot_params_ind_rs(float vd_max, float id_max, s32 time) {
  22. if (b_rs_ind || b_ldq_ind || b_flux_ind) {
  23. return;
  24. }
  25. b_rs_ind = true;
  26. b_rs_ested = false;
  27. rs_id_max = id_max;
  28. rs_vd_max = vd_max;
  29. rs_vd_now = 2.0f;
  30. rs_meas_time = time;
  31. mot_contrl_set_angle(&motor.controller, 0);
  32. mot_contrl_set_vdq_immediate(&motor.controller, rs_vd_now, 0);
  33. rs_ind_step = 1;
  34. shark_timer_post(&_rs_ind_timer, 10);
  35. }
  36. void mot_params_ind_stop(void) {
  37. shark_timer_cancel(&_rs_ind_timer);
  38. shark_timer_cancel(&_ldq_ind_timer);
  39. shark_timer_cancel(&_flux_ind_timer);
  40. u32 mask = cpu_enter_critical();
  41. b_rs_ind = false;
  42. b_ldq_ind = false;
  43. b_flux_ind = false;
  44. cpu_exit_critical(mask);
  45. mot_contrl_set_vdq(&motor.controller, 0, 0);
  46. mot_contrl_set_current(&motor.controller, 0);
  47. }
  48. static void _rs_ind_timer_handler(shark_timer_t *t) {
  49. bool finish = false;
  50. static int wait_iq_0_cnt = 0;
  51. if (!b_rs_ind) {
  52. mot_contrl_set_vdq(&motor.controller, 0, 0);
  53. return;
  54. }
  55. switch (rs_ind_step) {
  56. case 1:
  57. if (motor.controller.foc.out.curr_dq.d < rs_id_max) {
  58. rs_vd_now += 0.1f;
  59. wait_iq_0_cnt = 0;
  60. if (rs_vd_now >= rs_vd_max) {
  61. mot_contrl_set_vdq(&motor.controller, 0, 0);
  62. b_rs_ind = false;
  63. sys_debug("id not reach max id %f\n", motor.controller.foc.out.curr_dq.d);
  64. return;
  65. }
  66. mot_contrl_set_vdq_immediate(&motor.controller, rs_vd_now, 0);
  67. }else {
  68. rs_ind_step = 2;
  69. sys_debug("id reach the set\n");
  70. }
  71. break;
  72. case 2:
  73. if (ABS(motor.controller.foc.out.curr_dq.q) > 5.0f) {
  74. wait_iq_0_cnt++;
  75. if (wait_iq_0_cnt >= 200) {
  76. mot_contrl_set_vdq(&motor.controller, 0, 0);
  77. b_rs_ind = false;
  78. sys_debug("iq is larger %f\n", motor.controller.out_idq_filterd.q);
  79. return;
  80. }
  81. }else {
  82. wait_iq_0_cnt = 0;
  83. rs_ind_step = 3;
  84. sys_debug("start rs calc, %d\n", rs_meas_time);
  85. }
  86. break;
  87. case 3: {
  88. float *iabc = motor.controller.foc.in.curr_abc;
  89. float d, q;
  90. foc_abc_2_dq(SIGN(iabc[0]), SIGN(iabc[1]), SIGN(iabc[2]), &d, &q);
  91. float dtc = ((float)CONFIG_HW_DeadTime/(float)FOC_PWM_Half_Period) * motor.controller.foc.in.dc_vol * 1.5f;
  92. float vd = (rs_vd_now - dtc) * TWO_BY_THREE;
  93. float id = motor.controller.foc.out.curr_dq.d;
  94. float rs = vd / (id + 0.0001f);
  95. rs_est_value = LowPass_Filter(rs_est_value, rs, 0.2f);
  96. if (rs_meas_time-- <= 0) {
  97. mot_params_ind_stop();
  98. delay_ms(1000);
  99. mc_ind_motor_start(false);
  100. finish = true;
  101. b_rs_ested = true;
  102. sys_debug("est rs = %f\n", rs_est_value);
  103. sys_debug("vd-id is %f-%f-%f, wait %d\n", rs_vd_now, id, dtc/1.5f, wait_iq_0_cnt);
  104. }
  105. }
  106. default:
  107. break;
  108. }
  109. if (!finish) {
  110. shark_timer_post(&_rs_ind_timer, 10);
  111. }
  112. }
  113. float mot_params_get_est_rs(void) {
  114. return rs_est_value;
  115. }
  116. bool mot_params_rs_ested(void) {
  117. return b_rs_ested;
  118. }
  119. static float *v_samples = NULL, *i_samples = NULL;
  120. static float hj_v, hj_freq, hj_n, hj_w, hj_samples, K_terms, Vdead;
  121. static float hj_real, hj_image;
  122. static u16 n_ind_ld, n_samples;
  123. static float ld_est_value, lq_est_value;
  124. static s32 ldq_est_wait_cnt = 0;
  125. void mot_params_ind_inductance(float v, float freq, u16 l_type) {
  126. if (b_ldq_ind || b_rs_ind || b_flux_ind) {
  127. return;
  128. }
  129. if (!b_rs_ested) { //必须先识别相电阻
  130. return;
  131. }
  132. hj_v = v;
  133. hj_freq = freq;
  134. hj_n = (float)FOC_PWM_FS / hj_freq;
  135. hj_samples = hj_n * 50;
  136. K_terms = (s32) (0.5f + hj_samples*hj_freq/(float)FOC_PWM_FS);
  137. Vdead = motor.controller.foc.in.dc_vol * 0.5f * (float)CONFIG_HW_DeadTime / (float)FOC_PWM_period;
  138. hj_w = 360.0f / hj_n;
  139. sys_debug("hj %f, %f, %f, %f, %f, %f, %f\n", hj_v, hj_freq, hj_n, hj_samples, K_terms, Vdead, hj_w);
  140. float fft_angle = 360.0f / hj_samples * K_terms;
  141. arm_sin_cos(fft_angle, &hj_image, &hj_real);
  142. hj_real = hj_real * 2.0f;
  143. n_ind_ld = l_type;
  144. n_samples = 0;
  145. ldq_est_wait_cnt = 0;
  146. if (v_samples) {
  147. os_free(v_samples);
  148. }
  149. if (i_samples) {
  150. os_free(i_samples);
  151. }
  152. v_samples = os_alloc(sizeof(float) * hj_samples);
  153. i_samples = os_alloc(sizeof(float) * hj_samples);
  154. if (v_samples != NULL && i_samples != NULL) {
  155. b_ldq_ind = true;
  156. shark_timer_post(&_ldq_ind_timer, 10);
  157. }else {
  158. sys_debug("alloc error\n");
  159. }
  160. }
  161. static void _ldq_ind_timer_handler(shark_timer_t *t) {
  162. if (n_samples >= (hj_samples + 1)) {
  163. mc_ind_motor_start(false);
  164. mot_params_calc_inductance();
  165. mot_params_ind_stop();
  166. }else {
  167. ldq_est_wait_cnt ++;
  168. if (ldq_est_wait_cnt >= 20) {
  169. mc_ind_motor_start(false);
  170. mot_params_ind_stop();
  171. sys_debug("ldq ind timeout %d\n", ldq_est_wait_cnt);
  172. }else {
  173. shark_timer_post(&_ldq_ind_timer, 10);
  174. }
  175. }
  176. }
  177. void mot_params_ind_ld(float v, float freq) {
  178. b_ld_ested = false;
  179. mot_params_ind_inductance(v, freq, L_TYPE_D);
  180. }
  181. void mot_params_ind_lq(float v, float freq) {
  182. b_lq_ested = false;
  183. mot_params_ind_inductance(v, freq, L_TYPE_Q);
  184. }
  185. void mot_params_high_freq_inject(void) {
  186. if (!b_ldq_ind) {
  187. return;
  188. }
  189. float hj_angle = hj_w * (float)n_samples;
  190. rand_angle(hj_angle);
  191. float s, c;
  192. arm_sin_cos(hj_angle, &s, &c);
  193. float vd = 0, vq = 0;
  194. if (n_ind_ld == L_TYPE_D) {
  195. vd = hj_v * c;
  196. }else {
  197. vq = hj_v * c;
  198. }
  199. mot_contrl_set_vdq_immediate(&motor.controller, vd, vq);
  200. }
  201. bool mot_params_hj_sample_vi(float vd, float vq, float id, float iq) {
  202. if (!b_ldq_ind) {
  203. return true;
  204. }
  205. if ((n_samples >= 1) && (n_samples <= hj_samples)) {
  206. if (n_ind_ld == L_TYPE_D) {
  207. v_samples[n_samples - 1] = vd * TWO_BY_THREE;
  208. i_samples[n_samples - 1] = id;
  209. }else {
  210. v_samples[n_samples - 1] = vq * TWO_BY_THREE;
  211. i_samples[n_samples - 1] = iq;
  212. }
  213. }
  214. n_samples ++;
  215. return false;
  216. }
  217. void goertzel_dft(float *x, float *real, float *image, float *mag) {
  218. float y, d1 = 0, d2 = 0;
  219. for (int i = 0; i < hj_samples; i++) {
  220. y = x[i] + hj_real * d1 - d2;
  221. d2 = d1;
  222. d1 = y;
  223. }
  224. *real = d1 - (d2 * 0.5f * hj_real);
  225. *image = -d2 * hj_image;
  226. *mag = sqrtf(SQ(*real) + SQ(*image));
  227. }
  228. void mot_params_calc_inductance(void) {
  229. float v_real, v_image, v_mag;
  230. float i_real, i_image, i_mag;
  231. if (!b_ldq_ind) {
  232. return;
  233. }
  234. goertzel_dft(v_samples, &v_real, &v_image, &v_mag);
  235. goertzel_dft(i_samples, &i_real, &i_image, &i_mag);
  236. sys_debug("v %f, %f, %f\n", v_mag/(hj_samples*0.5f), v_real, v_image);
  237. sys_debug("i %f, %f, %f\n", i_mag/(hj_samples*0.5f), i_real, i_image);
  238. v_mag -= Vdead * hj_samples*0.5f;
  239. float z_angle = fast_atan_2(i_image, i_real) - fast_atan_2(v_image, v_real);
  240. float s,c;
  241. arm_sin_cos(pi_2_degree(z_angle), &s, &c);
  242. float z_mag = v_mag / (i_mag + 0.0000001f);
  243. float z_real = z_mag * c;
  244. float z_image = z_mag * s;
  245. float Rs = rs_est_value;
  246. float Ri = (SQ(z_real - Rs) + SQ(z_image))/(z_real - Rs + 0.0000001f);
  247. float l = Ri * (z_real - Rs)/(hj_freq * 2 * PI * z_image + 0.0000001f) * 0.83f; //0.83f just for v3 board
  248. if (n_ind_ld == L_TYPE_D) {
  249. ld_est_value = l;
  250. b_ld_ested = true;
  251. sys_debug("ld = %f\n", ld_est_value);
  252. }else {
  253. lq_est_value = l;
  254. b_lq_ested = true;
  255. sys_debug("lq = %f\n", lq_est_value);
  256. }
  257. b_ldq_ind = false;
  258. }
  259. static float motVelRadusPers, flux_wait_cnt = 0, flux_do_cnt = 0, flux_est_value = 0;
  260. static bool _pending_flux_mc_stop = false;
  261. static void _flux_ind_timer_handler(shark_timer_t *t) {
  262. float We = motor.controller.foc.mot_vel_radusPers;
  263. float delta = We - motVelRadusPers;
  264. motVelRadusPers = motor.controller.foc.mot_vel_radusPers;
  265. if (We > 100 && ABS(delta) < 40) {
  266. float dtc = ((float)CONFIG_HW_DeadTime/(float)FOC_PWM_Half_Period) * motor.controller.foc.in.dc_vol * 1.5f;
  267. float vq = (motor.controller.foc.out.vol_dq.q - dtc) * TWO_BY_THREE;
  268. float flux = vq / We;
  269. flux_est_value = LowPass_Filter(flux_est_value, flux, 0.1f);
  270. flux_do_cnt ++;
  271. }else {
  272. flux_wait_cnt ++;
  273. }
  274. if ((flux_wait_cnt >= 500) || (flux_do_cnt >= 400)) {
  275. b_flux_ind = false;
  276. if (flux_wait_cnt >= 500) {
  277. sys_debug("ind flux error\n");
  278. }else {
  279. b_flux_ested = true;
  280. sys_debug("ind_flux finish, %f\n", flux_est_value);
  281. }
  282. mot_params_ind_stop();
  283. if (mot_contrl_get_speed(&motor.controller) < CONFIG_ZERO_SPEED_RPM) {
  284. mc_ind_motor_start(false);
  285. }else {
  286. _pending_flux_mc_stop = true;
  287. }
  288. }else {
  289. shark_timer_post(&_flux_ind_timer, 100);
  290. }
  291. }
  292. void mot_params_ind_flux(float id, float iq) {
  293. if (b_rs_ind || b_ldq_ind || b_flux_ind || _pending_flux_mc_stop) {
  294. return;
  295. }
  296. b_flux_ind = true;
  297. flux_wait_cnt = 0;
  298. flux_do_cnt = 0;
  299. b_flux_ested = false;
  300. mc_set_foc_mode(CTRL_MODE_CURRENT);
  301. mot_contrl_set_current(&motor.controller ,iq);
  302. shark_timer_post(&_flux_ind_timer, 10);
  303. motVelRadusPers = motor.controller.foc.mot_vel_radusPers;
  304. }
  305. float mot_params_get_est_ld(void) {
  306. return ld_est_value;
  307. }
  308. float mot_params_get_est_lq(void) {
  309. return lq_est_value;
  310. }
  311. bool mot_params_ld_ested(void) {
  312. return b_ld_ested;
  313. }
  314. bool mot_params_lq_ested(void) {
  315. return b_lq_ested;
  316. }
  317. bool mot_params_flux_ested(void) {
  318. return b_flux_ested;
  319. }
  320. float mot_params_get_est_flux(void) {
  321. return flux_est_value;
  322. }
  323. void mot_params_flux_stop(void) {
  324. if (_pending_flux_mc_stop && (mot_contrl_get_speed(&motor.controller) < CONFIG_ZERO_SPEED_RPM)) {
  325. mc_ind_motor_start(false);
  326. _pending_flux_mc_stop = false;
  327. }
  328. }
  329. bool mot_params_flux_pending(void) {
  330. return _pending_flux_mc_stop;
  331. }