motor.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "foc/motor/motor.h"
  2. #include "foc/motor/current.h"
  3. #include "foc/foc_config.h"
  4. #include "foc/mc_error.h"
  5. #include "foc/samples.h"
  6. #include "math/fast_math.h"
  7. #include "bsp/timer_count32.h"
  8. #include "libs/time_measure.h"
  9. #include "bsp/delay.h"
  10. #include "bsp/bsp.h"
  11. #include "bsp/adc.h"
  12. #include "bsp/pwm.h"
  13. #include "foc/commands.h"
  14. #include "libs/logger.h"
  15. #include "bsp/sched_timer.h"
  16. #include "foc/core/e_ctrl.h"
  17. #include "foc/motor/motor_param.h"
  18. #include "foc/core/torque.h"
  19. #include "app/nv_storage.h"
  20. static bool mc_is_hwbrake(void);
  21. static void _pwm_brake_timer_handler(shark_timer_t *);
  22. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, _pwm_brake_timer_handler);
  23. static motor_t motor = {
  24. .s_direction = POSITIVE,
  25. };
  26. static void mc_gpio_init(void) {
  27. #ifdef GPIO_BRAKE_IN_GROUP
  28. rcu_periph_clock_enable(GPIO_BRAKE_IN_RCU);
  29. gpio_init(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_MODE, GPIO_OSPEED_50MHZ, GPIO_BRAKE_IN_PIN);
  30. gpio_exti_source_select(GPIO_BRAKE_EXIT_SRC_GROUP, GPIO_BRAKE_EXIT_SRC_PIN);
  31. exti_init(GPIO_BRAKE_EXTI, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  32. nvic_irq_enable(GPIO_BRAKE_IRQ, EBREAK_IRQ_PRIORITY, 0U);
  33. exti_interrupt_flag_clear(GPIO_BRAKE_EXTI);
  34. exti_interrupt_enable(GPIO_BRAKE_EXTI);
  35. #endif
  36. }
  37. static u32 _self_check_task(void *p) {
  38. if (ENC_Check_error()) {
  39. err_add_record(FOC_CRIT_Encoder_Err, 0);
  40. PMSM_FOC_SetCriticalError(FOC_CRIT_Encoder_Err);
  41. }
  42. return 0;
  43. }
  44. void mc_init(void) {
  45. adc_init();
  46. pwm_3phase_init();
  47. samples_init();
  48. motor_encoder_init();
  49. foc_command_init();
  50. PMSM_FOC_CoreInit();
  51. mc_gpio_init();
  52. sched_timer_enable(SPD_CTRL_MS);
  53. shark_task_create(_self_check_task, NULL);
  54. }
  55. motor_t * mc_params(void) {
  56. return &motor;
  57. }
  58. bool mc_start(u8 mode) {
  59. if (motor.b_start) {
  60. return true;
  61. }
  62. if (PMSM_FOC_GetCriticalError() != 0) {
  63. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  64. return false;
  65. }
  66. if (mode > CTRL_MODE_CURRENT) {
  67. PMSM_FOC_SetErrCode(FOC_Param_Err);
  68. return false;
  69. }
  70. if (PMSM_FOC_GetSpeed() > 10.0f) {
  71. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  72. return false;
  73. }
  74. if (!mc_throttle_released()) {
  75. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  76. return false;
  77. }
  78. motor.mode = mode;
  79. eCtrl_init(300, 3000);
  80. motor_encoder_start(motor.s_direction);
  81. PMSM_FOC_Start(mode);
  82. pwm_turn_on_low_side();
  83. delay_ms(500);
  84. phase_current_offset_calibrate();
  85. pwm_start();
  86. adc_start_convert();
  87. phase_current_calibrate_wait();
  88. motor.throttle = 0;
  89. motor.b_start = true;
  90. if (phase_curr_offset_check()) {
  91. PMSM_FOC_SetCriticalError(FOC_CRIT_CURR_OFF_Err);
  92. mc_stop();
  93. return false;
  94. }
  95. if (mc_is_hwbrake()) {
  96. PMSM_FOC_Brake(true);
  97. }
  98. gpio_beep(1000);
  99. return true;
  100. }
  101. bool mc_stop(void) {
  102. if (!motor.b_start) {
  103. return true;
  104. }
  105. if (PMSM_FOC_GetSpeed() > 10.0f) {
  106. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  107. sys_debug("speed error\n");
  108. return false;
  109. }
  110. if (!mc_throttle_released()) {
  111. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  112. sys_debug("throttle error\n");
  113. return false;
  114. }
  115. motor.mode = CTRL_MODE_OPEN;
  116. adc_stop_convert();
  117. pwm_stop();
  118. PMSM_FOC_Stop();
  119. motor.b_start = false;
  120. gpio_led2_enable(false);
  121. return true;
  122. }
  123. void mc_set_spd_torque(s32 target) {
  124. motor.b_ignor_throttle = true;
  125. motor.s_targetFix = target;
  126. }
  127. void mc_use_throttle(void) {
  128. motor.b_ignor_throttle = false;
  129. }
  130. void mc_encoder_off_calibrate(s16 vd) {
  131. if (PMSM_FOC_Is_Start()) {
  132. return;
  133. }
  134. pwm_turn_on_low_side();
  135. task_udelay(500);
  136. PMSM_FOC_Start(CTRL_MODE_OPEN);
  137. phase_current_offset_calibrate();
  138. pwm_start();
  139. adc_start_convert();
  140. phase_current_calibrate_wait();
  141. PMSM_FOC_Set_Angle(0);
  142. PMSM_FOC_SetOpenVdq(vd, 0);
  143. delay_ms(2000);
  144. motor_encoder_set_direction(POSITIVE);
  145. for (int i = 0; i < 1000000; i++) {
  146. for (float angle = 0; angle < 360; angle++) {
  147. PMSM_FOC_Set_Angle(angle);
  148. delay_ms(2);
  149. motor_encoder_offset(angle);
  150. }
  151. wdog_reload();
  152. if (motor_encoder_offset_is_finish()) {
  153. break;
  154. }
  155. }
  156. delay_ms(500);
  157. motor_encoder_set_direction(NEGATIVE);
  158. for (int i = 0; i < 1000000; i++) {
  159. for (float angle = 359; angle >= 0; angle--) {
  160. PMSM_FOC_Set_Angle(angle);
  161. delay_ms(2);
  162. motor_encoder_offset(angle);
  163. }
  164. wdog_reload();
  165. if (motor_encoder_offset_is_finish()) {
  166. break;
  167. }
  168. }
  169. delay_ms(500);
  170. PMSM_FOC_SetOpenVdq(0, 0);
  171. delay_ms(500);
  172. wdog_reload();
  173. adc_stop_convert();
  174. pwm_stop();
  175. PMSM_FOC_Stop();
  176. }
  177. bool mc_encoder_zero_calibrate(s16 vd) {
  178. if (PMSM_FOC_Is_Start()) {
  179. return false;
  180. }
  181. pwm_turn_on_low_side();
  182. task_udelay(500);
  183. PMSM_FOC_Start(CTRL_MODE_OPEN);
  184. phase_current_offset_calibrate();
  185. pwm_start();
  186. adc_start_convert();
  187. phase_current_calibrate_wait();
  188. PMSM_FOC_Set_Angle(0);
  189. PMSM_FOC_SetOpenVdq(vd, 0);
  190. delay_ms(2000);
  191. float phase = motor_encoder_zero_phase_detect();
  192. delay_ms(500);
  193. PMSM_FOC_SetOpenVdq(0, 0);
  194. delay_ms(500);
  195. adc_stop_convert();
  196. pwm_stop();
  197. PMSM_FOC_Stop();
  198. if (phase != INVALID_ANGLE) {
  199. nv_save_angle_offset(phase);
  200. return true;
  201. }
  202. return false;
  203. }
  204. bool mc_current_sensor_calibrate(float current) {
  205. if (!mc_start(CTRL_MODE_OPEN)) {
  206. return false;
  207. }
  208. phase_current_sensor_start_calibrate(current);
  209. phase_current_calibrate_wait();
  210. return true;
  211. }
  212. bool mc_lock_motor(bool lock) {
  213. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  214. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  215. return false;
  216. }
  217. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  218. if (!motor.b_start) {
  219. if (lock) {
  220. pwm_start();
  221. pwm_update_duty(0, 0, 0);
  222. }else {
  223. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  224. pwm_stop();
  225. }
  226. }
  227. return true;
  228. }
  229. bool mc_throttle_released(void) {
  230. return get_throttle_float() < CONFIG_THROTTLE_LOW_VALUE;
  231. }
  232. static bool mc_is_hwbrake(void) {
  233. #ifdef GPIO_BRAKE_IN_GROUP
  234. int count = 50;
  235. int settimes = 0;
  236. while(count-- > 0) {
  237. bool b1 = gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  238. if (b1) {
  239. settimes ++;
  240. }
  241. delay_us(1);
  242. }
  243. if (settimes == 0) {
  244. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  245. return true;
  246. #else
  247. return false;
  248. #endif
  249. }else if (settimes == 50) {
  250. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  251. return false;
  252. #else
  253. return true;
  254. #endif
  255. }else {
  256. //有干扰,do nothing
  257. motor.n_brake_errors++;
  258. return false;
  259. }
  260. #else
  261. return false;
  262. #endif
  263. }
  264. void MC_Brake_IRQHandler(void) {
  265. if (!motor.b_start) {
  266. return;
  267. }
  268. if (mc_is_hwbrake()) {
  269. PMSM_FOC_Brake(true);
  270. }else {
  271. PMSM_FOC_Brake(false);
  272. }
  273. }
  274. static void _pwm_brake_timer_handler(shark_timer_t *t){
  275. pwm_brake_enable(true);
  276. }
  277. void MC_Protect_IRQHandler(void){
  278. pwm_brake_enable(false);
  279. shark_timer_post(&_brake_timer, 1000);
  280. if (!motor.b_start) {
  281. return;
  282. }
  283. PMSM_FOC_SetCriticalError(FOC_CRIT_Phase_Err);
  284. PMSM_FOC_Stop(); //三相50%占空比输出,防止mos过压击穿
  285. pwm_start();
  286. adc_start_convert();
  287. }
  288. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  289. void TIMER_UP_IRQHandler(void){
  290. //phase_current_adc_triger();
  291. time_measure_start(&g_meas_timeup);
  292. }
  293. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  294. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  295. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  296. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  297. void ADC_IRQHandler(void) {
  298. if (phase_current_offset()) {//check if is adc offset checked
  299. return;
  300. }
  301. if (phase_current_sensor_do_calibrate()){
  302. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  303. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  304. return;
  305. }
  306. TIME_MEATURE_START();
  307. PMSM_FOC_Schedule();
  308. TIME_MEATURE_END();
  309. }
  310. //#define ANGLE_TEST
  311. #ifdef ANGLE_TEST
  312. static void _debug_angle(void) {
  313. if (motor.b_start) {
  314. PMSM_FOC_Set_Angle(motor.s_testAngle);
  315. if (++motor.s_testAngle >= 360) {
  316. motor.s_testAngle = 0;
  317. }
  318. }
  319. }
  320. #endif
  321. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  322. measure_time_t g_meas_MCTask;
  323. void Sched_MC_mTask(void) {
  324. time_measure_start(&g_meas_MCTask);
  325. u8 runMode = PMSM_FOC_CtrlMode();
  326. #if ANGLE_TEST
  327. _debug_angle();
  328. #endif
  329. PMSM_FOC_Calc_iDC();
  330. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  331. if (motor.mode != CTRL_MODE_OPEN) {
  332. u32 mask;
  333. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  334. mask = cpu_enter_critical();
  335. PMSM_FOC_Stop();
  336. pwm_disable_channel();
  337. cpu_exit_critical(mask);
  338. }else {
  339. mask = cpu_enter_critical();
  340. PMSM_FOC_Start(motor.mode);
  341. pwm_enable_channel();
  342. cpu_exit_critical(mask);
  343. }
  344. }
  345. if (runMode != CTRL_MODE_OPEN) {
  346. eCtrl_Running();
  347. float f_throttle = get_throttle_float();
  348. if (f_throttle != motor.throttle) {
  349. motor.throttle = f_throttle;
  350. torque_speed_target(runMode, f_throttle);
  351. }
  352. PMSM_FOC_idqCalc();
  353. }
  354. }
  355. }