etcs.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "foc/motor/motor.h"
  2. #include "foc/core/controller.h"
  3. #include "foc/core/etcs.h"
  4. #include "math/fast_math.h"
  5. #define CONFIG_ENTER_TCS_THRO 200
  6. #define CONFIG_EXIT_TCS_THRO 80
  7. void etcs_set_fvel(etcs_t *etcs, float vel) {
  8. float vel_delta = vel - etcs->f_fvel;
  9. float ts_delta = (float)get_delta_ms(etcs->n_fv_ts);
  10. etcs->f_fvel = vel;
  11. etcs->n_fv_ts = get_tick_ms();
  12. float acc = vel_delta / ts_delta;
  13. etcs->f_acc = LowPass_Filter(etcs->f_acc, acc, 0.2f);
  14. }
  15. float etcs_process(etcs_t *etcs) {
  16. if (!etcs->b_etcs_en) {
  17. etcs->n_etcs_run_cnt = 0;
  18. etcs->output = 1.0f;
  19. return etcs->output;
  20. }
  21. float mot_vel = mot_contrl_get_speed(&motor.controller);
  22. float f_vel = etcs->f_fvel + etcs->f_acc * (float)get_delta_ms(etcs->n_fv_ts);
  23. if (!etcs->b_etcs_running && (mot_vel >= (f_vel + CONFIG_ENTER_TCS_THRO))) {
  24. etcs->b_etcs_running = true;
  25. }else if (etcs->b_etcs_running && (mot_vel < (f_vel + CONFIG_EXIT_TCS_THRO))) {
  26. etcs->b_etcs_running = false;
  27. }
  28. if (etcs->b_etcs_running) {
  29. if (etcs->output > 0) {
  30. etcs->n_etcs_run_cnt ++;
  31. etcs->output = etcs->output - 0.01f;
  32. if (etcs->output < 0) {
  33. etcs->output = 0;
  34. }
  35. }
  36. }else {
  37. if (etcs->n_etcs_run_cnt > 0) {
  38. etcs->n_etcs_run_cnt--;
  39. etcs->output = etcs->output + 0.01f;
  40. if (etcs->n_etcs_run_cnt <= 0 || etcs->output >= 1.0f) {
  41. etcs->n_etcs_run_cnt = 0;
  42. etcs->output = 1.0f;
  43. }
  44. }
  45. }
  46. return etcs->output;
  47. }