limit.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "foc/limit.h"
  2. #include "foc/core/controller.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(limit_type type) {
  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)mot_contrl_get_speed(&motor.controller));
  114. }
  115. }else if (_can_recovery){
  116. mc_clr_critical_error(FOC_CRIT_MOTOR_TEMP_Lim);
  117. }
  118. gear_t *gear = mc_gear_conf();
  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. float max_value = (float)gear->max_torque;
  136. if (type == type_idc) {
  137. max_value = (float)gear->max_idc;
  138. }
  139. lim_value = (u16)((max_value * curr_value) / 100.0f);
  140. mc_set_motor_lim_level(i + 1);
  141. return lim_value;
  142. }else {
  143. mc_set_motor_lim_level(0);
  144. }
  145. }
  146. return HW_LIMIT_NONE;
  147. }
  148. static u16 _mos_limit(void) {
  149. static int temp_sensor_err = 0;
  150. static u16 lim_value = HW_LIMIT_NONE;
  151. s16 temp = get_mos_temp_raw();
  152. if ((temp == -40) || ABS(temp - mos_temp) >= 20) {
  153. if (temp_sensor_err < 20) {
  154. temp_sensor_err++;
  155. }else {
  156. if (mc_set_critical_error(FOC_CRIT_MOS_TEMP_Sensor)) {
  157. mc_crit_err_add(FOC_CRIT_MOS_TEMP_Sensor, temp, mos_temp);
  158. }
  159. }
  160. return lim_value; //温度传感器异常,返回上次的限流
  161. }else {
  162. mos_temp = temp;
  163. temp_sensor_err = 0;
  164. }
  165. for(int i = 0; i < ARRAY_SIZE(mos_temp_lim); i++) {
  166. limter_t *lim = mos_temp_lim + i;
  167. lim_value = _temp_limiter(temp, lim);
  168. if (lim_value != HW_LIMIT_NONE) {
  169. if (lim_value == 0) {
  170. if (mc_set_critical_error(FOC_CRIT_MOS_TEMP_Lim)) {
  171. mc_crit_err_add(FOC_CRIT_MOS_TEMP_Lim, temp, (s16)mot_contrl_get_speed(&motor.controller));
  172. }
  173. }else if (_can_recovery){
  174. mc_clr_critical_error(FOC_CRIT_MOS_TEMP_Lim);
  175. }
  176. gear_t *gear = mc_gear_conf();
  177. float prv_lim_value;
  178. float next_lim_tmp;
  179. if (i < (ARRAY_SIZE(mos_temp_lim)-1)) {
  180. prv_lim_value = (float)mos_temp_lim[i + 1].limit_value;
  181. }else {
  182. prv_lim_value = 100.0f; //最低一级限流
  183. }
  184. if (i != 0) {
  185. next_lim_tmp = (float)mos_temp_lim[i - 1].enter_pointer;
  186. }else {
  187. next_lim_tmp = (float)lim->enter_pointer + 10.0f; //最大一级限流
  188. }
  189. float delta_tmp = (next_lim_tmp - (float)lim->enter_pointer);
  190. float delta_value = (prv_lim_value - (float)lim->limit_value);
  191. float curr_value = prv_lim_value - (float)(temp - lim->enter_pointer)/delta_tmp * delta_value;
  192. curr_value = fclamp(curr_value, 0, prv_lim_value);
  193. lim_value = (u16)(((float)gear->max_torque * curr_value) / 100.0f);
  194. mc_set_mos_lim_level(i + 1);
  195. return lim_value;
  196. }else {
  197. mc_set_mos_lim_level(0);
  198. }
  199. }
  200. return HW_LIMIT_NONE;
  201. }
  202. /* this maybe limit power or torque, based on the current power */
  203. u16 motor_temp_high_limit(limit_type type) {
  204. if (!_inited) {
  205. _inited = true;
  206. limiter_init();
  207. }
  208. return _motor_limit(type);
  209. }
  210. /* limit the max torque(max phase current) */
  211. u16 mos_temp_high_limit(void) {
  212. if (!_inited) {
  213. _inited = true;
  214. limiter_init();
  215. }
  216. return _mos_limit();
  217. }
  218. /* limit the DC bus current */
  219. u16 vbus_under_vol_limit(void) {
  220. if (!_inited) {
  221. _inited = true;
  222. limiter_init();
  223. }
  224. s16 vol = (s16)sample_vbus_raw();
  225. for(int i = 0; i < ARRAY_SIZE(vol_under_lim); i++) {
  226. limter_t *lim = vol_under_lim + i;
  227. u16 lim_value = _vol_limiter(vol, lim);
  228. if (lim_value != HW_LIMIT_NONE) {
  229. if (mc_set_critical_error(FOC_CRIT_UN_Vol_Err)) {
  230. if (mot_contrl_get_speed(&motor.controller) > CONFIG_ZERO_SPEED_RPM) {
  231. mc_crit_err_add(FOC_CRIT_UN_Vol_Err, vol, (s16)mot_contrl_get_speed(&motor.controller));
  232. }
  233. }
  234. return lim_value;
  235. }
  236. }
  237. if (_can_recovery) {
  238. mc_clr_critical_error(FOC_CRIT_UN_Vol_Err);
  239. }
  240. return HW_LIMIT_NONE;
  241. }