mot_params_ind.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "foc/motor/motor.h"
  2. #include "foc/core/PMSM_FOC_Core.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. PMSM_FOC_Set_MotAngle(0);
  32. PMSM_FOC_SetOpenVdq_Immediate(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. PMSM_FOC_SetOpenVdq(0, 0);
  45. PMSM_FOC_Set_Current(0);
  46. cpu_exit_critical(mask);
  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. PMSM_FOC_SetOpenVdq(0, 0);
  53. return;
  54. }
  55. switch (rs_ind_step) {
  56. case 1:
  57. if (PMSM_FOC_Get()->out.s_RealIdq.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. PMSM_FOC_SetOpenVdq(0, 0);
  62. b_rs_ind = false;
  63. sys_debug("id not reach max id %f\n", PMSM_FOC_Get()->out.s_RealIdq.d);
  64. return;
  65. }
  66. PMSM_FOC_SetOpenVdq_Immediate(rs_vd_now, 0);
  67. }else {
  68. rs_ind_step = 2;
  69. }
  70. break;
  71. case 2:
  72. if (PMSM_FOC_Get()->out.s_RealIdq.d < rs_id_max) {
  73. rs_vd_now += 0.1f;
  74. wait_iq_0_cnt = 0;
  75. if (rs_vd_now >= rs_vd_max) {
  76. PMSM_FOC_SetOpenVdq(0, 0);
  77. b_rs_ind = false;
  78. sys_debug("id not reach max id %f\n", PMSM_FOC_Get()->out.s_RealIdq.d);
  79. return;
  80. }
  81. PMSM_FOC_SetOpenVdq_Immediate(rs_vd_now, 0);
  82. }else {
  83. rs_ind_step = 3;
  84. }
  85. break;
  86. case 3: {
  87. float *iabc = PMSM_FOC_Get()->in.s_iABC;
  88. float d, q;
  89. PMSM_FOC_ABC2Dq(SIGN(iabc[0]), SIGN(iabc[1]), SIGN(iabc[2]), &d, &q);
  90. float dtc = ((float)CONFIG_HW_DeadTime/(float)FOC_PWM_Half_Period) * PMSM_FOC_Get()->in.s_vDC * 1.5f;
  91. float vd = (rs_vd_now - dtc) * TWO_BY_THREE;
  92. float rs = vd / (PMSM_FOC_Get()->out.s_RealIdq.d + 0.00000000001f);
  93. rs_est_value = LowPass_Filter(rs_est_value, rs, 0.2f);
  94. if (rs_meas_time-- <= 0) {
  95. mc_ind_motor_start(false);
  96. mot_params_ind_stop();
  97. finish = true;
  98. b_rs_ested = true;
  99. sys_debug("est rs = %f\n", rs_est_value);
  100. sys_debug("vd is %f, wait %d\n", rs_vd_now, wait_iq_0_cnt);
  101. }
  102. }
  103. default:
  104. break;
  105. }
  106. if (!finish) {
  107. shark_timer_post(&_rs_ind_timer, 10);
  108. }
  109. }
  110. float mot_params_get_est_rs(void) {
  111. return rs_est_value;
  112. }
  113. bool mot_params_rs_ested(void) {
  114. return b_rs_ested;
  115. }
  116. static float *v_samples = NULL, *i_samples = NULL;
  117. static float hj_v, hj_freq, hj_n, hj_w, hj_samples, K_terms, Vdead;
  118. static float hj_real, hj_image;
  119. static u16 n_ind_ld, n_samples;
  120. static float ld_est_value, lq_est_value;
  121. static s32 ldq_est_wait_cnt = 0;
  122. void mot_params_ind_inductance(float v, float freq, u16 l_type) {
  123. if (b_ldq_ind || b_rs_ind || b_flux_ind) {
  124. return;
  125. }
  126. if (!b_rs_ested) { //必须先识别相电阻
  127. return;
  128. }
  129. hj_v = v;
  130. hj_freq = freq;
  131. hj_n = (float)FOC_PWM_FS / hj_freq;
  132. hj_samples = hj_n * 50;
  133. K_terms = (s32) (0.5f + hj_samples*hj_freq/(float)FOC_PWM_FS);
  134. Vdead = PMSM_FOC_Get()->in.s_vDC * 0.5f * (float)CONFIG_HW_DeadTime / (float)FOC_PWM_period;
  135. hj_w = 360.0f / hj_n;
  136. sys_debug("hj %f, %f, %f, %f, %f, %f, %f\n", hj_v, hj_freq, hj_n, hj_samples, K_terms, Vdead, hj_w);
  137. float fft_angle = 360.0f / hj_samples * K_terms;
  138. SinCos_Lut(fft_angle, &hj_image, &hj_real);
  139. hj_real = hj_real * 2.0f;
  140. n_ind_ld = l_type;
  141. n_samples = 0;
  142. ldq_est_wait_cnt = 0;
  143. if (v_samples) {
  144. os_free(v_samples);
  145. }
  146. if (i_samples) {
  147. os_free(i_samples);
  148. }
  149. v_samples = os_alloc(sizeof(float) * hj_samples);
  150. i_samples = os_alloc(sizeof(float) * hj_samples);
  151. if (v_samples != NULL && i_samples != NULL) {
  152. b_ldq_ind = true;
  153. shark_timer_post(&_ldq_ind_timer, 10);
  154. }else {
  155. sys_debug("alloc error\n");
  156. }
  157. }
  158. static void _ldq_ind_timer_handler(shark_timer_t *t) {
  159. if (n_samples >= (hj_samples + 1)) {
  160. mc_ind_motor_start(false);
  161. mot_params_calc_inductance();
  162. mot_params_ind_stop();
  163. }else {
  164. ldq_est_wait_cnt ++;
  165. if (ldq_est_wait_cnt >= 20) {
  166. mc_ind_motor_start(false);
  167. mot_params_ind_stop();
  168. sys_debug("ldq ind timeout %d\n", ldq_est_wait_cnt);
  169. }else {
  170. shark_timer_post(&_ldq_ind_timer, 10);
  171. }
  172. }
  173. }
  174. void mot_params_ind_ld(float v, float freq) {
  175. b_ld_ested = false;
  176. mot_params_ind_inductance(v, freq, L_TYPE_D);
  177. }
  178. void mot_params_ind_lq(float v, float freq) {
  179. b_lq_ested = false;
  180. mot_params_ind_inductance(v, freq, L_TYPE_Q);
  181. }
  182. void mot_params_high_freq_inject(void) {
  183. if (!b_ldq_ind) {
  184. return;
  185. }
  186. float hj_angle = hj_w * (float)n_samples;
  187. rand_angle(hj_angle);
  188. float s, c;
  189. SinCos_Lut(hj_angle, &s, &c);
  190. float vd = 0, vq = 0;
  191. if (n_ind_ld == L_TYPE_D) {
  192. vd = hj_v * c;
  193. }else {
  194. vq = hj_v * c;
  195. }
  196. PMSM_FOC_SetOpenVdq_Immediate(vd, vq);
  197. }
  198. bool mot_params_hj_sample_vi(float vd, float vq, float id, float iq) {
  199. if (!b_ldq_ind) {
  200. return true;
  201. }
  202. if ((n_samples >= 1) && (n_samples <= hj_samples)) {
  203. if (n_ind_ld == L_TYPE_D) {
  204. v_samples[n_samples - 1] = vd * TWO_BY_THREE;
  205. i_samples[n_samples - 1] = id;
  206. }else {
  207. v_samples[n_samples - 1] = vq * TWO_BY_THREE;
  208. i_samples[n_samples - 1] = iq;
  209. }
  210. }
  211. n_samples ++;
  212. return false;
  213. }
  214. void goertzel_dft(float *x, float *real, float *image, float *mag) {
  215. float y, d1 = 0, d2 = 0;
  216. for (int i = 0; i < hj_samples; i++) {
  217. y = x[i] + hj_real * d1 - d2;
  218. d2 = d1;
  219. d1 = y;
  220. }
  221. *real = d1 - (d2 * 0.5f * hj_real);
  222. *image = -d2 * hj_image;
  223. *mag = sqrtf(SQ(*real) + SQ(*image));
  224. }
  225. void mot_params_calc_inductance(void) {
  226. float v_real, v_image, v_mag;
  227. float i_real, i_image, i_mag;
  228. if (!b_ldq_ind) {
  229. return;
  230. }
  231. goertzel_dft(v_samples, &v_real, &v_image, &v_mag);
  232. goertzel_dft(i_samples, &i_real, &i_image, &i_mag);
  233. sys_debug("v %f, %f, %f\n", v_mag/(hj_samples*0.5f), v_real, v_image);
  234. sys_debug("i %f, %f, %f\n", i_mag/(hj_samples*0.5f), i_real, i_image);
  235. v_mag -= Vdead * hj_samples*0.5f;
  236. float z_angle = fast_atan_2(i_image, i_real) - fast_atan_2(v_image, v_real);
  237. float s,c;
  238. SinCos_Lut(pi_2_degree(z_angle), &s, &c);
  239. float z_mag = v_mag / (i_mag + 0.0000001f);
  240. float z_real = z_mag * c;
  241. float z_image = z_mag * s;
  242. float Rs = rs_est_value;
  243. float Ri = (SQ(z_real - Rs) + SQ(z_image))/(z_real - Rs + 0.0000001f);
  244. float l = Ri * (z_real - Rs)/(hj_freq * 2 * PI * z_image + 0.0000001f) * 0.83f; //0.83f just for v3 board
  245. if (n_ind_ld == L_TYPE_D) {
  246. ld_est_value = l;
  247. b_ld_ested = true;
  248. sys_debug("ld = %f\n", ld_est_value);
  249. }else {
  250. lq_est_value = l;
  251. b_lq_ested = true;
  252. sys_debug("lq = %f\n", lq_est_value);
  253. }
  254. b_ldq_ind = false;
  255. }
  256. static float motVelRadusPers, flux_wait_cnt = 0, flux_do_cnt = 0, flux_est_value = 0;
  257. static void _flux_ind_timer_handler(shark_timer_t *t) {
  258. float We = PMSM_FOC_Get()->in.s_motVelRadusPers;
  259. float delta = We - motVelRadusPers;
  260. if (We > 100 && ABS(delta) < 40) {
  261. float dtc = ((float)CONFIG_HW_DeadTime/(float)FOC_PWM_Half_Period) * PMSM_FOC_Get()->in.s_vDC * 1.5f;
  262. float vq = (PMSM_FOC_Get()->out.s_OutVdq.q - dtc) * TWO_BY_THREE;
  263. float flux = vq / We;
  264. flux_est_value = LowPass_Filter(flux_est_value, flux, 0.1f);
  265. flux_do_cnt ++;
  266. }else {
  267. flux_wait_cnt ++;
  268. }
  269. if ((flux_wait_cnt >= 100) || (flux_do_cnt >= 400)) {
  270. b_flux_ind = false;
  271. if (flux_wait_cnt >= 100) {
  272. sys_debug("ind flux error\n");
  273. }else {
  274. b_flux_ested = true;
  275. sys_debug("ind_flux finish, %f\n", flux_est_value);
  276. }
  277. }else {
  278. shark_timer_post(&_flux_ind_timer, 10);
  279. }
  280. }
  281. void mot_params_ind_flux(float id, float iq) {
  282. if (b_rs_ind || b_ldq_ind || b_flux_ind) {
  283. return;
  284. }
  285. b_flux_ind = true;
  286. flux_wait_cnt = 0;
  287. flux_do_cnt = 0;
  288. flux_est_value = 0;
  289. mc_set_foc_mode(CTRL_MODE_CURRENT);
  290. PMSM_FOC_Set_Current(iq);
  291. shark_timer_post(&_flux_ind_timer, 10);
  292. motVelRadusPers = PMSM_FOC_Get()->in.s_motVelRadusPers;
  293. }
  294. float mot_params_get_est_ld(void) {
  295. return ld_est_value;
  296. }
  297. float mot_params_get_est_lq(void) {
  298. return lq_est_value;
  299. }
  300. bool mot_params_ld_ested(void) {
  301. return b_ld_ested;
  302. }
  303. bool mot_params_lq_ested(void) {
  304. return b_lq_ested;
  305. }
  306. bool mot_params_flux_ested(void) {
  307. return b_flux_ested;
  308. }
  309. float mot_params_get_est_flux(void) {
  310. return flux_est_value;
  311. }