mc_interface.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /**
  2. ******************************************************************************
  3. * @file mc_interface.c
  4. * @author Motor Control SDK Team, ST Microelectronics
  5. * @brief This file provides firmware functions that implement the features
  6. * of the MC Interface component of the Motor Control SDK:
  7. *
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  12. * All rights reserved.</center></h2>
  13. *
  14. * This software component is licensed by ST under Ultimate Liberty license
  15. * SLA0044, the "License"; You may not use this file except in compliance with
  16. * the License. You may obtain a copy of the License at:
  17. * www.st.com/SLA0044
  18. *
  19. ******************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "mc_math.h"
  23. #include "speed_torq_ctrl.h"
  24. #include "mc_interface.h"
  25. /** @addtogroup MCSDK
  26. * @{
  27. */
  28. /** @defgroup MCInterface Motor Control Interface
  29. * @brief MC Interface component of the Motor Control SDK
  30. *
  31. * @todo Document the MC Interface "module".
  32. *
  33. * @{
  34. */
  35. /* Private macros ------------------------------------------------------------*/
  36. /**
  37. * @brief This macro converts the exported enum from the state machine to the corresponding bit field.
  38. */
  39. #define BC(state) (1u<<((uint16_t)((uint8_t)(state))))
  40. /* Functions -----------------------------------------------*/
  41. /**
  42. * @brief Initializes all the object variables, usually it has to be called
  43. * once right after object creation. It is also used to assign the
  44. * state machine object, the speed and torque controller, and the FOC
  45. * drive object to be used by MC Interface.
  46. * @param pHandle pointer on the component instance to initialize.
  47. * @param pSTM the state machine object used by the MCI.
  48. * @param pSTC the speed and torque controller used by the MCI.
  49. * @param pFOCVars pointer to FOC vars to be used by MCI.
  50. * @retval none.
  51. */
  52. __weak void MCI_Init( MCI_Handle_t * pHandle, STM_Handle_t * pSTM, SpeednTorqCtrl_Handle_t * pSTC, pFOCVars_t pFOCVars )
  53. {
  54. pHandle->pSTM = pSTM;
  55. pHandle->pSTC = pSTC;
  56. pHandle->pFOCVars = pFOCVars;
  57. /* Buffer related initialization */
  58. pHandle->lastCommand = MCI_NOCOMMANDSYET;
  59. pHandle->hFinalSpeed = 0;
  60. pHandle->hFinalTorque = 0;
  61. pHandle->hDurationms = 0;
  62. pHandle->CommandState = MCI_BUFFER_EMPTY;
  63. }
  64. /**
  65. * @brief This is a buffered command to set a motor speed ramp. This commands
  66. * don't become active as soon as it is called but it will be executed
  67. * when the pSTM state is START_RUN or RUN. User can check the status
  68. * of the command calling the MCI_IsCommandAcknowledged method.
  69. * @param pHandle Pointer on the component instance to operate on.
  70. * @param hFinalSpeed is the value of mechanical rotor speed reference at the
  71. * end of the ramp expressed in tenths of HZ.
  72. * @param hDurationms the duration of the ramp expressed in milliseconds. It
  73. * is possible to set 0 to perform an instantaneous change in the
  74. * value.
  75. * @retval none.
  76. */
  77. __weak void MCI_ExecSpeedRamp( MCI_Handle_t * pHandle, int16_t hFinalSpeed, uint16_t hDurationms )
  78. {
  79. pHandle->lastCommand = MCI_EXECSPEEDRAMP;
  80. pHandle->hFinalSpeed = hFinalSpeed;
  81. pHandle->hDurationms = hDurationms;
  82. pHandle->CommandState = MCI_COMMAND_NOT_ALREADY_EXECUTED;
  83. pHandle->LastModalitySetByUser = STC_SPEED_MODE;
  84. }
  85. /**
  86. * @brief This is a buffered command to set a motor torque ramp. This commands
  87. * don't become active as soon as it is called but it will be executed
  88. * when the pSTM state is START_RUN or RUN. User can check the status
  89. * of the command calling the MCI_IsCommandAcknowledged method.
  90. * @param pHandle Pointer on the component instance to work on.
  91. * @param hFinalTorque is the value of motor torque reference at the end of
  92. * the ramp. This value represents actually the Iq current expressed in
  93. * digit.
  94. * To convert current expressed in Amps to current expressed in digit
  95. * is possible to use the formula:
  96. * Current (digit) = [Current(Amp) * 65536 * Rshunt * Aop] / Vdd micro.
  97. * @param hDurationms the duration of the ramp expressed in milliseconds. It
  98. * is possible to set 0 to perform an instantaneous change in the
  99. * value.
  100. * @retval none.
  101. */
  102. __weak void MCI_ExecTorqueRamp( MCI_Handle_t * pHandle, int16_t hFinalTorque, uint16_t hDurationms )
  103. {
  104. pHandle->lastCommand = MCI_EXECTORQUERAMP;
  105. pHandle->hFinalTorque = hFinalTorque;
  106. pHandle->hDurationms = hDurationms;
  107. pHandle->CommandState = MCI_COMMAND_NOT_ALREADY_EXECUTED;
  108. pHandle->LastModalitySetByUser = STC_TORQUE_MODE;
  109. }
  110. /**
  111. * @brief This is a buffered command to set directly the motor current
  112. * references Iq and Id. This commands don't become active as soon as
  113. * it is called but it will be executed when the pSTM state is
  114. * START_RUN or RUN. User can check the status of the command calling
  115. * the MCI_IsCommandAcknowledged method.
  116. * @param pHandle Pointer on the component instance to work on.
  117. * @param Iqdref current references on qd reference frame in qd_t
  118. * format.
  119. * @retval none.
  120. */
  121. __weak void MCI_SetCurrentReferences( MCI_Handle_t * pHandle, qd_t Iqdref )
  122. {
  123. pHandle->lastCommand = MCI_SETCURRENTREFERENCES;
  124. pHandle->Iqdref.q = Iqdref.q;
  125. pHandle->Iqdref.d = Iqdref.d;
  126. pHandle->CommandState = MCI_COMMAND_NOT_ALREADY_EXECUTED;
  127. pHandle->LastModalitySetByUser = STC_TORQUE_MODE;
  128. }
  129. /**
  130. * @brief This is a user command used to begin the start-up procedure.
  131. * If the state machine is in IDLE state the command is executed
  132. * instantaneously otherwise the command is discarded. User must take
  133. * care of this possibility by checking the return value.
  134. * Before calling MCI_StartMotor it is mandatory to execute one of
  135. * these commands:\n
  136. * MCI_ExecSpeedRamp\n
  137. * MCI_ExecTorqueRamp\n
  138. * MCI_SetCurrentReferences\n
  139. * Otherwise the behaviour in run state will be unpredictable.\n
  140. * <B>Note:</B> The MCI_StartMotor command is used just to begin the
  141. * start-up procedure moving the state machine from IDLE state to
  142. * IDLE_START. The command MCI_StartMotor is not blocking the execution
  143. * of project until the motor is really running; to do this, the user
  144. * have to check the state machine and verify that the RUN state (or
  145. * any other state) has been reached.
  146. * @param pHandle Pointer on the component instance to work on.
  147. * @retval bool It returns true if the command is successfully executed
  148. * otherwise it return false.
  149. */
  150. __weak bool MCI_StartMotor( MCI_Handle_t * pHandle )
  151. {
  152. bool RetVal = STM_NextState( pHandle->pSTM, IDLE_START );
  153. if ( RetVal == true )
  154. {
  155. pHandle->CommandState = MCI_COMMAND_NOT_ALREADY_EXECUTED;
  156. }
  157. return RetVal;
  158. }
  159. /**
  160. * @brief This is a user command used to begin the stop motor procedure.
  161. * If the state machine is in RUN or START states the command is
  162. * executed instantaneously otherwise the command is discarded. User
  163. * must take care of this possibility by checking the return value.\n
  164. * <B>Note:</B> The MCI_StopMotor command is used just to begin the
  165. * stop motor procedure moving the state machine to ANY_STOP.
  166. * The command MCI_StopMotor is not blocking the execution of project
  167. * until the motor is really stopped; to do this, the user have to
  168. * check the state machine and verify that the IDLE state has been
  169. * reached again.
  170. * @param pHandle Pointer on the component instance to work on.
  171. * @retval bool It returns true if the command is successfully executed
  172. * otherwise it return false.
  173. */
  174. __weak bool MCI_StopMotor( MCI_Handle_t * pHandle )
  175. {
  176. return STM_NextState( pHandle->pSTM, ANY_STOP );
  177. }
  178. /**
  179. * @brief This is a user command used to indicate that the user has seen the
  180. * error condition. If is possible, the command is executed
  181. * instantaneously otherwise the command is discarded. User must take
  182. * care of this possibility by checking the return value.
  183. * @param pHandle Pointer on the component instance to work on.
  184. * @retval bool It returns true if the command is successfully executed
  185. * otherwise it return false.
  186. */
  187. __weak bool MCI_FaultAcknowledged( MCI_Handle_t * pHandle )
  188. {
  189. return STM_FaultAcknowledged( pHandle->pSTM );
  190. }
  191. /**
  192. * @brief This is a user command used to begin the encoder alignment procedure.
  193. * If the state machine is in IDLE state the command is executed
  194. * instantaneously otherwise the command is discarded. User must take
  195. * care of this possibility by checking the return value.\n
  196. * <B>Note:</B> The MCI_EncoderAlign command is used just to begin the
  197. * encoder alignment procedure moving the state machine from IDLE state
  198. * to IDLE_ALIGNMENT. The command MCI_EncoderAlign is not blocking the
  199. * execution of project until the encoder is really calibrated; to do
  200. * this, the user have to check the state machine and verify that the
  201. * IDLE state has been reached again.
  202. * @param pHandle Pointer on the component instance to work on.
  203. * @retval bool It returns true if the command is successfully executed
  204. * otherwise it return false.
  205. */
  206. __weak bool MCI_EncoderAlign( MCI_Handle_t * pHandle )
  207. {
  208. return STM_NextState( pHandle->pSTM, IDLE_ALIGNMENT );
  209. }
  210. /**
  211. * @brief This is usually a method managed by task. It must be called
  212. * periodically in order to check the status of the related pSTM object
  213. * and eventually to execute the buffered command if the condition
  214. * occurs.
  215. * @param pHandle Pointer on the component instance to work on.
  216. * @retval none.
  217. */
  218. __weak void MCI_ExecBufferedCommands( MCI_Handle_t * pHandle )
  219. {
  220. if ( pHandle != MC_NULL )
  221. {
  222. if ( pHandle->CommandState == MCI_COMMAND_NOT_ALREADY_EXECUTED )
  223. {
  224. bool commandHasBeenExecuted = false;
  225. switch ( pHandle->lastCommand )
  226. {
  227. case MCI_EXECSPEEDRAMP:
  228. {
  229. pHandle->pFOCVars->bDriveInput = INTERNAL;
  230. STC_SetControlMode( pHandle->pSTC, STC_SPEED_MODE );
  231. commandHasBeenExecuted = STC_ExecRamp( pHandle->pSTC, pHandle->hFinalSpeed, pHandle->hDurationms );
  232. }
  233. break;
  234. case MCI_EXECTORQUERAMP:
  235. {
  236. pHandle->pFOCVars->bDriveInput = INTERNAL;
  237. STC_SetControlMode( pHandle->pSTC, STC_TORQUE_MODE );
  238. commandHasBeenExecuted = STC_ExecRamp( pHandle->pSTC, pHandle->hFinalTorque, pHandle->hDurationms );
  239. }
  240. break;
  241. case MCI_SETCURRENTREFERENCES:
  242. {
  243. pHandle->pFOCVars->bDriveInput = EXTERNAL;
  244. pHandle->pFOCVars->Iqdref = pHandle->Iqdref;
  245. commandHasBeenExecuted = true;
  246. }
  247. break;
  248. default:
  249. break;
  250. }
  251. if ( commandHasBeenExecuted )
  252. {
  253. pHandle->CommandState = MCI_COMMAND_EXECUTED_SUCCESFULLY;
  254. }
  255. else
  256. {
  257. pHandle->CommandState = MCI_COMMAND_EXECUTED_UNSUCCESFULLY;
  258. }
  259. }
  260. }
  261. }
  262. /**
  263. * @brief It returns information about the state of the last buffered command.
  264. * @param pHandle Pointer on the component instance to work on.
  265. * @retval CommandState_t It can be one of the following codes:
  266. * - MCI_BUFFER_EMPTY if no buffered command has been called.
  267. * - MCI_COMMAND_NOT_ALREADY_EXECUTED if the buffered command
  268. * condition hasn't already occurred.
  269. * - MCI_COMMAND_EXECUTED_SUCCESFULLY if the buffered command has
  270. * been executed successfully. In this case calling this function reset
  271. * the command state to BC_BUFFER_EMPTY.
  272. * - MCI_COMMAND_EXECUTED_UNSUCCESFULLY if the buffered command has
  273. * been executed unsuccessfully. In this case calling this function
  274. * reset the command state to BC_BUFFER_EMPTY.
  275. */
  276. __weak MCI_CommandState_t MCI_IsCommandAcknowledged( MCI_Handle_t * pHandle )
  277. {
  278. MCI_CommandState_t retVal = pHandle->CommandState;
  279. if ( ( retVal == MCI_COMMAND_EXECUTED_SUCCESFULLY ) |
  280. ( retVal == MCI_COMMAND_EXECUTED_UNSUCCESFULLY ) )
  281. {
  282. pHandle->CommandState = MCI_BUFFER_EMPTY;
  283. }
  284. return retVal;
  285. }
  286. /**
  287. * @brief It returns information about the state of the related pSTM object.
  288. * @param pHandle Pointer on the component instance to work on.
  289. * @retval State_t It returns the current state of the related pSTM object.
  290. */
  291. __weak State_t MCI_GetSTMState( MCI_Handle_t * pHandle )
  292. {
  293. return STM_GetState( pHandle->pSTM );
  294. }
  295. /**
  296. * @brief It returns a 16 bit fields containing information about faults
  297. * historically occurred since the state machine has been moved into
  298. * FAULT_NOW state.
  299. * \n\link Fault_generation_error_codes Returned error codes are listed here \endlink
  300. * @param pHandle Pointer on the component instance to work on.
  301. * @retval uint16_t 16 bit fields with information about the faults
  302. * historically occurred since the state machine has been moved into
  303. * FAULT_NOW state.
  304. * \n\link Fault_generation_error_codes Returned error codes are listed here \endlink
  305. */
  306. __weak uint16_t MCI_GetOccurredFaults( MCI_Handle_t * pHandle )
  307. {
  308. return ( uint16_t )( STM_GetFaultState( pHandle->pSTM ) );
  309. }
  310. /**
  311. * @brief It returns a 16 bit fields containing information about faults
  312. * currently present.
  313. * \n\link Fault_generation_error_codes Returned error codes are listed here \endlink
  314. * @param pHandle Pointer on the component instance to work on.
  315. * @retval uint16_t 16 bit fields with information about about currently
  316. * present faults.
  317. * \n\link Fault_generation_error_codes Returned error codes are listed here \endlink
  318. */
  319. __weak uint16_t MCI_GetCurrentFaults( MCI_Handle_t * pHandle )
  320. {
  321. return ( uint16_t )( STM_GetFaultState( pHandle->pSTM ) >> 16 );
  322. }
  323. /**
  324. * @brief It returns the modality of the speed and torque controller.
  325. * @param pHandle Pointer on the component instance to work on.
  326. * @retval STC_Modality_t It returns the modality of STC. It can be one of
  327. * these two values: STC_TORQUE_MODE or STC_SPEED_MODE.
  328. */
  329. __weak STC_Modality_t MCI_GetControlMode( MCI_Handle_t * pHandle )
  330. {
  331. return pHandle->LastModalitySetByUser;
  332. }
  333. /**
  334. * @brief It returns the motor direction imposed by the last command
  335. * (MCI_ExecSpeedRamp, MCI_ExecTorqueRamp or MCI_SetCurrentReferences).
  336. * @param pHandle Pointer on the component instance to work on.
  337. * @retval int16_t It returns 1 or -1 according the sign of hFinalSpeed,
  338. * hFinalTorque or Iqdref.q of the last command.
  339. */
  340. __weak int16_t MCI_GetImposedMotorDirection( MCI_Handle_t * pHandle )
  341. {
  342. int16_t retVal = 1;
  343. switch ( pHandle->lastCommand )
  344. {
  345. case MCI_EXECSPEEDRAMP:
  346. if ( pHandle->hFinalSpeed < 0 )
  347. {
  348. retVal = -1;
  349. }
  350. break;
  351. case MCI_EXECTORQUERAMP:
  352. if ( pHandle->hFinalTorque < 0 )
  353. {
  354. retVal = -1;
  355. }
  356. break;
  357. case MCI_SETCURRENTREFERENCES:
  358. if ( pHandle->Iqdref.q < 0 )
  359. {
  360. retVal = -1;
  361. }
  362. break;
  363. default:
  364. break;
  365. }
  366. return retVal;
  367. }
  368. /**
  369. * @brief It returns information about the last ramp final speed sent by the
  370. * user expressed in tenths of HZ.
  371. * @param pHandle Pointer on the component instance to work on.
  372. * @retval int16_t last ramp final speed sent by the user expressed in tehts
  373. * of HZ.
  374. */
  375. __weak int16_t MCI_GetLastRampFinalSpeed( MCI_Handle_t * pHandle )
  376. {
  377. int16_t hRetVal = 0;
  378. /* Examine the last buffered commands */
  379. if ( pHandle->lastCommand == MCI_EXECSPEEDRAMP )
  380. {
  381. hRetVal = pHandle->hFinalSpeed;
  382. }
  383. return hRetVal;
  384. }
  385. /**
  386. * @brief Check if the settled speed or torque ramp has been completed.
  387. * @param pHandle Pointer on the component instance to work on.
  388. * @retval bool It returns true if the ramp is completed, false otherwise.
  389. */
  390. __weak bool MCI_RampCompleted( MCI_Handle_t * pHandle )
  391. {
  392. bool retVal = false;
  393. if ( ( STM_GetState( pHandle->pSTM ) ) == RUN )
  394. {
  395. retVal = STC_RampCompleted( pHandle->pSTC );
  396. }
  397. return retVal;
  398. }
  399. /**
  400. * @brief Stop the execution of speed ramp.
  401. * @param pHandle Pointer on the component instance to work on.
  402. * @retval bool It returns true if the command is executed, false otherwise.
  403. */
  404. __weak bool MCI_StopSpeedRamp( MCI_Handle_t * pHandle )
  405. {
  406. return STC_StopSpeedRamp( pHandle->pSTC );
  407. }
  408. /**
  409. * @brief Stop the execution of ongoing ramp.
  410. * @param pHandle Pointer on the component instance to work on.
  411. */
  412. __weak void MCI_StopRamp( MCI_Handle_t * pHandle)
  413. {
  414. STC_StopRamp( pHandle->pSTC );
  415. }
  416. /**
  417. * @brief It returns speed sensor reliability with reference to the sensor
  418. * actually used for reference frame transformation
  419. * @param pHandle Pointer on the component instance to work on.
  420. * @retval bool It returns true if the speed sensor utilized for reference
  421. * frame transformation and (in speed control mode) for speed
  422. * regulation is reliable, false otherwise
  423. */
  424. __weak bool MCI_GetSpdSensorReliability( MCI_Handle_t * pHandle )
  425. {
  426. SpeednPosFdbk_Handle_t * SpeedSensor = STC_GetSpeedSensor( pHandle->pSTC );
  427. return ( SPD_Check( SpeedSensor ) );
  428. }
  429. /**
  430. * @brief Returns the last computed average mechanical speed, expressed in
  431. * the unit defined by #SPEED_UNIT and related to the sensor actually
  432. * used by FOC algorithm
  433. * @param pHandle Pointer on the component instance to work on.
  434. */
  435. __weak int16_t MCI_GetAvrgMecSpeedUnit( MCI_Handle_t * pHandle )
  436. {
  437. SpeednPosFdbk_Handle_t * SpeedSensor = STC_GetSpeedSensor( pHandle->pSTC );
  438. return ( SPD_GetAvrgMecSpeedUnit( SpeedSensor ) );
  439. }
  440. /**
  441. * @brief Returns the current mechanical rotor speed reference expressed in the unit defined by #SPEED_UNIT
  442. *
  443. * @param pHandle Pointer on the component instance to work on.
  444. *
  445. */
  446. __weak int16_t MCI_GetMecSpeedRefUnit( MCI_Handle_t * pHandle )
  447. {
  448. return ( STC_GetMecSpeedRefUnit( pHandle->pSTC ) );
  449. }
  450. /**
  451. * @brief It returns stator current Iab in ab_t format
  452. * @param pHandle Pointer on the component instance to work on.
  453. * @retval ab_t Stator current Iab
  454. */
  455. __weak ab_t MCI_GetIab( MCI_Handle_t * pHandle )
  456. {
  457. return ( pHandle->pFOCVars->Iab );
  458. }
  459. /**
  460. * @brief It returns stator current Ialphabeta in alphabeta_t format
  461. * @param pHandle Pointer on the component instance to work on.
  462. * @retval alphabeta_t Stator current Ialphabeta
  463. */
  464. __weak alphabeta_t MCI_GetIalphabeta( MCI_Handle_t * pHandle )
  465. {
  466. return ( pHandle->pFOCVars->Ialphabeta );
  467. }
  468. /**
  469. * @brief It returns stator current Iqd in qd_t format
  470. * @param pHandle Pointer on the component instance to work on.
  471. * @retval qd_t Stator current Iqd
  472. */
  473. __weak qd_t MCI_GetIqd( MCI_Handle_t * pHandle )
  474. {
  475. return ( pHandle->pFOCVars->Iqd );
  476. }
  477. /**
  478. * @brief It returns stator current IqdHF in qd_t format
  479. * @param pHandle Pointer on the component instance to work on.
  480. * @retval qd_t Stator current IqdHF if HFI is selected as main
  481. * sensor. Otherwise it returns { 0, 0}.
  482. */
  483. __weak qd_t MCI_GetIqdHF( MCI_Handle_t * pHandle )
  484. {
  485. return ( pHandle->pFOCVars->IqdHF );
  486. }
  487. /**
  488. * @brief It returns stator current Iqdref in qd_t format
  489. * @param pHandle Pointer on the component instance to work on.
  490. * @retval qd_t Stator current Iqdref
  491. */
  492. __weak qd_t MCI_GetIqdref( MCI_Handle_t * pHandle )
  493. {
  494. return ( pHandle->pFOCVars->Iqdref );
  495. }
  496. /**
  497. * @brief It returns stator current Vqd in qd_t format
  498. * @param pHandle Pointer on the component instance to work on.
  499. * @retval qd_t Stator current Vqd
  500. */
  501. __weak qd_t MCI_GetVqd( MCI_Handle_t * pHandle )
  502. {
  503. return ( pHandle->pFOCVars->Vqd );
  504. }
  505. /**
  506. * @brief It returns stator current Valphabeta in alphabeta_t format
  507. * @param pHandle Pointer on the component instance to work on.
  508. * @retval alphabeta_t Stator current Valphabeta
  509. */
  510. __weak alphabeta_t MCI_GetValphabeta( MCI_Handle_t * pHandle )
  511. {
  512. return ( pHandle->pFOCVars->Valphabeta );
  513. }
  514. /**
  515. * @brief It returns the rotor electrical angle actually used for reference
  516. * frame transformation
  517. * @param pHandle Pointer on the component instance to work on.
  518. * @retval int16_t Rotor electrical angle in dpp format
  519. */
  520. __weak int16_t MCI_GetElAngledpp( MCI_Handle_t * pHandle )
  521. {
  522. return ( pHandle->pFOCVars->hElAngle );
  523. }
  524. /**
  525. * @brief It returns the reference eletrical torque, fed to derived class for
  526. * Iqref and Idref computation
  527. * @param pHandle Pointer on the component instance to work on.
  528. * @retval int16_t Teref
  529. */
  530. __weak int16_t MCI_GetTeref( MCI_Handle_t * pHandle )
  531. {
  532. return ( pHandle->pFOCVars->hTeref );
  533. }
  534. /**
  535. * @brief It returns the motor phase current amplitude (0-to-peak) in s16A
  536. * To convert s16A into Ampere following formula must be used:
  537. * Current(Amp) = [Current(s16A) * Vdd micro] / [65536 * Rshunt * Aop]
  538. * @param pHandle Pointer on the component instance to work on.
  539. * @retval int16_t Motor phase current (0-to-peak) in s16A
  540. */
  541. __weak int16_t MCI_GetPhaseCurrentAmplitude( MCI_Handle_t * pHandle )
  542. {
  543. alphabeta_t Local_Curr;
  544. int32_t wAux1, wAux2;
  545. Local_Curr = pHandle->pFOCVars->Ialphabeta;
  546. wAux1 = ( int32_t )( Local_Curr.alpha ) * Local_Curr.alpha;
  547. wAux2 = ( int32_t )( Local_Curr.beta ) * Local_Curr.beta;
  548. wAux1 += wAux2;
  549. wAux1 = MCM_Sqrt( wAux1 );
  550. if ( wAux1 > INT16_MAX )
  551. {
  552. wAux1 = ( int32_t ) INT16_MAX;
  553. }
  554. return ( ( int16_t )wAux1 );
  555. }
  556. /**
  557. * @brief It returns the applied motor phase voltage amplitude (0-to-peak) in
  558. * s16V. To convert s16V into Volts following formula must be used:
  559. * PhaseVoltage(V) = [PhaseVoltage(s16A) * Vbus(V)] /[sqrt(3) *32767]
  560. * @param pHandle Pointer on the component instance to work on.
  561. * @retval int16_t Motor phase voltage (0-to-peak) in s16V
  562. */
  563. __weak int16_t MCI_GetPhaseVoltageAmplitude( MCI_Handle_t * pHandle )
  564. {
  565. alphabeta_t Local_Voltage;
  566. int32_t wAux1, wAux2;
  567. Local_Voltage = pHandle->pFOCVars->Valphabeta;
  568. wAux1 = ( int32_t )( Local_Voltage.alpha ) * Local_Voltage.alpha;
  569. wAux2 = ( int32_t )( Local_Voltage.beta ) * Local_Voltage.beta;
  570. wAux1 += wAux2;
  571. wAux1 = MCM_Sqrt( wAux1 );
  572. if ( wAux1 > INT16_MAX )
  573. {
  574. wAux1 = ( int32_t ) INT16_MAX;
  575. }
  576. return ( ( int16_t ) wAux1 );
  577. }
  578. /**
  579. * @brief When bDriveInput is set to INTERNAL, Idref should is normally managed
  580. * by FOC_CalcCurrRef. Neverthless, this method allows forcing changing
  581. * Idref value. Method call has no effect when either flux weakening
  582. * region is entered or MTPA is enabled
  583. * @param pHandle Pointer on the component instance to work on.
  584. * @param int16_t New target Id value
  585. * @retval none
  586. */
  587. __weak void MCI_SetIdref( MCI_Handle_t * pHandle, int16_t hNewIdref )
  588. {
  589. pHandle->pFOCVars->Iqdref.d = hNewIdref;
  590. pHandle->pFOCVars->UserIdref = hNewIdref;
  591. }
  592. /**
  593. * @brief It re-initializes Iqdref variables with their default values.
  594. * @param pHandle Pointer on the component instance to work on.
  595. * @retval none
  596. */
  597. __weak void MCI_Clear_Iqdref( MCI_Handle_t * pHandle )
  598. {
  599. pHandle->pFOCVars->Iqdref = STC_GetDefaultIqdref( pHandle->pSTC );
  600. }
  601. /**
  602. * @}
  603. */
  604. /**
  605. * @}
  606. */
  607. /************************ (C) COPYRIGHT 2019 STMicroelectronics *****END OF FILE****/