mot_params_ind.c 10 KB

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