默认的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 中,其中主要的函数有
- /**
- * @brief Creates an object of the class UserInterface
- * @param pUserInterfaceParams pointer to an UserInterface parameters structure
- * @retval CUI new instance of UserInterface object
- */
- CUI UI_NewObject(pUserInterfaceParams_t pUserInterfaceParams);
-
- /**
- * @brief Initialization of UI object. It perform the link between the UI
- * object and the MC interface and MC tuning objects. It must be called
- * before the derived class initialization.
- * @param this related object of class CUI.
- * @param bMCNum Is the total number of MC object presnet in the list.
- * @param pMCI is the pointer of the list of MC interface objects to be linked
- * with the UI.
- * @param pMCT is the pointer of the list of MC tuning objects to be linked
- * with the UI.
- * @param pUICfg is the pointer of the user interface configuration list. Each
- * element of the list must be a bit field containing one (or more) of
- * the exported configuration option UI_CFGOPT_xxx (eventually OR-ed).
- * @retval none.
- */
- void UI_Init(CUI this, uint8_t bMCNum, CMCI* pMCI, CMCT* pMCT, uint32_t* pUICfg);
-
- /**
- * @brief It is used to select the MC on which UI operates.
- * @param this related object of class CUI.
- * @param bSelectMC The new selected MC, zero based, on which UI operates.
- * @retval bool It return true if the bSelectMC is valid oterwise return false.
- */
- bool UI_SelectMC(CUI this,uint8_t bSelectMC);
-
- /**
- * @brief It is used to retrive the MC on which UI currently operates.
- * @param this related object of class CUI.
- * @retval uint8_t It returns the currently selected MC, zero based, on which
- * UI operates.
- */
- uint8_t UI_GetSelectedMC(CUI this);
-
- /**
- * @brief It is used to retrive the configuration of the MC on which UI
- * currently operates.
- * @param this related object of class CUI.
- * @retval uint32_t It returns the currently configuration of selected MC on
- * which UI operates.
- * It represents a bit field containing one (or more) of
- * the exported configuration option UI_CFGOPT_xxx (eventually OR-ed).
- */
- uint32_t UI_GetSelectedMCConfig(CUI this);
-
- /**
- * @brief It is used to execute a SetReg command coming from the user.
- * @param this related object of class CUI.
- * @param bRegID Code of register to be updated. Valid code is one of the
- * MC_PROTOCOL_REG_xxx values exported by UserInterfaceClass.
- * @param wValue is the new value to be set.
- * @retval bool It returns true if the SetReg command has been performed
- * succesfully otherwise returns false.
- */
- bool UI_SetReg(CUI this, MC_Protocol_REG_t bRegID, int32_t wValue);
-
- /**
- * @brief It is used to execute a GetReg command coming from the user.
- * @param this related object of class CUI.
- * @param bRegID Code of register to be updated. Valid code is one of the
- * MC_PROTOCOL_REG_xxx values exported by UserInterfaceClass.
- * @retval int32_t is the current value of register bRegID.
- */
- int32_t UI_GetReg(CUI this, MC_Protocol_REG_t bRegID);
-
- /**
- * @brief It is used to retrieve the current selected MC tuning object.
- * @param this related object of class CUI.
- * @retval CMCT It returns the currently selected MC tuning object on which
- * UI operates.
- */
- CMCT UI_GetCurrentMCT(CUI this);
-
- /**
- * @brief It is used to execute a command coming from the user.
- * @param this related object of class CUI.
- * @param bCmdID Code of register to be updated. Valid code is one of the
- * MC_PROTOCOL_CMD_xxx define exported by UserInterfaceClass.
- * @retval bool It returns true if the command has been performed
- * succesfully otherwise returns false.
- */
- bool UI_ExecCmd(CUI this, uint8_t bCmdID);
-
- /**
- * @brief It is used to execute a speed ramp command coming from the user.
- * @param this related object of class CUI.
- * @param wFinalMecSpeedRPM final speed value expressed in RPM.
- * @param hDurationms the duration of the ramp expressed in milliseconds. It
- * is possible to set 0 to perform an instantaneous change in the value.
- * @retval bool It returns true if the command has been performed
- * succesfully otherwise returns false.
- */
- bool UI_ExecSpeedRamp(CUI this, int32_t wFinalMecSpeedRPM, uint16_t hDurationms);
-
- /**
- * @brief It is used to execute a torque ramp command coming from the user.
- * @param this related object of class CUI.
- * @param hTargetFinal final torque value. See MCI interface for more
- details.
- * @param hDurationms the duration of the ramp expressed in milliseconds. It
- * is possible to set 0 to perform an instantaneous change in the value.
- * @retval bool It returns true if the command has been performed
- * succesfully otherwise returns false.
- */
- bool UI_ExecTorqueRamp(CUI this, int16_t hTargetFinal, uint16_t hDurationms);
-
- /**
- * @brief It is used to execute a get Revup data command coming from the user.
- * @param this related object of class CUI.
- * @param bStage is the rev up phase, zero based, to be read.
- * @param pDurationms is the pointer to an uint16_t variable used to retrieve
- * the duration of the Revup stage.
- * @param pFinalMecSpeed01Hz is the pointer to an int16_t variable used to
- * retrieve the mechanical speed at the end of that stage expressed in
- * 0.1Hz.
- * @param pFinalTorque is the pointer to an int16_t variable used to
- * retrieve the value of motor torque at the end of that
- * stage. This value represents actually the Iq current expressed in
- * digit.
- * @retval bool It returns true if the command has been performed
- * succesfully otherwise returns false.
- */
- bool UI_GetRevupData(CUI this, uint8_t bStage, uint16_t* pDurationms,
- int16_t* pFinalMecSpeed01Hz, int16_t* pFinalTorque );
-
- /**
- * @brief It is used to execute a set Revup data command coming from the user.
- * @param this related object of class CUI.
- * @param bStage is the rev up phase, zero based, to be modified.
- * @param hDurationms is the new duration of the Revup stage.
- * @param hFinalMecSpeed01Hz is the new mechanical speed at the end of that
- * stage expressed in 0.1Hz.
- * @param hFinalTorque is the new value of motor torque at the end of that
- * stage. This value represents actually the Iq current expressed in
- * digit.
- * @retval bool It returns true if the command has been performed
- * succesfully otherwise returns false.
- */
- bool UI_SetRevupData(CUI this, uint8_t bStage, uint16_t hDurationms,
- int16_t hFinalMecSpeed01Hz, int16_t hFinalTorque );
-
- /**
- * @brief It is used to execute a set current reference command coming from
- * the user.
- * @param this related object of class CUI.
- * @param hIqRef is the current Iq reference on qd reference frame. This value
- * is expressed in digit. To convert current expressed in digit to
- * current expressed in Amps is possible to use the formula:
- * Current(Amp) = [Current(digit) * Vdd micro] / [65536 * Rshunt * Aop]
- * @param hIdRef is the current Id reference on qd reference frame. This value
- * is expressed in digit. See hIqRef param description.
- * @retval none.
- */
- void UI_SetCurrentReferences(CUI this, int16_t hIqRef, int16_t hIdRef);
-
- /**
- * @brief Hardware and software initialization of the DAC object. This is a
- * virtual function and is implemented by related object.
- * @param this related object of class UI. It must be a DACx_UI object casted
- * to CUI otherwise the DACInit method will have no effect.
- * @retval none.
- */
- void UI_DACInit(CUI this);
-
- /**
- * @brief This method is used to update the DAC outputs. The selected
- * variables will be provided in the related output channels. This is a
- * virtual function and is implemented by related object.
- * @param this related object of class UI. It must be a DACx_UI object casted
- * to CUI otherwise the DACInit method will have no effect.
- * @retval none.
- */
- void UI_DACExec(CUI this);
-
- /**
- * @brief This method is used to set up the DAC outputs. The selected
- * variables will be provided in the related output channels after next
- * DACExec. This is a virtual function and is implemented by related
- * object.
- * @param this related object of class UI. It must be a DACx_UI object casted
- * to CUI otherwise the DACInit method will have no effect.
- * @param bChannel the DAC channel to be programmed. It must be one of the
- * exported channels Ex. DAC_CH0.
- * @param bVariable the variables to be provided in out through the selected
- * channel. It must be one of the exported UI register Ex.
- * MC_PROTOCOL_REG_I_A.
- * @retval none.
- */
- void UI_SetDAC(CUI this, DAC_Channel_t bChannel,
- MC_Protocol_REG_t bVariable);
-
- /**
- * @brief This method is used to get the current DAC channel selected output.
- * @param this related object of class UI. It must be a DACx_UI object casted
- * to CUI otherwise the method will have no effect.
- * @param bChannel the inspected DAC channel. It must be one of the
- * exported channels (Ex. DAC_CH0).
- * @retval MC_Protocol_REG_t The variables provided in out through the inspected
- * channel. It will be one of the exported UI register (Ex.
- * MC_PROTOCOL_REG_I_A).
- */
- MC_Protocol_REG_t UI_GetDAC(CUI this, DAC_Channel_t bChannel);
-
- /**
- * @brief This method is used to set the value of the “User DAC channel”.
- * @param this related object of class UI. It must be a DACx_UI object casted
- * to CUI otherwise the DACInit method will have no effect.
- * @param bUserChNumber the “User DAC channel” to be programmed.
- * @param hValue the value to be put in output.
- * @retval none.
- */
- void UI_SetUserDAC(CUI this, DAC_UserChannel_t bUserChNumber, int16_t hValue);
-
- /**
- * @brief Initialization of LCD object. It must be called after the UI_Init.
- * @param this related object of class CUI. It must be a LCDx_UI object casted
- * to CUI otherwise the method will have no effect.
- * @param oDAC related DAC object upcasted to CUI. It can be MC_NULL.
- * @param s_fwVer String contating firmware version.
- * @retval none.
- */
- void UI_LCDInit(CUI this, CUI oDAC, const char* s_fwVer);
-
- /**
- * @brief Execute the LCD execution and refreshing. It must be called
- * periodically.
- * @param this related object of class CUI. It must be a LCDx_UI object casted
- * to CUI otherwise the method will have no effect.
- * @retval none.
- */
- void UI_LCDExec(CUI this);
-
- /**
- * @brief It is used to force a refresh of all LCD values.
- * @param this related object of class CUI. It must be a LCDx_UI object casted
- * to CUI otherwise the method will have no effect.
- * @retval none.
- */
- void UI_LCDUpdateAll(CUI this);
-
- /**
- * @brief It is used to force a refresh of only measured LCD values.
- * @param this related object of class CUI. It must be a LCDx_UI object casted
- * to CUI otherwise the method will have no effect.
- * @retval none.
- */
- 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, /*!< 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.
- */
基本的操作建立起来了,下一步就是具体的应用了。
</div>