e_ctrl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. extern float PMSM_FOC_GetSpeed(void);
  109. static void eRamp_X2_running(e_Ramp *r) {
  110. #if 1
  111. float target = r->target;
  112. float v_now = r->interpolation;
  113. bool cross_zero = false;
  114. if (target > 0) {
  115. if (v_now >= -CONFIG_RAMP_SECOND_TARGET && v_now <= CONFIG_RAMP_SECOND_TARGET * 1.5f) {
  116. if (PMSM_FOC_GetSpeed() <= 20.0f) {
  117. step_towards(&r->interpolation, target, 0.02f);
  118. }else {
  119. step_towards(&r->interpolation, target, 0.04f);
  120. }
  121. cross_zero = true;
  122. }
  123. }else if (target == 0) {
  124. if (v_now >= 0 && v_now <= CONFIG_RAMP_SECOND_TARGET) {
  125. step_towards(&r->interpolation, target, 0.01f);
  126. cross_zero = true;
  127. }
  128. }else {
  129. if (v_now >= -CONFIG_RAMP_SECOND_TARGET && v_now <= CONFIG_RAMP_SECOND_TARGET) {
  130. step_towards(&r->interpolation, target, 0.01f);
  131. cross_zero = true;
  132. }
  133. }
  134. if (!cross_zero) {
  135. step_towards(&r->interpolation, target, 1.0f);
  136. }
  137. #else
  138. if (r->first_step != 0) {
  139. float interpolation = r->interpolation + r->first_step;
  140. if ((r->first_step > 0) && (interpolation >= r->first_target)) {
  141. interpolation = r->first_target;
  142. r->first_step = r->first_target = 0;
  143. }else if ((r->first_step < 0) && (interpolation <= r->first_target)) {
  144. interpolation = r->first_target;
  145. r->first_step = r->first_target = 0;
  146. }
  147. r->interpolation = interpolation;
  148. return;
  149. }
  150. eRamp_running(r);
  151. #endif
  152. }
  153. static void eRamp_set_X2_target(e_Ramp *r, float c) {
  154. #if 1
  155. eRamp_set_target(r, c);
  156. #else
  157. float c_now = eRamp_get_intepolation(ramp);
  158. float step_val = 0;
  159. float delta = c - c_now;
  160. float step_ms = CONFIG_eCTRL_STEP_TS;
  161. if (delta > 0) {
  162. step_val = (delta)/(ramp->acct/step_ms);
  163. if (step_val > CONFIG_RAMP_SECOND_STEP) {
  164. float first_delta = min(delta, CONFIG_RAMP_SECOND_TARGET);
  165. ramp->first_target = c_now + first_delta;
  166. ramp->first_step = CONFIG_RAMP_SECOND_STEP;
  167. delta -= first_delta;
  168. step_val = (delta)/(ramp->acct/step_ms);
  169. }else {
  170. ramp->first_target = ramp->first_step = 0.0f;
  171. }
  172. }else if (delta < 0){
  173. step_val = (delta)/(ramp->dect/step_ms);
  174. if (ABS(step_val) > CONFIG_RAMP_SECOND_STEP) {
  175. float first_delta = MAX(delta, -CONFIG_RAMP_SECOND_TARGET);
  176. ramp->first_target = c_now + first_delta;
  177. ramp->first_step = -CONFIG_RAMP_SECOND_STEP;
  178. delta -= first_delta;
  179. step_val = (delta)/(ramp->dect/step_ms);
  180. }else {
  181. ramp->first_target = ramp->first_step = 0.0f;
  182. }
  183. }else {
  184. step_val = 0;
  185. ramp->first_step = ramp->first_target = 0;
  186. }
  187. eRamp_set_target(ramp, c);
  188. eRamp_set_step(ramp, step_val);
  189. #endif
  190. }
  191. //y=Ax^2;
  192. void eCtrl_init(u16 accl_time, u16 dec_time);
  193. void eCtrl_set_ebrk_time(u16 ebrk_time);
  194. void eCtrl_brake_signal(bool hw_brake);
  195. bool eCtrl_is_eBrk_Running(void);
  196. void eCtrl_set_TgtCurrent(float c);
  197. void eCtrl_set_TgtTorque(float t);
  198. void eCtrl_set_TgtSpeed(float s);
  199. bool eCtrl_enable_eBrake(bool enable);
  200. float eCtrl_get_RefSpeed(void);
  201. float eCtrl_get_RefCurrent(void);
  202. float eCtrl_get_RefTorque(void);
  203. float eCtrl_get_FinalSpeed(void);
  204. float eCtrl_get_FinalCurrent(void);
  205. float eCtrl_get_FinalTorque(void);
  206. void eCtrl_Running(void);
  207. void eCtrl_Reset(void);
  208. void eCtrl_reset_Torque(float init_trq);
  209. void eCtrl_reset_Current(float init_curr);
  210. #endif /* EBRAKE_CTRL_H__ */