limit.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "foc/limit.h"
  2. #include "foc/core/PMSM_FOC_Core.h"
  3. #include "foc/motor/motor.h"
  4. #include "foc/motor/motor_param.h"
  5. #include "foc/samples.h"
  6. #include "foc/mc_error.h"
  7. static limter_t motor_temp_lim[] = {//电机过温限流,限制相电流
  8. {.enter_pointer = 120, .exit_pointer = 110, .limit_value = 0},
  9. {.enter_pointer = 110, .exit_pointer = 100, .limit_value = CONFIG_MAX_MOTOR_TORQUE/2},
  10. {.enter_pointer = 100, .exit_pointer = 90, .limit_value = CONFIG_MAX_MOTOR_TORQUE/3*2},
  11. };
  12. static limter_t mos_temp_lim[] = { //mos过温限流,限制相电流
  13. {.enter_pointer = 95, .exit_pointer = 85, .limit_value = 0},
  14. {.enter_pointer = 86, .exit_pointer = 80, .limit_value = CONFIG_MAX_MOTOR_TORQUE/2},
  15. {.enter_pointer = 81, .exit_pointer = 70, .limit_value = CONFIG_MAX_MOTOR_TORQUE/3*2},
  16. };
  17. static limter_t vol_under_lim[] = { //欠压限流,限制母线
  18. {.enter_pointer = 20, .exit_pointer = 20, .limit_value = 10},
  19. };
  20. void limter_set_under_voltage(s16 und_vol) {
  21. vol_under_lim[0].enter_pointer = und_vol;
  22. vol_under_lim[0].exit_pointer = und_vol + 8;
  23. }
  24. static u16 _temp_limiter(s16 temp, limter_t *lim) {
  25. if (!lim->is_limit) {
  26. if (temp < lim->enter_pointer) {
  27. lim->ticks = 0;
  28. return HW_LIMIT_NONE;
  29. }
  30. if (lim->ticks == 0) {
  31. lim->ticks = get_tick_ms();
  32. }else if (get_delta_ms(lim->ticks) >= 5000){
  33. lim->is_limit = true;
  34. lim->ticks = 0;
  35. return lim->limit_value;
  36. }
  37. return HW_LIMIT_NONE;
  38. }else {
  39. if (temp >= lim->exit_pointer) {
  40. lim->ticks = 0;
  41. return lim->limit_value;
  42. }
  43. if (lim->ticks == 0) {
  44. lim->ticks = get_tick_ms();
  45. }else if (get_delta_ms(lim->ticks) >= 5000) {
  46. lim->is_limit = false;
  47. lim->ticks = 0;
  48. return HW_LIMIT_NONE;
  49. }
  50. return lim->limit_value;
  51. }
  52. }
  53. static u16 _vol_limiter(s16 vol, limter_t *lim) {
  54. if (!lim->is_limit) {
  55. if (vol > lim->enter_pointer) {
  56. lim->ticks = 0;
  57. return HW_LIMIT_NONE;
  58. }
  59. if (lim->ticks == 0) {
  60. lim->ticks = get_tick_ms();
  61. }else if (get_delta_ms(lim->ticks) >= 1000){
  62. lim->is_limit = true;
  63. lim->ticks = 0;
  64. return lim->limit_value;
  65. }
  66. return HW_LIMIT_NONE;
  67. }else {
  68. if (vol <= lim->exit_pointer) {
  69. lim->ticks = 0;
  70. return lim->limit_value;
  71. }
  72. if (lim->ticks == 0) {
  73. lim->ticks = get_tick_ms();
  74. }else if (get_delta_ms(lim->ticks) >= 1000) {
  75. lim->is_limit = false;
  76. lim->ticks = 0;
  77. return HW_LIMIT_NONE;
  78. }
  79. return lim->limit_value;
  80. }
  81. }
  82. static u16 _motor_limit(void) {
  83. s16 temp = get_motor_temp();
  84. for(int i = 0; i < ARRAY_SIZE(motor_temp_lim); i++) {
  85. limter_t *lim = motor_temp_lim + i;
  86. u16 lim_value = _temp_limiter(temp, lim);
  87. if (lim_value != HW_LIMIT_NONE) {
  88. if (lim_value == 0) {
  89. mc_set_critical_error(FOC_CRIT_MOTOR_TEMP_Err);
  90. }
  91. err_add_record(FOC_CRIT_MOTOR_TEMP_Err, temp);
  92. return lim_value;
  93. }
  94. }
  95. mc_clr_critical_error(FOC_CRIT_MOTOR_TEMP_Err);
  96. return HW_LIMIT_NONE;
  97. }
  98. static u16 _mos_limit(void) {
  99. s16 temp = get_mos_temp();
  100. for(int i = 0; i < ARRAY_SIZE(mos_temp_lim); i++) {
  101. limter_t *lim = mos_temp_lim + i;
  102. u16 lim_value = _temp_limiter(temp, lim);
  103. if (lim_value != HW_LIMIT_NONE) {
  104. if (lim_value == 0) {
  105. mc_set_critical_error(FOC_CRIT_MOS_TEMP_Err);
  106. }
  107. err_add_record(FOC_CRIT_MOS_TEMP_Err, temp);
  108. return lim_value;
  109. }
  110. }
  111. mc_clr_critical_error(FOC_CRIT_MOS_TEMP_Err);
  112. return HW_LIMIT_NONE;
  113. }
  114. u16 torque_temp_high_limit(void) {
  115. u16 motor_lim = _motor_limit();
  116. u16 mos_lim = _mos_limit();
  117. return min(motor_lim, mos_lim);
  118. }
  119. u16 vbus_current_vol_lower_limit(void) {
  120. s16 vol = get_vbus_int();
  121. for(int i = 0; i < ARRAY_SIZE(vol_under_lim); i++) {
  122. limter_t *lim = vol_under_lim + i;
  123. u16 lim_value = _vol_limiter(vol, lim);
  124. if (lim_value != HW_LIMIT_NONE) {
  125. mc_set_critical_error(FOC_CRIT_UN_Vol_Err);
  126. return lim_value;
  127. }
  128. }
  129. mc_clr_critical_error(FOC_CRIT_UN_Vol_Err);
  130. return HW_LIMIT_NONE;
  131. }