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