torque.c 6.4 KB

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