motor_param.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include "bsp/bsp.h"
  2. #include "foc/motor/motor_param.h"
  3. #include "foc/core/controller.h"
  4. #include "math/fast_math.h"
  5. #include "foc/mc_config.h"
  6. #include "foc/motor/motor.h"
  7. #include "libs/logger.h"
  8. #if defined(CONFIG_MOTOR_TORQUE_CONF)
  9. #include CONFIG_MOTOR_TORQUE_CONF
  10. #define MOT_HAVE_MAPS
  11. #define MOT_USE_PHASE_I //表示使用电流矢量和RPM查表,获取D轴电流,iq = 开根号(电流矢量的平方 - D轴电流的平方)
  12. #ifndef MOTOR_STATOR_5N
  13. #define RPM_MAX_IDX 11
  14. #define TRQ_MAX_IDX 10
  15. #define MOT_LQ_LOOKUP
  16. static int map_rpm[] = {2000, 3000, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000};
  17. #else
  18. #define RPM_MAX_IDX 11
  19. #define TRQ_MAX_IDX 10
  20. static int map_rpm[] = {4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000};
  21. #endif
  22. #endif
  23. /* 根据电机外特性map,获取当前转速下的最大扭矩,主要给计算当前扭矩需求使用 */
  24. s16 motor_max_torque_for_rpm(s16 rpm) {
  25. #ifdef MOT_HAVE_MAPS
  26. if (rpm <= mot_map[0].rpm) {
  27. return mot_map[0].torque;
  28. }
  29. int map_size = ARRAY_SIZE(mot_map);
  30. for (int i = 1; i < map_size; i++) {
  31. if (rpm <= mot_map[i].rpm) { //线性插值
  32. float trq1 = mot_map[i-1].torque;
  33. float min_rpm = mot_map[i-1].rpm;
  34. float trq2 = mot_map[i].torque;
  35. float max_rpm = mot_map[i].rpm;
  36. return (s16)f_map((float)rpm, min_rpm, max_rpm, trq1, trq2);
  37. }
  38. }
  39. return mot_map[map_size-1].torque;
  40. #else
  41. return mc_conf()->m.max_torque;
  42. #endif
  43. }
  44. float motor_get_lq_from_iq(s16 iq) {
  45. #ifdef MOT_LQ_LOOKUP
  46. iq = ABS(iq);
  47. int map_size = ARRAY_SIZE(iq_lq_map);
  48. for (int i = map_size-1; i >= 0; i--) {
  49. if (iq >= iq_lq_map[i].iq) {
  50. return iq_lq_map[i].lq;
  51. }
  52. }
  53. return iq_lq_map[0].lq;
  54. #else
  55. return mc_conf()->m.lq;
  56. #endif
  57. }
  58. s16 motor_map_rpm_idx(float rpm, int *ilow, int *ihigh, s16 *lowrpm, s16 *highrpm) {
  59. *ilow = *ihigh = 0xFF;
  60. s16 irpm = (s16)rpm;
  61. #ifdef MOT_HAVE_MAPS
  62. if (irpm >= map_rpm[RPM_MAX_IDX-1]) {
  63. irpm = map_rpm[RPM_MAX_IDX-1];
  64. *ilow = RPM_MAX_IDX-1;
  65. *ihigh = RPM_MAX_IDX-1;
  66. }else {
  67. for (int i = 0; i < RPM_MAX_IDX; i++) {
  68. if (irpm <= map_rpm[i]) {
  69. *ihigh = i;
  70. if (*ilow == 0xFF) {
  71. *ilow = 0;
  72. }
  73. break;
  74. }
  75. *ilow = i;
  76. }
  77. }
  78. *lowrpm = map_rpm[*ilow];
  79. *highrpm = map_rpm[*ihigh];
  80. #endif
  81. return irpm;
  82. }
  83. int motor_map_torque_max_count(void) {
  84. #ifdef MOT_HAVE_MAPS
  85. return TRQ_MAX_IDX;
  86. #else
  87. return 0;
  88. #endif
  89. }
  90. #define _DEBUG(fmt, args...) no_debug(fmt, ##args)
  91. #ifdef MOT_HAVE_MAPS
  92. vol_rpm_torque_map vol_rpm_curr_map[CONFIG_MAX_VOL_COUNT];
  93. u8 vol_n = CONFIG_MAX_VOL_COUNT;
  94. static void motor_vol_lookup(s16 vol, vol_rpm_torque_map *table, vol_rpm_torque_map **out_mapl, vol_rpm_torque_map **out_maph) {
  95. if (vol_n == 1) {
  96. *out_mapl = table;
  97. *out_maph = table;
  98. return;
  99. }
  100. if (vol <= table[0].vol) {
  101. *out_mapl = table;
  102. *out_maph = table + 1;
  103. return;
  104. }else if (vol >= table[vol_n - 1].vol){
  105. *out_mapl = table + vol_n - 2;
  106. *out_maph = table + vol_n - 1;
  107. return;
  108. }
  109. for (int i = 1; i < vol_n; i++) {
  110. if ((vol <= table[i].vol)) {
  111. *out_mapl = table + i - 1;
  112. *out_maph = table + i;
  113. break;
  114. }
  115. }
  116. }
  117. static void motor_rpm_lookup(s16 rpm, vol_rpm_torque_map *map, rpm_torque_map **out_map1, rpm_torque_map **out_maph) {
  118. rpm_torque_map *table = map->rpm_torque;
  119. if (rpm <= table[0].rpm) {
  120. *out_map1 = table;
  121. *out_maph = table + 1;
  122. return;
  123. }else if (rpm >= table[map->n - 1].rpm) {
  124. *out_map1 = *out_maph = table + map->n - 1;
  125. return;
  126. }
  127. for (int i = 1; i < map->n; i++) {
  128. if (rpm <= table[i].rpm) {
  129. *out_map1 = table + i - 1;
  130. *out_maph = table + i;
  131. break;
  132. }
  133. }
  134. }
  135. static void motor_current_lookup(s16 torque, rpm_torque_map *map, torque2dq_t**currl, torque2dq_t **currh) {
  136. torque2dq_t *table = map->dqmap;
  137. if (torque <= table[0].torque) {
  138. *currl = table;
  139. *currh = table + 1;
  140. }else if (torque >= table[map->n - 1].torque) {
  141. *currl = table + map->n - 2;
  142. *currh = table + map->n - 1;
  143. }
  144. for (int i = 1; i < map->n; i++) {
  145. if (torque <= table[i].torque) {
  146. *currl = table + i - 1;
  147. *currh = table + i;
  148. break;
  149. }
  150. }
  151. }
  152. static void motor_torque_lookup(s16 vel, s16 torque, rpm_torque_map *mapl, rpm_torque_map *maph, dq_t *dq_out) {
  153. torque2dq_t *currl, *currh;
  154. motor_current_lookup(torque, mapl, &currl, &currh);
  155. float dl = line_intp(torque, currl->torque, currh->torque, currl->d, currh->d);
  156. _DEBUG("rpm %d -> curr %d : %d, id=%f\n", mapl->rpm, currl->torque, currh->torque, dl);
  157. motor_current_lookup(torque, maph, &currl, &currh);
  158. float dh = line_intp(torque, currl->torque, currh->torque, currl->d, currh->d);
  159. _DEBUG("rpm %d -> curr %d : %d, id=%f\n", maph->rpm, currl->torque, currh->torque, dh);
  160. float d = line_intp(vel, mapl->rpm, maph->rpm, dl, dh);
  161. _DEBUG("id = %f\n", d);
  162. float ql = line_intp(torque, currl->torque, currh->torque, currl->q, currh->q);
  163. _DEBUG("rpm %d -> curr %d : %d, iq=%f\n", mapl->rpm, currl->torque, currh->torque, ql);
  164. motor_current_lookup(torque, maph, &currl, &currh);
  165. float qh = line_intp(torque, currl->torque, currh->torque, currl->q, currh->q);
  166. _DEBUG("rpm %d -> curr %d : %d, id=%f\n", maph->rpm, currl->torque, currh->torque, qh);
  167. float q = line_intp(vel, mapl->rpm, maph->rpm, ql, qh);
  168. _DEBUG("id = %f\n", d);
  169. dq_out->d = d;
  170. dq_out->q = q;
  171. }
  172. #define SCALE(X) (X/10)
  173. void motor_mpta_fw_lookup2(float rpm, float vdc, float torque, dq_t *dq_out) {
  174. vol_rpm_torque_map *vmapl, *vmaph;
  175. rpm_torque_map *rmapl, *rmaph;
  176. dq_t dql, dqh;
  177. s16 vel = ABS(((s16)rpm));
  178. s16 bus_vol = vdc;
  179. torque = torque * 10;
  180. /* 通过当前电压查找 对应的转速扭矩 */
  181. motor_vol_lookup(bus_vol, vol_rpm_curr_map, &vmapl, &vmaph);
  182. _DEBUG("vol %d : %d\n", vmapl->vol, vmaph->vol);
  183. /* 通过当前转速查找对应的扭矩 */
  184. motor_rpm_lookup(vel, vmapl, &rmapl, &rmaph);
  185. _DEBUG("vol %d -> rpm %d : %d\n", vmapl->vol, rmapl->rpm, rmaph->rpm);
  186. /* 通过当前扭矩需求插值计算dq电流 */
  187. motor_torque_lookup(vel, ABS(torque), rmapl, rmaph, &dql);
  188. motor_rpm_lookup(vel, vmaph, &rmapl, &rmaph);
  189. _DEBUG("vol %d -> rpm %d : %d\n", vmaph->vol, rmapl->rpm, rmaph->rpm);
  190. motor_torque_lookup(vel, ABS(torque), rmapl, rmaph, &dqh);
  191. float d = line_intp(bus_vol, vmapl->vol, vmaph->vol, dql.d, dqh.d);
  192. float q = line_intp(bus_vol, vmapl->vol, vmaph->vol, dql.q, dqh.q);
  193. dq_out->d = SCALE(d);
  194. dq_out->q = SCALE(q) * SIGN(torque);
  195. }
  196. /* 通过电机外特性曲线获取当前转速和母线电压下的最大扭矩 */
  197. s16 motor_torque_ext_char_curve(s16 rpm, s16 vdc) {
  198. vol_rpm_torque_map *vmapl, *vmaph;
  199. rpm_torque_map *rmapl, *rmaph;
  200. s16 vel = ABS(rpm);
  201. /* 通过当前电压查找 对应的转速扭矩 */
  202. motor_vol_lookup(vdc, vol_rpm_curr_map, &vmapl, &vmaph);
  203. _DEBUG("vol %d : %d\n", vmapl->vol, vmaph->vol);
  204. /* 获取低电压对应当前转速的最大扭矩 */
  205. motor_rpm_lookup(vel, vmapl, &rmapl, &rmaph);
  206. _DEBUG("vol %d -> rpm %d : %d\n", vmapl->vol, rmapl->rpm, rmaph->rpm);
  207. s16 max_t1 = rmapl->dqmap[rmapl->n - 1].torque;
  208. s16 max_t2 = rmaph->dqmap[rmaph->n - 1].torque;
  209. s16 low_vel_max_t = line_intp(vel, rmapl->rpm, rmaph->rpm, max_t1, max_t2);
  210. /* 获取高电压对应当前转速的最大扭矩 */
  211. motor_rpm_lookup(vel, vmaph, &rmapl, &rmaph);
  212. _DEBUG("vol %d -> rpm %d : %d\n", vmaph->vol, rmapl->rpm, rmaph->rpm);
  213. max_t1 = rmapl->dqmap[rmapl->n - 1].torque;
  214. max_t2 = rmaph->dqmap[rmaph->n - 1].torque;
  215. s16 high_vel_max_t = line_intp(vel, rmaph->rpm, rmaph->rpm, max_t1, max_t2);
  216. /* 对两个电压的扭矩插值,获取最终的最大扭矩 */
  217. return line_intp(vdc, vmapl->vol, vmaph->vol, low_vel_max_t, high_vel_max_t);
  218. }
  219. //x -> rpm
  220. //z -> torque
  221. static void intp_line2(float frac_x, s16 z, torque_map_t **map, float *d, float *q) {
  222. float frac_z1 = 0; //对应x1索引的t_maps
  223. float frac_z2 = 0; //对应x2索引的t_maps
  224. _DEBUG("z: %d, low --> %d %d\n", z, map[1]->torque, map[0]->torque);
  225. if ((map[1]->torque != map[0]->torque)) {
  226. frac_z1 = (float)(z - map[0]->torque)/(map[1]->torque - map[0]->torque);
  227. }
  228. _DEBUG("high --> %d %d\n", map[3]->torque, map[2]->torque);
  229. if ((map[3]->torque != map[2]->torque)) {
  230. frac_z2 = (float)(z - map[2]->torque)/(map[3]->torque - map[2]->torque);
  231. }
  232. _DEBUG("%f -- %f -- %f\n", frac_x, frac_z1, frac_z2);
  233. float c1 = (1.0f - frac_z1) * map[0]->d + frac_z1 * map[1]->d; //第一行插值
  234. float c2 = (1.0f - frac_z2) * map[2]->d + frac_z2 * map[3]->d; //第二行插值
  235. *d = c1 * (1.0f - frac_x) + c2 * frac_x; //两行插值
  236. #ifdef MOT_USE_PHASE_I
  237. if (z != 0) {
  238. if (z >= ABS(*d)) {
  239. *q = sqrtsub2_f(z, (*d));
  240. }else {
  241. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  242. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  243. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  244. }
  245. }else {
  246. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  247. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  248. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  249. }
  250. #else
  251. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  252. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  253. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  254. #endif
  255. }
  256. static void get_torque_range(s16 z, int index, int max_index, int *left, int *right) {
  257. int low_left = max_index - 1, low_right = max_index - 1;
  258. if (z < mtpa_fw_map[index][0].torque) {
  259. low_right = low_left = 0;
  260. _DEBUG("---%d, %d--%d\n", z, mtpa_fw_map[index][0].torque, mtpa_fw_map[0][0].torque);
  261. }else if (z > mtpa_fw_map[index][max_index - 1].torque) {
  262. low_right = low_left = max_index - 1;
  263. }else {
  264. for (int i = 0; i < max_index; i++) {
  265. if (z >= mtpa_fw_map[index][i].torque) {
  266. low_left = i;
  267. low_right = i + 1;
  268. if (i == max_index - 1) {
  269. low_right = low_left;
  270. break;
  271. }
  272. }
  273. }
  274. }
  275. *left = low_left;
  276. *right = low_right;
  277. }
  278. void motor_mpta_fw_lookup(float rpm, float torque, dq_t *dq_out) {
  279. bool neg_trq = false;
  280. s16 itorque = torque * 10;
  281. if (itorque < 0) {
  282. neg_trq = true;
  283. itorque = -itorque;
  284. }
  285. int low = 0, high = 0;
  286. s16 x1 = 0, x2 = 0;
  287. rpm = ABS(rpm);
  288. s16 irpm = motor_map_rpm_idx(rpm, &low, &high, &x1, &x2);
  289. _DEBUG("speed %d-%d, %d-%d\n", low, high, x1, x2);
  290. int max_trq_idx = TRQ_MAX_IDX;
  291. int low_left = max_trq_idx - 1, low_right = max_trq_idx - 1;
  292. get_torque_range(itorque, low, max_trq_idx, &low_left, &low_right);
  293. _DEBUG("low speed torque %d-%d\n", low_left, low_right);
  294. int high_left = max_trq_idx - 1, high_right = max_trq_idx - 1;
  295. get_torque_range(itorque, high, max_trq_idx, &high_left, &high_right);
  296. _DEBUG("high speed torque %d-%d\n", high_left, high_right);
  297. torque_map_t *maps[4];
  298. maps[0] = &mtpa_fw_map[low][low_left];
  299. maps[1] = &mtpa_fw_map[low][low_right];
  300. maps[2] = &mtpa_fw_map[high][high_left];
  301. maps[3] = &mtpa_fw_map[high][high_right];
  302. float frac_x = 0, d = 0, q = 0;
  303. if (x1 != x2) {
  304. frac_x = (float)(irpm - x1)/(x2 - x1);
  305. }
  306. intp_line2(frac_x, itorque, maps, &d, &q);
  307. if (itorque != 0) {
  308. dq_out->d = d / 10.0f;
  309. dq_out->q = q / 10.0f;
  310. if (neg_trq) {
  311. dq_out->d = dq_out->d;
  312. dq_out->q = -dq_out->q;
  313. }
  314. }else {
  315. step_towards(&dq_out->d, d/10.0f, 0.5f);
  316. step_towards(&dq_out->q, q/10.0f, 0.5f);
  317. }
  318. }
  319. #else
  320. void motor_mpta_fw_lookup(float rpm, float torque, dq_t *dq_out) {
  321. float d = 0;
  322. float q = 0;
  323. #if defined(CONFIG_MOT_ADV_ANGLE)
  324. if (torque != 0) {
  325. float advanced_angle = CONFIG_MOT_ADV_ANGLE;
  326. float s, c;
  327. arm_sin_cos(advanced_angle + 90.0f, &s, &c);
  328. d = ABS(torque) * c;
  329. d = fclamp(d, -mc_conf()->m.max_fw_id, mc_conf()->m.max_fw_id);
  330. q = sqrtsub2_f(torque, d);
  331. if (torque < 0) {
  332. q = -q;
  333. }
  334. }else {
  335. if (ABS(rpm) < 1000) {
  336. d = 0;
  337. }else if (ABS(rpm) < 3000) {
  338. d = -10;
  339. }else if (ABS(rpm) < 5000) {
  340. d = -30;
  341. }else {
  342. d = -50;
  343. }
  344. }
  345. #else
  346. #if CONFIG_CONTRL_FW_ENABLE
  347. if ((mot_contrl()->duty_filterd >= CONFIG_CONTRL_FW_START_DUTY) && (CONFIG_CONTRL_FW_START_DUTY < CONFIG_SVM_MODULATION)) {
  348. d = -f_map(mot_contrl()->duty_filterd, CONFIG_CONTRL_FW_START_DUTY, CONFIG_SVM_MODULATION, 0, mc_conf()->m.max_fw_id);
  349. }
  350. #endif
  351. q = torque;
  352. #endif
  353. step_towards(&dq_out->d, d, 10.0f);
  354. step_towards(&dq_out->q, q, 6.0f);
  355. }
  356. #endif
  357. float motor_get_ebreak_toruqe(float rpm) {
  358. float max_e_trq = mot_contrl_get_ebrk_torque(&motor.controller);
  359. if (rpm >= 2000) {
  360. return -max_e_trq;
  361. }else if (rpm >= 1000) {
  362. return -max_e_trq * ((rpm - 1000.0f) / 1000.0f * 0.25f + 0.75f);
  363. }else if (rpm > CONFIG_MIN_RPM_EXIT_EBRAKE) {
  364. return -max_e_trq * 0.75f * (rpm - CONFIG_MIN_RPM_EXIT_EBRAKE)/((float)(1000 - CONFIG_MIN_RPM_EXIT_EBRAKE));
  365. }
  366. return 0.0f;
  367. }