STM32 motor control--API function application

 基于ST MC SDK 5.2版本的应用
 一般的电机操作调用API就足够控制基本的电机运行
 下图是API函数的列表: 

Insert picture description here
Insert picture description here

1. Application 1: API speed control-control the start and stop of the motor

程序启动后,电机以3000RMP的速度运行10S后停止
停止5S后,电机重新以3000RPM转速运行10S后停止
以上过程重复操作

注意:速度指令参数是以0.1Hz为单位,3000RPM=3000/6(0.1Hz)

Insert picture description here
Insert picture description here

Speed ​​command

Add code based on the program generated by MotorControl Workbench 5.2.0 and add it in main.c

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */
void Delay_Handler(void)
{
    
    
  delay_cnt++;
  if(delay_cnt>2000)          //1s计时
	{
    
    
		delay_cnt=0;

		if(delay_start_flag==0x30)  //启动10s计时
		{
    
    
			delay_cnt1++;
			if(delay_cnt1>9)          //10S
			{
    
    
				delay_cnt1=0;
				delay_start_flag=0x20;    
				delay_10s_flag=1;       //10S时间到标志位
			} 
		}
		else if(delay_start_flag==0x20)  //启动5s计时
		{
    
    
			delay_cnt1++;
			if(delay_cnt1>4)           //5S
			{
    
    
			  delay_cnt1=0;
			  delay_start_flag=0x30;
			  delay_5s_flag=1;   //5S时间到标志位
			}
		}  
	}
}
/* USER CODE END 0 */

int main(void)中

  /* USER CODE BEGIN 2 */
     
  MC_ProgramSpeedRampMotor1(3000/6,1000);  //1s内加速到3000RPM
  MC_StartMotor1();      //启动电机
	
  /* USER CODE END 2 */
  /* USER CODE BEGIN 3 */
		
		if(delay_10s_flag==1)     //10S
		{
    
    
			MC_StopMotor1();         //停止电机运行
			delay_10s_flag=0;
		}
		if(delay_5s_flag==1)     //5S
		{
    
    
			MC_StartMotor1();
			delay_5s_flag=0;
		}

In stm32f4xx_mc_it.c, add the
tick timer interrupt program to call the defined function Delay_Handler(void);
Insert picture description here
after adding, compile and download to achieve 3000RPM running 10S stop 5S running 10S...
Insert picture description here

2. Application 2: PI component interface function for online parameter modification

在程序中修改速度PI值
修改为原始值的2倍
修改为原始值的0.3倍
观察参数修改后的速度曲线

Add code based on the program generated by MotorControl Workbench 5.2.0 and add it in main.c

Add header file mc_extended_api.h

/* USER CODE BEGIN Includes */
#include "mc_extended_api.h"
/* USER CODE END Includes */

Variable definitions

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
MCT_Handle_t *pMctHdl;                       //定义的变量
static int16_t Speed_Kp,Speed_Ki;
/* USER CODE END PV */

Add in the main function

  /* USER CODE BEGIN 2 */
  pMctHdl = GetMCT(M1);
  Speed_Kp= PID_GetKP(pMctHdl->pPIDSpeed);
  Speed_Ki= PID_GetKI(pMctHdl->pPIDSpeed);
  
  PID_SetKP(pMctHdl->pPIDSpeed,Speed_Kp*2);
  PID_SetKI(pMctHdl->pPIDSpeed,Speed_Ki*2);
  /* USER CODE END 2 */

Compile, download and view the speed curve

  • When the original multiple
  PID_SetKP(pMctHdl->pPIDSpeed,Speed_Kp*1);
  PID_SetKI(pMctHdl->pPIDSpeed,Speed_Ki*1);

Insert picture description here

  • When 2 times PI
  PID_SetKP(pMctHdl->pPIDSpeed,Speed_Kp*2);
  PID_SetKI(pMctHdl->pPIDSpeed,Speed_Ki*2);

Insert picture description here

  • When 0.3 times PI
  PID_SetKP(pMctHdl->pPIDSpeed,Speed_Kp*0.3);
  PID_SetKI(pMctHdl->pPIDSpeed,Speed_Ki*0.3);

Insert picture description here

3. Application 3: Switch based on MC SDK status

速度正转3000rpm
速度立刻反转-3000rpm
产生状态报错,需要程序返回报错信息
清除报错信息,返回到IDLE状态
然后继续执行反转-3000rpm速度指令

Insert picture description here
At the beginning of this experiment, the HALL sensor was used to test, but it was found that the speed can be switched normally, and no fault shutdown will be reported. Then after the sensorless mode is changed, the fault shutdown with the speed feedback error will be reported. The following is based on the sensorless mode. Mode of:

Add code based on the program (Sensorless) generated by MotorControl Workbench 5.2.0 and add it in main.c

Add in the main function

  /* USER CODE BEGIN 2 */
  MC_ProgramSpeedRampMotor1(3000/6,1000);  
  MC_StartMotor1();  
  HAL_Delay(5000);                        
  MC_ProgramSpeedRampMotor1(-3000/6,1000); 
  /* USER CODE END 2 */

After the speed rotates forward at 3000rpm, it immediately reverses -3000rpm, a status error (speed feedback) is generated, the program needs to return the error message
Insert picture description here
Insert picture description here
status to report an error, the program needs to return the error message, clear the error message, return to the IDLE state, and then continue to perform the reverse -3000rpm speed instruction

Program added, in the main function

  /* USER CODE BEGIN 1 */    
	 State_t sts_motor1; 
  /* USER CODE END 1 */

  /* USER CODE BEGIN 3 */
    sts_motor1 = MC_GetSTMStateMotor1();     //获取电机状态
		
    if(sts_motor1 == FAULT_OVER)             //电机报错
    {
    
    
      MC_AcknowledgeFaultMotor1();           //清除故障,返回到IDLE状态
      
    }
    else if(sts_motor1 == IDLE)              //继续执行反转-3000rpm速度指令
    {
    
    
      MC_StartMotor1();
      MC_ProgramSpeedRampMotor1(MC_GetLastRampFinalSpeedMotor1(), 1000);
    }

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq1291917670/article/details/111945076