motor_param.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #define MOT_HAVE_MAPS
  10. #define MOT_USE_PHASE_I //表示使用电流矢量和RPM查表,获取D轴电流,iq = 开根号(电流矢量的平方 - D轴电流的平方)
  11. #ifndef MOTOR_STATOR_5N
  12. #define RPM_MAX_IDX 11
  13. #define TRQ_MAX_IDX 10
  14. #define MOT_LQ_LOOKUP
  15. static int map_rpm[] = {2000, 3000, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000};
  16. #else
  17. #define RPM_MAX_IDX 11
  18. #define TRQ_MAX_IDX 10
  19. static int map_rpm[] = {4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000};
  20. #endif
  21. #include CONFIG_MOTOR_TORQUE_CONF
  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. //x -> rpm
  93. //z -> torque
  94. static void intp_line2(float frac_x, s16 z, torque_map_t **map, float *d, float *q) {
  95. float frac_z1 = 0; //对应x1索引的t_maps
  96. float frac_z2 = 0; //对应x2索引的t_maps
  97. _DEBUG("z: %d, low --> %d %d\n", z, map[1]->torque, map[0]->torque);
  98. if ((map[1]->torque != map[0]->torque)) {
  99. frac_z1 = (float)(z - map[0]->torque)/(map[1]->torque - map[0]->torque);
  100. }
  101. _DEBUG("high --> %d %d\n", map[3]->torque, map[2]->torque);
  102. if ((map[3]->torque != map[2]->torque)) {
  103. frac_z2 = (float)(z - map[2]->torque)/(map[3]->torque - map[2]->torque);
  104. }
  105. _DEBUG("%f -- %f -- %f\n", frac_x, frac_z1, frac_z2);
  106. float c1 = (1.0f - frac_z1) * map[0]->d + frac_z1 * map[1]->d; //第一行插值
  107. float c2 = (1.0f - frac_z2) * map[2]->d + frac_z2 * map[3]->d; //第二行插值
  108. *d = c1 * (1.0f - frac_x) + c2 * frac_x; //两行插值
  109. #ifdef MOT_USE_PHASE_I
  110. if (z != 0) {
  111. if (z >= ABS(*d)) {
  112. *q = sqrtf(z*z - (*d)*(*d));
  113. }else {
  114. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  115. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  116. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  117. }
  118. }else {
  119. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  120. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  121. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  122. }
  123. #else
  124. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  125. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  126. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  127. #endif
  128. }
  129. static void get_torque_range(s16 z, int index, int max_index, int *left, int *right) {
  130. int low_left = max_index - 1, low_right = max_index - 1;
  131. if (z < mtpa_fw_map[index][0].torque) {
  132. low_right = low_left = 0;
  133. _DEBUG("---%d, %d--%d\n", z, mtpa_fw_map[index][0].torque, mtpa_fw_map[0][0].torque);
  134. }else if (z > mtpa_fw_map[index][max_index - 1].torque) {
  135. low_right = low_left = max_index - 1;
  136. }else {
  137. for (int i = 0; i < max_index; i++) {
  138. if (z >= mtpa_fw_map[index][i].torque) {
  139. low_left = i;
  140. low_right = i + 1;
  141. if (i == max_index - 1) {
  142. low_right = low_left;
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. *left = low_left;
  149. *right = low_right;
  150. }
  151. void motor_mpta_fw_lookup(float rpm, float torque, DQ_t *dq_out) {
  152. bool neg_trq = false;
  153. s16 itorque = torque * 10;
  154. if (itorque < 0) {
  155. neg_trq = true;
  156. itorque = -itorque;
  157. }
  158. int low = 0, high = 0;
  159. s16 x1 = 0, x2 = 0;
  160. rpm = ABS(rpm);
  161. s16 irpm = motor_map_rpm_idx(rpm, &low, &high, &x1, &x2);
  162. _DEBUG("speed %d-%d, %d-%d\n", low, high, x1, x2);
  163. int max_trq_idx = TRQ_MAX_IDX;
  164. int low_left = max_trq_idx - 1, low_right = max_trq_idx - 1;
  165. get_torque_range(itorque, low, max_trq_idx, &low_left, &low_right);
  166. _DEBUG("low speed torque %d-%d\n", low_left, low_right);
  167. int high_left = max_trq_idx - 1, high_right = max_trq_idx - 1;
  168. get_torque_range(itorque, high, max_trq_idx, &high_left, &high_right);
  169. _DEBUG("high speed torque %d-%d\n", high_left, high_right);
  170. torque_map_t *maps[4];
  171. maps[0] = &mtpa_fw_map[low][low_left];
  172. maps[1] = &mtpa_fw_map[low][low_right];
  173. maps[2] = &mtpa_fw_map[high][high_left];
  174. maps[3] = &mtpa_fw_map[high][high_right];
  175. float frac_x = 0, d = 0, q = 0;
  176. if (x1 != x2) {
  177. frac_x = (float)(irpm - x1)/(x2 - x1);
  178. }
  179. intp_line2(frac_x, itorque, maps, &d, &q);
  180. if (itorque != 0) {
  181. dq_out->d = d / 10.0f;
  182. dq_out->q = q / 10.0f;
  183. if (neg_trq) {
  184. dq_out->d = dq_out->d;
  185. dq_out->q = -dq_out->q;
  186. }
  187. }else {
  188. step_towards(&dq_out->d, d/10.0f, 0.5f);
  189. step_towards(&dq_out->q, q/10.0f, 0.5f);
  190. }
  191. }
  192. #else
  193. void motor_mpta_fw_lookup(float rpm, float torque, dq_t *dq_out) {
  194. float d = 0;
  195. float q = 0;
  196. #if defined(CONFIG_MOT_ADV_ANGLE)
  197. if (torque != 0) {
  198. float advanced_angle = CONFIG_MOT_ADV_ANGLE;
  199. float s, c;
  200. arm_sin_cos(advanced_angle + 90.0f, &s, &c);
  201. d = ABS(torque) * c;
  202. d = fclamp(d, -mc_conf()->m.max_fw_id, mc_conf()->m.max_fw_id);
  203. q = sqrtf(SQ(torque) - SQ(d));
  204. if (torque < 0) {
  205. q = -q;
  206. }
  207. }else {
  208. if (ABS(rpm) < 1000) {
  209. d = 0;
  210. }else if (ABS(rpm) < 3000) {
  211. d = -10;
  212. }else if (ABS(rpm) < 5000) {
  213. d = -30;
  214. }else {
  215. d = -50;
  216. }
  217. }
  218. #else
  219. q = torque;
  220. #endif
  221. step_towards(&dq_out->d, d, 1.0f);
  222. step_towards(&dq_out->q, q, 0.7f);
  223. }
  224. #endif
  225. float motor_get_ebreak_toruqe(float rpm) {
  226. float max_e_trq = mot_contrl_get_ebrk_torque(&motor.controller);
  227. if (rpm >= 2000) {
  228. return -max_e_trq;
  229. }else if (rpm >= 1000) {
  230. return -max_e_trq * ((rpm - 1000.0f) / 1000.0f * 0.25f + 0.75f);
  231. }else if (rpm > CONFIG_MIN_RPM_EXIT_EBRAKE) {
  232. return -max_e_trq * 0.75f * (rpm - CONFIG_MIN_RPM_EXIT_EBRAKE)/((float)(1000 - CONFIG_MIN_RPM_EXIT_EBRAKE));
  233. }
  234. return 0.0f;
  235. }