motor_param.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. static void motor_vol_lookup(s16 vol, vol_rpm_torque_map *table, vol_rpm_torque_map **out_mapl, vol_rpm_torque_map **out_maph) {
  94. if (vol <= table[0].vol) {
  95. *out_mapl = table;
  96. *out_maph = table + 1;
  97. return;
  98. }else if (vol >= table[CONFIG_MAX_VOL_COUNT-1].vol){
  99. *out_mapl = table + CONFIG_MAX_VOL_COUNT - 2;
  100. *out_maph = table + CONFIG_MAX_VOL_COUNT - 1;
  101. return;
  102. }
  103. for (int i = 1; i < CONFIG_MAX_VOL_COUNT; i++) {
  104. if ((vol <= table[i].vol)) {
  105. *out_mapl = table + i - 1;
  106. *out_maph = table + i;
  107. break;
  108. }
  109. }
  110. }
  111. static void motor_rpm_lookup(s16 rpm, vol_rpm_torque_map *map, rpm_torque_map **out_map1, rpm_torque_map **out_maph) {
  112. rpm_torque_map *table = map->rpm_torque;
  113. if (rpm <= table[0].rpm) {
  114. *out_map1 = table;
  115. *out_maph = table + 1;
  116. return;
  117. }else if (rpm >= table[map->n - 1].rpm) {
  118. *out_map1 = *out_maph = table + map->n - 1;
  119. return;
  120. }
  121. for (int i = 1; i < map->n; i++) {
  122. if (rpm <= table[i].rpm) {
  123. *out_map1 = table + i - 1;
  124. *out_maph = table + i;
  125. break;
  126. }
  127. }
  128. }
  129. static void motor_current_lookup(s16 torque, rpm_torque_map *map, torque2dq_t**currl, torque2dq_t **currh) {
  130. torque2dq_t *table = map->dqmap;
  131. if (torque <= table[0].torque) {
  132. *currl = table;
  133. *currh = table + 1;
  134. }else if (torque >= table[map->n - 1].torque) {
  135. *currl = table + map->n - 2;
  136. *currh = table + map->n - 1;
  137. }
  138. for (int i = 1; i < map->n; i++) {
  139. if (torque <= table[i].torque) {
  140. *currl = table + i - 1;
  141. *currh = table + i;
  142. break;
  143. }
  144. }
  145. }
  146. static void motor_torque_lookup(s16 vel, s16 torque, rpm_torque_map *mapl, rpm_torque_map *maph, dq_t *dq_out) {
  147. torque2dq_t *currl, *currh;
  148. motor_current_lookup(torque, mapl, &currl, &currh);
  149. float dl = line_intp(torque, currl->torque, currh->torque, currl->d, currh->d);
  150. _DEBUG("rpm %d -> curr %d : %d, id=%f\n", mapl->rpm, currl->torque, currh->torque, dl);
  151. motor_current_lookup(torque, maph, &currl, &currh);
  152. float dh = line_intp(torque, currl->torque, currh->torque, currl->d, currh->d);
  153. _DEBUG("rpm %d -> curr %d : %d, id=%f\n", maph->rpm, currl->torque, currh->torque, dh);
  154. float d = line_intp(vel, mapl->rpm, maph->rpm, dl, dh);
  155. _DEBUG("id = %f\n", d);
  156. float ql = line_intp(torque, currl->torque, currh->torque, currl->q, currh->q);
  157. _DEBUG("rpm %d -> curr %d : %d, iq=%f\n", mapl->rpm, currl->torque, currh->torque, ql);
  158. motor_current_lookup(torque, maph, &currl, &currh);
  159. float qh = line_intp(torque, currl->torque, currh->torque, currl->q, currh->q);
  160. _DEBUG("rpm %d -> curr %d : %d, id=%f\n", maph->rpm, currl->torque, currh->torque, qh);
  161. float q = line_intp(vel, mapl->rpm, maph->rpm, ql, qh);
  162. _DEBUG("id = %f\n", d);
  163. dq_out->d = d;
  164. dq_out->q = q;
  165. }
  166. #define SCALE(X) (X/10)
  167. void motor_mpta_fw_lookup2(float rpm, float vdc, float torque, dq_t *dq_out) {
  168. vol_rpm_torque_map *vmapl, *vmaph;
  169. rpm_torque_map *rmapl, *rmaph;
  170. dq_t dql, dqh;
  171. s16 vel = ABS(((s16)rpm));
  172. s16 bus_vol = vdc;
  173. torque = torque * 10;
  174. /* 通过当前电压查找 对应的转速扭矩 */
  175. motor_vol_lookup(bus_vol, vol_rpm_curr_map, &vmapl, &vmaph);
  176. _DEBUG("vol %d : %d\n", vmapl->vol, vmaph->vol);
  177. /* 通过当前转速查找对应的扭矩 */
  178. motor_rpm_lookup(vel, vmapl, &rmapl, &rmaph);
  179. _DEBUG("vol %d -> rpm %d : %d\n", vmapl->vol, rmapl->rpm, rmaph->rpm);
  180. /* 通过当前扭矩需求插值计算dq电流 */
  181. motor_torque_lookup(vel, ABS(torque), rmapl, rmaph, &dql);
  182. motor_rpm_lookup(vel, vmaph, &rmapl, &rmaph);
  183. _DEBUG("vol %d -> rpm %d : %d\n", vmaph->vol, rmapl->rpm, rmaph->rpm);
  184. motor_torque_lookup(vel, ABS(torque), rmapl, rmaph, &dqh);
  185. float d = line_intp(bus_vol, vmapl->vol, vmaph->vol, dql.d, dqh.d);
  186. float q = line_intp(bus_vol, vmapl->vol, vmaph->vol, dql.q, dqh.q);
  187. dq_out->d = SCALE(d);
  188. dq_out->q = SCALE(q) * SIGN(torque);
  189. }
  190. /* 通过电机外特性曲线获取当前转速和母线电压下的最大扭矩 */
  191. s16 motor_torque_ext_char_curve(s16 rpm, s16 vdc) {
  192. vol_rpm_torque_map *vmapl, *vmaph;
  193. rpm_torque_map *rmapl, *rmaph;
  194. s16 vel = ABS(rpm);
  195. /* 通过当前电压查找 对应的转速扭矩 */
  196. motor_vol_lookup(vdc, vol_rpm_curr_map, &vmapl, &vmaph);
  197. _DEBUG("vol %d : %d\n", vmapl->vol, vmaph->vol);
  198. /* 获取低电压对应当前转速的最大扭矩 */
  199. motor_rpm_lookup(vel, vmapl, &rmapl, &rmaph);
  200. _DEBUG("vol %d -> rpm %d : %d\n", vmapl->vol, rmapl->rpm, rmaph->rpm);
  201. s16 max_t1 = rmapl->dqmap[rmapl->n - 1].torque;
  202. s16 max_t2 = rmaph->dqmap[rmaph->n-1].torque;
  203. s16 low_vel_max_t = line_intp(vel, rmapl->rpm, rmaph->rpm, max_t1, max_t2);
  204. /* 获取高电压对应当前转速的最大扭矩 */
  205. motor_rpm_lookup(vel, vmaph, &rmapl, &rmaph);
  206. _DEBUG("vol %d -> rpm %d : %d\n", vmaph->vol, rmapl->rpm, rmaph->rpm);
  207. max_t1 = rmapl->dqmap[rmapl->n-1].torque;
  208. max_t2 = rmaph->dqmap[rmaph->n-1].torque;
  209. s16 high_vel_max_t = line_intp(vel, rmaph->rpm, rmaph->rpm, max_t1, max_t2);
  210. /* 对两个电压的扭矩插值,获取最终的最大扭矩 */
  211. return line_intp(vdc, vmapl->vol, vmaph->vol, low_vel_max_t, high_vel_max_t);
  212. }
  213. //x -> rpm
  214. //z -> torque
  215. static void intp_line2(float frac_x, s16 z, torque_map_t **map, float *d, float *q) {
  216. float frac_z1 = 0; //对应x1索引的t_maps
  217. float frac_z2 = 0; //对应x2索引的t_maps
  218. _DEBUG("z: %d, low --> %d %d\n", z, map[1]->torque, map[0]->torque);
  219. if ((map[1]->torque != map[0]->torque)) {
  220. frac_z1 = (float)(z - map[0]->torque)/(map[1]->torque - map[0]->torque);
  221. }
  222. _DEBUG("high --> %d %d\n", map[3]->torque, map[2]->torque);
  223. if ((map[3]->torque != map[2]->torque)) {
  224. frac_z2 = (float)(z - map[2]->torque)/(map[3]->torque - map[2]->torque);
  225. }
  226. _DEBUG("%f -- %f -- %f\n", frac_x, frac_z1, frac_z2);
  227. float c1 = (1.0f - frac_z1) * map[0]->d + frac_z1 * map[1]->d; //第一行插值
  228. float c2 = (1.0f - frac_z2) * map[2]->d + frac_z2 * map[3]->d; //第二行插值
  229. *d = c1 * (1.0f - frac_x) + c2 * frac_x; //两行插值
  230. #ifdef MOT_USE_PHASE_I
  231. if (z != 0) {
  232. if (z >= ABS(*d)) {
  233. *q = sqrtsub2_f(z, (*d));
  234. }else {
  235. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  236. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  237. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  238. }
  239. }else {
  240. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  241. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  242. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  243. }
  244. #else
  245. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  246. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  247. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  248. #endif
  249. }
  250. static void get_torque_range(s16 z, int index, int max_index, int *left, int *right) {
  251. int low_left = max_index - 1, low_right = max_index - 1;
  252. if (z < mtpa_fw_map[index][0].torque) {
  253. low_right = low_left = 0;
  254. _DEBUG("---%d, %d--%d\n", z, mtpa_fw_map[index][0].torque, mtpa_fw_map[0][0].torque);
  255. }else if (z > mtpa_fw_map[index][max_index - 1].torque) {
  256. low_right = low_left = max_index - 1;
  257. }else {
  258. for (int i = 0; i < max_index; i++) {
  259. if (z >= mtpa_fw_map[index][i].torque) {
  260. low_left = i;
  261. low_right = i + 1;
  262. if (i == max_index - 1) {
  263. low_right = low_left;
  264. break;
  265. }
  266. }
  267. }
  268. }
  269. *left = low_left;
  270. *right = low_right;
  271. }
  272. void motor_mpta_fw_lookup(float rpm, float torque, dq_t *dq_out) {
  273. bool neg_trq = false;
  274. s16 itorque = torque * 10;
  275. if (itorque < 0) {
  276. neg_trq = true;
  277. itorque = -itorque;
  278. }
  279. int low = 0, high = 0;
  280. s16 x1 = 0, x2 = 0;
  281. rpm = ABS(rpm);
  282. s16 irpm = motor_map_rpm_idx(rpm, &low, &high, &x1, &x2);
  283. _DEBUG("speed %d-%d, %d-%d\n", low, high, x1, x2);
  284. int max_trq_idx = TRQ_MAX_IDX;
  285. int low_left = max_trq_idx - 1, low_right = max_trq_idx - 1;
  286. get_torque_range(itorque, low, max_trq_idx, &low_left, &low_right);
  287. _DEBUG("low speed torque %d-%d\n", low_left, low_right);
  288. int high_left = max_trq_idx - 1, high_right = max_trq_idx - 1;
  289. get_torque_range(itorque, high, max_trq_idx, &high_left, &high_right);
  290. _DEBUG("high speed torque %d-%d\n", high_left, high_right);
  291. torque_map_t *maps[4];
  292. maps[0] = &mtpa_fw_map[low][low_left];
  293. maps[1] = &mtpa_fw_map[low][low_right];
  294. maps[2] = &mtpa_fw_map[high][high_left];
  295. maps[3] = &mtpa_fw_map[high][high_right];
  296. float frac_x = 0, d = 0, q = 0;
  297. if (x1 != x2) {
  298. frac_x = (float)(irpm - x1)/(x2 - x1);
  299. }
  300. intp_line2(frac_x, itorque, maps, &d, &q);
  301. if (itorque != 0) {
  302. dq_out->d = d / 10.0f;
  303. dq_out->q = q / 10.0f;
  304. if (neg_trq) {
  305. dq_out->d = dq_out->d;
  306. dq_out->q = -dq_out->q;
  307. }
  308. }else {
  309. step_towards(&dq_out->d, d/10.0f, 0.5f);
  310. step_towards(&dq_out->q, q/10.0f, 0.5f);
  311. }
  312. }
  313. #else
  314. void motor_mpta_fw_lookup(float rpm, float torque, dq_t *dq_out) {
  315. float d = 0;
  316. float q = 0;
  317. #if defined(CONFIG_MOT_ADV_ANGLE)
  318. if (torque != 0) {
  319. float advanced_angle = CONFIG_MOT_ADV_ANGLE;
  320. float s, c;
  321. arm_sin_cos(advanced_angle + 90.0f, &s, &c);
  322. d = ABS(torque) * c;
  323. d = fclamp(d, -mc_conf()->m.max_fw_id, mc_conf()->m.max_fw_id);
  324. q = sqrtsub2_f(torque, d);
  325. if (torque < 0) {
  326. q = -q;
  327. }
  328. }else {
  329. if (ABS(rpm) < 1000) {
  330. d = 0;
  331. }else if (ABS(rpm) < 3000) {
  332. d = -10;
  333. }else if (ABS(rpm) < 5000) {
  334. d = -30;
  335. }else {
  336. d = -50;
  337. }
  338. }
  339. #else
  340. #if CONFIG_CONTRL_FW_ENABLE
  341. if ((mot_contrl()->duty_filterd >= CONFIG_CONTRL_FW_START_DUTY) && (CONFIG_CONTRL_FW_START_DUTY < CONFIG_SVM_MODULATION)) {
  342. d = -f_map(mot_contrl()->duty_filterd, CONFIG_CONTRL_FW_START_DUTY, CONFIG_SVM_MODULATION, 0, mc_conf()->m.max_fw_id);
  343. }
  344. #endif
  345. q = torque;
  346. #endif
  347. step_towards(&dq_out->d, d, 10.0f);
  348. step_towards(&dq_out->q, q, 6.0f);
  349. }
  350. #endif
  351. float motor_get_ebreak_toruqe(float rpm) {
  352. float max_e_trq = mot_contrl_get_ebrk_torque(&motor.controller);
  353. if (rpm >= 2000) {
  354. return -max_e_trq;
  355. }else if (rpm >= 1000) {
  356. return -max_e_trq * ((rpm - 1000.0f) / 1000.0f * 0.25f + 0.75f);
  357. }else if (rpm > CONFIG_MIN_RPM_EXIT_EBRAKE) {
  358. return -max_e_trq * 0.75f * (rpm - CONFIG_MIN_RPM_EXIT_EBRAKE)/((float)(1000 - CONFIG_MIN_RPM_EXIT_EBRAKE));
  359. }
  360. return 0.0f;
  361. }