limit.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #include "libs/logger.h"
  8. static limter_t motor_temp_lim[3];
  9. static limter_t mos_temp_lim[3];
  10. static limter_t vol_under_lim[1];
  11. static bool _inited = false;
  12. static bool _can_recovery = true;
  13. static s16 mot_temp, mos_temp;
  14. static void limiter_init(void) {
  15. for (int i = 0; i < CONFIG_TEMP_PROT_NUM; i++) {
  16. motor_temp_lim[i].enter_pointer = mc_conf()->p_mot[i].enter_pointer;
  17. motor_temp_lim[i].exit_pointer = mc_conf()->p_mot[i].exit_pointer;
  18. motor_temp_lim[i].limit_value = mc_conf()->p_mot[i].limit_value;
  19. sys_debug("%d-%d-%d\n", motor_temp_lim[i].enter_pointer, motor_temp_lim[i].exit_pointer, motor_temp_lim[i].limit_value);
  20. mos_temp_lim[i].enter_pointer = mc_conf()->p_mos[i].enter_pointer;
  21. mos_temp_lim[i].exit_pointer = mc_conf()->p_mos[i].exit_pointer;
  22. mos_temp_lim[i].limit_value = mc_conf()->p_mos[i].limit_value;
  23. sys_debug("%d-%d-%d\n", mos_temp_lim[i].enter_pointer, mos_temp_lim[i].exit_pointer, mos_temp_lim[i].limit_value);
  24. }
  25. vol_under_lim[0].enter_pointer = mc_conf()->p_vol.enter_pointer;
  26. vol_under_lim[0].exit_pointer = mc_conf()->p_vol.exit_pointer;
  27. vol_under_lim[0].limit_value = mc_conf()->p_vol.limit_value;
  28. //sys_debug("%d-%d-%d\n", vol_under_lim[0].enter_pointer, vol_under_lim[0].exit_pointer, vol_under_lim[0].limit_value);
  29. mot_temp = sample_motor_temp();
  30. mos_temp = sample_mos_temp();
  31. }
  32. static u16 _temp_limiter(s16 temp, limter_t *lim) {
  33. if (!lim->is_limit) {
  34. if (temp < lim->enter_pointer) {
  35. lim->ticks = 0;
  36. return HW_LIMIT_NONE;
  37. }
  38. if (lim->ticks == 0) {
  39. lim->ticks = get_tick_ms();
  40. }else if (get_delta_ms(lim->ticks) >= 500){
  41. lim->is_limit = true;
  42. lim->ticks = 0;
  43. return lim->limit_value;
  44. }
  45. return HW_LIMIT_NONE;
  46. }else {
  47. if (temp >= lim->exit_pointer) {
  48. lim->ticks = 0;
  49. return lim->limit_value;
  50. }
  51. if (lim->ticks == 0) {
  52. lim->ticks = get_tick_ms();
  53. }else if (get_delta_ms(lim->ticks) >= 500) {
  54. lim->is_limit = false;
  55. lim->ticks = 0;
  56. return HW_LIMIT_NONE;
  57. }
  58. return lim->limit_value;
  59. }
  60. }
  61. static u16 _vol_limiter(s16 vol, limter_t *lim) {
  62. if (!lim->is_limit) {
  63. if (vol > lim->enter_pointer) {
  64. lim->ticks = 0;
  65. return HW_LIMIT_NONE;
  66. }
  67. if (lim->ticks == 0) {
  68. lim->ticks = get_tick_ms();
  69. }else if (get_delta_ms(lim->ticks) >= 5){
  70. lim->is_limit = true;
  71. lim->ticks = 0;
  72. return lim->limit_value;
  73. }
  74. return HW_LIMIT_NONE;
  75. }else {
  76. if (vol <= lim->exit_pointer) {
  77. lim->ticks = 0;
  78. return lim->limit_value;
  79. }
  80. if (lim->ticks == 0) {
  81. lim->ticks = get_tick_ms();
  82. }else if (get_delta_ms(lim->ticks) >= 100) {
  83. lim->is_limit = false;
  84. lim->ticks = 0;
  85. return HW_LIMIT_NONE;
  86. }
  87. return lim->limit_value;
  88. }
  89. }
  90. static u16 _motor_limit(void) {
  91. static int temp_sensor_err = 0;
  92. static u16 lim_value = HW_LIMIT_NONE;
  93. s16 temp = get_motor_temp_raw();
  94. if ((temp == 300) || ABS(temp - mot_temp) >= 20) {
  95. if (temp_sensor_err < 20) {
  96. temp_sensor_err++;
  97. }else {
  98. if (mc_set_critical_error(FOC_CRIT_MOT_TEMP_Sensor)) {
  99. mc_crit_err_add(FOC_CRIT_MOT_TEMP_Sensor, temp, mot_temp);
  100. }
  101. }
  102. return lim_value; //温度传感器异常,返回上次的限流
  103. }else {
  104. mot_temp = temp;
  105. temp_sensor_err = 0;
  106. }
  107. for(int i = 0; i < ARRAY_SIZE(motor_temp_lim); i++) {
  108. limter_t *lim = motor_temp_lim + i;
  109. lim_value = _temp_limiter(temp, lim);
  110. if (lim_value != HW_LIMIT_NONE) {
  111. if (lim_value == 0) {
  112. if (mc_set_critical_error(FOC_CRIT_MOTOR_TEMP_Lim)) {
  113. mc_crit_err_add(FOC_CRIT_MOTOR_TEMP_Lim, temp, (s16)PMSM_FOC_GetSpeed());
  114. }
  115. }else if (_can_recovery){
  116. mc_clr_critical_error(FOC_CRIT_MOTOR_TEMP_Lim);
  117. }
  118. gear_t *gear = mc_get_gear_config();
  119. float prv_lim_value;
  120. float next_lim_tmp;
  121. if (i < (ARRAY_SIZE(motor_temp_lim)-1)) {
  122. prv_lim_value = (float)motor_temp_lim[i + 1].limit_value;
  123. }else {
  124. prv_lim_value = 100.0f; //最低一级限流
  125. }
  126. if (i != 0) {
  127. next_lim_tmp = (float)motor_temp_lim[i - 1].enter_pointer;
  128. }else {
  129. next_lim_tmp = (float)lim->enter_pointer + 10.0f; //最大一级限流
  130. }
  131. float delta_tmp = (next_lim_tmp - (float)lim->enter_pointer);
  132. float delta_value = (prv_lim_value - (float)lim->limit_value);
  133. float curr_value = prv_lim_value - (float)(temp - lim->enter_pointer)/delta_tmp * delta_value;
  134. curr_value = fclamp(curr_value, 0, prv_lim_value);
  135. lim_value = (u16)(((float)gear->max_torque * curr_value) / 100.0f);
  136. mc_set_motor_lim_level(i + 1);
  137. return lim_value;
  138. }else {
  139. mc_set_motor_lim_level(0);
  140. }
  141. }
  142. return HW_LIMIT_NONE;
  143. }
  144. static u16 _mos_limit(void) {
  145. static int temp_sensor_err = 0;
  146. static u16 lim_value = HW_LIMIT_NONE;
  147. s16 temp = get_mos_temp_raw();
  148. if ((temp == -40) || ABS(temp - mos_temp) >= 20) {
  149. if (temp_sensor_err < 20) {
  150. temp_sensor_err++;
  151. }else {
  152. if (mc_set_critical_error(FOC_CRIT_MOS_TEMP_Sensor)) {
  153. mc_crit_err_add(FOC_CRIT_MOS_TEMP_Sensor, temp, mos_temp);
  154. }
  155. }
  156. return lim_value; //温度传感器异常,返回上次的限流
  157. }else {
  158. mos_temp = temp;
  159. temp_sensor_err = 0;
  160. }
  161. for(int i = 0; i < ARRAY_SIZE(mos_temp_lim); i++) {
  162. limter_t *lim = mos_temp_lim + i;
  163. lim_value = _temp_limiter(temp, lim);
  164. if (lim_value != HW_LIMIT_NONE) {
  165. if (lim_value == 0) {
  166. if (mc_set_critical_error(FOC_CRIT_MOS_TEMP_Lim)) {
  167. mc_crit_err_add(FOC_CRIT_MOS_TEMP_Lim, temp, (s16)PMSM_FOC_GetSpeed());
  168. }
  169. }else if (_can_recovery){
  170. mc_clr_critical_error(FOC_CRIT_MOS_TEMP_Lim);
  171. }
  172. gear_t *gear = mc_get_gear_config();
  173. float prv_lim_value;
  174. float next_lim_tmp;
  175. if (i < (ARRAY_SIZE(mos_temp_lim)-1)) {
  176. prv_lim_value = (float)mos_temp_lim[i + 1].limit_value;
  177. }else {
  178. prv_lim_value = 100.0f; //最低一级限流
  179. }
  180. if (i != 0) {
  181. next_lim_tmp = (float)mos_temp_lim[i - 1].enter_pointer;
  182. }else {
  183. next_lim_tmp = (float)lim->enter_pointer + 10.0f; //最大一级限流
  184. }
  185. float delta_tmp = (next_lim_tmp - (float)lim->enter_pointer);
  186. float delta_value = (prv_lim_value - (float)lim->limit_value);
  187. float curr_value = prv_lim_value - (float)(temp - lim->enter_pointer)/delta_tmp * delta_value;
  188. curr_value = fclamp(curr_value, 0, prv_lim_value);
  189. lim_value = (u16)(((float)gear->max_torque * curr_value) / 100.0f);
  190. mc_set_mos_lim_level(i + 1);
  191. return lim_value;
  192. }else {
  193. mc_set_mos_lim_level(0);
  194. }
  195. }
  196. return HW_LIMIT_NONE;
  197. }
  198. /* this maybe limit power or torque, based on the current power */
  199. u16 motor_temp_high_limit(void) {
  200. if (!_inited) {
  201. _inited = true;
  202. limiter_init();
  203. }
  204. return _motor_limit();
  205. }
  206. /* limit the max torque(max phase current) */
  207. u16 mos_temp_high_limit(void) {
  208. if (!_inited) {
  209. _inited = true;
  210. limiter_init();
  211. }
  212. return _mos_limit();
  213. }
  214. /* limit the DC bus current */
  215. u16 vbus_under_vol_limit(void) {
  216. if (!_inited) {
  217. _inited = true;
  218. limiter_init();
  219. }
  220. s16 vol = (s16)sample_vbus_raw();
  221. for(int i = 0; i < ARRAY_SIZE(vol_under_lim); i++) {
  222. limter_t *lim = vol_under_lim + i;
  223. u16 lim_value = _vol_limiter(vol, lim);
  224. if (lim_value != HW_LIMIT_NONE) {
  225. if (mc_set_critical_error(FOC_CRIT_UN_Vol_Err)) {
  226. if (PMSM_FOC_GetSpeed() > CONFIG_ZERO_SPEED_RPM) {
  227. mc_crit_err_add(FOC_CRIT_UN_Vol_Err, vol, (s16)PMSM_FOC_GetSpeed());
  228. }
  229. }
  230. return lim_value;
  231. }
  232. }
  233. if (_can_recovery) {
  234. mc_clr_critical_error(FOC_CRIT_UN_Vol_Err);
  235. }
  236. return HW_LIMIT_NONE;
  237. }