mc_tasks.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /**
  2. ******************************************************************************
  3. * @file mc_tasks.c
  4. * @author Motor Control SDK Team, ST Microelectronics
  5. * @brief This file implements tasks definition
  6. *
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under Ultimate Liberty license
  14. * SLA0044, the "License"; You may not use this file except in compliance with
  15. * the License. You may obtain a copy of the License at:
  16. * www.st.com/SLA0044
  17. *
  18. ******************************************************************************
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "mc_type.h"
  23. #include "mc_math.h"
  24. #include "motorcontrol.h"
  25. #include "regular_conversion_manager.h"
  26. #include "mc_interface.h"
  27. #include "mc_tuning.h"
  28. #include "digital_output.h"
  29. #include "state_machine.h"
  30. #include "pwm_common.h"
  31. #include "mc_tasks.h"
  32. #include "parameters_conversion.h"
  33. /* USER CODE BEGIN Includes */
  34. /* USER CODE END Includes */
  35. /* USER CODE BEGIN Private define */
  36. /* Private define ------------------------------------------------------------*/
  37. #define CHARGE_BOOT_CAP_MS 10
  38. #define CHARGE_BOOT_CAP_MS2 10
  39. #define OFFCALIBRWAIT_MS 0
  40. #define OFFCALIBRWAIT_MS2 0
  41. #define STOPPERMANENCY_MS 400
  42. #define STOPPERMANENCY_MS2 400
  43. #define CHARGE_BOOT_CAP_TICKS (uint16_t)((SYS_TICK_FREQUENCY * CHARGE_BOOT_CAP_MS)/ 1000)
  44. #define CHARGE_BOOT_CAP_TICKS2 (uint16_t)((SYS_TICK_FREQUENCY * CHARGE_BOOT_CAP_MS2)/ 1000)
  45. #define OFFCALIBRWAITTICKS (uint16_t)((SYS_TICK_FREQUENCY * OFFCALIBRWAIT_MS)/ 1000)
  46. #define OFFCALIBRWAITTICKS2 (uint16_t)((SYS_TICK_FREQUENCY * OFFCALIBRWAIT_MS2)/ 1000)
  47. #define STOPPERMANENCY_TICKS (uint16_t)((SYS_TICK_FREQUENCY * STOPPERMANENCY_MS)/ 1000)
  48. #define STOPPERMANENCY_TICKS2 (uint16_t)((SYS_TICK_FREQUENCY * STOPPERMANENCY_MS2)/ 1000)
  49. /* Un-Comment this macro define in order to activate the smooth
  50. braking action on over voltage */
  51. /* #define MC.SMOOTH_BRAKING_ACTION_ON_OVERVOLTAGE */
  52. /* USER CODE END Private define */
  53. #define VBUS_TEMP_ERR_MASK ~(0 | 0 | MC_OVER_TEMP)
  54. /* Private variables----------------------------------------------------------*/
  55. FOCVars_t FOCVars[NBR_OF_MOTORS];
  56. MCI_Handle_t Mci[NBR_OF_MOTORS];
  57. MCI_Handle_t * oMCInterface[NBR_OF_MOTORS];
  58. MCT_Handle_t MCT[NBR_OF_MOTORS];
  59. STM_Handle_t STM[NBR_OF_MOTORS];
  60. SpeednTorqCtrl_Handle_t *pSTC[NBR_OF_MOTORS];
  61. PID_Handle_t *pPIDSpeed[NBR_OF_MOTORS];
  62. PID_Handle_t *pPIDIq[NBR_OF_MOTORS];
  63. PID_Handle_t *pPIDId[NBR_OF_MOTORS];
  64. RDivider_Handle_t *pBusSensorM1;
  65. NTC_Handle_t *pTemperatureSensor[NBR_OF_MOTORS];
  66. PWMC_Handle_t * pwmcHandle[NBR_OF_MOTORS];
  67. DOUT_handle_t *pR_Brake[NBR_OF_MOTORS];
  68. DOUT_handle_t *pOCPDisabling[NBR_OF_MOTORS];
  69. PQD_MotorPowMeas_Handle_t *pMPM[NBR_OF_MOTORS];
  70. CircleLimitation_Handle_t *pCLM[NBR_OF_MOTORS];
  71. RampExtMngr_Handle_t *pREMNG[NBR_OF_MOTORS]; /*!< Ramp manager used to modify the Iq ref
  72. during the start-up switch over.*/
  73. static volatile uint16_t hMFTaskCounterM1 = 0;
  74. static volatile uint16_t hBootCapDelayCounterM1 = 0;
  75. static volatile uint16_t hStopPermanencyCounterM1 = 0;
  76. uint8_t bMCBootCompleted = 0;
  77. /* USER CODE BEGIN Private Variables */
  78. /* USER CODE END Private Variables */
  79. /* Private functions ---------------------------------------------------------*/
  80. void TSK_MediumFrequencyTaskM1(void);
  81. void FOC_Clear(uint8_t bMotor);
  82. void FOC_InitAdditionalMethods(uint8_t bMotor);
  83. void FOC_CalcCurrRef(uint8_t bMotor);
  84. static uint16_t FOC_CurrControllerM1(void);
  85. void TSK_SetChargeBootCapDelayM1(uint16_t hTickCount);
  86. bool TSK_ChargeBootCapDelayHasElapsedM1(void);
  87. void TSK_SetStopPermanencyTimeM1(uint16_t hTickCount);
  88. bool TSK_StopPermanencyTimeHasElapsedM1(void);
  89. void TSK_SafetyTask_PWMOFF(uint8_t motor);
  90. void UI_Scheduler(void);
  91. /* USER CODE BEGIN Private Functions */
  92. /* USER CODE END Private Functions */
  93. /**
  94. * @brief It initializes the whole MC core according to user defined
  95. * parameters.
  96. * @param pMCIList pointer to the vector of MCInterface objects that will be
  97. * created and initialized. The vector must have length equal to the
  98. * number of motor drives.
  99. * @param pMCTList pointer to the vector of MCTuning objects that will be
  100. * created and initialized. The vector must have length equal to the
  101. * number of motor drives.
  102. * @retval None
  103. */
  104. __weak void MCboot( MCI_Handle_t* pMCIList[NBR_OF_MOTORS],MCT_Handle_t* pMCTList[NBR_OF_MOTORS] )
  105. {
  106. /* USER CODE BEGIN MCboot 0 */
  107. /* USER CODE END MCboot 0 */
  108. /**************************************/
  109. /* State machine initialization */
  110. /**************************************/
  111. STM_Init(&STM[M1]);
  112. bMCBootCompleted = 0;
  113. pCLM[M1] = &CircleLimitationM1;
  114. /**********************************************************/
  115. /* PWM and current sensing component initialization */
  116. /**********************************************************/
  117. pwmcHandle[M1] = &PWM_Handle_M1._Super;
  118. R3_2_Init(&PWM_Handle_M1);
  119. /* USER CODE BEGIN MCboot 1 */
  120. /* USER CODE END MCboot 1 */
  121. /**************************************/
  122. /* Start timers synchronously */
  123. /**************************************/
  124. startTimers();
  125. /******************************************************/
  126. /* PID component initialization: speed regulation */
  127. /******************************************************/
  128. PID_HandleInit(&PIDSpeedHandle_M1);
  129. pPIDSpeed[M1] = &PIDSpeedHandle_M1;
  130. /******************************************************/
  131. /* Main speed sensor component initialization */
  132. /******************************************************/
  133. pSTC[M1] = &SpeednTorqCtrlM1;
  134. HALL_Init (&HALL_M1);
  135. /******************************************************/
  136. /* Speed & torque component initialization */
  137. /******************************************************/
  138. STC_Init(pSTC[M1],pPIDSpeed[M1], &HALL_M1._Super);
  139. /********************************************************/
  140. /* PID component initialization: current regulation */
  141. /********************************************************/
  142. PID_HandleInit(&PIDIqHandle_M1);
  143. PID_HandleInit(&PIDIdHandle_M1);
  144. pPIDIq[M1] = &PIDIqHandle_M1;
  145. pPIDId[M1] = &PIDIdHandle_M1;
  146. /********************************************************/
  147. /* Bus voltage sensor component initialization */
  148. /********************************************************/
  149. pBusSensorM1 = &RealBusVoltageSensorParamsM1;
  150. RVBS_Init(pBusSensorM1);
  151. /*************************************************/
  152. /* Power measurement component initialization */
  153. /*************************************************/
  154. pMPM[M1] = &PQD_MotorPowMeasM1;
  155. pMPM[M1]->pVBS = &(pBusSensorM1->_Super);
  156. pMPM[M1]->pFOCVars = &FOCVars[M1];
  157. /*******************************************************/
  158. /* Temperature measurement component initialization */
  159. /*******************************************************/
  160. NTC_Init(&TempSensorParamsM1);
  161. pTemperatureSensor[M1] = &TempSensorParamsM1;
  162. pREMNG[M1] = &RampExtMngrHFParamsM1;
  163. REMNG_Init(pREMNG[M1]);
  164. FOC_Clear(M1);
  165. FOCVars[M1].bDriveInput = EXTERNAL;
  166. FOCVars[M1].Iqdref = STC_GetDefaultIqdref(pSTC[M1]);
  167. FOCVars[M1].UserIdref = STC_GetDefaultIqdref(pSTC[M1]).d;
  168. oMCInterface[M1] = & Mci[M1];
  169. MCI_Init(oMCInterface[M1], &STM[M1], pSTC[M1], &FOCVars[M1] );
  170. MCI_ExecSpeedRamp(oMCInterface[M1],
  171. STC_GetMecSpeedRefUnitDefault(pSTC[M1]),0); /*First command to STC*/
  172. pMCIList[M1] = oMCInterface[M1];
  173. MCT[M1].pPIDSpeed = pPIDSpeed[M1];
  174. MCT[M1].pPIDIq = pPIDIq[M1];
  175. MCT[M1].pPIDId = pPIDId[M1];
  176. MCT[M1].pPIDFluxWeakening = MC_NULL; /* if M1 doesn't has FW */
  177. MCT[M1].pPWMnCurrFdbk = pwmcHandle[M1];
  178. MCT[M1].pRevupCtrl = MC_NULL; /* only if M1 is not sensorless*/
  179. MCT[M1].pSpeedSensorMain = (SpeednPosFdbk_Handle_t *) &HALL_M1;
  180. MCT[M1].pSpeedSensorAux = MC_NULL;
  181. MCT[M1].pSpeedSensorVirtual = MC_NULL;
  182. MCT[M1].pSpeednTorqueCtrl = pSTC[M1];
  183. MCT[M1].pStateMachine = &STM[M1];
  184. MCT[M1].pTemperatureSensor = (NTC_Handle_t *) pTemperatureSensor[M1];
  185. MCT[M1].pBusVoltageSensor = &(pBusSensorM1->_Super);
  186. MCT[M1].pBrakeDigitalOutput = MC_NULL; /* brake is defined, oBrakeM1*/
  187. MCT[M1].pNTCRelay = MC_NULL; /* relay is defined, oRelayM1*/
  188. MCT[M1].pMPM = (MotorPowMeas_Handle_t*)pMPM[M1];
  189. MCT[M1].pFW = MC_NULL;
  190. MCT[M1].pFF = MC_NULL;
  191. MCT[M1].pPosCtrl = MC_NULL;
  192. MCT[M1].pSCC = MC_NULL;
  193. MCT[M1].pOTT = MC_NULL;
  194. pMCTList[M1] = &MCT[M1];
  195. /* USER CODE BEGIN MCboot 2 */
  196. /* USER CODE END MCboot 2 */
  197. bMCBootCompleted = 1;
  198. }
  199. /**
  200. * @brief Runs all the Tasks of the Motor Control cockpit
  201. *
  202. * This function is to be called periodically at least at the Medium Frequency task
  203. * rate (It is typically called on the Systick interrupt). Exact invokation rate is
  204. * the Speed regulator execution rate set in the Motor Contorl Workbench.
  205. *
  206. * The following tasks are executed in this order:
  207. *
  208. * - Medium Frequency Tasks of each motors
  209. * - Safety Task
  210. * - Power Factor Correction Task (if enabled)
  211. * - User Interface task.
  212. */
  213. __weak void MC_RunMotorControlTasks(void)
  214. {
  215. if ( bMCBootCompleted ) {
  216. /* ** Medium Frequency Tasks ** */
  217. MC_Scheduler();
  218. /* Safety task is run after Medium Frequency task so that
  219. * it can overcome actions they initiated if needed. */
  220. TSK_SafetyTask();
  221. /* ** User Interface Task ** */
  222. UI_Scheduler();
  223. }
  224. }
  225. /**
  226. * @brief Executes the Medium Frequency Task functions for each drive instance.
  227. *
  228. * It is to be clocked at the Systick frequency.
  229. */
  230. __weak void MC_Scheduler(void)
  231. {
  232. /* USER CODE BEGIN MC_Scheduler 0 */
  233. /* USER CODE END MC_Scheduler 0 */
  234. if (bMCBootCompleted == 1)
  235. {
  236. if(hMFTaskCounterM1 > 0u)
  237. {
  238. hMFTaskCounterM1--;
  239. }
  240. else
  241. {
  242. TSK_MediumFrequencyTaskM1();
  243. /* USER CODE BEGIN MC_Scheduler 1 */
  244. /* USER CODE END MC_Scheduler 1 */
  245. hMFTaskCounterM1 = MF_TASK_OCCURENCE_TICKS;
  246. }
  247. if(hBootCapDelayCounterM1 > 0u)
  248. {
  249. hBootCapDelayCounterM1--;
  250. }
  251. if(hStopPermanencyCounterM1 > 0u)
  252. {
  253. hStopPermanencyCounterM1--;
  254. }
  255. }
  256. else
  257. {
  258. }
  259. /* USER CODE BEGIN MC_Scheduler 2 */
  260. /* USER CODE END MC_Scheduler 2 */
  261. }
  262. /**
  263. * @brief Executes medium frequency periodic Motor Control tasks
  264. *
  265. * This function performs some of the control duties on Motor 1 according to the
  266. * present state of its state machine. In particular, duties requiring a periodic
  267. * execution at a medium frequency rate (such as the speed controller for instance)
  268. * are executed here.
  269. */
  270. __weak void TSK_MediumFrequencyTaskM1(void)
  271. {
  272. /* USER CODE BEGIN MediumFrequencyTask M1 0 */
  273. /* USER CODE END MediumFrequencyTask M1 0 */
  274. State_t StateM1;
  275. int16_t wAux = 0;
  276. bool IsSpeedReliable = HALL_CalcAvrgMecSpeedUnit( &HALL_M1, &wAux );
  277. PQD_CalcElMotorPower( pMPM[M1] );
  278. StateM1 = STM_GetState( &STM[M1] );
  279. switch ( StateM1 )
  280. {
  281. case IDLE_START:
  282. R3_2_TurnOnLowSides( pwmcHandle[M1] );
  283. TSK_SetChargeBootCapDelayM1( CHARGE_BOOT_CAP_TICKS );
  284. STM_NextState( &STM[M1], CHARGE_BOOT_CAP );
  285. break;
  286. case CHARGE_BOOT_CAP:
  287. if ( TSK_ChargeBootCapDelayHasElapsedM1() )
  288. {
  289. PWMC_CurrentReadingCalibr( pwmcHandle[M1], CRC_START );
  290. /* USER CODE BEGIN MediumFrequencyTask M1 Charge BootCap elapsed */
  291. /* USER CODE END MediumFrequencyTask M1 Charge BootCap elapsed */
  292. STM_NextState(&STM[M1],OFFSET_CALIB);
  293. }
  294. break;
  295. case OFFSET_CALIB:
  296. if ( PWMC_CurrentReadingCalibr( pwmcHandle[M1], CRC_EXEC ) )
  297. {
  298. STM_NextState( &STM[M1], CLEAR );
  299. }
  300. break;
  301. case CLEAR:
  302. HALL_Clear( &HALL_M1 );
  303. if ( STM_NextState( &STM[M1], START ) == true )
  304. {
  305. FOC_Clear( M1 );
  306. R3_2_SwitchOnPWM( pwmcHandle[M1] );
  307. }
  308. break;
  309. case START:
  310. {
  311. STM_NextState( &STM[M1], START_RUN ); /* only for sensored*/
  312. }
  313. break;
  314. case START_RUN:
  315. {
  316. /* USER CODE BEGIN MediumFrequencyTask M1 1 */
  317. /* USER CODE END MediumFrequencyTask M1 1 */
  318. FOC_InitAdditionalMethods(M1);
  319. FOC_CalcCurrRef( M1 );
  320. STM_NextState( &STM[M1], RUN );
  321. }
  322. STC_ForceSpeedReferenceToCurrentSpeed( pSTC[M1] ); /* Init the reference speed to current speed */
  323. MCI_ExecBufferedCommands( oMCInterface[M1] ); /* Exec the speed ramp after changing of the speed sensor */
  324. break;
  325. case RUN:
  326. /* USER CODE BEGIN MediumFrequencyTask M1 2 */
  327. /* USER CODE END MediumFrequencyTask M1 2 */
  328. MCI_ExecBufferedCommands( oMCInterface[M1] );
  329. FOC_CalcCurrRef( M1 );
  330. if( !IsSpeedReliable )
  331. {
  332. STM_FaultProcessing( &STM[M1], MC_SPEED_FDBK, 0 );
  333. }
  334. /* USER CODE BEGIN MediumFrequencyTask M1 3 */
  335. /* USER CODE END MediumFrequencyTask M1 3 */
  336. break;
  337. case ANY_STOP:
  338. R3_2_SwitchOffPWM( pwmcHandle[M1] );
  339. FOC_Clear( M1 );
  340. MPM_Clear( (MotorPowMeas_Handle_t*) pMPM[M1] );
  341. TSK_SetStopPermanencyTimeM1( STOPPERMANENCY_TICKS );
  342. /* USER CODE BEGIN MediumFrequencyTask M1 4 */
  343. /* USER CODE END MediumFrequencyTask M1 4 */
  344. STM_NextState( &STM[M1], STOP );
  345. break;
  346. case STOP:
  347. if ( TSK_StopPermanencyTimeHasElapsedM1() )
  348. {
  349. STM_NextState( &STM[M1], STOP_IDLE );
  350. }
  351. break;
  352. case STOP_IDLE:
  353. /* USER CODE BEGIN MediumFrequencyTask M1 5 */
  354. /* USER CODE END MediumFrequencyTask M1 5 */
  355. STM_NextState( &STM[M1], IDLE );
  356. break;
  357. default:
  358. break;
  359. }
  360. /* USER CODE BEGIN MediumFrequencyTask M1 6 */
  361. /* USER CODE END MediumFrequencyTask M1 6 */
  362. }
  363. /**
  364. * @brief It re-initializes the current and voltage variables. Moreover
  365. * it clears qd currents PI controllers, voltage sensor and SpeednTorque
  366. * controller. It must be called before each motor restart.
  367. * It does not clear speed sensor.
  368. * @param bMotor related motor it can be M1 or M2
  369. * @retval none
  370. */
  371. __weak void FOC_Clear(uint8_t bMotor)
  372. {
  373. /* USER CODE BEGIN FOC_Clear 0 */
  374. /* USER CODE END FOC_Clear 0 */
  375. ab_t NULL_ab = {(int16_t)0, (int16_t)0};
  376. qd_t NULL_qd = {(int16_t)0, (int16_t)0};
  377. alphabeta_t NULL_alphabeta = {(int16_t)0, (int16_t)0};
  378. FOCVars[bMotor].Iab = NULL_ab;
  379. FOCVars[bMotor].Ialphabeta = NULL_alphabeta;
  380. FOCVars[bMotor].Iqd = NULL_qd;
  381. FOCVars[bMotor].Iqdref = NULL_qd;
  382. FOCVars[bMotor].hTeref = (int16_t)0;
  383. FOCVars[bMotor].Vqd = NULL_qd;
  384. FOCVars[bMotor].Valphabeta = NULL_alphabeta;
  385. FOCVars[bMotor].hElAngle = (int16_t)0;
  386. PID_SetIntegralTerm(pPIDIq[bMotor], (int32_t)0);
  387. PID_SetIntegralTerm(pPIDId[bMotor], (int32_t)0);
  388. STC_Clear(pSTC[bMotor]);
  389. PWMC_SwitchOffPWM(pwmcHandle[bMotor]);
  390. /* USER CODE BEGIN FOC_Clear 1 */
  391. /* USER CODE END FOC_Clear 1 */
  392. }
  393. /**
  394. * @brief Use this method to initialize additional methods (if any) in
  395. * START_TO_RUN state
  396. * @param bMotor related motor it can be M1 or M2
  397. * @retval none
  398. */
  399. __weak void FOC_InitAdditionalMethods(uint8_t bMotor)
  400. {
  401. /* USER CODE BEGIN FOC_InitAdditionalMethods 0 */
  402. /* USER CODE END FOC_InitAdditionalMethods 0 */
  403. }
  404. /**
  405. * @brief It computes the new values of Iqdref (current references on qd
  406. * reference frame) based on the required electrical torque information
  407. * provided by oTSC object (internally clocked).
  408. * If implemented in the derived class it executes flux weakening and/or
  409. * MTPA algorithm(s). It must be called with the periodicity specified
  410. * in oTSC parameters
  411. * @param bMotor related motor it can be M1 or M2
  412. * @retval none
  413. */
  414. __weak void FOC_CalcCurrRef(uint8_t bMotor)
  415. {
  416. /* USER CODE BEGIN FOC_CalcCurrRef 0 */
  417. /* USER CODE END FOC_CalcCurrRef 0 */
  418. if(FOCVars[bMotor].bDriveInput == INTERNAL)
  419. {
  420. FOCVars[bMotor].hTeref = STC_CalcTorqueReference(pSTC[bMotor]);
  421. FOCVars[bMotor].Iqdref.q = FOCVars[bMotor].hTeref;
  422. }
  423. /* USER CODE BEGIN FOC_CalcCurrRef 1 */
  424. /* USER CODE END FOC_CalcCurrRef 1 */
  425. }
  426. /**
  427. * @brief It set a counter intended to be used for counting the delay required
  428. * for drivers boot capacitors charging of motor 1
  429. * @param hTickCount number of ticks to be counted
  430. * @retval void
  431. */
  432. __weak void TSK_SetChargeBootCapDelayM1(uint16_t hTickCount)
  433. {
  434. hBootCapDelayCounterM1 = hTickCount;
  435. }
  436. /**
  437. * @brief Use this function to know whether the time required to charge boot
  438. * capacitors of motor 1 has elapsed
  439. * @param none
  440. * @retval bool true if time has elapsed, false otherwise
  441. */
  442. __weak bool TSK_ChargeBootCapDelayHasElapsedM1(void)
  443. {
  444. bool retVal = false;
  445. if (hBootCapDelayCounterM1 == 0)
  446. {
  447. retVal = true;
  448. }
  449. return (retVal);
  450. }
  451. /**
  452. * @brief It set a counter intended to be used for counting the permanency
  453. * time in STOP state of motor 1
  454. * @param hTickCount number of ticks to be counted
  455. * @retval void
  456. */
  457. __weak void TSK_SetStopPermanencyTimeM1(uint16_t hTickCount)
  458. {
  459. hStopPermanencyCounterM1 = hTickCount;
  460. }
  461. /**
  462. * @brief Use this function to know whether the permanency time in STOP state
  463. * of motor 1 has elapsed
  464. * @param none
  465. * @retval bool true if time is elapsed, false otherwise
  466. */
  467. __weak bool TSK_StopPermanencyTimeHasElapsedM1(void)
  468. {
  469. bool retVal = false;
  470. if (hStopPermanencyCounterM1 == 0)
  471. {
  472. retVal = true;
  473. }
  474. return (retVal);
  475. }
  476. #if defined (CCMRAM_ENABLED)
  477. #if defined (__ICCARM__)
  478. #pragma location = ".ccmram"
  479. #elif defined (__CC_ARM)
  480. __attribute__((section (".ccmram")))
  481. #endif
  482. #endif
  483. /**
  484. * @brief Executes the Motor Control duties that require a high frequency rate and a precise timing
  485. *
  486. * This is mainly the FOC current control loop. It is executed depending on the state of the Motor Control
  487. * subsystem (see the state machine(s)).
  488. *
  489. * @retval Number of the motor instance which FOC loop was executed.
  490. */
  491. __weak uint8_t TSK_HighFrequencyTask(void)
  492. {
  493. /* USER CODE BEGIN HighFrequencyTask 0 */
  494. /* USER CODE END HighFrequencyTask 0 */
  495. uint8_t bMotorNbr = 0;
  496. uint16_t hFOCreturn;
  497. HALL_CalcElAngle (&HALL_M1);
  498. /* USER CODE BEGIN HighFrequencyTask SINGLEDRIVE_1 */
  499. /* USER CODE END HighFrequencyTask SINGLEDRIVE_1 */
  500. hFOCreturn = FOC_CurrControllerM1();
  501. /* USER CODE BEGIN HighFrequencyTask SINGLEDRIVE_2 */
  502. /* USER CODE END HighFrequencyTask SINGLEDRIVE_2 */
  503. if(hFOCreturn == MC_FOC_DURATION)
  504. {
  505. STM_FaultProcessing(&STM[M1], MC_FOC_DURATION, 0);
  506. }
  507. else
  508. {
  509. /* USER CODE BEGIN HighFrequencyTask SINGLEDRIVE_3 */
  510. /* USER CODE END HighFrequencyTask SINGLEDRIVE_3 */
  511. }
  512. /* USER CODE BEGIN HighFrequencyTask 1 */
  513. /* USER CODE END HighFrequencyTask 1 */
  514. return bMotorNbr;
  515. }
  516. #if defined (CCMRAM)
  517. #if defined (__ICCARM__)
  518. #pragma location = ".ccmram"
  519. #elif defined (__CC_ARM) || defined(__GNUC__)
  520. __attribute__((section (".ccmram")))
  521. #endif
  522. #endif
  523. /**
  524. * @brief It executes the core of FOC drive that is the controllers for Iqd
  525. * currents regulation. Reference frame transformations are carried out
  526. * accordingly to the active speed sensor. It must be called periodically
  527. * when new motor currents have been converted
  528. * @param this related object of class CFOC.
  529. * @retval int16_t It returns MC_NO_FAULTS if the FOC has been ended before
  530. * next PWM Update event, MC_FOC_DURATION otherwise
  531. */
  532. inline uint16_t FOC_CurrControllerM1(void)
  533. {
  534. qd_t Iqd, Vqd;
  535. ab_t Iab;
  536. alphabeta_t Ialphabeta, Valphabeta;
  537. int16_t hElAngle;
  538. uint16_t hCodeError;
  539. SpeednPosFdbk_Handle_t *speedHandle;
  540. speedHandle = STC_GetSpeedSensor(pSTC[M1]);
  541. hElAngle = SPD_GetElAngle(speedHandle);
  542. PWMC_GetPhaseCurrents(pwmcHandle[M1], &Iab);
  543. Ialphabeta = MCM_Clarke(Iab);
  544. Iqd = MCM_Park(Ialphabeta, hElAngle);
  545. Vqd.q = PI_Controller(pPIDIq[M1],
  546. (int32_t)(FOCVars[M1].Iqdref.q) - Iqd.q);
  547. Vqd.d = PI_Controller(pPIDId[M1],
  548. (int32_t)(FOCVars[M1].Iqdref.d) - Iqd.d);
  549. Vqd = Circle_Limitation(pCLM[M1], Vqd);
  550. hElAngle += SPD_GetInstElSpeedDpp(speedHandle)*REV_PARK_ANGLE_COMPENSATION_FACTOR;
  551. Valphabeta = MCM_Rev_Park(Vqd, hElAngle);
  552. hCodeError = PWMC_SetPhaseVoltage(pwmcHandle[M1], Valphabeta);
  553. FOCVars[M1].Vqd = Vqd;
  554. FOCVars[M1].Iab = Iab;
  555. FOCVars[M1].Ialphabeta = Ialphabeta;
  556. FOCVars[M1].Iqd = Iqd;
  557. FOCVars[M1].Valphabeta = Valphabeta;
  558. FOCVars[M1].hElAngle = hElAngle;
  559. return(hCodeError);
  560. }
  561. /**
  562. * @brief Executes safety checks (e.g. bus voltage and temperature) for all drive instances.
  563. *
  564. * Faults flags are updated here.
  565. */
  566. __weak void TSK_SafetyTask(void)
  567. {
  568. /* USER CODE BEGIN TSK_SafetyTask 0 */
  569. /* USER CODE END TSK_SafetyTask 0 */
  570. if (bMCBootCompleted == 1)
  571. {
  572. TSK_SafetyTask_PWMOFF(M1);
  573. /* User conversion execution */
  574. RCM_ExecUserConv ();
  575. /* USER CODE BEGIN TSK_SafetyTask 1 */
  576. /* USER CODE END TSK_SafetyTask 1 */
  577. }
  578. }
  579. /**
  580. * @brief Safety task implementation if MC.ON_OVER_VOLTAGE == TURN_OFF_PWM
  581. * @param bMotor Motor reference number defined
  582. * \link Motors_reference_number here \endlink
  583. * @retval None
  584. */
  585. __weak void TSK_SafetyTask_PWMOFF(uint8_t bMotor)
  586. {
  587. /* USER CODE BEGIN TSK_SafetyTask_PWMOFF 0 */
  588. /* USER CODE END TSK_SafetyTask_PWMOFF 0 */
  589. uint16_t CodeReturn = MC_NO_ERROR;
  590. uint16_t errMask[NBR_OF_MOTORS] = {VBUS_TEMP_ERR_MASK};
  591. CodeReturn |= errMask[bMotor] & NTC_CalcAvTemp(pTemperatureSensor[bMotor]); /* check for fault if FW protection is activated. It returns MC_OVER_TEMP or MC_NO_ERROR */
  592. CodeReturn |= PWMC_CheckOverCurrent(pwmcHandle[bMotor]); /* check for fault. It return MC_BREAK_IN or MC_NO_FAULTS
  593. (for STM32F30x can return MC_OVER_VOLT in case of HW Overvoltage) */
  594. if(bMotor == M1)
  595. {
  596. CodeReturn |= errMask[bMotor] &RVBS_CalcAvVbus(pBusSensorM1);
  597. }
  598. STM_FaultProcessing(&STM[bMotor], CodeReturn, ~CodeReturn); /* Update the STM according error code */
  599. switch (STM_GetState(&STM[bMotor])) /* Acts on PWM outputs in case of faults */
  600. {
  601. case FAULT_NOW:
  602. PWMC_SwitchOffPWM(pwmcHandle[bMotor]);
  603. FOC_Clear(bMotor);
  604. MPM_Clear((MotorPowMeas_Handle_t*)pMPM[bMotor]);
  605. /* USER CODE BEGIN TSK_SafetyTask_PWMOFF 1 */
  606. /* USER CODE END TSK_SafetyTask_PWMOFF 1 */
  607. break;
  608. case FAULT_OVER:
  609. PWMC_SwitchOffPWM(pwmcHandle[bMotor]);
  610. /* USER CODE BEGIN TSK_SafetyTask_PWMOFF 2 */
  611. /* USER CODE END TSK_SafetyTask_PWMOFF 2 */
  612. break;
  613. default:
  614. break;
  615. }
  616. /* USER CODE BEGIN TSK_SafetyTask_PWMOFF 3 */
  617. /* USER CODE END TSK_SafetyTask_PWMOFF 3 */
  618. }
  619. /**
  620. * @brief This function returns the reference of the MCInterface relative to
  621. * the selected drive.
  622. * @param bMotor Motor reference number defined
  623. * \link Motors_reference_number here \endlink
  624. * @retval MCI_Handle_t * Reference to MCInterface relative to the selected drive.
  625. * Note: it can be MC_NULL if MCInterface of selected drive is not
  626. * allocated.
  627. */
  628. __weak MCI_Handle_t * GetMCI(uint8_t bMotor)
  629. {
  630. MCI_Handle_t * retVal = MC_NULL;
  631. if (bMotor < NBR_OF_MOTORS)
  632. {
  633. retVal = oMCInterface[bMotor];
  634. }
  635. return retVal;
  636. }
  637. /**
  638. * @brief This function returns the reference of the MCTuning relative to
  639. * the selected drive.
  640. * @param bMotor Motor reference number defined
  641. * \link Motors_reference_number here \endlink
  642. * @retval MCT_Handle_t motor control tuning handler for the selected drive.
  643. * Note: it can be MC_NULL if MCInterface of selected drive is not
  644. * allocated.
  645. */
  646. __weak MCT_Handle_t* GetMCT(uint8_t bMotor)
  647. {
  648. MCT_Handle_t* retVal = MC_NULL;
  649. if (bMotor < NBR_OF_MOTORS)
  650. {
  651. retVal = &MCT[bMotor];
  652. }
  653. return retVal;
  654. }
  655. /**
  656. * @brief Puts the Motor Control subsystem in in safety conditions on a Hard Fault
  657. *
  658. * This function is to be executed when a general hardware failure has been detected
  659. * by the microcontroller and is used to put the system in safety condition.
  660. */
  661. __weak void TSK_HardwareFaultTask(void)
  662. {
  663. /* USER CODE BEGIN TSK_HardwareFaultTask 0 */
  664. /* USER CODE END TSK_HardwareFaultTask 0 */
  665. R3_2_SwitchOffPWM(pwmcHandle[M1]);
  666. STM_FaultProcessing(&STM[M1], MC_SW_ERROR, 0);
  667. /* USER CODE BEGIN TSK_HardwareFaultTask 1 */
  668. /* USER CODE END TSK_HardwareFaultTask 1 */
  669. }
  670. /**
  671. * @brief Locks GPIO pins used for Motor Control to prevent accidental reconfiguration
  672. */
  673. __weak void mc_lock_pins (void)
  674. {
  675. LL_GPIO_LockPin(M1_HALL_H2_GPIO_Port, M1_HALL_H2_Pin);
  676. LL_GPIO_LockPin(M1_HALL_H3_GPIO_Port, M1_HALL_H3_Pin);
  677. LL_GPIO_LockPin(M1_HALL_H1_GPIO_Port, M1_HALL_H1_Pin);
  678. LL_GPIO_LockPin(M1_PWM_UH_GPIO_Port, M1_PWM_UH_Pin);
  679. LL_GPIO_LockPin(M1_PWM_VH_GPIO_Port, M1_PWM_VH_Pin);
  680. LL_GPIO_LockPin(M1_OCP_GPIO_Port, M1_OCP_Pin);
  681. LL_GPIO_LockPin(M1_PWM_VL_GPIO_Port, M1_PWM_VL_Pin);
  682. LL_GPIO_LockPin(M1_PWM_WH_GPIO_Port, M1_PWM_WH_Pin);
  683. LL_GPIO_LockPin(M1_PWM_WL_GPIO_Port, M1_PWM_WL_Pin);
  684. LL_GPIO_LockPin(M1_PWM_UL_GPIO_Port, M1_PWM_UL_Pin);
  685. LL_GPIO_LockPin(M1_CURR_AMPL_W_GPIO_Port, M1_CURR_AMPL_W_Pin);
  686. LL_GPIO_LockPin(M1_CURR_AMPL_U_GPIO_Port, M1_CURR_AMPL_U_Pin);
  687. LL_GPIO_LockPin(M1_CURR_AMPL_V_GPIO_Port, M1_CURR_AMPL_V_Pin);
  688. LL_GPIO_LockPin(M1_BUS_VOLTAGE_GPIO_Port, M1_BUS_VOLTAGE_Pin);
  689. }
  690. /* USER CODE BEGIN mc_task 0 */
  691. /* USER CODE END mc_task 0 */
  692. /******************* (C) COPYRIGHT 2019 STMicroelectronics *****END OF FILE****/