throttle.c 4.0 KB

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