throttle.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.pedal_opening = _throttle.prev_pedal_opening = 0.0f;
  22. _throttle.torque_req = _throttle.torque_real = _throttle.torque_compensation = 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. return true;
  85. }else if (throttle1_is_error() && !throttle2_is_error()) {
  86. float thr = get_thro2_5v_float() - get_throttle2_float();
  87. signal = fclamp(thr, _start_v, _end_v);
  88. }else if (!throttle1_is_error() && throttle2_is_error()) {
  89. signal = get_throttle_float();
  90. }else {
  91. float thr1 = get_throttle_float();
  92. float thr2 = get_thro2_5v_float() - get_throttle2_float();
  93. signal = (thr1+thr2)/2.0f;
  94. }
  95. return signal <= _start_v;
  96. #else
  97. return get_throttle_float() <= _start_v;
  98. #endif
  99. }
  100. bool throttle_not_released_err(void)
  101. {
  102. return ((err_mask & THRO_NOT_RELEASED) != 0);
  103. }
  104. void throttle_force_detect(void) {
  105. u32 mask = cpu_enter_critical();
  106. throttle_init();
  107. _auto_detect_sv = true;
  108. _auto_detect_sv_cnt = 0;
  109. _auto_detect_sv_totle = 0;
  110. _detect_release_cnt = 0;
  111. err_mask = 0; //clear err mask
  112. cpu_exit_critical(mask);
  113. }
  114. /* 获取转把电压对应的油门开度 */
  115. float throttle_vol_to_opening(float thro_val) {
  116. if (thro_val <= throttle_start_vol()) {
  117. return 0;
  118. }
  119. float delta = thro_val - throttle_start_vol();
  120. int ration = (delta * 100.0f) / throttle_vol_range();
  121. return ((float)ration)/100.0f;
  122. }
  123. /* 获取油门开度 */
  124. float throttle_get_open_ration(void) {
  125. float thro_val = throttle_get_signal();
  126. return throttle_vol_to_opening(thro_val);
  127. }
  128. /* 获取油门开度对应的转把电压 */
  129. float throttle_opening_to_vol(float r) {
  130. if (r == 0) {
  131. return 0;
  132. }else if (r > 1.0f) {
  133. r = 1.0f;
  134. }
  135. return (throttle_start_vol() + r * throttle_vol_range());
  136. }
  137. void throttle_detect(bool ready) {
  138. float thr_5v, thr_sig;
  139. sample_throttle();
  140. thr_5v = get_thro_5v_float();
  141. thr_sig = get_throttle_float();
  142. if (thr_sig <= mc_conf()->c.thro_min_vol || thr_sig >= mc_conf()->c.thro_max_vol) {
  143. err_mask |= THRO1_SIG_ERR_BIT;
  144. }
  145. if (thr_5v <= 4.5f || thr_5v >= 5.5f) {
  146. err_mask |= THRO1_5V_ERR_BIT;
  147. }
  148. #if CONFIG_DAUL_THROTTLE==1
  149. thr_5v = get_thro2_5v_float();
  150. if (thr_5v <= 4.5f || thr_5v >= 5.5f) {
  151. err_mask |= THRO2_5V_ERR_BIT;
  152. }else {
  153. float thr2_sig = get_thro2_5v_float() - get_throttle2_float();
  154. if (thr2_sig <= mc_conf()->c.thro_min_vol || thr2_sig >= mc_conf()->c.thro_max_vol) {
  155. err_mask |= THRO2_SIG_ERR_BIT;
  156. }else {
  157. if (ABS(thr2_sig - thr_sig) > 0.5f) {
  158. err_mask |= THRO2_SIG_ERR_BIT;
  159. err_mask |= THRO1_SIG_ERR_BIT;
  160. }
  161. }
  162. }
  163. #endif
  164. if (!ready && throttle_get_signal() > _start_v) {
  165. if (_detect_release_cnt < 500) {
  166. _detect_release_cnt ++;
  167. }else {
  168. err_mask |= THRO_NOT_RELEASED;
  169. }
  170. }
  171. if (ready) {
  172. _auto_detect_sv = false;
  173. }else if (!throttle_is_all_error() && !throttle_not_released_err() && !ready && _auto_detect_sv) {
  174. float v = throttle_get_signal();
  175. if (v < _start_v) {
  176. _auto_detect_sv_totle += v;
  177. _auto_detect_sv_cnt ++;
  178. if (_auto_detect_sv_cnt == 200) {
  179. _start_v = _auto_detect_sv_totle / (float)_auto_detect_sv_cnt + CONFIG_SAFE_INV_V;
  180. _auto_detect_sv = false;
  181. mc_crit_err_add_s16(FOC_EV_THRO_START_V, (s16)(_start_v * 100.0f));
  182. }
  183. }
  184. }
  185. }
  186. static float _throttle_torque_for_accelerate(float ration) {
  187. float max_torque = mc_gear_max_torque((s16)_throttle.vel_filted, _throttle.gear);
  188. float thro_torque = max_torque * ration;
  189. float pedal_acc = 1.0f;
  190. if (_throttle.prev_pedal_opening < 1.0f) {
  191. pedal_acc = (ration - _throttle.prev_pedal_opening)/ (1.0f - _throttle.prev_pedal_opening);
  192. }
  193. pedal_acc = fclamp(pedal_acc, 0, 1.0f);
  194. float acc_torque = _throttle.torque_real + pedal_acc * (max_torque - _throttle.torque_real);
  195. if (acc_torque < 0) {
  196. acc_torque = 0;
  197. }
  198. /*
  199. 直接获取油门开度对应的加速扭矩thro_torque 不小于间接计算得到的 acc_torque
  200. */
  201. float torque_compensation = thro_torque - acc_torque;
  202. float step = 0.0f;
  203. if (torque_compensation > 0) {
  204. float acc_t = mc_gear_conf()->accl_time;
  205. step = torque_compensation / (acc_t + 0.00001f);
  206. }else {
  207. torque_compensation = 0;
  208. }
  209. step_towards(&_throttle.torque_compensation, torque_compensation, step);
  210. return (acc_torque + _throttle.torque_compensation);
  211. }
  212. static float throttle_torque_for_accelerate(void) {
  213. return _throttle_torque_for_accelerate(_throttle.pedal_opening);
  214. }
  215. static float throttle_torque_for_decelerate(void) {
  216. if (_throttle.prev_pedal_opening == 0.0f) {
  217. return 0;
  218. }
  219. float pedal_dec = _throttle.pedal_opening / _throttle.prev_pedal_opening;
  220. pedal_dec = fclamp(pedal_dec, 0.0f, 1.0f);
  221. return pedal_dec * _throttle.torque_real;
  222. }
  223. float throttle_get_open_ration_filted(void) {
  224. return _throttle.pedal_opening;
  225. }
  226. #define THRO_RPM_LP_CEOF 0.01f
  227. float throttle_get_torque(mot_contrl_t * ctrl, float vol) {
  228. float pedal_opening = throttle_vol_to_opening(vol);
  229. float vel = mot_contrl_get_speed(ctrl);
  230. _throttle.gear = mc_get_internal_gear();
  231. LowPass_Filter(_throttle.vel_filted, vel, THRO_RPM_LP_CEOF);
  232. if (pedal_opening > _throttle.pedal_opening) {
  233. if (!_throttle.accl) {
  234. _throttle.prev_pedal_opening = _throttle.pedal_opening;
  235. _throttle.torque_real = _throttle.torque_req;
  236. if (_throttle.torque_real < 0) { //电子刹车的时候,扭矩可能为负
  237. _throttle.torque_real = 0;
  238. }
  239. _throttle.torque_compensation = 0;
  240. }
  241. _throttle.accl = true;
  242. }else if (pedal_opening < _throttle.pedal_opening) {
  243. if (_throttle.accl) {
  244. _throttle.prev_pedal_opening = _throttle.pedal_opening;
  245. _throttle.torque_real = ctrl->target_torque_raw;
  246. /* 如果扭矩给定的ramp没有结束,使用原始扭矩请求作为减扭矩的起始点 */
  247. if (_throttle.torque_req - line_ramp_get_interp(&ctrl->ramp_input_torque) >= 10.0f ) {
  248. _throttle.torque_real = _throttle.torque_req;
  249. }
  250. if (_throttle.torque_real < 0) { //电子刹车的时候,扭矩可能为负
  251. _throttle.torque_real = 0;
  252. }
  253. }
  254. _throttle.accl = false;
  255. }
  256. _throttle.pedal_opening = pedal_opening;
  257. if (_throttle.accl) {
  258. return throttle_torque_for_accelerate();
  259. }else {
  260. return throttle_torque_for_decelerate();
  261. }
  262. }
  263. void throttle_set_torque(mot_contrl_t * ctrl, float torque) {
  264. float curr_vel = mot_contrl_get_speed(ctrl);
  265. float ref_torque = torque;
  266. if (_throttle.accl) { //加速需求
  267. float hold_torque = ctrl->autohold_torque;
  268. if (hold_torque < 0) { //下坡驻车,最小给0扭矩
  269. hold_torque = 0;
  270. }
  271. ref_torque = MAX(hold_torque, ref_torque);
  272. if (curr_vel <= CONFIG_ZERO_SPEED_RPM) {//从静止开始加速
  273. if (_throttle.torque_req < hold_torque) {
  274. _throttle.torque_req = hold_torque;
  275. line_ramp_reset(&ctrl->ramp_input_torque, hold_torque);
  276. }
  277. }else {
  278. ctrl->autohold_torque = 0;
  279. }
  280. /* 处理加速ramp时间的变化,需要缓慢变小,变大可以立即处理,
  281. * 加速时间缓慢变小可以防止突然大扭矩加速
  282. */
  283. u16 now_ramp_time = mot_contrl_get_torque_acc_time(ctrl);
  284. u16 next_ramp_time = mc_gear_conf()->accl_time;
  285. if (curr_vel < CONFIG_ZERO_SPEED_RAMP_RMP) {
  286. next_ramp_time = mc_gear_conf()->zero_accl;
  287. }
  288. if (now_ramp_time != next_ramp_time) {
  289. if (next_ramp_time > now_ramp_time) {
  290. mot_contrl_set_torque_acc_time(ctrl, next_ramp_time);
  291. }else {
  292. float f_now = (float)now_ramp_time;
  293. float f_next = (float)next_ramp_time;
  294. step_towards(&f_now, f_next, 5.0f);
  295. mot_contrl_set_torque_acc_time(ctrl ,(u16)f_now);
  296. }
  297. }
  298. _throttle.torque_req = ref_torque;
  299. }else {
  300. float ref_torque = throttle_torque_for_decelerate();
  301. /* autohold 启动的情况下,转把在0位置附近小幅抖动 */
  302. if (curr_vel <= CONFIG_ZERO_SPEED_RPM) {
  303. float hold_torque = ctrl->autohold_torque;
  304. ref_torque = MAX(hold_torque, ref_torque);
  305. }
  306. _throttle.torque_req = ref_torque;
  307. }
  308. mot_contrl_set_torque(ctrl, _throttle.torque_req);
  309. }
  310. /* 定速巡航需要判断是否需要加速 */
  311. float get_user_request_torque(void) {
  312. if (_throttle.accl) {
  313. return throttle_torque_for_accelerate();
  314. }
  315. return throttle_torque_for_decelerate();
  316. }
  317. void throttle_log(void) {
  318. sys_debug("r:%f, last %f, real:%f, req %f\n", _throttle.pedal_opening, _throttle.prev_pedal_opening, _throttle.torque_real, _throttle.torque_req);
  319. sys_debug("thro: %f-%f, %f\n", throttle_start_vol(), throttle_end_vol(), mc_gear_max_torque((s16)_throttle.vel_filted, _throttle.gear));
  320. }