motor_param.c 11 KB

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