e_ctrl.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "foc/foc_config.h"
  6. #include "math/fast_math.h"
  7. #include "math/fix_math.h"
  8. typedef struct {
  9. float start;
  10. float target;
  11. float interpolation;
  12. float step_val;
  13. float first_target;
  14. float first_step;
  15. float A;
  16. float acct;
  17. float dect;
  18. float time;
  19. }e_Ramp;
  20. typedef struct {
  21. u16 ebrk_time; //能量回收,时间越短,刹车性能或者回收越好
  22. u16 accl_time; //加速时间(ms),时间越短,加速性能越好
  23. u16 dec_time; //降速时间
  24. bool hw_brake;
  25. bool is_ebrake;
  26. u32 brake_ts;//检测到刹车开始时间
  27. e_Ramp current;
  28. e_Ramp torque;
  29. e_Ramp speed;
  30. u16 ebrk_time_shadow;
  31. u16 accl_time_shadow;
  32. u16 dec_time_shadow;
  33. float ebrake_current;
  34. float current_shadow;
  35. float torque_shadow;
  36. float speed_shadow;
  37. }e_Ctrl;
  38. static void eRamp_init(e_Ramp *r, u32 acc, u32 dec) {
  39. r->start = 0;
  40. r->target = 0;
  41. r->first_target = 0;
  42. r->interpolation = 0;
  43. r->step_val = 0;
  44. r->first_step = 0;
  45. r->acct = acc;
  46. r->dect = dec;
  47. }
  48. static void eRamp_init_target(e_Ramp *r, float target, u32 acc, u32 dec) {
  49. r->start = target;
  50. r->target = target;
  51. r->first_target = target;
  52. r->interpolation = target;
  53. r->step_val = 0;
  54. r->first_step = 0;
  55. r->acct = acc;
  56. r->dect = dec;
  57. }
  58. static void eRamp_reset_target(e_Ramp *r, float target) {
  59. r->start = target;
  60. r->target = target;
  61. r->first_target = target;
  62. r->interpolation = target;
  63. r->step_val = 0;
  64. r->first_step = 0;
  65. }
  66. static void eRamp_set_time(e_Ramp *r, u32 acc, u32 dec) {
  67. r->acct = acc;
  68. r->dect = dec;
  69. }
  70. static void eRamp_set_target(e_Ramp *r, float target) {
  71. r->target = target;
  72. }
  73. static void eRamp_set_step(e_Ramp *r, float step) {
  74. r->step_val = step;
  75. }
  76. static void eRamp_running(e_Ramp *r) {
  77. float target = r->interpolation + r->step_val;
  78. if (r->step_val < 0) {
  79. if (target < r->target) {
  80. target = r->target;
  81. }
  82. }else {
  83. if (target > r->target) {
  84. target = r->target;
  85. }
  86. }
  87. r->interpolation = target;
  88. }
  89. static float eRamp_get_intepolation(e_Ramp *r) {
  90. return r->interpolation;
  91. }
  92. static float eRamp_get_target(e_Ramp *r) {
  93. return r->target;
  94. }
  95. static void eRamp_set_step_target(e_Ramp *ramp, float c, u32 intval) {
  96. float c_now = eRamp_get_intepolation(ramp);
  97. float step_val = 0;
  98. float delta = c - c_now;
  99. float step_ms = intval;
  100. if (delta >= 0) {
  101. step_val = (delta)/(ramp->acct/step_ms);
  102. }else {
  103. step_val = (delta)/(ramp->dect/step_ms);
  104. }
  105. eRamp_set_target(ramp, c);
  106. eRamp_set_step(ramp, step_val);
  107. }
  108. static void eRamp_X2_running(e_Ramp *r) {
  109. #if 0
  110. if (r->first_target != 0) {
  111. r->interpolation += r->step_val;
  112. if (r->step_val > 0) {
  113. if (r->interpolation >= r->first_target) {
  114. r->interpolation = r->first_target;
  115. r->first_target = 0.0f;
  116. }
  117. }else {
  118. if (r->interpolation <= r->first_target) {
  119. r->interpolation = r->first_target;
  120. r->first_target = 0.0f;
  121. }
  122. }
  123. }
  124. if (r->first_target != 0) {
  125. return;
  126. }
  127. if(r->time < 0xFFFFFFFu) {
  128. r->time ++;
  129. }
  130. r->interpolation = r->start + r->A * (float)SQ(r->time);
  131. if ((r->A > 0) && (r->interpolation > r->target)) {
  132. r->interpolation = r->target;
  133. }else if ((r->A < 0) && (r->interpolation < r->target)) {
  134. r->interpolation = r->target;
  135. }
  136. #else
  137. if (r->first_step != 0) {
  138. float interpolation = r->interpolation + r->first_step;
  139. if ((r->first_step > 0) && (interpolation >= r->first_target)) {
  140. interpolation = r->first_target;
  141. r->first_step = r->first_target = 0;
  142. }else if ((r->first_step < 0) && (interpolation <= r->first_target)) {
  143. interpolation = r->first_target;
  144. r->first_step = r->first_target = 0;
  145. }
  146. r->interpolation = interpolation;
  147. return;
  148. }
  149. eRamp_running(r);
  150. #endif
  151. }
  152. static void eRamp_set_X2_target(e_Ramp *ramp, float c) {
  153. #if 0
  154. float c_now = eRamp_get_intepolation(ramp);
  155. float delta = c - c_now;
  156. float first_delta = 0;
  157. if (delta > 0) {
  158. first_delta = 0;//min(delta, 20.0f);
  159. ramp->first_target = 0;//c_now + first_delta;
  160. ramp->step_val = 0.05f;
  161. ramp->A = (delta - first_delta)/SQ(ramp->acct);
  162. }else {
  163. first_delta = 0;//MAX(delta, -10.0f);
  164. ramp->first_target = 0;//c_now + first_delta;
  165. ramp->step_val = -0.01f;
  166. ramp->A = (delta - first_delta)/SQ(ramp->dect);
  167. }
  168. ramp->start = c_now + first_delta;
  169. ramp->time = 0;
  170. eRamp_set_target(ramp, c);
  171. #else
  172. float c_now = eRamp_get_intepolation(ramp);
  173. float step_val = 0;
  174. float delta = c - c_now;
  175. float step_ms = CONFIG_eCTRL_STEP_TS;
  176. if (delta > 0) {
  177. step_val = (delta)/(ramp->acct/step_ms);
  178. if (step_val > 0.1f) {
  179. float first_delta = min(delta, 5.0f);
  180. ramp->first_target = c_now + first_delta;
  181. ramp->first_step = 0.1f;
  182. delta -= first_delta;
  183. step_val = (delta)/(ramp->acct/step_ms);
  184. }else {
  185. ramp->first_target = ramp->first_step = 0.0f;
  186. }
  187. }else if (delta < 0){
  188. step_val = (delta)/(ramp->dect/step_ms);
  189. if (ABS(step_val) > 0.1f) {
  190. float first_delta = MAX(delta, -5.0f);
  191. ramp->first_target = c_now + first_delta;
  192. ramp->first_step = -0.1f;
  193. delta -= first_delta;
  194. step_val = (delta)/(ramp->dect/step_ms);
  195. }else {
  196. ramp->first_target = ramp->first_step = 0.0f;
  197. }
  198. }else {
  199. step_val = 0;
  200. ramp->first_step = ramp->first_target = 0;
  201. }
  202. eRamp_set_target(ramp, c);
  203. eRamp_set_step(ramp, step_val);
  204. #endif
  205. }
  206. //y=Ax^2;
  207. void eCtrl_init(u16 accl_time, u16 dec_time);
  208. void eCtrl_set_ebrk_time(u16 ebrk_time);
  209. void eCtrl_brake_signal(bool hw_brake);
  210. bool eCtrl_is_eBrk_enabled(void);
  211. void eCtrl_set_TgtCurrent(float c);
  212. void eCtrl_set_TgtTorque(float t);
  213. void eCtrl_set_TgtSpeed(float s);
  214. bool eCtrl_enable_eBrake(bool enable);
  215. float eCtrl_get_RefSpeed(void);
  216. float eCtrl_get_RefCurrent(void);
  217. float eCtrl_get_RefTorque(void);
  218. float eCtrl_get_FinalSpeed(void);
  219. float eCtrl_get_FinalCurrent(void);
  220. float eCtrl_get_FinalTorque(void);
  221. void eCtrl_Running(void);
  222. void eCtrl_Reset(void);
  223. void eCtrl_reset_Torque(float init_trq);
  224. void eCtrl_reset_Current(float init_curr);
  225. #endif /* EBRAKE_CTRL_H__ */