e_ctrl.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef EBRAKE_CTRL_H__
  2. #define EBRAKE_CTRL_H__
  3. #include "os/os_types.h"
  4. #include "foc/core/ramp_ctrl.h"
  5. #include "math/fix_math.h"
  6. typedef struct {
  7. //float start;
  8. float target;
  9. float interpolation;
  10. float step_val;
  11. }e_Ramp;
  12. typedef struct {
  13. u16 ebrk_time; //能量回收,时间越短,刹车性能或者回收越好
  14. u16 accl_time; //加速时间(ms),时间越短,加速性能越好
  15. u16 dec_time; //降速时间
  16. bool hw_brake;
  17. bool is_ebrake;
  18. u32 brake_ts;//检测到刹车开始时间
  19. e_Ramp current;
  20. e_Ramp torque;
  21. e_Ramp speed;
  22. u16 ebrk_shadow;
  23. u16 accl_shadow;
  24. u16 dec_shadow;
  25. float ebrake_current;
  26. float current_shadow;
  27. float torque_shadow;
  28. float speed_shadow;
  29. bool is_ebrake_shadow;
  30. }e_Ctrl;
  31. static void eRamp_init(e_Ramp *r) {
  32. //r->start = 0;
  33. r->target = 0;
  34. r->interpolation = 0;
  35. r->step_val = 0;
  36. }
  37. static void eRamp_init_target(e_Ramp *r, float target) {
  38. //r->start = 0;
  39. r->target = target;
  40. r->interpolation = target;
  41. r->step_val = 0;
  42. }
  43. static void eRamp_set_target(e_Ramp *r, float target) {
  44. r->target = target;
  45. }
  46. static void eRamp_set_step(e_Ramp *r, float step) {
  47. r->step_val = step;
  48. }
  49. static void eRamp_running(e_Ramp *r) {
  50. float target = r->interpolation + r->step_val;
  51. if (r->step_val < 0) {
  52. if (target < r->target) {
  53. target = r->target;
  54. }
  55. }else {
  56. if (target > r->target) {
  57. target = r->target;
  58. }
  59. }
  60. r->interpolation = target;
  61. }
  62. static float eRamp_get_intepolation(e_Ramp *r) {
  63. return r->interpolation;
  64. }
  65. static float eRamp_get_target(e_Ramp *r) {
  66. return r->target;
  67. }
  68. static void eRamp_set_step_target(e_Ramp *ramp, float c, u32 intval, u32 acct, u32 dect) {
  69. float c_now = eRamp_get_intepolation(ramp);
  70. float step_val = 0;
  71. int sign = 1;
  72. if (c < c_now) {
  73. sign = -1;
  74. }
  75. u32 step_ms = intval;
  76. if (sign > 0) { //增加扭矩
  77. step_val = (c - c_now)/(acct/step_ms);
  78. if (step_val < MIN_FLOAT) {
  79. step_val = MIN_FLOAT;
  80. }
  81. }else if (sign < 0) {
  82. step_val = (c_now - c)/(dect/step_ms);
  83. if (step_val < MIN_FLOAT) {
  84. step_val = MIN_FLOAT;
  85. }
  86. step_val = -step_val;
  87. }
  88. eRamp_set_target(ramp, c);
  89. eRamp_set_step(ramp, step_val);
  90. }
  91. void eCtrl_init(u16 accl_time, u16 dec_time);
  92. void eCtrl_set_ebrk_time(u16 ebrk_time);
  93. void eCtrl_brake_signal(bool hw_brake);
  94. bool eCtrl_is_eBrk_enabled(void);
  95. void eCtrl_set_TgtCurrent(float c);
  96. void eCtrl_set_TgtTorque(float t);
  97. void eCtrl_set_TgtSpeed(float s);
  98. bool eCtrl_enable_eBrake(bool enable);
  99. float eCtrl_get_RefSpeed(void);
  100. float eCtrl_get_RefCurrent(void);
  101. float eCtrl_get_RefTorque(void);
  102. float eCtrl_get_FinalSpeed(void);
  103. float eCtrl_get_FinalCurrent(void);
  104. float eCtrl_get_FinalTorque(void);
  105. void eCtrl_Running(void);
  106. #endif /* EBRAKE_CTRL_H__ */