throttle.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. static u8 err_mask;
  9. bool throttle1_is_error(void) {
  10. if (err_mask & (THRO1_5V_ERR_BIT | THRO1_SIG_ERR_BIT)) {
  11. return true;
  12. }
  13. return false;
  14. }
  15. bool throttle2_is_error(void) {
  16. if (err_mask & (THRO2_5V_ERR_BIT | THRO2_SIG_ERR_BIT)) {
  17. return true;
  18. }
  19. return false;
  20. }
  21. u8 throttle_get_errors(void) {
  22. return err_mask;
  23. }
  24. bool throttle_is_all_error(void) {
  25. #if CONFIG_DAUL_THROTTLE==1
  26. return throttle1_is_error() && throttle2_is_error();
  27. #else
  28. return throttle1_is_error();
  29. #endif
  30. }
  31. float throttle_get_signal(void) {
  32. #if CONFIG_DAUL_THROTTLE==1
  33. if (throttle1_is_error() && throttle2_is_error()) {
  34. return 0.0f;
  35. }else if (throttle1_is_error() && !throttle2_is_error()) {
  36. float thr = get_thro2_5v_float() - get_throttle2_float();
  37. return fclamp(thr, nv_get_foc_params()->n_startThroVol, nv_get_foc_params()->n_endThroVol);
  38. }else if (!throttle1_is_error() && throttle2_is_error()) {
  39. return get_throttle_float();
  40. }else {
  41. float thr1 = get_throttle_float();
  42. float thr2 = get_thro2_5v_float() - get_throttle2_float();
  43. return (thr1+thr2)/2.0f;
  44. }
  45. #else
  46. return get_throttle_float();
  47. #endif
  48. }
  49. bool throttle_is_released(void) {
  50. #if CONFIG_DAUL_THROTTLE==1
  51. float signal = 0;
  52. if (throttle1_is_error() && !throttle2_is_error()) {
  53. float thr = get_thro2_5v_float() - get_throttle2_float();
  54. signal = fclamp(thr, nv_get_foc_params()->n_startThroVol, nv_get_foc_params()->n_endThroVol);
  55. }else if (!throttle1_is_error() && throttle2_is_error()) {
  56. signal = get_throttle_float();
  57. }else {
  58. float thr1 = get_throttle_float();
  59. float thr2 = get_thro2_5v_float() - get_throttle2_float();
  60. signal = (thr1+thr2)/2.0f;
  61. }
  62. return signal <= nv_get_foc_params()->n_startThroVol;
  63. #else
  64. return get_throttle_float() <= nv_get_foc_params()->n_startThroVol;
  65. #endif
  66. }
  67. void throttle_detect(void) {
  68. float thr_5v = get_thro_5v_float();
  69. float thr_sig = get_throttle_float();
  70. if (thr_sig <= nv_get_foc_params()->f_minThroVol || thr_sig >=nv_get_foc_params()->f_maxThroVol) {
  71. err_mask |= THRO1_SIG_ERR_BIT;
  72. }
  73. if (thr_5v <= 4.5f || thr_5v >= 5.5f) {
  74. err_mask |= THRO1_5V_ERR_BIT;
  75. }
  76. #if CONFIG_DAUL_THROTTLE==1
  77. thr_5v = get_thro2_5v_float();
  78. if (thr_5v <= 4.5f || thr_5v >= 5.5f) {
  79. err_mask |= THRO2_5V_ERR_BIT;
  80. }else {
  81. float thr2_sig = get_thro2_5v_float() - get_throttle2_float();
  82. if (thr2_sig <= nv_get_foc_params()->f_minThroVol || thr2_sig >=nv_get_foc_params()->f_maxThroVol) {
  83. err_mask |= THRO2_SIG_ERR_BIT;
  84. }else {
  85. if (ABS(thr2_sig - thr_sig) > 0.5f) {
  86. err_mask |= THRO2_SIG_ERR_BIT;
  87. err_mask |= THRO1_SIG_ERR_BIT;
  88. }
  89. }
  90. }
  91. #endif
  92. }