trq2dq_table.c 3.3 KB

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