trq2dq_table.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "os/os_types.h"
  2. #include "foc/core/PMSM_FOC_Core.h"
  3. #include "foc/motor/motor_param.h"
  4. #include "libs/logger.h"
  5. #define _DEBUG(fmt, args...) sys_debug(fmt, ##args)
  6. extern torque_map_t table_map[14][10];
  7. //x -> rpm
  8. //z -> torque
  9. static void intp_line2(float frac_x, s16 z, torque_map_t **map, float *d, float *q) {
  10. float frac_z1 = 0; //对应x1索引的t_maps
  11. float frac_z2 = 0; //对应x2索引的t_maps
  12. _DEBUG("low --> %d %d\n", map[1]->torque, map[0]->torque);
  13. if ((map[1]->torque != map[0]->torque)) {
  14. frac_z1 = (float)(z - map[0]->torque)/(map[1]->torque - map[0]->torque);
  15. }
  16. _DEBUG("high --> %d %d\n", map[3]->torque, map[2]->torque);
  17. if ((map[3]->torque != map[2]->torque)) {
  18. frac_z2 = (float)(z - map[2]->torque)/(map[3]->torque - map[2]->torque);
  19. }
  20. _DEBUG("%f -- %f -- %f\n", frac_x, frac_z1, frac_z2);
  21. float c1 = (1.0f - frac_z1) * map[0]->d + frac_z1 * map[1]->d; //第一行插值
  22. float c2 = (1.0f - frac_z2) * map[2]->d + frac_z2 * map[3]->d; //第二行插值
  23. *d = c1 * (1.0f - frac_x) + c2 * frac_x; //两行插值
  24. c1 = (1.0f - frac_z1) * map[0]->q + frac_z1 * map[1]->q;
  25. c2 = (1.0f - frac_z2) * map[2]->q + frac_z2 * map[3]->q;
  26. *q = c1 * (1.0f - frac_x) + c2 * frac_x;
  27. }
  28. static void get_torque_range(s16 z, int index, int max_index, int *left, int *right) {
  29. int low_left = max_index - 1, low_right = max_index - 1;
  30. if (z < table_map[index][0].torque) {
  31. low_right = low_left = 0;
  32. _DEBUG("---%d, %d--%d\n", z, table_map[index][0].torque, table_map[0][0].torque);
  33. }else if (z > table_map[index][max_index - 1].torque) {
  34. low_right = low_left = max_index - 1;
  35. }else {
  36. for (int i = 0; i < max_index; i++) {
  37. //_DEBUG("index %d, trq %d\n", i, table_map[index][i].torque);
  38. if (z >= table_map[index][i].torque) {
  39. low_left = i;
  40. low_right = i + 1;
  41. if (i == max_index - 1) {
  42. low_right = low_left;
  43. break;
  44. }
  45. }
  46. }
  47. }
  48. *left = low_left;
  49. *right = low_right;
  50. }
  51. void trq2dq_lookup_init(void) {
  52. //if (table_map == NULL) {
  53. //table_map = get_torque2dq_maps();
  54. //}
  55. }
  56. void trq2dq_lookup(int rpm, s16 torque, DQ_t *dq_out) {
  57. //if (table_map == NULL) {
  58. // step_towards(&dq_out->d, 0, 10.0f);
  59. // dq_out->q = torque;
  60. // return;
  61. //}
  62. if (torque < 0.0f) {
  63. dq_out->d = 0.0f;
  64. dq_out->q = torque;
  65. return;
  66. }
  67. int low = 0, high = 0;
  68. s16 x1 = 0, x2 = 0;
  69. rpm = motor_map_rpm_idx(rpm, &low, &high, &x1, &x2);
  70. _DEBUG("speed %d-%d, %d-%d\n", low, high, x1, x2);
  71. int max_trq_idx = motor_map_torque_max_count();
  72. int low_left = max_trq_idx - 1, low_right = max_trq_idx - 1;
  73. get_torque_range(torque, low, max_trq_idx, &low_left, &low_right);
  74. _DEBUG("low speed torque %d-%d\n", low_left, low_right);
  75. int high_left = max_trq_idx - 1, high_right = max_trq_idx - 1;
  76. get_torque_range(torque, high, max_trq_idx, &high_left, &high_right);
  77. _DEBUG("high speed torque %d-%d\n", high_left, high_right);
  78. torque_map_t *maps[4];
  79. maps[0] = &table_map[low][low_left];
  80. maps[1] = &table_map[low][low_right];
  81. maps[2] = &table_map[high][high_left];
  82. maps[3] = &table_map[high][high_right];
  83. float frac_x = 0, d = 0, q = 0;
  84. if (x1 != x2) {
  85. frac_x = (float)(rpm - x1)/(x2 - x1);
  86. }
  87. intp_line2(frac_x, torque, maps, &d, &q);
  88. //step_towards(&dq_out->d, d, 10.0f);
  89. dq_out->d = d;
  90. dq_out->q = q;
  91. }