| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #ifndef EBRAKE_CTRL_H__
- #define EBRAKE_CTRL_H__
- #include "os/os_types.h"
- #include "foc/core/ramp_ctrl.h"
- #include "foc/foc_config.h"
- #include "math/fast_math.h"
- #include "math/fix_math.h"
- typedef struct {
- float start;
- float target;
- float first_target;
- float interpolation;
- float step_val;
- float A;
- u32 acct;
- u32 dect;
- u32 time;
- }e_Ramp;
- typedef struct {
- u16 ebrk_time; //能量回收,时间越短,刹车性能或者回收越好
- u16 accl_time; //加速时间(ms),时间越短,加速性能越好
- u16 dec_time; //降速时间
- bool hw_brake;
- bool is_ebrake;
- u32 brake_ts;//检测到刹车开始时间
- e_Ramp current;
- e_Ramp torque;
- e_Ramp speed;
- u16 ebrk_time_shadow;
- u16 accl_time_shadow;
- u16 dec_time_shadow;
- float ebrake_current;
- float current_shadow;
- float torque_shadow;
- float speed_shadow;
- bool is_ebrake_shadow;
- }e_Ctrl;
- static void eRamp_init(e_Ramp *r, u32 acc, u32 dec) {
- r->start = 0;
- r->target = 0;
- r->first_target = 0;
- r->interpolation = 0;
- r->step_val = 0;
- r->acct = acc;
- r->dect = dec;
- }
- static void eRamp_init_target(e_Ramp *r, float target, u32 acc, u32 dec) {
- r->start = target;
- r->target = target;
- r->first_target = 0;
- r->interpolation = target;
- r->step_val = 0;
- r->acct = acc;
- r->dect = dec;
- }
- static void eRamp_reset_target(e_Ramp *r, float target) {
- r->start = target;
- r->target = target;
- r->first_target = 0;
- r->interpolation = target;
- r->step_val = 0;
- }
- static void eRamp_set_time(e_Ramp *r, u32 acc, u32 dec) {
- r->acct = acc;
- r->dect = dec;
- }
- static void eRamp_set_target(e_Ramp *r, float target) {
- r->target = target;
- }
- static void eRamp_set_step(e_Ramp *r, float step) {
- r->step_val = step;
- }
- static void eRamp_running(e_Ramp *r) {
- float target = r->interpolation + r->step_val;
- if (r->step_val < 0) {
- if (target < r->target) {
- target = r->target;
- }
- }else {
- if (target > r->target) {
- target = r->target;
- }
- }
- r->interpolation = target;
- }
- static float eRamp_get_intepolation(e_Ramp *r) {
- return r->interpolation;
- }
- static float eRamp_get_target(e_Ramp *r) {
- return r->target;
- }
- static void eRamp_set_step_target(e_Ramp *ramp, float c, u32 intval) {
- float c_now = eRamp_get_intepolation(ramp);
- float step_val = 0;
- int sign = 1;
- if (c < c_now) {
- sign = -1;
- }
- u32 step_ms = intval;
- if (sign > 0) { //增加扭矩
- step_val = (c - c_now)/(ramp->acct/step_ms);
- if (step_val < MIN_FLOAT) {
- step_val = MIN_FLOAT;
- }
- }else if (sign < 0) {
- step_val = (c_now - c)/(ramp->dect/step_ms);
- if (step_val < MIN_FLOAT) {
- step_val = MIN_FLOAT;
- }
- step_val = -step_val;
- }
- eRamp_set_target(ramp, c);
- eRamp_set_step(ramp, step_val);
- }
- static void eRamp_X2_running(e_Ramp *r) {
- #if 0
- if(r->target == r->interpolation) {
- return;
- }
- if ((r->first_target != 0) && (r->interpolation != r->first_target)) {
- r->interpolation += r->step_val;
- if (r->step_val > 0) {
- if (r->interpolation >= r->first_target) {
- r->interpolation = r->first_target;
- r->first_target = 0.0f;
- }
- }else {
- if (r->interpolation <= r->first_target) {
- r->interpolation = r->first_target;
- r->first_target = 0.0f;
- }
- }
- return;
- }
- if(r->time < 0xFFFFu) {
- r->time ++;
- }
- r->interpolation = r->start + r->A * (float)SQ(r->time);
- if ((r->A > 0) && (r->interpolation > r->target)) {
- r->interpolation = r->target;
- }else if ((r->A < 0) && (r->interpolation < r->target)) {
- r->interpolation = r->target;
- }
- #else
- eRamp_running(r);
- #endif
- }
- static void eRamp_set_X2_target(e_Ramp *ramp, float c) {
- #if 0
- float c_now = eRamp_get_intepolation(ramp);
- float delta = c - c_now;
- if (delta > 0) {
- ramp->first_target = 0;//min(delta, 10.0f);
- ramp->step_val = 0.01f;
- ramp->A = (delta - ramp->first_target)/SQ(ramp->acct);
- }else {
- ramp->first_target = 0;//MAX(delta, -10.0f);
- ramp->step_val = -0.01f;
- ramp->A = (delta - ramp->first_target)/SQ(ramp->dect);
- }
- ramp->start = c_now + ramp->first_target;
- ramp->time = 0;
- eRamp_set_target(ramp, c);
- #else
- eRamp_set_step_target(ramp, c, CONFIG_eCTRL_STEP_TS);
- #endif
- }
- //y=Ax^2;
- void eCtrl_init(u16 accl_time, u16 dec_time);
- void eCtrl_set_ebrk_time(u16 ebrk_time);
- void eCtrl_brake_signal(bool hw_brake);
- bool eCtrl_is_eBrk_enabled(void);
- void eCtrl_set_TgtCurrent(float c);
- void eCtrl_set_TgtTorque(float t);
- void eCtrl_set_TgtSpeed(float s);
- bool eCtrl_enable_eBrake(bool enable);
- float eCtrl_get_RefSpeed(void);
- float eCtrl_get_RefCurrent(void);
- float eCtrl_get_RefTorque(void);
- float eCtrl_get_FinalSpeed(void);
- float eCtrl_get_FinalCurrent(void);
- float eCtrl_get_FinalTorque(void);
- void eCtrl_Running(void);
- void eCtrl_Reset(void);
- void eCtrl_reset_Torque(float init_trq);
- #endif /* EBRAKE_CTRL_H__ */
|