e_ctrl.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/fast_math.h"
  6. #include "math/fix_math.h"
  7. typedef struct {
  8. float start;
  9. float target;
  10. float interpolation;
  11. float step_val;
  12. float A;
  13. u32 acct;
  14. u32 dect;
  15. u32 time;
  16. }e_Ramp;
  17. typedef struct {
  18. u16 ebrk_time; //能量回收,时间越短,刹车性能或者回收越好
  19. u16 accl_time; //加速时间(ms),时间越短,加速性能越好
  20. u16 dec_time; //降速时间
  21. bool hw_brake;
  22. bool is_ebrake;
  23. u32 brake_ts;//检测到刹车开始时间
  24. e_Ramp current;
  25. e_Ramp torque;
  26. e_Ramp speed;
  27. u16 ebrk_time_shadow;
  28. u16 accl_time_shadow;
  29. u16 dec_time_shadow;
  30. float ebrake_current;
  31. float current_shadow;
  32. float torque_shadow;
  33. float speed_shadow;
  34. bool is_ebrake_shadow;
  35. }e_Ctrl;
  36. static void eRamp_init(e_Ramp *r, u32 acc, u32 dec) {
  37. r->start = 0;
  38. r->target = 0;
  39. r->interpolation = 0;
  40. r->step_val = 0;
  41. r->acct = acc;
  42. r->dect = dec;
  43. }
  44. static void eRamp_init_target(e_Ramp *r, float target, u32 acc, u32 dec) {
  45. r->start = target;
  46. r->target = target;
  47. r->interpolation = target;
  48. r->step_val = 0;
  49. r->acct = acc;
  50. r->dect = dec;
  51. }
  52. static void eRamp_set_time(e_Ramp *r, u32 acc, u32 dec) {
  53. r->acct = acc;
  54. r->dect = dec;
  55. }
  56. static void eRamp_set_target(e_Ramp *r, float target) {
  57. r->target = target;
  58. }
  59. static void eRamp_set_step(e_Ramp *r, float step) {
  60. r->step_val = step;
  61. }
  62. static void eRamp_running(e_Ramp *r) {
  63. float target = r->interpolation + r->step_val;
  64. if (r->step_val < 0) {
  65. if (target < r->target) {
  66. target = r->target;
  67. }
  68. }else {
  69. if (target > r->target) {
  70. target = r->target;
  71. }
  72. }
  73. r->interpolation = target;
  74. }
  75. static float eRamp_get_intepolation(e_Ramp *r) {
  76. return r->interpolation;
  77. }
  78. static float eRamp_get_target(e_Ramp *r) {
  79. return r->target;
  80. }
  81. static void eRamp_set_step_target(e_Ramp *ramp, float c, u32 intval) {
  82. float c_now = eRamp_get_intepolation(ramp);
  83. float step_val = 0;
  84. int sign = 1;
  85. if (c < c_now) {
  86. sign = -1;
  87. }
  88. u32 step_ms = intval;
  89. if (sign > 0) { //增加扭矩
  90. step_val = (c - c_now)/(ramp->acct/step_ms);
  91. if (step_val < MIN_FLOAT) {
  92. step_val = MIN_FLOAT;
  93. }
  94. }else if (sign < 0) {
  95. step_val = (c_now - c)/(ramp->dect/step_ms);
  96. if (step_val < MIN_FLOAT) {
  97. step_val = MIN_FLOAT;
  98. }
  99. step_val = -step_val;
  100. }
  101. eRamp_set_target(ramp, c);
  102. eRamp_set_step(ramp, step_val);
  103. }
  104. static void eRamp_X2_running(e_Ramp *r) {
  105. if(r->target == r->interpolation || (r->A == 0)) {
  106. return;
  107. }
  108. if(r->time < 0xFFFFu) {
  109. r->time ++;
  110. }
  111. r->interpolation = r->start + r->A * (float)SQ(r->time);
  112. if ((r->A > 0) && (r->interpolation > r->target)) {
  113. r->interpolation = r->target;
  114. }else if ((r->A < 0) && (r->interpolation < r->target)) {
  115. r->interpolation = r->target;
  116. }
  117. }
  118. static void eRamp_set_X2_target(e_Ramp *ramp, float c) {
  119. float c_now = eRamp_get_intepolation(ramp);
  120. float delta = c - c_now;
  121. if (delta > 0) {
  122. ramp->A = delta/SQ(ramp->acct);
  123. }else {
  124. ramp->A = delta/SQ(ramp->dect);
  125. }
  126. ramp->start = c_now;
  127. ramp->time = 0;
  128. eRamp_set_target(ramp, c);
  129. }
  130. //y=Ax^2;
  131. void eCtrl_init(u16 accl_time, u16 dec_time);
  132. void eCtrl_set_ebrk_time(u16 ebrk_time);
  133. void eCtrl_brake_signal(bool hw_brake);
  134. bool eCtrl_is_eBrk_enabled(void);
  135. void eCtrl_set_TgtCurrent(float c);
  136. void eCtrl_set_TgtTorque(float t);
  137. void eCtrl_set_TgtSpeed(float s);
  138. bool eCtrl_enable_eBrake(bool enable);
  139. float eCtrl_get_RefSpeed(void);
  140. float eCtrl_get_RefCurrent(void);
  141. float eCtrl_get_RefTorque(void);
  142. float eCtrl_get_FinalSpeed(void);
  143. float eCtrl_get_FinalCurrent(void);
  144. float eCtrl_get_FinalTorque(void);
  145. void eCtrl_Running(void);
  146. void eCtrl_Reset(void);
  147. #endif /* EBRAKE_CTRL_H__ */