etcs.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_process(void) {
  30. if (!b_etcs_en) {
  31. PMSM_FOC_Set_Torque(f_torque_ref);
  32. return;
  33. }
  34. float mot_vel = PMSM_FOC_GetSpeed();
  35. float f_vel = f_fvel + f_acc * (float)get_delta_ms(n_fv_ts);
  36. if (!b_etcs_running && (mot_vel >= (f_vel + CONFIG_ENTER_TCS_THRO))) {
  37. b_etcs_running = true;
  38. }else if (b_etcs_running && (mot_vel < (f_vel + CONFIG_EXIT_TCS_THRO))) {
  39. b_etcs_running = false;
  40. }
  41. if (b_etcs_running) {
  42. if (f_torque_tcs > 0) {
  43. n_etcs_run_cnt ++;
  44. f_torque_tcs = f_torque_ref - n_etcs_run_cnt * 1.0f;
  45. if (f_torque_tcs < 0) {
  46. f_torque_tcs = 0;
  47. }
  48. }
  49. }else {
  50. if (n_etcs_run_cnt <= 0) {
  51. f_torque_tcs = f_torque_ref;
  52. }else {
  53. n_etcs_run_cnt--;
  54. f_torque_tcs = f_torque_tcs + 1.0f;
  55. if (n_etcs_run_cnt <= 0 || f_torque_tcs >= f_torque_ref) {
  56. n_etcs_run_cnt = 0;
  57. f_torque_tcs = f_torque_ref;
  58. }
  59. }
  60. }
  61. PMSM_FOC_Set_Torque(f_torque_tcs);
  62. }