torque_unused.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "foc/core/torque.h"
  2. #include "foc/foc_config.h"
  3. #include "foc/motor/motor.h"
  4. #include "foc/motor/motor_param.h"
  5. #include "foc/core/e_ctrl.h"
  6. #include "foc/core/PMSM_FOC_Core.h"
  7. #include "app/nv_storage.h"
  8. #include "libs/logger.h"
  9. #include "prot/can_foc_msg.h"
  10. static motor_map_t gear_torques[5][5] = {
  11. [0] = {{500, 100}, {1200, 100}, {1700, 80}, {2200, 75}, {2800, 50},},
  12. [1] = {{800, 130}, {1600, 120}, {2400, 110}, {3200, 80}, {4300, 60},},
  13. [2] = {{1200, 150}, {2200, 140}, {3200, 120}, {4200, 80}, {5300, 70},},
  14. [3] = {{3000, 200}, {4740, 150}, {5050, 110}, {5200, 85}, {5300, 70},},
  15. [4] = {{4500, 200}, {4740, 150}, {5050, 110}, {5200, 85}, {5300, 70},},
  16. };
  17. /*
  18. 通过查表获取对应扭矩和速度时的Id和IQ的分配
  19. */
  20. static torque_manager_t torque_ctrl;
  21. void torque_init(void) {
  22. trq2dq_lookup_init();
  23. torque_reset();
  24. }
  25. void torque_reset(void) {
  26. memset(&torque_ctrl, 0, sizeof(torque_ctrl));
  27. }
  28. float torque_max_from_gear_rpm(void) {
  29. u8 gear = mc_get_internal_gear();
  30. if (gear > 4) {
  31. gear = 0;
  32. }
  33. motor_map_t *map = &gear_torques[gear][0];
  34. s16 rpm = (s16)torque_ctrl.spd_filted;
  35. if (rpm <= map[0].rpm) {
  36. return (float)map[0].torque;
  37. }
  38. int map_size = 5;
  39. for (int i = 1; i < map_size; i++) {
  40. if (rpm <= map[i].rpm) { //线性插值
  41. float trq1 = map[i-1].torque;
  42. float min_rpm = map[i-1].rpm;
  43. float trq2 = map[i].torque;
  44. float max_rpm = map[i].rpm;
  45. return f_map((float)rpm, min_rpm, max_rpm, trq1, trq2);
  46. }
  47. }
  48. return (float)map[map_size-1].torque;
  49. }
  50. /* 获取油门开度 */
  51. static float throttle_ration(float f_throttle) {
  52. if (f_throttle <= nv_get_foc_params()->n_startThroVol) {
  53. return 0;
  54. }
  55. float delta = f_throttle - (nv_get_foc_params()->n_startThroVol);
  56. int ration = (delta * 100.0f) / (nv_get_foc_params()->n_endThroVol - nv_get_foc_params()->n_startThroVol);
  57. return ((float)ration)/100.0f;
  58. }
  59. float throttle_to_speed(float f_throttle) {
  60. return (PMSM_FOC_GetSpeedLimit() * throttle_ration(f_throttle));
  61. }
  62. float throttle_to_torque(float f_throttle) {
  63. return PMSM_FOC_GetTorqueLimit() * throttle_ration(f_throttle);
  64. }
  65. #define FUNC_SEG1_X_END 0.5F
  66. #define FUNC_SEG1_Y_END 0.25F
  67. #define FUNC_SEG1_K (FUNC_SEG1_Y_END/FUNC_SEG1_X_END)
  68. #define FUNC_SEG2_X_OFF FUNC_SEG1_X_END
  69. #define FUNC_SEG2_Y_OFF FUNC_SEG1_Y_END
  70. #define FUNC_SEG2_K ((1.0F-FUNC_SEG1_Y_END)/(1.0F - FUNC_SEG1_X_END))
  71. static float __INLINE unline_func(float x) {
  72. if (x <= FUNC_SEG1_X_END) {
  73. return x * FUNC_SEG1_K;
  74. }
  75. return (x - FUNC_SEG1_X_END) * FUNC_SEG2_K + FUNC_SEG2_Y_OFF;
  76. }
  77. #define REAL_DQ_CEOF 0.9f
  78. #define FINAL_DQ_CEFO 1.1F
  79. static float get_throttle_torque(float trq_ration) {
  80. float curr_rpm = PMSM_FOC_GetSpeed();
  81. if (curr_rpm == 0) {
  82. torque_ctrl.spd_filted = 0;
  83. }else {
  84. LowPass_Filter(torque_ctrl.spd_filted, curr_rpm, 0.01f);
  85. }
  86. float torque_map = torque_max_from_gear_rpm();// (float)motor_max_torque_for_rpm((s16)torque_ctrl.spd_filted);
  87. float torque_lim = PMSM_FOC_GetTorqueLimit();
  88. float max_torque = min(torque_map, torque_lim);
  89. return max_torque * unline_func(trq_ration);
  90. }
  91. float get_thro_request_torque(void) {
  92. return get_throttle_torque(torque_ctrl.throttle_opening);
  93. }
  94. void request_torque(float throttle_opening) {
  95. float curr_rpm = PMSM_FOC_GetSpeed();
  96. float ref_torque = get_throttle_torque(throttle_opening);
  97. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  98. if (mc_is_cruise_enabled() && mc_throttle_released()) {
  99. return;
  100. }
  101. #endif
  102. if ((mc_throttle_released()) && eCtrl_enable_eBrake(true)) {
  103. return;
  104. }
  105. if (!mc_throttle_released()) {
  106. if (curr_rpm <= CONFIG_ZERO_SPEED_RPM) {
  107. torque_ctrl.torque_req = eCtrl_get_FinalTorque() * FINAL_DQ_CEFO;
  108. ref_torque = MAX(eCtrl_get_FinalTorque() * FINAL_DQ_CEFO, ref_torque);
  109. }
  110. if (ref_torque > torque_ctrl.torque_req) { //加扭矩step给定
  111. step_towards(&torque_ctrl.torque_req, ref_torque, 1.0f);
  112. }else { //减扭矩,直接给定
  113. torque_ctrl.torque_req = ref_torque;
  114. }
  115. PMSM_FOC_Set_Torque(torque_ctrl.torque_req);
  116. }else if (mc_throttle_released() && eCtrl_get_FinalTorque() != 0){
  117. float real_trq = PMSM_FOC_Get_Real_dqVector() * REAL_DQ_CEOF;
  118. float ref_now = min(real_trq, eCtrl_get_RefTorque());
  119. eCtrl_reset_Torque(ref_now);
  120. PMSM_FOC_Set_Torque(0);
  121. }
  122. }
  123. void request_speed(float throttle_opening) {
  124. if (!mc_is_cruise_enabled()) {
  125. float speed_Ref = PMSM_FOC_GetSpeedLimit() * throttle_opening;
  126. PMSM_FOC_Set_TgtSpeed(speed_Ref);
  127. }
  128. }
  129. #define THRO_REF_LP_CEOF 0.2f
  130. void throttle_process(u8 run_mode, float f_throttle) {
  131. if (mc_throttle_released()){
  132. torque_ctrl.throttle_value = 0;
  133. torque_ctrl.torque_req = 0;
  134. }else {
  135. LowPass_Filter(torque_ctrl.throttle_value, f_throttle, THRO_REF_LP_CEOF);
  136. }
  137. torque_ctrl.throttle_opening = throttle_ration(f_throttle);
  138. if (run_mode == CTRL_MODE_TRQ) {
  139. request_torque(torque_ctrl.throttle_opening);
  140. }else if (run_mode == CTRL_MODE_SPD) {
  141. request_speed(torque_ctrl.throttle_opening);
  142. }else if (run_mode == CTRL_MODE_EBRAKE) {
  143. eCtrl_reset_Torque(0);
  144. if (eCtrl_get_FinalCurrent() < 0.0001f && PMSM_FOC_GetSpeed() < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  145. eCtrl_enable_eBrake(false);
  146. }
  147. if (!mc_throttle_released() || (mc_throttle_released() && (PMSM_FOC_GetSpeed() == 0.0f))) {
  148. eCtrl_enable_eBrake(false);
  149. }
  150. }
  151. }