mc_api.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /**
  2. ******************************************************************************
  3. * @file mc_api.c
  4. * @author Motor Control SDK Team, ST Microelectronics
  5. * @brief This file implements the high level interface of the Motor Control SDK.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. #include "mc_interface.h"
  20. #include "mc_api.h"
  21. #include "mc_config.h"
  22. /** @addtogroup MCSDK
  23. * @{
  24. */
  25. /** @defgroup MCIAPI Motor Control API
  26. *
  27. * @brief High level Programming Interface of the Motor Control SDK
  28. *
  29. * This interface allows for performing basic operations on the motor(s) driven by an
  30. * Motor Control SDK based application. With it, motors can be started and stopped, speed or
  31. * torque ramps can be programmed and executed and information on the state of the motors can
  32. * be retrieved, among others.
  33. *
  34. * This interface consists in functions that target a specific motor, indicated in their name.
  35. * These functions aims at being the main interface used by an Application to control motors.
  36. *
  37. * The current Motor Control API can cope with up to 2 motors.
  38. * @{
  39. */
  40. extern MCI_Handle_t * pMCI[NBR_OF_MOTORS];
  41. /**
  42. * @brief Initiates the start-up procedure for Motor 1
  43. *
  44. * If the state machine of Motor 1 is in #IDLE state, the command is immediately
  45. * executed. Otherwise the command is discarded. The Application can check the
  46. * return value to know whether the command was executed or discarded.
  47. *
  48. * One of the following commands must be executed before calling MC_StartMotor1():
  49. *
  50. * - MC_ProgramSpeedRampMotor1()
  51. * - MC_ProgramTorqueRampMotor1()
  52. * - MC_SetCurrentReferenceMotor1()
  53. *
  54. * Failing to do so results in an unpredictable behaviour.
  55. *
  56. * @note The MC_StartMotor1() command only triggers the start-up procedure:
  57. * It moves Motor 1's state machine from the #IDLE to the #IDLE_START state and then
  58. * returns. It is not blocking the application until the motor is indeed running.
  59. * To know if it is running, the application can query Motor 1's state machine and
  60. * check if it has reached the #RUN state. See MC_GetSTMStateMotor1() for more details.
  61. *
  62. * @retval returns true if the command is successfully executed, false otherwise.
  63. */
  64. __weak bool MC_StartMotor1(void)
  65. {
  66. return MCI_StartMotor( pMCI[M1] );
  67. }
  68. /**
  69. * @brief Initiates the stop procedure for Motor 1.
  70. *
  71. * If the state machine is in #RUN or #START states the command is immediately
  72. * executed. Otherwise, the command is discarded. The Application can check the
  73. * return value to know whether the command was executed or discarded.
  74. *
  75. * @note The MCI_StopMotor1() command only triggers the stop motor procedure
  76. * moving Motor 1's state machine to #ANY_STOP and then returns. It is not
  77. * blocking the application until the motor is indeed stopped. To know if it has
  78. * stopped, the application can query Motor 1's state machine ans check if the
  79. * #IDLE state has been reached back.
  80. *
  81. * @retval returns true if the command is successfully executed, false otherwise.
  82. */
  83. __weak bool MC_StopMotor1(void)
  84. {
  85. return MCI_StopMotor( pMCI[M1] );
  86. }
  87. /**
  88. * @brief Programs a speed ramp for Motor 1 for later or immediate execution.
  89. *
  90. * A speed ramp is a linear change from the current speed reference to the @p hFinalSpeed
  91. * target speed in the given @p hDurationms time.
  92. *
  93. * Invoking the MC_ProgramSpeedRampMotor1() function programs a new speed ramp
  94. * with the provided parameters. The programmed ramp is executed immediately if
  95. * Motor 1's state machine is in the #START_RUN or #RUN states. Otherwise, the
  96. * ramp is buffered and will be executed when the state machine reaches any of
  97. * the aforementioned state.
  98. *
  99. * The Application can check the status of the command with the MC_GetCommandStateMotor1()
  100. * to know whether the last command was executed immediately or not.
  101. *
  102. * Only one command can be buffered at any given time. If another ramp - whether a
  103. * speed or a torque one - or if another buffered command is programmed before the
  104. * current one has completed, the latter replaces the former.
  105. *
  106. * @note A ramp cannot reverse the rotation direction if the Application is using
  107. * sensorless motor control techniques. If the sign of the hFinalSpeed parameter
  108. * differs from that of the current speed, the ramp will not complete and a Speed
  109. * Feedback error (#MC_SPEED_FDBK) will occur when the rotation speed is about to
  110. * reach 0 rpm.
  111. *
  112. * @param hFinalSpeed Mechanical rotor speed reference at the end of the ramp.
  113. * Expressed in the unit defined by #SPEED_UNIT.
  114. * @param hDurationms Duration of the ramp expressed in milliseconds. It
  115. * is possible to set 0 to perform an instantaneous change in the speed
  116. * value.
  117. */
  118. __weak void MC_ProgramSpeedRampMotor1( int16_t hFinalSpeed, uint16_t hDurationms )
  119. {
  120. MCI_ExecSpeedRamp( pMCI[M1], hFinalSpeed, hDurationms );
  121. }
  122. /**
  123. * @brief Programs a torque ramp for Motor 1 for later or immediate execution.
  124. *
  125. * A torque ramp is a linear change from the current torque reference to the @p hFinalTorque
  126. * target torque reference in the given @p hDurationms time.
  127. *
  128. * Invoking the MC_ProgramTorqueRampMotor1() function programs a new torque ramp
  129. * with the provided parameters. The programmed ramp is executed immediately if
  130. * Motor 1's state machine is in the #START_RUN or #RUN states. Otherwise, the
  131. * ramp is buffered and will be executed when the state machine reaches any of
  132. * the aforementioned state.
  133. *
  134. * The Application can check the status of the command with the MC_GetCommandStateMotor1()
  135. * to know whether the last command was executed immediately or not.
  136. *
  137. * Only one command can be buffered at any given time. If another ramp - whether a
  138. * torque or a speed one - or if another buffered command is programmed before the
  139. * current one has completed, the latter replaces the former.
  140. *
  141. * @note A ramp cannot reverse the rotation direction if the Application is using
  142. * sensorless motor control techniques. If the sign of the hFinalTorque parameter
  143. * differs from that of the current torque, the ramp will not complete and a Speed
  144. * Feedback error (#MC_SPEED_FDBK) will occur when the rotation speed is about to
  145. * reach 0 rpm.
  146. *
  147. * @param hFinalTorque Mechanical motor torque reference at the end of the ramp.
  148. * This value represents actually the Iq current expressed in digit.
  149. * @param hDurationms Duration of the ramp expressed in milliseconds. It
  150. * is possible to set 0 to perform an instantaneous change in the torque
  151. * value.
  152. */
  153. __weak void MC_ProgramTorqueRampMotor1( int16_t hFinalTorque, uint16_t hDurationms )
  154. {
  155. MCI_ExecTorqueRamp( pMCI[M1], hFinalTorque, hDurationms );
  156. }
  157. /**
  158. * @brief Programs the current reference to Motor 1 for later or immediate execution.
  159. *
  160. * The current reference to consider is made of the Id and Iq current components.
  161. *
  162. * Invoking the MC_SetCurrentReferenceMotor1() function programs a current reference
  163. * with the provided parameters. The programmed reference is executed immediately if
  164. * Motor 1's state machine is in the #START_RUN or #RUN states. Otherwise, the
  165. * command is buffered and will be executed when the state machine reaches any of
  166. * the aforementioned state.
  167. *
  168. * The Application can check the status of the command with the MC_GetCommandStateMotor1()
  169. * to know whether the last command was executed immediately or not.
  170. *
  171. * Only one command can be buffered at any given time. If another buffered command is
  172. * programmed before the current one has completed, the latter replaces the former.
  173. *
  174. * @param Iqdref current reference in the Direct-Quadratic reference frame. Expressed
  175. * in the qd_t format.
  176. */
  177. __weak void MC_SetCurrentReferenceMotor1( qd_t Iqdref )
  178. {
  179. MCI_SetCurrentReferences( pMCI[M1], Iqdref );
  180. }
  181. /**
  182. * @brief Returns the status of the last buffered command for Motor 1.
  183. * The status can be one of the following values:
  184. * - #MCI_BUFFER_EMPTY: no buffered command is currently programmed.
  185. * - #MCI_COMMAND_NOT_ALREADY_EXECUTED: A command has been buffered but the conditions for its
  186. * execution have not occurred yet. The command is still in the buffer, pending execution.
  187. * - #MCI_COMMAND_EXECUTED_SUCCESFULLY: the last buffered command has been executed successfully.
  188. * In this case calling this function reset the command state to #BC_BUFFER_EMPTY.
  189. * - #MCI_COMMAND_EXECUTED_UNSUCCESFULLY: the buffered command has been executed unsuccessfully.
  190. * In this case calling this function reset the command state to #BC_BUFFER_EMPTY.
  191. */
  192. __weak MCI_CommandState_t MC_GetCommandStateMotor1( void)
  193. {
  194. return MCI_IsCommandAcknowledged( pMCI[M1] );
  195. }
  196. /**
  197. * @brief Stops the execution of the on-going speed ramp for Motor 1, if any.
  198. *
  199. * If a speed ramp is currently being executed, it is immediately stopped, the rotation
  200. * speed of Motor 1 is maintained to its current value and true is returned. If no speed
  201. * ramp is on-going, nothing is done and false is returned.
  202. */
  203. __weak bool MC_StopSpeedRampMotor1(void)
  204. {
  205. return MCI_StopSpeedRamp( pMCI[M1] );
  206. }
  207. /**
  208. * @brief Stops the execution of the on-going ramp for Motor 1, if any.
  209. *
  210. * If a ramp is currently being executed, it is immediately stopped, the torque or the speed
  211. * of Motor 1 is maintained to its current value.
  212. */
  213. __weak void MC_StopRampMotor1(void)
  214. {
  215. MCI_StopRamp( pMCI[M1] );
  216. }
  217. /**
  218. * @brief Returns true if the last ramp submited for Motor 1 has completed, false otherwise
  219. */
  220. __weak bool MC_HasRampCompletedMotor1(void)
  221. {
  222. return MCI_RampCompleted( pMCI[M1] );
  223. }
  224. /**
  225. * @brief Returns the current mechanical rotor speed reference set for Motor 1, expressed in the unit defined by #SPEED_UNIT
  226. */
  227. __weak int16_t MC_GetMecSpeedReferenceMotor1(void)
  228. {
  229. return MCI_GetMecSpeedRefUnit( pMCI[M1] );
  230. }
  231. /**
  232. * @brief Returns the last computed average mechanical rotor speed for Motor 1, expressed in the unit defined by #SPEED_UNIT
  233. */
  234. __weak int16_t MC_GetMecSpeedAverageMotor1(void)
  235. {
  236. return MCI_GetAvrgMecSpeedUnit( pMCI[M1] );
  237. }
  238. /**
  239. * @brief Returns the final speed of the last ramp programmed for Motor 1 if this ramp was a speed ramp, 0 otherwise.
  240. */
  241. __weak int16_t MC_GetLastRampFinalSpeedMotor1(void)
  242. {
  243. return MCI_GetLastRampFinalSpeed( pMCI[M1] );
  244. }
  245. /**
  246. * @brief Returns the Control Mode used for Motor 1 (either Speed or Torque)
  247. */
  248. __weak STC_Modality_t MC_GetControlModeMotor1(void)
  249. {
  250. return MCI_GetControlMode( pMCI[M1] );
  251. }
  252. /**
  253. * @brief Returns the rotation direction imposed by the last command on Motor 1
  254. *
  255. * The last command is either MC_ProgramSpeedRampMotor1(), MC_ProgramTorqueRampMotor1() or
  256. * MC_SetCurrentReferenceMotor1().
  257. *
  258. * The function returns -1 if the sign of the final speed, the final torque or the Iq current
  259. * reference component of the last command is negative. Otherwise, 1 is returned.
  260. *
  261. * @note if no such command has ever been submitted, 1 is returned as well.
  262. */
  263. __weak int16_t MC_GetImposedDirectionMotor1(void)
  264. {
  265. return MCI_GetImposedMotorDirection( pMCI[M1] );
  266. }
  267. /**
  268. * @brief Returns true if the speed sensor used for Motor 1 is reliable, false otherwise
  269. */
  270. __weak bool MC_GetSpeedSensorReliabilityMotor1(void)
  271. {
  272. return MCI_GetSpdSensorReliability( pMCI[M1] );
  273. }
  274. /**
  275. * @brief returns the amplitude of the phase current injected in Motor 1
  276. *
  277. * The returned amplitude (0-to-peak) is expressed in s16A unit. To convert it to amperes, use the following formula:
  278. *
  279. * @f[
  280. * I_{Amps} = \frac{ I_{s16A} \times V_{dd}}{ 65536 \times R_{shunt} \times A_{op} }
  281. * @f]
  282. *
  283. */
  284. __weak int16_t MC_GetPhaseCurrentAmplitudeMotor1(void)
  285. {
  286. return MCI_GetPhaseCurrentAmplitude( pMCI[M1] );
  287. }
  288. /**
  289. * @brief returns the amplitude of the phase voltage applied to Motor 1
  290. *
  291. * The returned amplitude (0-to-peak) is expressed in s16V unit. To convert it to volts, use the following formula:
  292. *
  293. * @f[
  294. * U_{Volts} = \frac{ U_{s16V} \times V_{bus}}{ \sqrt{3} \times 32768 }
  295. * @f]
  296. *
  297. */
  298. __weak int16_t MC_GetPhaseVoltageAmplitudeMotor1(void)
  299. {
  300. return MCI_GetPhaseVoltageAmplitude( pMCI[M1] );
  301. }
  302. /**
  303. * @brief returns Ia and Ib current values for Motor 1 in ab_t format
  304. */
  305. __weak ab_t MC_GetIabMotor1(void)
  306. {
  307. return MCI_GetIab( pMCI[M1] );
  308. }
  309. /**
  310. * @brief returns Ialpha and Ibeta current values for Motor 1 in alphabeta_t format
  311. */
  312. __weak alphabeta_t MC_GetIalphabetaMotor1(void)
  313. {
  314. return MCI_GetIalphabeta( pMCI[M1] );
  315. }
  316. /**
  317. * @brief returns Iq and Id current values for Motor 1 in qd_t format
  318. */
  319. __weak qd_t MC_GetIqdMotor1(void)
  320. {
  321. return MCI_GetIqd( pMCI[M1] );
  322. }
  323. /**
  324. * @brief returns Iq and Id reference current values for Motor 1 in qd_t format
  325. */
  326. __weak qd_t MC_GetIqdrefMotor1(void)
  327. {
  328. return MCI_GetIqdref( pMCI[M1] );
  329. }
  330. /**
  331. * @brief returns Vq and Vd voltage values for Motor 1 in qd_t format
  332. */
  333. __weak qd_t MC_GetVqdMotor1(void)
  334. {
  335. return MCI_GetVqd( pMCI[M1] );
  336. }
  337. /**
  338. * @brief returns Valpha and Vbeta voltage values for Motor 1 in alphabeta_t format
  339. */
  340. __weak alphabeta_t MC_GetValphabetaMotor1(void)
  341. {
  342. return MCI_GetValphabeta( pMCI[M1] );
  343. }
  344. /**
  345. * @brief returns the electrical angle of the rotor of Motor 1, in DDP format
  346. */
  347. __weak int16_t MC_GetElAngledppMotor1(void)
  348. {
  349. return MCI_GetElAngledpp( pMCI[M1] );
  350. }
  351. /**
  352. * @brief returns the electrical torque reference for Motor 1
  353. */
  354. __weak int16_t MC_GetTerefMotor1(void)
  355. {
  356. return MCI_GetTeref( pMCI[M1] );
  357. }
  358. /**
  359. * @brief Sets Id reference value for Motor 2
  360. *
  361. * When the current reference drive is internal (the FOCVars_t.bDriveInput field is set to #INTERNAL),
  362. * Idref is normally managed by the FOC_CalcCurrRef() function. Neverthless, this function allows to
  363. * force a change in Idref value.
  364. *
  365. * Calling this function has no effect when either flux weakening region is entered or MTPA is enabled.
  366. */
  367. __weak void MC_SetIdrefMotor1( int16_t hNewIdref )
  368. {
  369. MCI_SetIdref( pMCI[M1], hNewIdref );
  370. }
  371. /**
  372. * @brief re-initializes Iq and Id references to their default values for Motor 1
  373. *
  374. * The default values for the Iq and Id references are comming from the Speed
  375. * or the Torque controller depending on the control mode.
  376. *
  377. * @see SpeednTorqCtrl for more details.
  378. */
  379. __weak void MC_Clear_IqdrefMotor1(void)
  380. {
  381. MCI_Clear_Iqdref( pMCI[M1] );
  382. }
  383. /**
  384. * @brief Acknowledge a Motor Control fault that occured on Motor 1
  385. *
  386. * This function informs Motor 1's state machine that the Application has taken
  387. * the error condition that occured into account. If no error condition exists when
  388. * the function is called, nothing is done and false is returned. Otherwise, true is
  389. * returned.
  390. */
  391. __weak bool MC_AcknowledgeFaultMotor1( void )
  392. {
  393. return MCI_FaultAcknowledged( pMCI[M1] );
  394. }
  395. /**
  396. * @brief Returns a bitfiled showing "new" faults that occured on Motor 1
  397. *
  398. * This function returns a 16 bit fields containing the Motor Control faults
  399. * that have occurred on Motor 1 since its state machine moved to the #FAULT_NOW state.
  400. *
  401. * See \link Fault_generation_error_codes Motor Control Faults\endlink for a list of
  402. * of all possible faults codes.
  403. */
  404. __weak uint16_t MC_GetOccurredFaultsMotor1(void)
  405. {
  406. return MCI_GetOccurredFaults( pMCI[M1] );
  407. }
  408. /**
  409. * @brief returns a bitfield showing all current faults on Motor 1
  410. *
  411. * This function returns a 16 bit fields containing the Motor Control faults
  412. * that are currently active.
  413. *
  414. * See \link Fault_generation_error_codes Motor Control Faults\endlink for a list of
  415. * of all possible faults codes.
  416. */
  417. __weak uint16_t MC_GetCurrentFaultsMotor1(void)
  418. {
  419. return MCI_GetCurrentFaults( pMCI[M1] );
  420. }
  421. /**
  422. * @brief returns the current state of Motor 1 state machine
  423. */
  424. __weak State_t MC_GetSTMStateMotor1(void)
  425. {
  426. return MCI_GetSTMState( pMCI[M1] );
  427. }