etcs.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "foc/motor/motor.h"
  2. #include "foc/core/PMSM_FOC_Core.h"
  3. #include "foc/core/etcs.h"
  4. #include "math/fast_math.h"
  5. static bool b_etcs_en = true;
  6. static bool b_etcs_running = false;
  7. static float f_fvel = 0, f_torque_ref, f_torque_tcs, f_acc = 0.0f;
  8. static u32 n_fv_ts = 0;
  9. static int n_etcs_run_cnt = 0;
  10. #define CONFIG_ENTER_TCS_THRO 200
  11. #define CONFIG_EXIT_TCS_THRO 80
  12. void etcs_set_fvel(float vel) {
  13. float vel_delta = vel - f_fvel;
  14. float ts_delta = (float)get_delta_ms(n_fv_ts);
  15. f_fvel = vel;
  16. n_fv_ts = get_tick_ms();
  17. float acc = vel_delta / ts_delta;
  18. f_acc = LowPass_Filter(f_acc, acc, 0.2f);
  19. }
  20. void etcs_set_torque(float torque) {
  21. f_torque_ref = torque;
  22. }
  23. bool etcs_is_running(void) {
  24. return b_etcs_running;
  25. }
  26. void etcs_enable(bool enable) {
  27. b_etcs_en = enable;
  28. }
  29. void etcs_reset_torque(float torque) {
  30. f_torque_ref = torque;
  31. eCtrl_reset_Torque(torque);
  32. }
  33. void etcs_process(void) {
  34. if (!b_etcs_en) {
  35. PMSM_FOC_Set_Torque(f_torque_ref);
  36. return;
  37. }
  38. float mot_vel = PMSM_FOC_GetSpeed();
  39. float f_vel = f_fvel + f_acc * (float)get_delta_ms(n_fv_ts);
  40. if (!b_etcs_running && (mot_vel >= (f_vel + CONFIG_ENTER_TCS_THRO))) {
  41. b_etcs_running = true;
  42. }else if (b_etcs_running && (mot_vel < (f_vel + CONFIG_EXIT_TCS_THRO))) {
  43. b_etcs_running = false;
  44. }
  45. if (b_etcs_running) {
  46. if (f_torque_tcs > 0) {
  47. n_etcs_run_cnt ++;
  48. f_torque_tcs = f_torque_ref - n_etcs_run_cnt * 1.0f;
  49. if (f_torque_tcs < 0) {
  50. f_torque_tcs = 0;
  51. }
  52. }
  53. }else {
  54. if (n_etcs_run_cnt <= 0) {
  55. f_torque_tcs = f_torque_ref;
  56. }else {
  57. n_etcs_run_cnt--;
  58. f_torque_tcs = f_torque_tcs + 1.0f;
  59. if (n_etcs_run_cnt <= 0 || f_torque_tcs >= f_torque_ref) {
  60. n_etcs_run_cnt = 0;
  61. f_torque_tcs = f_torque_ref;
  62. }
  63. }
  64. }
  65. PMSM_FOC_Set_Torque(f_torque_tcs);
  66. }