throttle.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "foc/foc_config.h"
  2. #include "foc/core/PMSM_FOC_Core.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/mc_error.h"
  10. static u8 err_mask;
  11. static float _start_v, _end_v;
  12. static bool _auto_detect_sv = true;
  13. static int _auto_detect_sv_cnt = 0;
  14. static float _auto_detect_sv_totle = 0;
  15. static int _detect_release_cnt = 0;
  16. #define CONFIG_SAFE_INV_V 0.06f
  17. void throttle_init(void) {
  18. _start_v = mc_conf()->c.thro_start_vol;
  19. _end_v = mc_conf()->c.thro_end_vol;
  20. }
  21. bool throttle1_is_error(void) {
  22. if (err_mask & (THRO1_5V_ERR_BIT | THRO1_SIG_ERR_BIT)) {
  23. return true;
  24. }
  25. return false;
  26. }
  27. bool throttle2_is_error(void) {
  28. if (err_mask & (THRO2_5V_ERR_BIT | THRO2_SIG_ERR_BIT)) {
  29. return true;
  30. }
  31. return false;
  32. }
  33. u8 throttle_get_errors(void) {
  34. return err_mask;
  35. }
  36. bool throttle_is_all_error(void) {
  37. #if CONFIG_DAUL_THROTTLE==1
  38. return throttle1_is_error() && throttle2_is_error();
  39. #else
  40. return throttle1_is_error();
  41. #endif
  42. }
  43. float throttle_start_vol(void) {
  44. return _start_v;
  45. }
  46. float throttle_end_vol(void) {
  47. return _end_v;
  48. }
  49. float throttle_vol_range(void) {
  50. return (_end_v - _start_v);
  51. }
  52. float throttle_get_signal(void) {
  53. #if CONFIG_DAUL_THROTTLE==1
  54. if (throttle1_is_error() && throttle2_is_error()) {
  55. return 0.0f;
  56. }else if (throttle1_is_error() && !throttle2_is_error()) {
  57. float thr = get_thro2_5v_float() - get_throttle2_float();
  58. return fclamp(thr, _start_v, _end_v);
  59. }else if (!throttle1_is_error() && throttle2_is_error()) {
  60. return get_throttle_float();
  61. }else {
  62. float thr1 = get_throttle_float();
  63. float thr2 = get_thro2_5v_float() - get_throttle2_float();
  64. return (thr1+thr2)/2.0f;
  65. }
  66. #else
  67. return get_throttle_float();
  68. #endif
  69. }
  70. bool throttle_is_released(void) {
  71. #if CONFIG_DAUL_THROTTLE==1
  72. float signal = 0;
  73. if (throttle1_is_error() && !throttle2_is_error()) {
  74. float thr = get_thro2_5v_float() - get_throttle2_float();
  75. signal = fclamp(thr, _start_v, _end_v);
  76. }else if (!throttle1_is_error() && throttle2_is_error()) {
  77. signal = get_throttle_float();
  78. }else {
  79. float thr1 = get_throttle_float();
  80. float thr2 = get_thro2_5v_float() - get_throttle2_float();
  81. signal = (thr1+thr2)/2.0f;
  82. }
  83. return signal <= _start_v;
  84. #else
  85. return get_throttle_float() <= _start_v;
  86. #endif
  87. }
  88. bool throttle_not_released_err(void)
  89. {
  90. return ((err_mask & THRO_NOT_RELEASED) != 0);
  91. }
  92. void throttle_force_detect(void) {
  93. u32 mask = cpu_enter_critical();
  94. throttle_init();
  95. _auto_detect_sv = true;
  96. _auto_detect_sv_cnt = 0;
  97. _auto_detect_sv_totle = 0;
  98. _detect_release_cnt = 0;
  99. err_mask = 0; //clear err mask
  100. cpu_exit_critical(mask);
  101. }
  102. void throttle_detect(bool ready) {
  103. float thr_5v, thr_sig;
  104. sample_throttle();
  105. thr_5v = get_thro_5v_float();
  106. thr_sig = get_throttle_float();
  107. if (thr_sig <= mc_conf()->c.thro_min_vol || thr_sig >= mc_conf()->c.thro_max_vol) {
  108. err_mask |= THRO1_SIG_ERR_BIT;
  109. }
  110. if (thr_5v <= 4.5f || thr_5v >= 5.5f) {
  111. err_mask |= THRO1_5V_ERR_BIT;
  112. }
  113. #if CONFIG_DAUL_THROTTLE==1
  114. thr_5v = get_thro2_5v_float();
  115. if (thr_5v <= 4.5f || thr_5v >= 5.5f) {
  116. err_mask |= THRO2_5V_ERR_BIT;
  117. }else {
  118. float thr2_sig = get_thro2_5v_float() - get_throttle2_float();
  119. if (thr2_sig <= mc_conf()->c.thro_min_vol || thr2_sig >= mc_conf()->c.thro_max_vol) {
  120. err_mask |= THRO2_SIG_ERR_BIT;
  121. }else {
  122. if (ABS(thr2_sig - thr_sig) > 0.5f) {
  123. err_mask |= THRO2_SIG_ERR_BIT;
  124. err_mask |= THRO1_SIG_ERR_BIT;
  125. }
  126. }
  127. }
  128. #endif
  129. if (!ready && throttle_get_signal() > _start_v) {
  130. if (_detect_release_cnt < 500) {
  131. _detect_release_cnt ++;
  132. }else {
  133. err_mask |= THRO_NOT_RELEASED;
  134. }
  135. }
  136. if (ready) {
  137. _auto_detect_sv = false;
  138. }else if (!throttle_is_all_error() && !throttle_not_released_err() && !ready && _auto_detect_sv) {
  139. float v = throttle_get_signal();
  140. if (v < _start_v) {
  141. _auto_detect_sv_totle += v;
  142. _auto_detect_sv_cnt ++;
  143. if (_auto_detect_sv_cnt == 200) {
  144. _start_v = _auto_detect_sv_totle / (float)_auto_detect_sv_cnt + CONFIG_SAFE_INV_V;
  145. _auto_detect_sv = false;
  146. mc_crit_err_add_s16(FOC_EV_THRO_START_V, (s16)(_start_v * 100.0f));
  147. }
  148. }
  149. }
  150. }