(转)STM32 FOC SDK 上电电机自动旋转(https://blog.csdn.net/u0

2019-04-14 22:11发布

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 中,其中主要的函数有
  1. /**
  2. * @brief Creates an object of the class UserInterface
  3. * @param pUserInterfaceParams pointer to an UserInterface parameters structure
  4. * @retval CUI new instance of UserInterface object
  5. */
  6. CUI UI_NewObject(pUserInterfaceParams_t pUserInterfaceParams);
  7. /**
  8. * @brief Initialization of UI object. It perform the link between the UI
  9. * object and the MC interface and MC tuning objects. It must be called
  10. * before the derived class initialization.
  11. * @param this related object of class CUI.
  12. * @param bMCNum Is the total number of MC object presnet in the list.
  13. * @param pMCI is the pointer of the list of MC interface objects to be linked
  14. * with the UI.
  15. * @param pMCT is the pointer of the list of MC tuning objects to be linked
  16. * with the UI.
  17. * @param pUICfg is the pointer of the user interface configuration list. Each
  18. * element of the list must be a bit field containing one (or more) of
  19. * the exported configuration option UI_CFGOPT_xxx (eventually OR-ed).
  20. * @retval none.
  21. */
  22. void UI_Init(CUI this, uint8_t bMCNum, CMCI* pMCI, CMCT* pMCT, uint32_t* pUICfg);
  23. /**
  24. * @brief It is used to select the MC on which UI operates.
  25. * @param this related object of class CUI.
  26. * @param bSelectMC The new selected MC, zero based, on which UI operates.
  27. * @retval bool It return true if the bSelectMC is valid oterwise return false.
  28. */
  29. bool UI_SelectMC(CUI this,uint8_t bSelectMC);
  30. /**
  31. * @brief It is used to retrive the MC on which UI currently operates.
  32. * @param this related object of class CUI.
  33. * @retval uint8_t It returns the currently selected MC, zero based, on which
  34. * UI operates.
  35. */
  36. uint8_t UI_GetSelectedMC(CUI this);
  37. /**
  38. * @brief It is used to retrive the configuration of the MC on which UI
  39. * currently operates.
  40. * @param this related object of class CUI.
  41. * @retval uint32_t It returns the currently configuration of selected MC on
  42. * which UI operates.
  43. * It represents a bit field containing one (or more) of
  44. * the exported configuration option UI_CFGOPT_xxx (eventually OR-ed).
  45. */
  46. uint32_t UI_GetSelectedMCConfig(CUI this);
  47. /**
  48. * @brief It is used to execute a SetReg command coming from the user.
  49. * @param this related object of class CUI.
  50. * @param bRegID Code of register to be updated. Valid code is one of the
  51. * MC_PROTOCOL_REG_xxx values exported by UserInterfaceClass.
  52. * @param wValue is the new value to be set.
  53. * @retval bool It returns true if the SetReg command has been performed
  54. * succesfully otherwise returns false.
  55. */
  56. bool UI_SetReg(CUI this, MC_Protocol_REG_t bRegID, int32_t wValue);
  57. /**
  58. * @brief It is used to execute a GetReg command coming from the user.
  59. * @param this related object of class CUI.
  60. * @param bRegID Code of register to be updated. Valid code is one of the
  61. * MC_PROTOCOL_REG_xxx values exported by UserInterfaceClass.
  62. * @retval int32_t is the current value of register bRegID.
  63. */
  64. int32_t UI_GetReg(CUI this, MC_Protocol_REG_t bRegID);
  65. /**
  66. * @brief It is used to retrieve the current selected MC tuning object.
  67. * @param this related object of class CUI.
  68. * @retval CMCT It returns the currently selected MC tuning object on which
  69. * UI operates.
  70. */
  71. CMCT UI_GetCurrentMCT(CUI this);
  72. /**
  73. * @brief It is used to execute a command coming from the user.
  74. * @param this related object of class CUI.
  75. * @param bCmdID Code of register to be updated. Valid code is one of the
  76. * MC_PROTOCOL_CMD_xxx define exported by UserInterfaceClass.
  77. * @retval bool It returns true if the command has been performed
  78. * succesfully otherwise returns false.
  79. */
  80. bool UI_ExecCmd(CUI this, uint8_t bCmdID);
  81. /**
  82. * @brief It is used to execute a speed ramp command coming from the user.
  83. * @param this related object of class CUI.
  84. * @param wFinalMecSpeedRPM final speed value expressed in RPM.
  85. * @param hDurationms the duration of the ramp expressed in milliseconds. It
  86. * is possible to set 0 to perform an instantaneous change in the value.
  87. * @retval bool It returns true if the command has been performed
  88. * succesfully otherwise returns false.
  89. */
  90. bool UI_ExecSpeedRamp(CUI this, int32_t wFinalMecSpeedRPM, uint16_t hDurationms);
  91. /**
  92. * @brief It is used to execute a torque ramp command coming from the user.
  93. * @param this related object of class CUI.
  94. * @param hTargetFinal final torque value. See MCI interface for more
  95. details.
  96. * @param hDurationms the duration of the ramp expressed in milliseconds. It
  97. * is possible to set 0 to perform an instantaneous change in the value.
  98. * @retval bool It returns true if the command has been performed
  99. * succesfully otherwise returns false.
  100. */
  101. bool UI_ExecTorqueRamp(CUI this, int16_t hTargetFinal, uint16_t hDurationms);
  102. /**
  103. * @brief It is used to execute a get Revup data command coming from the user.
  104. * @param this related object of class CUI.
  105. * @param bStage is the rev up phase, zero based, to be read.
  106. * @param pDurationms is the pointer to an uint16_t variable used to retrieve
  107. * the duration of the Revup stage.
  108. * @param pFinalMecSpeed01Hz is the pointer to an int16_t variable used to
  109. * retrieve the mechanical speed at the end of that stage expressed in
  110. * 0.1Hz.
  111. * @param pFinalTorque is the pointer to an int16_t variable used to
  112. * retrieve the value of motor torque at the end of that
  113. * stage. This value represents actually the Iq current expressed in
  114. * digit.
  115. * @retval bool It returns true if the command has been performed
  116. * succesfully otherwise returns false.
  117. */
  118. bool UI_GetRevupData(CUI this, uint8_t bStage, uint16_t* pDurationms,
  119. int16_t* pFinalMecSpeed01Hz, int16_t* pFinalTorque );
  120. /**
  121. * @brief It is used to execute a set Revup data command coming from the user.
  122. * @param this related object of class CUI.
  123. * @param bStage is the rev up phase, zero based, to be modified.
  124. * @param hDurationms is the new duration of the Revup stage.
  125. * @param hFinalMecSpeed01Hz is the new mechanical speed at the end of that
  126. * stage expressed in 0.1Hz.
  127. * @param hFinalTorque is the new value of motor torque at the end of that
  128. * stage. This value represents actually the Iq current expressed in
  129. * digit.
  130. * @retval bool It returns true if the command has been performed
  131. * succesfully otherwise returns false.
  132. */
  133. bool UI_SetRevupData(CUI this, uint8_t bStage, uint16_t hDurationms,
  134. int16_t hFinalMecSpeed01Hz, int16_t hFinalTorque );
  135. /**
  136. * @brief It is used to execute a set current reference command coming from
  137. * the user.
  138. * @param this related object of class CUI.
  139. * @param hIqRef is the current Iq reference on qd reference frame. This value
  140. * is expressed in digit. To convert current expressed in digit to
  141. * current expressed in Amps is possible to use the formula:
  142. * Current(Amp) = [Current(digit) * Vdd micro] / [65536 * Rshunt * Aop]
  143. * @param hIdRef is the current Id reference on qd reference frame. This value
  144. * is expressed in digit. See hIqRef param description.
  145. * @retval none.
  146. */
  147. void UI_SetCurrentReferences(CUI this, int16_t hIqRef, int16_t hIdRef);
  148. /**
  149. * @brief Hardware and software initialization of the DAC object. This is a
  150. * virtual function and is implemented by related object.
  151. * @param this related object of class UI. It must be a DACx_UI object casted
  152. * to CUI otherwise the DACInit method will have no effect.
  153. * @retval none.
  154. */
  155. void UI_DACInit(CUI this);
  156. /**
  157. * @brief This method is used to update the DAC outputs. The selected
  158. * variables will be provided in the related output channels. This is a
  159. * virtual function and is implemented by related object.
  160. * @param this related object of class UI. It must be a DACx_UI object casted
  161. * to CUI otherwise the DACInit method will have no effect.
  162. * @retval none.
  163. */
  164. void UI_DACExec(CUI this);
  165. /**
  166. * @brief This method is used to set up the DAC outputs. The selected
  167. * variables will be provided in the related output channels after next
  168. * DACExec. This is a virtual function and is implemented by related
  169. * object.
  170. * @param this related object of class UI. It must be a DACx_UI object casted
  171. * to CUI otherwise the DACInit method will have no effect.
  172. * @param bChannel the DAC channel to be programmed. It must be one of the
  173. * exported channels Ex. DAC_CH0.
  174. * @param bVariable the variables to be provided in out through the selected
  175. * channel. It must be one of the exported UI register Ex.
  176. * MC_PROTOCOL_REG_I_A.
  177. * @retval none.
  178. */
  179. void UI_SetDAC(CUI this, DAC_Channel_t bChannel,
  180. MC_Protocol_REG_t bVariable);
  181. /**
  182. * @brief This method is used to get the current DAC channel selected output.
  183. * @param this related object of class UI. It must be a DACx_UI object casted
  184. * to CUI otherwise the method will have no effect.
  185. * @param bChannel the inspected DAC channel. It must be one of the
  186. * exported channels (Ex. DAC_CH0).
  187. * @retval MC_Protocol_REG_t The variables provided in out through the inspected
  188. * channel. It will be one of the exported UI register (Ex.
  189. * MC_PROTOCOL_REG_I_A).
  190. */
  191. MC_Protocol_REG_t UI_GetDAC(CUI this, DAC_Channel_t bChannel);
  192. /**
  193. * @brief This method is used to set the value of the “User DAC channel”.
  194. * @param this related object of class UI. It must be a DACx_UI object casted
  195. * to CUI otherwise the DACInit method will have no effect.
  196. * @param bUserChNumber the “User DAC channel” to be programmed.
  197. * @param hValue the value to be put in output.
  198. * @retval none.
  199. */
  200. void UI_SetUserDAC(CUI this, DAC_UserChannel_t bUserChNumber, int16_t hValue);
  201. /**
  202. * @brief Initialization of LCD object. It must be called after the UI_Init.
  203. * @param this related object of class CUI. It must be a LCDx_UI object casted
  204. * to CUI otherwise the method will have no effect.
  205. * @param oDAC related DAC object upcasted to CUI. It can be MC_NULL.
  206. * @param s_fwVer String contating firmware version.
  207. * @retval none.
  208. */
  209. void UI_LCDInit(CUI this, CUI oDAC, const char* s_fwVer);
  210. /**
  211. * @brief Execute the LCD execution and refreshing. It must be called
  212. * periodically.
  213. * @param this related object of class CUI. It must be a LCDx_UI object casted
  214. * to CUI otherwise the method will have no effect.
  215. * @retval none.
  216. */
  217. void UI_LCDExec(CUI this);
  218. /**
  219. * @brief It is used to force a refresh of all LCD values.
  220. * @param this related object of class CUI. It must be a LCDx_UI object casted
  221. * to CUI otherwise the method will have no effect.
  222. * @retval none.
  223. */
  224. void UI_LCDUpdateAll(CUI this);
  225. /**
  226. * @brief It is used to force a refresh of only measured LCD values.
  227. * @param this related object of class CUI. It must be a LCDx_UI object casted
  228. * to CUI otherwise the method will have no effect.
  229. * @retval none.
  230. */
  231. void UI_LCDUpdateMeasured(CUI this);
  232. /**
  233. * @}
  234. */
  235. /**
  236. * @}
  237. */
  238. /**
  239. * @}
  240. */


通过 bool UI_ExecCmd(CUI this, uint8_t bCmdID);来对电机的运行状态进行改变,新的状态可以为
  1. #define MC_PROTOCOL_CMD_START_MOTOR 0x01
  2. #define MC_PROTOCOL_CMD_STOP_MOTOR 0x02
  3. #define MC_PROTOCOL_CMD_STOP_RAMP 0x03
  4. #define MC_PROTOCOL_CMD_RESET 0x04
  5. #define MC_PROTOCOL_CMD_PING 0x05
  6. #define MC_PROTOCOL_CMD_START_STOP 0x06
  7. #define MC_PROTOCOL_CMD_FAULT_ACK 0x07
  8. #define MC_PROTOCOL_CMD_ENCODER_ALIGN 0x08
  9. #define MC_PROTOCOL_CMD_IQDREF_CLEAR 0x09
  10. #define MC_PROTOCOL_CMD_PFC_ENABLE 0x0A
  11. #define MC_PROTOCOL_CMD_PFC_DISABLE 0x0B
  12. #define MC_PROTOCOL_CMD_PFC_FAULT_ACK 0x0C
  13. #define MC_PROTOCOL_CMD_SC_START 0x0D
  14. #define MC_PROTOCOL_CMD_SC_STOP 0x0E


根据手册描述,在main函数里面添加以下代码即可
  1. oUI = UI_NewObject(MC_NULL);
  2. UI_Init(oUI, MC_NUM, oMCI, oMCT, MC_NULL);
  3. UI_SelectMC(oUI, 2);
  4. UI_ExecSpeedRamp(oUI, -2000, 0);
  5. UI_ExecCmd (oUI, MC_PROTOCOL_CMD_START_MOTOR);

对了,变量应该先声明后使用 CUI oUI;
如何读取电机的状态呢?读寄存器MC_PROTOCOL_REG_STATUS即可 或者用MCI_GetSTMState(oMCI) 返回的值可能是
  1. ICLWAIT = 12, /*!< Persistent state, the system is waiting for ICL
  2. deactivation. Is not possible to run the motor if
  3. ICL is active. Until the ICL is active the state is
  4. forced to ICLWAIT, when ICL become inactive the state
  5. is moved to IDLE */
  6. IDLE = 0, /*!< Persistent state, following state can be IDLE_START
  7. if a start motor command has been given or
  8. IDLE_ALIGNMENT if a start alignment command has been
  9. given */
  10. IDLE_ALIGNMENT = 1, /*!< "Pass-through" state containg the code to be executed
  11. only once after encoder alignment command.
  12. Next states can be ALIGN_CHARGE_BOOT_CAP or
  13. ALIGN_OFFSET_CALIB according the configuration. It
  14. can also be ANY_STOP if a stop motor command has been
  15. given. */
  16. ALIGN_CHARGE_BOOT_CAP = 13,/*!< Persistent state where the gate driver boot
  17. capacitors will be charged. Next states will be
  18. ALIGN_OFFSET_CALIB. It can also be ANY_STOP if a stop
  19. motor command has been given. */
  20. ALIGN_OFFSET_CALIB = 14,/*!< Persistent state where the offset of motor currents
  21. measurements will be calibrated. Next state will be
  22. ALIGN_CLEAR. It can also be ANY_STOP if a stop motor
  23. command has been given. */
  24. ALIGN_CLEAR = 15, /*!< "Pass-through" state in which object is cleared and
  25. set for the startup.
  26. Next state will be ALIGNMENT. It can also be ANY_STOP
  27. if a stop motor command has been given. */
  28. ALIGNMENT = 2, /*!< Persistent state in which the encoder are properly
  29. aligned to set mechanical angle, following state can
  30. only be ANY_STOP */
  31. IDLE_START = 3, /*!< "Pass-through" state containg the code to be executed
  32. only once after start motor command.
  33. Next states can be CHARGE_BOOT_CAP or OFFSET_CALIB
  34. according the configuration. It can also be ANY_STOP
  35. if a stop motor command has been given. */
  36. CHARGE_BOOT_CAP = 16, /*!< Persistent state where the gate driver boot
  37. capacitors will be charged. Next states will be
  38. OFFSET_CALIB. It can also be ANY_STOP if a stop motor
  39. command has been given. */
  40. OFFSET_CALIB = 17, /*!< Persistent state where the offset of motor currents
  41. measurements will be calibrated. Next state will be
  42. CLEAR. It can also be ANY_STOP if a stop motor
  43. command has been given. */
  44. CLEAR = 18, /*!< "Pass-through" state in which object is cleared and
  45. set for the startup.
  46. Next state will be START. It can also be ANY_STOP if
  47. a stop motor command has been given. */
  48. START = 4, /*!< Persistent state where the motor start-up is intended
  49. to be executed. The following state is normally
  50. START_RUN as soon as first validated speed is
  51. detected. Another possible following state is
  52. ANY_STOP if a stop motor command has been executed */
  53. START_RUN = 5, /*!< "Pass-through" state, the code to be executed only
  54. once between START and RUN states it’s intended to be
  55. here executed. Following state is normally RUN but
  56. it can also be ANY_STOP if a stop motor command has
  57. been given */
  58. RUN = 6, /*!< Persistent state with running motor. The following
  59. state is normally ANY_STOP when a stop motor command
  60. has been executed */
  61. ANY_STOP = 7, /*!< "Pass-through" state, the code to be executed only
  62. once between any state and STOP it’s intended to be
  63. here executed. Following state is normally STOP */
  64. STOP = 8, /*!< Persistent state. Following state is normally
  65. STOP_IDLE as soon as conditions for moving state
  66. machine are detected */
  67. STOP_IDLE = 9, /*!< "Pass-through" state, the code to be executed only
  68. once between STOP and IDLE it’s intended to be here
  69. executed. Following state is normally IDLE */
  70. FAULT_NOW = 10, /*!< Persistent state, the state machine can be moved from
  71. any condition directly to this state by
  72. STM_FaultProcessing method. This method also manage
  73. the passage to the only allowed following state that
  74. is FAULT_OVER */
  75. FAULT_OVER = 11 /*!< Persistent state where the application is intended to
  76. stay when the fault conditions disappeared. Following
  77. state is normally STOP_IDLE, state machine is moved as
  78. soon as the user has acknowledged the fault condition.
  79. */


基本的操作建立起来了,下一步就是具体的应用了。