e_ctrl.h 4.7 KB

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