etcs.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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_init(etcs_t *etcs) {
  8. memset(etcs, 0, sizeof(etcs_t));
  9. etcs->output = 1.0f;
  10. }
  11. void etcs_set_fvel(etcs_t *etcs, float vel) {
  12. float vel_delta = vel - etcs->f_fvel;
  13. float ts_delta = (float)get_delta_ms(etcs->n_fv_ts);
  14. etcs->f_fvel = vel;
  15. etcs->n_fv_ts = get_tick_ms();
  16. float acc = vel_delta / ts_delta;
  17. etcs->f_acc = LowPass_Filter(etcs->f_acc, acc, 0.2f);
  18. }
  19. float etcs_process(etcs_t *etcs) {
  20. if (!etcs->b_etcs_en) {
  21. etcs->n_etcs_run_cnt = 0;
  22. etcs->output = 1.0f;
  23. return etcs->output;
  24. }
  25. float mot_vel = mot_contrl_get_speed(&motor.controller);
  26. float f_vel = etcs->f_fvel + etcs->f_acc * (float)get_delta_ms(etcs->n_fv_ts);
  27. if (!etcs->b_etcs_running && (mot_vel >= (f_vel + CONFIG_ENTER_TCS_THRO))) {
  28. etcs->b_etcs_running = true;
  29. }else if (etcs->b_etcs_running && (mot_vel < (f_vel + CONFIG_EXIT_TCS_THRO))) {
  30. etcs->b_etcs_running = false;
  31. }
  32. if (etcs->b_etcs_running) {
  33. if (etcs->output > 0) {
  34. etcs->n_etcs_run_cnt ++;
  35. etcs->output = etcs->output - 0.01f;
  36. if (etcs->output < 0) {
  37. etcs->output = 0;
  38. }
  39. }
  40. }else {
  41. if (etcs->n_etcs_run_cnt > 0) {
  42. etcs->n_etcs_run_cnt--;
  43. etcs->output = etcs->output + 0.01f;
  44. if (etcs->n_etcs_run_cnt <= 0 || etcs->output >= 1.0f) {
  45. etcs->n_etcs_run_cnt = 0;
  46. etcs->output = 1.0f;
  47. }
  48. }
  49. }
  50. return etcs->output;
  51. }