ST SDK5.0电机正反转及转速控制

2019-04-13 20:54发布

一、转速控制

 实现如图控制: while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ MC_ProgramSpeedRampMotor1(3000/6,1000); //设定转速为3000 MC_StartMotor1(); //马达运转 HAL_Delay(10000); //延时10S MC_ProgramSpeedRampMotor1(5000/6,1000); //设定转速为5000 HAL_Delay(10000); MC_ProgramSpeedRampMotor1(2000/6,1000); //设定转速为2000 HAL_Delay(10000); MC_StopMotor1(); //马达停转 HAL_Delay(5000); //延时5S } /* USER CODE END 3 */ 通俗易懂,不做过多解释

二、正反转控制

正反转控制会遇到图中的报错问题,应先清除再重新启动,手动workbench的道理相同。 

1、添加头文件

如果不添加这三个头文件,在使用State_t等定义的类型时会报错 #include "state_machine.h" #include "mc_type.h" #include "mc_tasks.h"

2、 电机的状态

在ST的设定中电机有以上状态,可在单步调试时设置全局变量,调用API观测电机状态 int cr=1; ste = MCI_GetSTMStateMotor1(); API解释如下 说明:State_t为一枚举类型 typedef enum { ICLWAIT = 12, /*!< Persistent state, the system is waiting for ICL deactivation. Is not possible to run the motor if ICL is active. Until the ICL is active the state is forced to ICLWAIT, when ICL become inactive the state is moved to IDLE */ IDLE = 0, /*!< Persistent state, following state can be IDLE_START if a start motor command has been given or IDLE_ALIGNMENT if a start alignment command has been given */ IDLE_ALIGNMENT = 1, /*!< "Pass-through" state containg the code to be executed only once after encoder alignment command. Next states can be ALIGN_CHARGE_BOOT_CAP or ALIGN_OFFSET_CALIB according the configuration. It can also be ANY_STOP if a stop motor command has been given. */ ALIGN_CHARGE_BOOT_CAP = 13,/*!< Persistent state where the gate driver boot capacitors will be charged. Next states will be ALIGN_OFFSET_CALIB. It can also be ANY_STOP if a stop motor command has been given. */ ALIGN_OFFSET_CALIB = 14,/*!< Persistent state where the offset of motor currents measurements will be calibrated. Next state will be ALIGN_CLEAR. It can also be ANY_STOP if a stop motor command has been given. */ ALIGN_CLEAR = 15, /*!< "Pass-through" state in which object is cleared and set for the startup. Next state will be ALIGNMENT. It can also be ANY_STOP if a stop motor command has been given. */ ALIGNMENT = 2, /*!< Persistent state in which the encoder are properly aligned to set mechanical angle, following state can only be ANY_STOP */ IDLE_START = 3, /*!< "Pass-through" state containg the code to be executed only once after start motor command. Next states can be CHARGE_BOOT_CAP or OFFSET_CALIB according the configuration. It can also be ANY_STOP if a stop motor command has been given. */ CHARGE_BOOT_CAP = 16, /*!< Persistent state where the gate driver boot capacitors will be charged. Next states will be OFFSET_CALIB. It can also be ANY_STOP if a stop motor command has been given. */ OFFSET_CALIB = 17, /*!< Persistent state where the offset of motor currents measurements will be calibrated. Next state will be CLEAR. It can also be ANY_STOP if a stop motor command has been given. */ CLEAR = 18, /*!< "Pass-through" state in which object is cleared and set for the startup. Next state will be START. It can also be ANY_STOP if a stop motor command has been given. */ START = 4, /*!< Persistent state where the motor start-up is intended to be executed. The following state is normally START_RUN as soon as first validated speed is detected. Another possible following state is ANY_STOP if a stop motor command has been executed */ START_RUN = 5, /*!< "Pass-through" state, the code to be executed only once between START and RUN states it’s intended to be here executed. Following state is normally RUN but it can also be ANY_STOP if a stop motor command has been given */ RUN = 6, /*!< Persistent state with running motor. The following state is normally ANY_STOP when a stop motor command has been executed */ ANY_STOP = 7, /*!< "Pass-through" state, the code to be executed only once between any state and STOP it’s intended to be here executed. Following state is normally STOP */ STOP = 8, /*!< Persistent state. Following state is normally STOP_IDLE as soon as conditions for moving state machine are detected */ STOP_IDLE = 9, /*!< "Pass-through" state, the code to be executed only once between STOP and IDLE it’s intended to be here executed. Following state is normally IDLE */ FAULT_NOW = 10, /*!< Persistent state, the state machine can be moved from any condition directly to this state by STM_FaultProcessing method. This method also manage the passage to the only allowed following state that is FAULT_OVER */ FAULT_OVER = 11 /*!< Persistent state where the application is intended to stay when the fault conditions disappeared. Following state is normally STOP_IDLE, state machine is moved as soon as the user has acknowledged the fault condition. */ } State_t;  

3、正反转控制

在文档范例上有其他控制方法,以下为自己思考,对电机状态理解有好处 while(1) { //======第一圈 if(cr) //cr为while外一整型,用来凑第一圈 { MC_StartMotor1(); MC_ProgramSpeedRampMotor1(1000/6,1000); HAL_Delay(10000); cr=cr-1; } //====其他圈 else { //MC_StartMotor1(); MC_ProgramSpeedRampMotor1(1000/6,1000); HAL_Delay(5000); //反转速度后必须延时,留一个报错的时间 ste = MCI_GetSTMStateMotor1(); if(ste==11||ste==10) //如果状态为Fault { MC_AcknowledgeFaultMotor1(); //清除报错 MC_StartMotor1(); HAL_Delay(5000); //启动延时,启动是一个过程 int16_t lastspeed=MC_GetLastRampFinalSpeedMotor1(); MC_ProgramSpeedRampMotor1(1000/6, 1000); MC_StartMotor1(); HAL_Delay(10000); } } //=========反转=========== //MC_StartMotor1(); MC_ProgramSpeedRampMotor1(-1000/6,1000); HAL_Delay(5000); //反转速度后必须延时,留一个报错的时间 ste = MCI_GetSTMStateMotor1(); if(ste==11||ste==10) { MC_AcknowledgeFaultMotor1(); MC_StartMotor1(); HAL_Delay(5000); int16_t lastspeed=MC_GetLastRampFinalSpeedMotor1(); MC_ProgramSpeedRampMotor1(-1000/6, 1000); MC_StartMotor1(); HAL_Delay(10000); } else { HAL_Delay(10000); } }

三、用Workbench查看运行状态以及用Plott查看波形: