throttle.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "foc/foc_config.h"
  2. #include "foc/core/controller.h"
  3. #include "foc/samples.h"
  4. #include "math/fast_math.h"
  5. #include "bsp/bsp_driver.h"
  6. #include "libs/logger.h"
  7. #include "foc/mc_config.h"
  8. #include "foc/motor/throttle.h"
  9. #include "foc/motor/motor.h"
  10. #include "foc/mc_error.h"
  11. static u8 err_mask;
  12. static float _start_v, _end_v;
  13. static bool _auto_detect_sv = true;
  14. static int _auto_detect_sv_cnt = 0;
  15. static float _auto_detect_sv_totle = 0;
  16. static int _detect_release_cnt = 0;
  17. #define CONFIG_SAFE_INV_V 0.1f
  18. static throttle_torque_t _throttle;
  19. void throttle_torque_reset(void) {
  20. _throttle.accl = false;
  21. _throttle.throttle_opening = _throttle.throttle_opening_last = 0.0f;
  22. _throttle.torque_req = _throttle.torque_real = _throttle.torque_acc_ = 0.0f;
  23. _throttle.gear = mc_get_internal_gear();
  24. }
  25. void throttle_init(void) {
  26. _start_v = mc_conf()->c.thro_start_vol;
  27. _end_v = mc_conf()->c.thro_end_vol;
  28. throttle_torque_reset();
  29. _throttle.vel_filted = 0;
  30. }
  31. bool throttle1_is_error(void) {
  32. if (err_mask & (THRO1_5V_ERR_BIT | THRO1_SIG_ERR_BIT)) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. bool throttle2_is_error(void) {
  38. if (err_mask & (THRO2_5V_ERR_BIT | THRO2_SIG_ERR_BIT)) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. u8 throttle_get_errors(void) {
  44. return err_mask;
  45. }
  46. bool throttle_is_all_error(void) {
  47. #if CONFIG_DAUL_THROTTLE==1
  48. return throttle1_is_error() && throttle2_is_error();
  49. #else
  50. return throttle1_is_error();
  51. #endif
  52. }
  53. float throttle_start_vol(void) {
  54. return _start_v;
  55. }
  56. float throttle_end_vol(void) {
  57. return _end_v;
  58. }
  59. float throttle_vol_range(void) {
  60. return (_end_v - _start_v);
  61. }
  62. float throttle_get_signal(void) {
  63. #if CONFIG_DAUL_THROTTLE==1
  64. if (throttle1_is_error() && throttle2_is_error()) {
  65. return 0.0f;
  66. }else if (throttle1_is_error() && !throttle2_is_error()) {
  67. float thr = get_thro2_5v_float() - get_throttle2_float();
  68. return fclamp(thr, _start_v, _end_v);
  69. }else if (!throttle1_is_error() && throttle2_is_error()) {
  70. return get_throttle_float();
  71. }else {
  72. float thr1 = get_throttle_float();
  73. float thr2 = get_thro2_5v_float() - get_throttle2_float();
  74. return (thr1+thr2)/2.0f;
  75. }
  76. #else
  77. return get_throttle_float();
  78. #endif
  79. }
  80. bool throttle_is_released(void) {
  81. #if CONFIG_DAUL_THROTTLE==1
  82. float signal = 0;
  83. if (throttle1_is_error() && !throttle2_is_error()) {
  84. float thr = get_thro2_5v_float() - get_throttle2_float();
  85. signal = fclamp(thr, _start_v, _end_v);
  86. }else if (!throttle1_is_error() && throttle2_is_error()) {
  87. signal = get_throttle_float();
  88. }else {
  89. float thr1 = get_throttle_float();
  90. float thr2 = get_thro2_5v_float() - get_throttle2_float();
  91. signal = (thr1+thr2)/2.0f;
  92. }
  93. return signal <= _start_v;
  94. #else
  95. return get_throttle_float() <= _start_v;
  96. #endif
  97. }
  98. bool throttle_not_released_err(void)
  99. {
  100. return ((err_mask & THRO_NOT_RELEASED) != 0);
  101. }
  102. void throttle_force_detect(void) {
  103. u32 mask = cpu_enter_critical();
  104. throttle_init();
  105. _auto_detect_sv = true;
  106. _auto_detect_sv_cnt = 0;
  107. _auto_detect_sv_totle = 0;
  108. _detect_release_cnt = 0;
  109. err_mask = 0; //clear err mask
  110. cpu_exit_critical(mask);
  111. }
  112. /* 获取转把电压对应的油门开度 */
  113. float throttle_vol_to_opening(float thro_val) {
  114. if (thro_val <= throttle_start_vol()) {
  115. return 0;
  116. }
  117. float delta = thro_val - throttle_start_vol();
  118. int ration = (delta * 100.0f) / throttle_vol_range();
  119. return ((float)ration)/100.0f;
  120. }
  121. /* 获取油门开度 */
  122. float throttle_get_open_ration(void) {
  123. float thro_val = throttle_get_signal();
  124. return throttle_vol_to_opening(thro_val);
  125. }
  126. /* 获取油门开度对应的转把电压 */
  127. float throttle_opening_to_vol(float r) {
  128. if (r == 0) {
  129. return 0;
  130. }else if (r > 1.0f) {
  131. r = 1.0f;
  132. }
  133. return (throttle_start_vol() + r * throttle_vol_range());
  134. }
  135. void throttle_detect(bool ready) {
  136. float thr_5v, thr_sig;
  137. sample_throttle();
  138. thr_5v = get_thro_5v_float();
  139. thr_sig = get_throttle_float();
  140. if (thr_sig <= mc_conf()->c.thro_min_vol || thr_sig >= mc_conf()->c.thro_max_vol) {
  141. err_mask |= THRO1_SIG_ERR_BIT;
  142. }
  143. if (thr_5v <= 4.5f || thr_5v >= 5.5f) {
  144. err_mask |= THRO1_5V_ERR_BIT;
  145. }
  146. #if CONFIG_DAUL_THROTTLE==1
  147. thr_5v = get_thro2_5v_float();
  148. if (thr_5v <= 4.5f || thr_5v >= 5.5f) {
  149. err_mask |= THRO2_5V_ERR_BIT;
  150. }else {
  151. float thr2_sig = get_thro2_5v_float() - get_throttle2_float();
  152. if (thr2_sig <= mc_conf()->c.thro_min_vol || thr2_sig >= mc_conf()->c.thro_max_vol) {
  153. err_mask |= THRO2_SIG_ERR_BIT;
  154. }else {
  155. if (ABS(thr2_sig - thr_sig) > 0.5f) {
  156. err_mask |= THRO2_SIG_ERR_BIT;
  157. err_mask |= THRO1_SIG_ERR_BIT;
  158. }
  159. }
  160. }
  161. #endif
  162. if (!ready && throttle_get_signal() > _start_v) {
  163. if (_detect_release_cnt < 500) {
  164. _detect_release_cnt ++;
  165. }else {
  166. err_mask |= THRO_NOT_RELEASED;
  167. }
  168. }
  169. if (ready) {
  170. _auto_detect_sv = false;
  171. }else if (!throttle_is_all_error() && !throttle_not_released_err() && !ready && _auto_detect_sv) {
  172. float v = throttle_get_signal();
  173. if (v < _start_v) {
  174. _auto_detect_sv_totle += v;
  175. _auto_detect_sv_cnt ++;
  176. if (_auto_detect_sv_cnt == 200) {
  177. _start_v = _auto_detect_sv_totle / (float)_auto_detect_sv_cnt + CONFIG_SAFE_INV_V;
  178. _auto_detect_sv = false;
  179. mc_crit_err_add_s16(FOC_EV_THRO_START_V, (s16)(_start_v * 100.0f));
  180. }
  181. }
  182. }
  183. }
  184. static float _throttle_torque_for_accelerate(float ration) {
  185. float max_torque = mc_gear_max_torque((s16)_throttle.vel_filted, _throttle.gear);
  186. float thro_torque = max_torque * ration;
  187. float acc_r = 1.0f;
  188. if (_throttle.throttle_opening_last < 1.0f) {
  189. acc_r = (ration - _throttle.throttle_opening_last)/ (1.0f - _throttle.throttle_opening_last);
  190. }
  191. acc_r = fclamp(acc_r, 0, 1.0f);
  192. float acc_torque = _throttle.torque_real + acc_r * (max_torque - _throttle.torque_real);
  193. if (acc_torque < 0) {
  194. acc_torque = 0;
  195. }
  196. /*
  197. 直接获取油门开度对应的加速扭矩thro_torque 不小于间接计算得到的 acc_torque
  198. */
  199. float torque_acc_ = thro_torque - acc_torque;
  200. float step = 0.0f;
  201. if (torque_acc_ > 0) {
  202. float acc_t = mc_gear_conf()->accl_time;
  203. step = torque_acc_ / (acc_t + 0.00001f);
  204. }else {
  205. torque_acc_ = 0;
  206. }
  207. step_towards(&_throttle.torque_acc_, torque_acc_, step);
  208. return (acc_torque + _throttle.torque_acc_);
  209. }
  210. static float throttle_torque_for_accelerate(void) {
  211. return _throttle_torque_for_accelerate(_throttle.throttle_opening);
  212. }
  213. static float throttle_torque_for_decelerate(void) {
  214. if (_throttle.throttle_opening_last == 0.0f) {
  215. return 0;
  216. }
  217. float dec_r = _throttle.throttle_opening / _throttle.throttle_opening_last;
  218. dec_r = fclamp(dec_r, 0.0f, 1.0f);
  219. return dec_r * _throttle.torque_real;
  220. }
  221. float throttle_get_open_ration_filted(void) {
  222. return _throttle.throttle_opening;
  223. }
  224. #define THRO_RPM_LP_CEOF 0.01f
  225. float throttle_get_torque(mot_contrl_t * ctrl, float vol) {
  226. float throttle_opening = throttle_vol_to_opening(vol);
  227. float vel = mot_contrl_get_speed(ctrl);
  228. _throttle.gear = mc_get_internal_gear();
  229. LowPass_Filter(_throttle.vel_filted, vel, THRO_RPM_LP_CEOF);
  230. if (throttle_opening > _throttle.throttle_opening) {
  231. if (!_throttle.accl) {
  232. _throttle.throttle_opening_last = _throttle.throttle_opening;
  233. _throttle.torque_real = _throttle.torque_req;
  234. if (_throttle.torque_real < 0) { //电子刹车的时候,扭矩可能为负
  235. _throttle.torque_real = 0;
  236. }
  237. _throttle.torque_acc_ = 0;
  238. }
  239. _throttle.accl = true;
  240. }else if (throttle_opening < _throttle.throttle_opening) {
  241. if (_throttle.accl) {
  242. _throttle.throttle_opening_last = _throttle.throttle_opening;
  243. _throttle.torque_real = ctrl->target_torque_raw;
  244. /* 如果扭矩给定的ramp没有结束,使用原始扭矩请求作为减扭矩的起始点 */
  245. if (_throttle.torque_req - line_ramp_get_interp(&ctrl->input_torque) >= 10.0f ) {
  246. _throttle.torque_real = _throttle.torque_req;
  247. }
  248. if (_throttle.torque_real < 0) { //电子刹车的时候,扭矩可能为负
  249. _throttle.torque_real = 0;
  250. }
  251. }
  252. _throttle.accl = false;
  253. }
  254. _throttle.throttle_opening = throttle_opening;
  255. if (_throttle.accl) {
  256. return throttle_torque_for_accelerate();
  257. }else {
  258. return throttle_torque_for_decelerate();
  259. }
  260. }
  261. void throttle_set_torque(mot_contrl_t * ctrl, float torque) {
  262. float curr_vel = mot_contrl_get_speed(ctrl);
  263. float ref_torque = torque;
  264. if (_throttle.accl) { //加速需求
  265. float hold_torque = ctrl->autohold_torque;
  266. if (hold_torque < 0) { //下坡驻车,最小给0扭矩
  267. hold_torque = 0;
  268. }
  269. ref_torque = MAX(hold_torque, ref_torque);
  270. if (curr_vel <= CONFIG_ZERO_SPEED_RPM) {//从静止开始加速
  271. if (_throttle.torque_req < hold_torque) {
  272. _throttle.torque_req = hold_torque;
  273. line_ramp_reset(&ctrl->input_torque, hold_torque);
  274. }
  275. }else {
  276. ctrl->autohold_torque = 0;
  277. }
  278. /* 处理加速ramp时间的变化,需要缓慢变小,变大可以立即处理,
  279. * 加速时间缓慢变小可以防止突然大扭矩加速
  280. */
  281. u16 now_ramp_time = mot_contrl_get_torque_acc_time(ctrl);
  282. u16 next_ramp_time = mc_gear_conf()->accl_time;
  283. if (curr_vel < CONFIG_ZERO_SPEED_RAMP_RMP) {
  284. next_ramp_time = mc_gear_conf()->zero_accl;
  285. }
  286. if (now_ramp_time != next_ramp_time) {
  287. if (next_ramp_time > now_ramp_time) {
  288. mot_contrl_set_torque_acc_time(ctrl, next_ramp_time);
  289. }else {
  290. float f_now = (float)now_ramp_time;
  291. float f_next = (float)next_ramp_time;
  292. step_towards(&f_now, f_next, 0.5f);
  293. mot_contrl_set_torque_acc_time(ctrl ,(u16)f_now);
  294. }
  295. }
  296. _throttle.torque_req = ref_torque;
  297. }else {
  298. float ref_torque = throttle_torque_for_decelerate();
  299. /* autohold 启动的情况下,转把在0位置附近小幅抖动 */
  300. if (curr_vel <= CONFIG_ZERO_SPEED_RPM) {
  301. float hold_torque = ctrl->autohold_torque;
  302. ref_torque = MAX(hold_torque, ref_torque);
  303. }
  304. _throttle.torque_req = ref_torque;
  305. }
  306. mot_contrl_set_torque(ctrl, _throttle.torque_req);
  307. }
  308. /* 定速巡航需要判断是否需要加速 */
  309. float get_user_request_torque(void) {
  310. if (_throttle.accl) {
  311. return throttle_torque_for_accelerate();
  312. }
  313. return throttle_torque_for_decelerate();
  314. }
  315. void throttle_log(void) {
  316. sys_debug("r:%f, last %f, real:%f, req %f\n", _throttle.throttle_opening, _throttle.throttle_opening_last, _throttle.torque_real, _throttle.torque_req);
  317. sys_debug("thro: %f-%f, %f\n", throttle_start_vol(), throttle_end_vol(), mc_gear_max_torque((s16)_throttle.vel_filted, _throttle.gear));
  318. }