e_ctrl.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 = (float)acc;
  46. r->dect = (float)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 = (float)acc;
  56. r->dect = (float)dec;
  57. }
  58. static void eRamp_init_target2(e_Ramp *r, float target, u32 time) {
  59. eRamp_init_target(r, target, time, time);
  60. }
  61. static void eRamp_reset_target(e_Ramp *r, float target) {
  62. r->start = target;
  63. r->target = target;
  64. r->first_target = target;
  65. r->interpolation = target;
  66. r->step_val = 0;
  67. r->first_step = 0;
  68. }
  69. static void eRamp_set_time(e_Ramp *r, u32 acc, u32 dec) {
  70. r->acct = (float)acc;
  71. r->dect = (float)dec;
  72. }
  73. static void eRamp_set_target(e_Ramp *r, float target) {
  74. r->target = target;
  75. }
  76. static void eRamp_set_step(e_Ramp *r, float step) {
  77. r->step_val = step;
  78. }
  79. static void eRamp_running(e_Ramp *r) {
  80. float target = r->interpolation + r->step_val;
  81. if (r->step_val < 0) {
  82. if (target < r->target) {
  83. target = r->target;
  84. }
  85. }else {
  86. if (target > r->target) {
  87. target = r->target;
  88. }
  89. }
  90. r->interpolation = target;
  91. }
  92. static float eRamp_get_intepolation(e_Ramp *r) {
  93. return r->interpolation;
  94. }
  95. static float eRamp_get_target(e_Ramp *r) {
  96. return r->target;
  97. }
  98. static void eRamp_set_step_target(e_Ramp *ramp, float c, u32 intval) {
  99. float c_now = eRamp_get_intepolation(ramp);
  100. float step_val = 0;
  101. float delta = c - c_now;
  102. float step_ms = intval;
  103. if (delta >= 0) {
  104. step_val = (delta)/(ramp->acct/step_ms);
  105. }else {
  106. step_val = (delta)/(ramp->dect/step_ms);
  107. }
  108. eRamp_set_target(ramp, c);
  109. eRamp_set_step(ramp, step_val);
  110. }
  111. #if 0
  112. extern float PMSM_FOC_GetSpeed(void);
  113. static void eRamp_X2_running(e_Ramp *r) {
  114. #if 1
  115. float target = r->target;
  116. float v_now = r->interpolation;
  117. bool cross_zero = false;
  118. if (target > 0) {
  119. if (v_now >= -CONFIG_RAMP_SECOND_TARGET && v_now <= CONFIG_RAMP_SECOND_TARGET * 1.5f) {
  120. if (PMSM_FOC_GetSpeed() <= 20.0f) {
  121. step_towards(&r->interpolation, target, 0.02f);
  122. }else {
  123. step_towards(&r->interpolation, target, 0.04f);
  124. }
  125. cross_zero = true;
  126. }
  127. }else if (target == 0) {
  128. if (v_now >= 0 && v_now <= CONFIG_RAMP_SECOND_TARGET) {
  129. step_towards(&r->interpolation, target, 0.01f);
  130. cross_zero = true;
  131. }
  132. }else {
  133. if (v_now >= -CONFIG_RAMP_SECOND_TARGET && v_now <= CONFIG_RAMP_SECOND_TARGET) {
  134. step_towards(&r->interpolation, target, 0.01f);
  135. cross_zero = true;
  136. }
  137. }
  138. if (!cross_zero) {
  139. step_towards(&r->interpolation, target, 1.0f);
  140. }
  141. #else
  142. if (r->first_step != 0) {
  143. float interpolation = r->interpolation + r->first_step;
  144. if ((r->first_step > 0) && (interpolation >= r->first_target)) {
  145. interpolation = r->first_target;
  146. r->first_step = r->first_target = 0;
  147. }else if ((r->first_step < 0) && (interpolation <= r->first_target)) {
  148. interpolation = r->first_target;
  149. r->first_step = r->first_target = 0;
  150. }
  151. r->interpolation = interpolation;
  152. return;
  153. }
  154. eRamp_running(r);
  155. #endif
  156. }
  157. #else
  158. static void eRamp_X2_running(e_Ramp *r) {
  159. eRamp_running(r);
  160. }
  161. #endif
  162. #if 0
  163. static void eRamp_set_X2_target(e_Ramp *r, float c) {
  164. #if 1
  165. eRamp_set_target(r, c);
  166. #else
  167. float c_now = eRamp_get_intepolation(ramp);
  168. float step_val = 0;
  169. float delta = c - c_now;
  170. float step_ms = CONFIG_eCTRL_STEP_TS;
  171. if (delta > 0) {
  172. step_val = (delta)/(ramp->acct/step_ms);
  173. if (step_val > CONFIG_RAMP_SECOND_STEP) {
  174. float first_delta = min(delta, CONFIG_RAMP_SECOND_TARGET);
  175. ramp->first_target = c_now + first_delta;
  176. ramp->first_step = CONFIG_RAMP_SECOND_STEP;
  177. delta -= first_delta;
  178. step_val = (delta)/(ramp->acct/step_ms);
  179. }else {
  180. ramp->first_target = ramp->first_step = 0.0f;
  181. }
  182. }else if (delta < 0){
  183. step_val = (delta)/(ramp->dect/step_ms);
  184. if (ABS(step_val) > CONFIG_RAMP_SECOND_STEP) {
  185. float first_delta = MAX(delta, -CONFIG_RAMP_SECOND_TARGET);
  186. ramp->first_target = c_now + first_delta;
  187. ramp->first_step = -CONFIG_RAMP_SECOND_STEP;
  188. delta -= first_delta;
  189. step_val = (delta)/(ramp->dect/step_ms);
  190. }else {
  191. ramp->first_target = ramp->first_step = 0.0f;
  192. }
  193. }else {
  194. step_val = 0;
  195. ramp->first_step = ramp->first_target = 0;
  196. }
  197. eRamp_set_target(ramp, c);
  198. eRamp_set_step(ramp, step_val);
  199. #endif
  200. }
  201. #else
  202. static void eRamp_set_X2_target(e_Ramp *r, float c) {
  203. eRamp_set_step_target(r, c, CONFIG_eCTRL_STEP_TS);
  204. }
  205. #endif
  206. //y=Ax^2;
  207. void eCtrl_init(u16 accl_time, u16 dec_time);
  208. void eCtrl_brake_signal(bool hw_brake);
  209. bool eCtrl_is_eBrk_Running(void);
  210. void eCtrl_set_TgtCurrent(float c);
  211. void eCtrl_set_TgtTorque(float t);
  212. void eCtrl_set_TgtSpeed(float s);
  213. bool eCtrl_enable_eBrake(bool enable);
  214. float eCtrl_get_RefSpeed(void);
  215. float eCtrl_get_RefCurrent(void);
  216. float eCtrl_get_RefTorque(void);
  217. float eCtrl_get_FinalSpeed(void);
  218. float eCtrl_get_FinalCurrent(void);
  219. float eCtrl_get_FinalTorque(void);
  220. void eCtrl_Running(void);
  221. void eCtrl_Reset(void);
  222. void eCtrl_reset_Torque(float init_trq);
  223. void eCtrl_reset_Current(float init_curr);
  224. void eCtrl_set_accl_time(u16 time);
  225. #endif /* EBRAKE_CTRL_H__ */