1. 程式人生 > >ST SDK5.0電機正反轉及轉速控制

ST SDK5.0電機正反轉及轉速控制

一、轉速控制

 實現如圖控制:

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);
    }
}