mot_params_ind.c 9.7 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 "foc/samples.h"
  7. #include "prot/can_foc_msg.h"
  8. /*
  9. 参考 MC_Simulink\modules\off_line_params_ind 仿真模型
  10. 必须空载测试
  11. */
  12. static void _rs_ind_timer_handler(shark_timer_t *);
  13. static shark_timer_t _rs_ind_timer = TIMER_INIT(_rs_ind_timer, _rs_ind_timer_handler);
  14. static void _ldq_ind_timer_handler(shark_timer_t *);
  15. static shark_timer_t _ldq_ind_timer = TIMER_INIT(_ldq_ind_timer, _ldq_ind_timer_handler);
  16. static void _flux_ind_timer_handler(shark_timer_t *);
  17. static shark_timer_t _flux_ind_timer = TIMER_INIT(_flux_ind_timer, _flux_ind_timer_handler);
  18. static float rs_id_max, rs_vd_max, rs_vd_now, rs_est_value;
  19. static s32 rs_meas_time;
  20. 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;
  21. static u8 rs_ind_step = 0;
  22. void mot_params_ind_rs(float vd_max, float id_max, s32 time) {
  23. if (b_rs_ind || b_ldq_ind || b_flux_ind) {
  24. return;
  25. }
  26. b_rs_ind = true;
  27. b_rs_ested = false;
  28. rs_id_max = id_max;
  29. rs_vd_max = vd_max;
  30. rs_vd_now = 2.0f;
  31. rs_meas_time = time;
  32. mot_contrl_set_angle(&motor.controller, 0);
  33. mot_contrl_set_vdq_immediate(&motor.controller, rs_vd_now, 0);
  34. rs_ind_step = 1;
  35. shark_timer_post(&_rs_ind_timer, 10);
  36. }
  37. void mot_params_ind_stop(void) {
  38. shark_timer_cancel(&_rs_ind_timer);
  39. shark_timer_cancel(&_ldq_ind_timer);
  40. shark_timer_cancel(&_flux_ind_timer);
  41. u32 mask = cpu_enter_critical();
  42. b_rs_ind = false;
  43. b_ldq_ind = false;
  44. b_flux_ind = false;
  45. cpu_exit_critical(mask);
  46. mot_contrl_set_vdq(&motor.controller, 0, 0);
  47. mot_contrl_set_current(&motor.controller, 0);
  48. }
  49. static void _rs_ind_timer_handler(shark_timer_t *t) {
  50. bool finish = false;
  51. static int wait_iq_0_cnt = 0;
  52. if (!b_rs_ind) {
  53. mot_contrl_set_vdq(&motor.controller, 0, 0);
  54. return;
  55. }
  56. switch (rs_ind_step) {
  57. case 1:
  58. if (motor.controller.foc.out.curr_dq.d < rs_id_max) {
  59. rs_vd_now += 0.1f;
  60. wait_iq_0_cnt = 0;
  61. if (rs_vd_now >= rs_vd_max) {
  62. mot_contrl_set_vdq(&motor.controller, 0, 0);
  63. b_rs_ind = false;
  64. sys_debug("id not reach max id %f\n", motor.controller.foc.out.curr_dq.d);
  65. return;
  66. }
  67. mot_contrl_set_vdq_immediate(&motor.controller, rs_vd_now, 0);
  68. }else {
  69. rs_ind_step = 2;
  70. sys_debug("id reach the set\n");
  71. }
  72. break;
  73. case 2:
  74. if (ABS(motor.controller.foc.out.curr_dq.q) > 5.0f) {
  75. wait_iq_0_cnt++;
  76. if (wait_iq_0_cnt >= 200) {
  77. mot_contrl_set_vdq(&motor.controller, 0, 0);
  78. b_rs_ind = false;
  79. sys_debug("iq is larger %f\n", motor.controller.out_idq_filterd.q);
  80. return;
  81. }
  82. }else {
  83. wait_iq_0_cnt = 0;
  84. delay_ms(100);
  85. rs_ind_step = 3;
  86. sys_debug("start rs calc, %d\n", rs_meas_time);
  87. }
  88. break;
  89. case 3: {
  90. float dtc = ((float)CONFIG_HW_DeadTime/(float)FOC_PWM_period) * motor.controller.foc.in.dc_vol * 0.2f;
  91. float vd = rs_vd_now * TWO_BY_THREE - dtc;
  92. float id = motor.controller.foc.out.curr_dq.d;
  93. float rs = vd / (id + 0.0001f);
  94. rs_est_value = LowPass_Filter(rs_est_value, rs, 0.2f);
  95. if (rs_meas_time-- <= 0) {
  96. mot_params_ind_stop();
  97. delay_ms(1000);
  98. mc_ind_motor_start(false);
  99. finish = true;
  100. b_rs_ested = true;
  101. sys_debug("est rs = %f-%f\n", rs_est_value, rs);
  102. sys_debug("vd-id is %f-%f-%f-%f, wait %d\n", rs_vd_now, id, dtc, vd, wait_iq_0_cnt);
  103. }
  104. }
  105. default:
  106. break;
  107. }
  108. if (!finish) {
  109. shark_timer_post(&_rs_ind_timer, 10);
  110. }
  111. }
  112. float mot_params_get_est_rs(void) {
  113. return rs_est_value;
  114. }
  115. bool mot_params_rs_ested(void) {
  116. return b_rs_ested;
  117. }
  118. static float *v_samples = NULL, *i_samples = NULL;
  119. static float hj_v, hj_freq, hj_n, hj_w, hj_samples, K_terms, Vdead;
  120. static float hj_real, hj_image;
  121. static u16 n_ind_ld, n_samples;
  122. static float ld_est_value, lq_est_value;
  123. static s32 ldq_est_wait_cnt = 0;
  124. void mot_params_ind_inductance(float v, float freq, u16 l_type) {
  125. if (b_ldq_ind || b_rs_ind || b_flux_ind) {
  126. return;
  127. }
  128. if (!b_rs_ested) { //必须先识别相电阻
  129. return;
  130. }
  131. hj_v = v;
  132. hj_freq = freq;
  133. hj_n = (float)FOC_PWM_FS / hj_freq;
  134. hj_samples = hj_n * 50;
  135. K_terms = (s32) (0.5f + hj_samples*hj_freq/(float)FOC_PWM_FS);
  136. Vdead = motor.controller.foc.in.dc_vol * (float)CONFIG_HW_DeadTime / (float)FOC_PWM_period;
  137. hj_w = 360.0f / hj_n;
  138. sys_debug("hj %f, %f, %f, %f, %f, %f, %f\n", hj_v, hj_freq, hj_n, hj_samples, K_terms, Vdead, hj_w);
  139. float fft_angle = 360.0f / hj_samples * K_terms;
  140. arm_sin_cos(fft_angle, &hj_image, &hj_real);
  141. hj_real = hj_real * 2.0f;
  142. n_ind_ld = l_type;
  143. n_samples = 0;
  144. ldq_est_wait_cnt = 0;
  145. if (v_samples) {
  146. os_free(v_samples);
  147. }
  148. if (i_samples) {
  149. os_free(i_samples);
  150. }
  151. v_samples = os_alloc(sizeof(float) * hj_samples);
  152. i_samples = os_alloc(sizeof(float) * hj_samples);
  153. if (v_samples != NULL && i_samples != NULL) {
  154. b_ldq_ind = true;
  155. shark_timer_post(&_ldq_ind_timer, 10);
  156. }else {
  157. sys_debug("alloc error\n");
  158. }
  159. }
  160. static void _ldq_ind_timer_handler(shark_timer_t *t) {
  161. if (n_samples >= (hj_samples + 1)) {
  162. mc_ind_motor_start(false);
  163. mot_params_calc_inductance();
  164. mot_params_ind_stop();
  165. }else {
  166. ldq_est_wait_cnt ++;
  167. if (ldq_est_wait_cnt >= 20) {
  168. mc_ind_motor_start(false);
  169. mot_params_ind_stop();
  170. sys_debug("ldq ind timeout %d\n", ldq_est_wait_cnt);
  171. }else {
  172. shark_timer_post(&_ldq_ind_timer, 10);
  173. }
  174. }
  175. }
  176. void mot_params_ind_ld(float v, float freq) {
  177. b_ld_ested = false;
  178. mot_params_ind_inductance(v, freq, L_TYPE_D);
  179. }
  180. void mot_params_ind_lq(float v, float freq) {
  181. b_lq_ested = false;
  182. mot_params_ind_inductance(v, freq, L_TYPE_Q);
  183. }
  184. void mot_params_high_freq_inject(void) {
  185. if (!b_ldq_ind) {
  186. return;
  187. }
  188. float hj_angle = hj_w * (float)n_samples;
  189. rand_angle(hj_angle);
  190. float s, c;
  191. arm_sin_cos(hj_angle, &s, &c);
  192. float vd = 0, vq = 0;
  193. if (n_ind_ld == L_TYPE_D) {
  194. vd = hj_v * c;
  195. }else {
  196. vq = hj_v * c;
  197. }
  198. mot_contrl_set_vdq_immediate(&motor.controller, vd, vq);
  199. }
  200. bool mot_params_hj_sample_vi(float vd, float vq, float id, float iq) {
  201. if (!b_ldq_ind) {
  202. return true;
  203. }
  204. if ((n_samples >= 1) && (n_samples <= hj_samples)) {
  205. if (n_ind_ld == L_TYPE_D) {
  206. v_samples[n_samples - 1] = vd * TWO_BY_THREE;
  207. i_samples[n_samples - 1] = id;
  208. }else {
  209. v_samples[n_samples - 1] = vq * TWO_BY_THREE;
  210. i_samples[n_samples - 1] = iq;
  211. }
  212. }
  213. n_samples ++;
  214. return false;
  215. }
  216. void goertzel_dft(float *x, float *real, float *image, float *mag) {
  217. float y, d1 = 0, d2 = 0;
  218. for (int i = 0; i < hj_samples; i++) {
  219. y = x[i] + hj_real * d1 - d2;
  220. d2 = d1;
  221. d1 = y;
  222. }
  223. *real = d1 - (d2 * 0.5f * hj_real);
  224. *image = -d2 * hj_image;
  225. *mag = sqrtf(SQ(*real) + SQ(*image));
  226. }
  227. void mot_params_calc_inductance(void) {
  228. float v_real, v_image, v_mag;
  229. float i_real, i_image, i_mag;
  230. if (!b_ldq_ind) {
  231. return;
  232. }
  233. goertzel_dft(v_samples, &v_real, &v_image, &v_mag);
  234. goertzel_dft(i_samples, &i_real, &i_image, &i_mag);
  235. sys_debug("v %f, %f, %f\n", v_mag/(hj_samples*0.5f), v_real, v_image);
  236. sys_debug("i %f, %f, %f\n", i_mag/(hj_samples*0.5f), i_real, i_image);
  237. sys_debug("vmag %f, %f\n", v_mag, Vdead * hj_samples*0.5f);
  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);
  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_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_ctrl_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. }