e_ctrl.h 5.8 KB

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