class="markdown_views prism-atom-one-light">
默认的STM32F FOC SDK提供的工程文件下载到STM32以后不会电机不会自动转,想要让电机转,必须通过串口上位机ST Motor Control Workbench这个软件
若想脱离上位机让电机上电自动旋转,需要在main函数里面调用电机启动函数
UI_ExecCmd (oUI, MC_PROTOCOL_CMD_START_MOTOR);
根据UM1052 User manual STM32F PMSM single/dual FOC SDK v4.2手册中描述的可以利用FOC提供的UI函数来屏蔽底层驱动,直接在用户层编写程序
提供的函数定义在 UserInterfaceClass.c 中,其中主要的函数有
- CUI UI_NewObject(pUserInterfaceParams_t pUserInterfaceParams);
-
- void UI_Init(CUI this, uint8_t bMCNum, CMCI* pMCI, CMCT* pMCT, uint32_t* pUICfg);
-
- bool UI_SelectMC(CUI this,uint8_t bSelectMC);
-
- uint8_t UI_GetSelectedMC(CUI this);
-
- uint32_t UI_GetSelectedMCConfig(CUI this);
-
- bool UI_SetReg(CUI this, MC_Protocol_REG_t bRegID, int32_t wValue);
-
- int32_t UI_GetReg(CUI this, MC_Protocol_REG_t bRegID);
-
- CMCT UI_GetCurrentMCT(CUI this);
-
- bool UI_ExecCmd(CUI this, uint8_t bCmdID);
-
- bool UI_ExecSpeedRamp(CUI this, int32_t wFinalMecSpeedRPM, uint16_t hDurationms);
-
- bool UI_ExecTorqueRamp(CUI this, int16_t hTargetFinal, uint16_t hDurationms);
-
- bool UI_GetRevupData(CUI this, uint8_t bStage, uint16_t* pDurationms,
- int16_t* pFinalMecSpeed01Hz, int16_t* pFinalTorque );
-
- bool UI_SetRevupData(CUI this, uint8_t bStage, uint16_t hDurationms,
- int16_t hFinalMecSpeed01Hz, int16_t hFinalTorque );
-
- void UI_SetCurrentReferences(CUI this, int16_t hIqRef, int16_t hIdRef);
-
- void UI_DACInit(CUI this);
-
- void UI_DACExec(CUI this);
-
- void UI_SetDAC(CUI this, DAC_Channel_t bChannel,
- MC_Protocol_REG_t bVariable);
-
- MC_Protocol_REG_t UI_GetDAC(CUI this, DAC_Channel_t bChannel);
-
- void UI_SetUserDAC(CUI this, DAC_UserChannel_t bUserChNumber, int16_t hValue);
-
- void UI_LCDInit(CUI this, CUI oDAC, const char* s_fwVer);
-
- void UI_LCDExec(CUI this);
-
- void UI_LCDUpdateAll(CUI this);
-
- void UI_LCDUpdateMeasured(CUI this);
-
-
-
通过
bool UI_ExecCmd(CUI this, uint8_t bCmdID);
来对电机的运行状态进行改变,新的状态可以为
- #define MC_PROTOCOL_CMD_START_MOTOR 0x01
- #define MC_PROTOCOL_CMD_STOP_MOTOR 0x02
- #define MC_PROTOCOL_CMD_STOP_RAMP 0x03
- #define MC_PROTOCOL_CMD_RESET 0x04
- #define MC_PROTOCOL_CMD_PING 0x05
- #define MC_PROTOCOL_CMD_START_STOP 0x06
- #define MC_PROTOCOL_CMD_FAULT_ACK 0x07
- #define MC_PROTOCOL_CMD_ENCODER_ALIGN 0x08
- #define MC_PROTOCOL_CMD_IQDREF_CLEAR 0x09
- #define MC_PROTOCOL_CMD_PFC_ENABLE 0x0A
- #define MC_PROTOCOL_CMD_PFC_DISABLE 0x0B
- #define MC_PROTOCOL_CMD_PFC_FAULT_ACK 0x0C
- #define MC_PROTOCOL_CMD_SC_START 0x0D
- #define MC_PROTOCOL_CMD_SC_STOP 0x0E
根据手册描述,在main函数里面添加以下代码即可
- oUI = UI_NewObject(MC_NULL);
- UI_Init(oUI, MC_NUM, oMCI, oMCT, MC_NULL);
- UI_SelectMC(oUI, 2);
- UI_ExecSpeedRamp(oUI, -2000, 0);
- UI_ExecCmd (oUI, MC_PROTOCOL_CMD_START_MOTOR);
对了,变量应该先声明后使用
CUI oUI;
如何读取电机的状态呢?读寄存器MC_PROTOCOL_REG_STATUS即可
或者用MCI_GetSTMState(oMCI)
返回的值可能是
- ICLWAIT = 12,
- IDLE = 0,
- IDLE_ALIGNMENT = 1,
- ALIGN_CHARGE_BOOT_CAP = 13,
- ALIGN_OFFSET_CALIB = 14,
- ALIGN_CLEAR = 15,
- ALIGNMENT = 2,
- IDLE_START = 3,
- CHARGE_BOOT_CAP = 16,
- OFFSET_CALIB = 17,
- CLEAR = 18,
- START = 4,
- START_RUN = 5,
- RUN = 6,
- ANY_STOP = 7,
- STOP = 8,
- STOP_IDLE = 9,
- FAULT_NOW = 10,
- FAULT_OVER = 11
基本的操作建立起来了,下一步就是具体的应用了。