torque.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "foc/core/torque.h"
  2. #include "foc/foc_config.h"
  3. #include "foc/motor/motor.h"
  4. #include "foc/core/e_ctrl.h"
  5. #include "foc/core/PMSM_FOC_Core.h"
  6. #include "app/nv_storage.h"
  7. #include "libs/logger.h"
  8. /*
  9. 通过查表获取对应扭矩和速度时的Id和IQ的分配
  10. */
  11. static torque_lut_t *_trq_tbl = NULL;
  12. static torque_manager_t g_trq_mn;
  13. void torque_init(void) {
  14. _trq_tbl = nv_get_trq_tlb();
  15. torque_reset();
  16. }
  17. void torque_reset(void) {
  18. memset(&g_trq_mn, 0, sizeof(g_trq_mn));
  19. }
  20. void torque_get_idq(float torque, float rpm, DQ_t *dq_out) {
  21. if ((_trq_tbl == NULL) || (torque < 0 || rpm < 0)) {
  22. dq_out->d = 0;
  23. dq_out->q = torque;
  24. return;
  25. }
  26. int trq_idx = (int)torque / TBL_TRQ_INTVAL;
  27. int rpm_idx = (int)rpm / TBL_SPD_INTVAL;
  28. if (trq_idx >= MAX_TRQ_POINTS) {
  29. trq_idx = MAX_TRQ_POINTS -1;
  30. }
  31. if (rpm_idx >= MAX_SPD_POINTS) {
  32. rpm_idx = MAX_SPD_POINTS -1;
  33. }
  34. s16 d = _trq_tbl->dq[trq_idx][rpm_idx].d;
  35. s16 q = _trq_tbl->dq[trq_idx][rpm_idx].q;
  36. if (trq_idx < MAX_TRQ_POINTS - 1) {
  37. trq_idx += 1;
  38. }
  39. if (rpm_idx < MAX_SPD_POINTS - 1) {
  40. rpm_idx += 1;
  41. }
  42. s16 d_delta = _trq_tbl->dq[trq_idx][rpm_idx].d - d;
  43. s16 q_delta = _trq_tbl->dq[trq_idx][rpm_idx].q - q;
  44. float comp_ceof = 0.5f * ((torque - torque/TBL_TRQ_INTVAL*TBL_TRQ_INTVAL)/(float)TBL_TRQ_INTVAL + (rpm - rpm/TBL_SPD_INTVAL*TBL_SPD_INTVAL)/(float)TBL_SPD_INTVAL);
  45. dq_out->d = d + d_delta * comp_ceof;
  46. dq_out->q = q + q_delta * comp_ceof;
  47. }
  48. /* 获取油门开度 */
  49. static float throttle_ration(float f_throttle) {
  50. if (f_throttle <= nv_get_foc_params()->n_minThroVol) {
  51. return 0;
  52. }
  53. float delta = f_throttle - (nv_get_foc_params()->n_minThroVol);
  54. int ration = (delta * 1000.0f) / (nv_get_foc_params()->n_maxThroVol - nv_get_foc_params()->n_minThroVol);
  55. return ((float)ration)/1000.0f;
  56. }
  57. float throttle_to_speed(float f_throttle) {
  58. return (PMSM_FOC_GetSpeedLimit() * throttle_ration(f_throttle));
  59. }
  60. float throttle_to_torque(float f_throttle) {
  61. return PMSM_FOC_GetTorqueLimit() * throttle_ration(f_throttle);
  62. }
  63. #define REAL_DQ_CEOF 1.1f
  64. #if 1
  65. void torque_mode_process(void) {
  66. float ref_trq = PMSM_FOC_GetTorqueLimit() * g_trq_mn.thro_value;
  67. if ((mc_throttle_released()) && eCtrl_enable_eBrake(true)) {
  68. g_trq_mn.thro_value = 0;
  69. return;
  70. }
  71. if (!mc_throttle_released()) {
  72. if (PMSM_FOC_GetSpeed() <= 1.0f) {
  73. ref_trq = MAX(eCtrl_get_FinalTorque() * REAL_DQ_CEOF, ref_trq );
  74. }
  75. PMSM_FOC_Set_Torque(ref_trq );
  76. }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
  77. float real_trq = PMSM_FOC_Get_Real_Torque();
  78. float ref_now = min(real_trq, eCtrl_get_RefTorque());
  79. eCtrl_reset_Torque(ref_now);
  80. PMSM_FOC_Set_Torque(0);
  81. g_trq_mn.thro_value = 0;
  82. }
  83. }
  84. #else
  85. void torque_mode_process(void) {
  86. float thro_curr = g_trq_mn.thro_value;
  87. float thro_prev = g_trq_mn.thro_prev;
  88. float trq_ref = PMSM_FOC_GetTorqueLimit() * thro_curr;
  89. if ((thro_curr == 0.0f) && eCtrl_enable_eBrake(true)) {
  90. g_trq_mn.thro_value = 0;
  91. g_trq_mn.accl = false;
  92. g_trq_mn.thro_prev = 0.0f;
  93. return;
  94. }
  95. float real_trq = eCtrl_get_FinalTorque() * REAL_DQ_CEOF;
  96. if (trq_ref > 0) {
  97. if (PMSM_FOC_GetSpeed() <= 10.0f) {
  98. g_trq_mn.accl_ref = MAX(real_trq, trq_ref); //不能小于autohold产生的扭矩
  99. trq_ref = g_trq_mn.accl_ref;
  100. }else {
  101. if (thro_curr > thro_prev) {
  102. if (!g_trq_mn.accl){
  103. g_trq_mn.accl_ref = eCtrl_get_RefTorque();
  104. }
  105. if (g_trq_mn.accl_ref > PMSM_FOC_GetTorqueLimit()) {
  106. g_trq_mn.accl_ref = PMSM_FOC_GetTorqueLimit();
  107. }
  108. trq_ref = g_trq_mn.accl_ref + thro_curr * (PMSM_FOC_GetTorqueLimit() - g_trq_mn.accl_ref);
  109. g_trq_mn.accl = true;
  110. }else if (thro_curr < thro_prev){
  111. if (g_trq_mn.accl) {
  112. g_trq_mn.accl_ref = min(real_trq, eCtrl_get_RefTorque());
  113. eCtrl_reset_Torque(g_trq_mn.accl_ref);
  114. }
  115. trq_ref = thro_curr * g_trq_mn.accl_ref;
  116. g_trq_mn.accl = false;
  117. }else {
  118. if (g_trq_mn.accl) {
  119. trq_ref = g_trq_mn.accl_ref + thro_curr * (PMSM_FOC_GetTorqueLimit() - g_trq_mn.accl_ref);
  120. }else {
  121. trq_ref = thro_curr * g_trq_mn.accl_ref;
  122. }
  123. }
  124. }
  125. PMSM_FOC_Set_Torque(trq_ref);
  126. }
  127. else if (eCtrl_get_FinalTorque() != 0){
  128. float ref_now = min(real_trq, eCtrl_get_RefTorque());
  129. eCtrl_reset_Torque(ref_now);
  130. PMSM_FOC_Set_Torque(0);
  131. g_trq_mn.thro_value = 0;
  132. }
  133. g_trq_mn.thro_prev = thro_curr;
  134. }
  135. #endif
  136. void speed_mode_process(void) {
  137. float speed_Ref = g_trq_mn.spd_ref;
  138. PMSM_FOC_Set_Speed(speed_Ref);
  139. }
  140. #define THRO_REF_LP_CEOF 0.2f
  141. void throttle_process(u8 run_mode, float f_throttle) {
  142. if (run_mode == CTRL_MODE_TRQ) {
  143. float thro_value = throttle_ration(f_throttle);
  144. g_trq_mn.thro_value = LowPass_Filter(g_trq_mn.thro_value, thro_value, THRO_REF_LP_CEOF);
  145. if ((g_trq_mn.count++ % 20) == 0) {
  146. torque_mode_process();
  147. }
  148. }else if (run_mode == CTRL_MODE_SPD) {
  149. float spd_ref = throttle_to_speed(f_throttle);
  150. g_trq_mn.spd_ref = LowPass_Filter(g_trq_mn.spd_ref, spd_ref, THRO_REF_LP_CEOF);
  151. if ((g_trq_mn.count++ % 20) == 0) {
  152. speed_mode_process();
  153. }
  154. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  155. eCtrl_reset_Torque(0);
  156. if (eCtrl_get_FinalCurrent() < 0.0001f && PMSM_FOC_GetSpeed() < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  157. eCtrl_enable_eBrake(false);
  158. }
  159. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  160. eCtrl_enable_eBrake(false);
  161. }
  162. }
  163. }
  164. /*
  165. void torque_manager(u8 run_mode, float f_throttle) {
  166. if ((g_trq_mn.count++ % 20) != 0) {
  167. return;
  168. }
  169. if (run_mode == CTRL_MODE_SPD) {
  170. float speed_Ref = throttle_to_speed(f_throttle);
  171. PMSM_FOC_Set_Speed(speed_Ref);
  172. }else if (run_mode == CTRL_MODE_TRQ) {
  173. if (mc_throttle_released()) {
  174. eCtrl_enable_eBrake(true);
  175. PMSM_FOC_Set_Torque(0);
  176. g_trq_mn.torque_prev = 0;
  177. }else {
  178. float torque = throttle_to_torque(f_throttle);
  179. eCtrl_enable_eBrake(false);
  180. PMSM_FOC_Set_Torque(torque);
  181. g_trq_mn.torque_prev = torque;
  182. }
  183. }else if (run_mode == CTRL_MODE_CURRENT_BRK) {
  184. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  185. eCtrl_enable_eBrake(false);
  186. }
  187. }
  188. } */