trq2dq_table.c 3.2 KB

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