controller_functions.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /******************************************************************************
  2. * @file controller_functions.h
  3. * @brief Public header file for CMSIS DSP Library
  4. * @version V1.10.0
  5. * @date 08 July 2021
  6. * Target Processor: Cortex-M and Cortex-A cores
  7. ******************************************************************************/
  8. /*
  9. * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. #ifndef _CONTROLLER_FUNCTIONS_H_
  26. #define _CONTROLLER_FUNCTIONS_H_
  27. #include "arm_math_types.h"
  28. #include "arm_math_memory.h"
  29. #include "dsp/none.h"
  30. #include "dsp/utils.h"
  31. #ifdef __cplusplus
  32. extern "C"
  33. {
  34. #endif
  35. /**
  36. * @brief Macros required for SINE and COSINE Controller functions
  37. */
  38. #define CONTROLLER_Q31_SHIFT (32 - 9)
  39. /* 1.31(q31) Fixed value of 2/360 */
  40. /* -1 to +1 is divided into 360 values so total spacing is (2/360) */
  41. #define INPUT_SPACING 0xB60B61
  42. /**
  43. * @defgroup groupController Controller Functions
  44. */
  45. /**
  46. * @ingroup groupController
  47. */
  48. /**
  49. * @addtogroup SinCos
  50. * @{
  51. */
  52. /**
  53. * @brief Floating-point sin_cos function.
  54. * @param[in] theta input value in degrees
  55. * @param[out] pSinVal points to the processed sine output.
  56. * @param[out] pCosVal points to the processed cos output.
  57. */
  58. void arm_sin_cos_f32(
  59. float32_t theta,
  60. float32_t * pSinVal,
  61. float32_t * pCosVal);
  62. /**
  63. * @brief Q31 sin_cos function.
  64. * @param[in] theta scaled input value in degrees
  65. * @param[out] pSinVal points to the processed sine output.
  66. * @param[out] pCosVal points to the processed cosine output.
  67. */
  68. void arm_sin_cos_q31(
  69. q31_t theta,
  70. q31_t * pSinVal,
  71. q31_t * pCosVal);
  72. /**
  73. * @} end of SinCos group
  74. */
  75. /**
  76. * @ingroup groupController
  77. */
  78. /**
  79. * @defgroup PID PID Motor Control
  80. *
  81. * A Proportional Integral Derivative (PID) controller is a generic feedback control
  82. * loop mechanism widely used in industrial control systems.
  83. * A PID controller is the most commonly used type of feedback controller.
  84. *
  85. * This set of functions implements (PID) controllers
  86. * for Q15, Q31, and floating-point data types. The functions operate on a single sample
  87. * of data and each call to the function returns a single processed value.
  88. * <code>S</code> points to an instance of the PID control data structure. <code>in</code>
  89. * is the input sample value. The functions return the output value.
  90. *
  91. * \par Algorithm:
  92. * <pre>
  93. * y[n] = y[n-1] + A0 * x[n] + A1 * x[n-1] + A2 * x[n-2]
  94. * A0 = Kp + Ki + Kd
  95. * A1 = (-Kp ) - (2 * Kd )
  96. * A2 = Kd
  97. * </pre>
  98. *
  99. * \par
  100. * where \c Kp is proportional constant, \c Ki is Integral constant and \c Kd is Derivative constant
  101. *
  102. * \par
  103. * \image html PID.gif "Proportional Integral Derivative Controller"
  104. *
  105. * \par
  106. * The PID controller calculates an "error" value as the difference between
  107. * the measured output and the reference input.
  108. * The controller attempts to minimize the error by adjusting the process control inputs.
  109. * The proportional value determines the reaction to the current error,
  110. * the integral value determines the reaction based on the sum of recent errors,
  111. * and the derivative value determines the reaction based on the rate at which the error has been changing.
  112. *
  113. * \par Instance Structure
  114. * The Gains A0, A1, A2 and state variables for a PID controller are stored together in an instance data structure.
  115. * A separate instance structure must be defined for each PID Controller.
  116. * There are separate instance structure declarations for each of the 3 supported data types.
  117. *
  118. * \par Reset Functions
  119. * There is also an associated reset function for each data type which clears the state array.
  120. *
  121. * \par Initialization Functions
  122. * There is also an associated initialization function for each data type.
  123. * The initialization function performs the following operations:
  124. * - Initializes the Gains A0, A1, A2 from Kp,Ki, Kd gains.
  125. * - Zeros out the values in the state buffer.
  126. *
  127. * \par
  128. * Instance structure cannot be placed into a const data section and it is recommended to use the initialization function.
  129. *
  130. * \par Fixed-Point Behavior
  131. * Care must be taken when using the fixed-point versions of the PID Controller functions.
  132. * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
  133. * Refer to the function specific documentation below for usage guidelines.
  134. */
  135. /**
  136. * @brief Instance structure for the Q15 PID Control.
  137. */
  138. typedef struct
  139. {
  140. q15_t A0; /**< The derived gain, A0 = Kp + Ki + Kd . */
  141. #if !defined (ARM_MATH_DSP)
  142. q15_t A1; /**< The derived gain A1 = -Kp - 2Kd */
  143. q15_t A2; /**< The derived gain A1 = Kd. */
  144. #else
  145. q31_t A1; /**< The derived gain A1 = -Kp - 2Kd | Kd.*/
  146. #endif
  147. q15_t state[3]; /**< The state array of length 3. */
  148. q15_t Kp; /**< The proportional gain. */
  149. q15_t Ki; /**< The integral gain. */
  150. q15_t Kd; /**< The derivative gain. */
  151. } arm_pid_instance_q15;
  152. /**
  153. * @brief Instance structure for the Q31 PID Control.
  154. */
  155. typedef struct
  156. {
  157. q31_t A0; /**< The derived gain, A0 = Kp + Ki + Kd . */
  158. q31_t A1; /**< The derived gain, A1 = -Kp - 2Kd. */
  159. q31_t A2; /**< The derived gain, A2 = Kd . */
  160. q31_t state[3]; /**< The state array of length 3. */
  161. q31_t Kp; /**< The proportional gain. */
  162. q31_t Ki; /**< The integral gain. */
  163. q31_t Kd; /**< The derivative gain. */
  164. } arm_pid_instance_q31;
  165. /**
  166. * @brief Instance structure for the floating-point PID Control.
  167. */
  168. typedef struct
  169. {
  170. float32_t A0; /**< The derived gain, A0 = Kp + Ki + Kd . */
  171. float32_t A1; /**< The derived gain, A1 = -Kp - 2Kd. */
  172. float32_t A2; /**< The derived gain, A2 = Kd . */
  173. float32_t state[3]; /**< The state array of length 3. */
  174. float32_t Kp; /**< The proportional gain. */
  175. float32_t Ki; /**< The integral gain. */
  176. float32_t Kd; /**< The derivative gain. */
  177. } arm_pid_instance_f32;
  178. /**
  179. * @brief Initialization function for the floating-point PID Control.
  180. * @param[in,out] S points to an instance of the PID structure.
  181. * @param[in] resetStateFlag flag to reset the state. 0 = no change in state 1 = reset the state.
  182. */
  183. void arm_pid_init_f32(
  184. arm_pid_instance_f32 * S,
  185. int32_t resetStateFlag);
  186. /**
  187. * @brief Reset function for the floating-point PID Control.
  188. * @param[in,out] S is an instance of the floating-point PID Control structure
  189. */
  190. void arm_pid_reset_f32(
  191. arm_pid_instance_f32 * S);
  192. /**
  193. * @brief Initialization function for the Q31 PID Control.
  194. * @param[in,out] S points to an instance of the Q15 PID structure.
  195. * @param[in] resetStateFlag flag to reset the state. 0 = no change in state 1 = reset the state.
  196. */
  197. void arm_pid_init_q31(
  198. arm_pid_instance_q31 * S,
  199. int32_t resetStateFlag);
  200. /**
  201. * @brief Reset function for the Q31 PID Control.
  202. * @param[in,out] S points to an instance of the Q31 PID Control structure
  203. */
  204. void arm_pid_reset_q31(
  205. arm_pid_instance_q31 * S);
  206. /**
  207. * @brief Initialization function for the Q15 PID Control.
  208. * @param[in,out] S points to an instance of the Q15 PID structure.
  209. * @param[in] resetStateFlag flag to reset the state. 0 = no change in state 1 = reset the state.
  210. */
  211. void arm_pid_init_q15(
  212. arm_pid_instance_q15 * S,
  213. int32_t resetStateFlag);
  214. /**
  215. * @brief Reset function for the Q15 PID Control.
  216. * @param[in,out] S points to an instance of the q15 PID Control structure
  217. */
  218. void arm_pid_reset_q15(
  219. arm_pid_instance_q15 * S);
  220. /**
  221. * @addtogroup PID
  222. * @{
  223. */
  224. /**
  225. * @brief Process function for the floating-point PID Control.
  226. * @param[in,out] S is an instance of the floating-point PID Control structure
  227. * @param[in] in input sample to process
  228. * @return processed output sample.
  229. */
  230. __STATIC_FORCEINLINE float32_t arm_pid_f32(
  231. arm_pid_instance_f32 * S,
  232. float32_t in)
  233. {
  234. float32_t out;
  235. /* y[n] = y[n-1] + A0 * x[n] + A1 * x[n-1] + A2 * x[n-2] */
  236. out = (S->A0 * in) +
  237. (S->A1 * S->state[0]) + (S->A2 * S->state[1]) + (S->state[2]);
  238. /* Update state */
  239. S->state[1] = S->state[0];
  240. S->state[0] = in;
  241. S->state[2] = out;
  242. /* return to application */
  243. return (out);
  244. }
  245. /**
  246. @brief Process function for the Q31 PID Control.
  247. @param[in,out] S points to an instance of the Q31 PID Control structure
  248. @param[in] in input sample to process
  249. @return processed output sample.
  250. \par Scaling and Overflow Behavior
  251. The function is implemented using an internal 64-bit accumulator.
  252. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
  253. Thus, if the accumulator result overflows it wraps around rather than clip.
  254. In order to avoid overflows completely the input signal must be scaled down by 2 bits as there are four additions.
  255. After all multiply-accumulates are performed, the 2.62 accumulator is truncated to 1.32 format and then saturated to 1.31 format.
  256. */
  257. __STATIC_FORCEINLINE q31_t arm_pid_q31(
  258. arm_pid_instance_q31 * S,
  259. q31_t in)
  260. {
  261. q63_t acc;
  262. q31_t out;
  263. /* acc = A0 * x[n] */
  264. acc = (q63_t) S->A0 * in;
  265. /* acc += A1 * x[n-1] */
  266. acc += (q63_t) S->A1 * S->state[0];
  267. /* acc += A2 * x[n-2] */
  268. acc += (q63_t) S->A2 * S->state[1];
  269. /* convert output to 1.31 format to add y[n-1] */
  270. out = (q31_t) (acc >> 31U);
  271. /* out += y[n-1] */
  272. out += S->state[2];
  273. /* Update state */
  274. S->state[1] = S->state[0];
  275. S->state[0] = in;
  276. S->state[2] = out;
  277. /* return to application */
  278. return (out);
  279. }
  280. /**
  281. @brief Process function for the Q15 PID Control.
  282. @param[in,out] S points to an instance of the Q15 PID Control structure
  283. @param[in] in input sample to process
  284. @return processed output sample.
  285. \par Scaling and Overflow Behavior
  286. The function is implemented using a 64-bit internal accumulator.
  287. Both Gains and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
  288. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
  289. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
  290. After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits.
  291. Lastly, the accumulator is saturated to yield a result in 1.15 format.
  292. */
  293. __STATIC_FORCEINLINE q15_t arm_pid_q15(
  294. arm_pid_instance_q15 * S,
  295. q15_t in)
  296. {
  297. q63_t acc;
  298. q15_t out;
  299. #if defined (ARM_MATH_DSP)
  300. /* Implementation of PID controller */
  301. /* acc = A0 * x[n] */
  302. acc = (q31_t) __SMUAD((uint32_t)S->A0, (uint32_t)in);
  303. /* acc += A1 * x[n-1] + A2 * x[n-2] */
  304. acc = (q63_t)__SMLALD((uint32_t)S->A1, (uint32_t)read_q15x2 (S->state), (uint64_t)acc);
  305. #else
  306. /* acc = A0 * x[n] */
  307. acc = ((q31_t) S->A0) * in;
  308. /* acc += A1 * x[n-1] + A2 * x[n-2] */
  309. acc += (q31_t) S->A1 * S->state[0];
  310. acc += (q31_t) S->A2 * S->state[1];
  311. #endif
  312. /* acc += y[n-1] */
  313. acc += (q31_t) S->state[2] << 15;
  314. /* saturate the output */
  315. out = (q15_t) (__SSAT((q31_t)(acc >> 15), 16));
  316. /* Update state */
  317. S->state[1] = S->state[0];
  318. S->state[0] = in;
  319. S->state[2] = out;
  320. /* return to application */
  321. return (out);
  322. }
  323. /**
  324. * @} end of PID group
  325. */
  326. /**
  327. * @ingroup groupController
  328. */
  329. /**
  330. * @defgroup park Vector Park Transform
  331. *
  332. * Forward Park transform converts the input two-coordinate vector to flux and torque components.
  333. * The Park transform can be used to realize the transformation of the <code>Ialpha</code> and the <code>Ibeta</code> currents
  334. * from the stationary to the moving reference frame and control the spatial relationship between
  335. * the stator vector current and rotor flux vector.
  336. * If we consider the d axis aligned with the rotor flux, the diagram below shows the
  337. * current vector and the relationship from the two reference frames:
  338. * \image html park.gif "Stator current space vector and its component in (a,b) and in the d,q rotating reference frame"
  339. *
  340. * The function operates on a single sample of data and each call to the function returns the processed output.
  341. * The library provides separate functions for Q31 and floating-point data types.
  342. * \par Algorithm
  343. * \image html parkFormula.gif
  344. * where <code>Ialpha</code> and <code>Ibeta</code> are the stator vector components,
  345. * <code>pId</code> and <code>pIq</code> are rotor vector components and <code>cosVal</code> and <code>sinVal</code> are the
  346. * cosine and sine values of theta (rotor flux position).
  347. * \par Fixed-Point Behavior
  348. * Care must be taken when using the Q31 version of the Park transform.
  349. * In particular, the overflow and saturation behavior of the accumulator used must be considered.
  350. * Refer to the function specific documentation below for usage guidelines.
  351. */
  352. /**
  353. * @addtogroup park
  354. * @{
  355. */
  356. /**
  357. * @brief Floating-point Park transform
  358. * @param[in] Ialpha input two-phase vector coordinate alpha
  359. * @param[in] Ibeta input two-phase vector coordinate beta
  360. * @param[out] pId points to output rotor reference frame d
  361. * @param[out] pIq points to output rotor reference frame q
  362. * @param[in] sinVal sine value of rotation angle theta
  363. * @param[in] cosVal cosine value of rotation angle theta
  364. * @return none
  365. *
  366. * The function implements the forward Park transform.
  367. *
  368. */
  369. __STATIC_FORCEINLINE void arm_park_f32(
  370. float32_t Ialpha,
  371. float32_t Ibeta,
  372. float32_t * pId,
  373. float32_t * pIq,
  374. float32_t sinVal,
  375. float32_t cosVal)
  376. {
  377. /* Calculate pId using the equation, pId = Ialpha * cosVal + Ibeta * sinVal */
  378. *pId = Ialpha * cosVal + Ibeta * sinVal;
  379. /* Calculate pIq using the equation, pIq = - Ialpha * sinVal + Ibeta * cosVal */
  380. *pIq = -Ialpha * sinVal + Ibeta * cosVal;
  381. }
  382. /**
  383. @brief Park transform for Q31 version
  384. @param[in] Ialpha input two-phase vector coordinate alpha
  385. @param[in] Ibeta input two-phase vector coordinate beta
  386. @param[out] pId points to output rotor reference frame d
  387. @param[out] pIq points to output rotor reference frame q
  388. @param[in] sinVal sine value of rotation angle theta
  389. @param[in] cosVal cosine value of rotation angle theta
  390. @return none
  391. \par Scaling and Overflow Behavior
  392. The function is implemented using an internal 32-bit accumulator.
  393. The accumulator maintains 1.31 format by truncating lower 31 bits of the intermediate multiplication in 2.62 format.
  394. There is saturation on the addition and subtraction, hence there is no risk of overflow.
  395. */
  396. __STATIC_FORCEINLINE void arm_park_q31(
  397. q31_t Ialpha,
  398. q31_t Ibeta,
  399. q31_t * pId,
  400. q31_t * pIq,
  401. q31_t sinVal,
  402. q31_t cosVal)
  403. {
  404. q31_t product1, product2; /* Temporary variables used to store intermediate results */
  405. q31_t product3, product4; /* Temporary variables used to store intermediate results */
  406. /* Intermediate product is calculated by (Ialpha * cosVal) */
  407. product1 = (q31_t) (((q63_t) (Ialpha) * (cosVal)) >> 31);
  408. /* Intermediate product is calculated by (Ibeta * sinVal) */
  409. product2 = (q31_t) (((q63_t) (Ibeta) * (sinVal)) >> 31);
  410. /* Intermediate product is calculated by (Ialpha * sinVal) */
  411. product3 = (q31_t) (((q63_t) (Ialpha) * (sinVal)) >> 31);
  412. /* Intermediate product is calculated by (Ibeta * cosVal) */
  413. product4 = (q31_t) (((q63_t) (Ibeta) * (cosVal)) >> 31);
  414. /* Calculate pId by adding the two intermediate products 1 and 2 */
  415. *pId = __QADD(product1, product2);
  416. /* Calculate pIq by subtracting the two intermediate products 3 from 4 */
  417. *pIq = __QSUB(product4, product3);
  418. }
  419. /**
  420. * @} end of park group
  421. */
  422. /**
  423. * @ingroup groupController
  424. */
  425. /**
  426. * @defgroup inv_park Vector Inverse Park transform
  427. * Inverse Park transform converts the input flux and torque components to two-coordinate vector.
  428. *
  429. * The function operates on a single sample of data and each call to the function returns the processed output.
  430. * The library provides separate functions for Q31 and floating-point data types.
  431. * \par Algorithm
  432. * \image html parkInvFormula.gif
  433. * where <code>pIalpha</code> and <code>pIbeta</code> are the stator vector components,
  434. * <code>Id</code> and <code>Iq</code> are rotor vector components and <code>cosVal</code> and <code>sinVal</code> are the
  435. * cosine and sine values of theta (rotor flux position).
  436. * \par Fixed-Point Behavior
  437. * Care must be taken when using the Q31 version of the Park transform.
  438. * In particular, the overflow and saturation behavior of the accumulator used must be considered.
  439. * Refer to the function specific documentation below for usage guidelines.
  440. */
  441. /**
  442. * @addtogroup inv_park
  443. * @{
  444. */
  445. /**
  446. * @brief Floating-point Inverse Park transform
  447. * @param[in] Id input coordinate of rotor reference frame d
  448. * @param[in] Iq input coordinate of rotor reference frame q
  449. * @param[out] pIalpha points to output two-phase orthogonal vector axis alpha
  450. * @param[out] pIbeta points to output two-phase orthogonal vector axis beta
  451. * @param[in] sinVal sine value of rotation angle theta
  452. * @param[in] cosVal cosine value of rotation angle theta
  453. * @return none
  454. */
  455. __STATIC_FORCEINLINE void arm_inv_park_f32(
  456. float32_t Id,
  457. float32_t Iq,
  458. float32_t * pIalpha,
  459. float32_t * pIbeta,
  460. float32_t sinVal,
  461. float32_t cosVal)
  462. {
  463. /* Calculate pIalpha using the equation, pIalpha = Id * cosVal - Iq * sinVal */
  464. *pIalpha = Id * cosVal - Iq * sinVal;
  465. /* Calculate pIbeta using the equation, pIbeta = Id * sinVal + Iq * cosVal */
  466. *pIbeta = Id * sinVal + Iq * cosVal;
  467. }
  468. /**
  469. @brief Inverse Park transform for Q31 version
  470. @param[in] Id input coordinate of rotor reference frame d
  471. @param[in] Iq input coordinate of rotor reference frame q
  472. @param[out] pIalpha points to output two-phase orthogonal vector axis alpha
  473. @param[out] pIbeta points to output two-phase orthogonal vector axis beta
  474. @param[in] sinVal sine value of rotation angle theta
  475. @param[in] cosVal cosine value of rotation angle theta
  476. @return none
  477. @par Scaling and Overflow Behavior
  478. The function is implemented using an internal 32-bit accumulator.
  479. The accumulator maintains 1.31 format by truncating lower 31 bits of the intermediate multiplication in 2.62 format.
  480. There is saturation on the addition, hence there is no risk of overflow.
  481. */
  482. __STATIC_FORCEINLINE void arm_inv_park_q31(
  483. q31_t Id,
  484. q31_t Iq,
  485. q31_t * pIalpha,
  486. q31_t * pIbeta,
  487. q31_t sinVal,
  488. q31_t cosVal)
  489. {
  490. q31_t product1, product2; /* Temporary variables used to store intermediate results */
  491. q31_t product3, product4; /* Temporary variables used to store intermediate results */
  492. /* Intermediate product is calculated by (Id * cosVal) */
  493. product1 = (q31_t) (((q63_t) (Id) * (cosVal)) >> 31);
  494. /* Intermediate product is calculated by (Iq * sinVal) */
  495. product2 = (q31_t) (((q63_t) (Iq) * (sinVal)) >> 31);
  496. /* Intermediate product is calculated by (Id * sinVal) */
  497. product3 = (q31_t) (((q63_t) (Id) * (sinVal)) >> 31);
  498. /* Intermediate product is calculated by (Iq * cosVal) */
  499. product4 = (q31_t) (((q63_t) (Iq) * (cosVal)) >> 31);
  500. /* Calculate pIalpha by using the two intermediate products 1 and 2 */
  501. *pIalpha = __QSUB(product1, product2);
  502. /* Calculate pIbeta by using the two intermediate products 3 and 4 */
  503. *pIbeta = __QADD(product4, product3);
  504. }
  505. /**
  506. * @} end of Inverse park group
  507. */
  508. /**
  509. * @ingroup groupController
  510. */
  511. /**
  512. * @defgroup clarke Vector Clarke Transform
  513. * Forward Clarke transform converts the instantaneous stator phases into a two-coordinate time invariant vector.
  514. * Generally the Clarke transform uses three-phase currents <code>Ia, Ib and Ic</code> to calculate currents
  515. * in the two-phase orthogonal stator axis <code>Ialpha</code> and <code>Ibeta</code>.
  516. * When <code>Ialpha</code> is superposed with <code>Ia</code> as shown in the figure below
  517. * \image html clarke.gif Stator current space vector and its components in (a,b).
  518. * and <code>Ia + Ib + Ic = 0</code>, in this condition <code>Ialpha</code> and <code>Ibeta</code>
  519. * can be calculated using only <code>Ia</code> and <code>Ib</code>.
  520. *
  521. * The function operates on a single sample of data and each call to the function returns the processed output.
  522. * The library provides separate functions for Q31 and floating-point data types.
  523. * \par Algorithm
  524. * \image html clarkeFormula.gif
  525. * where <code>Ia</code> and <code>Ib</code> are the instantaneous stator phases and
  526. * <code>pIalpha</code> and <code>pIbeta</code> are the two coordinates of time invariant vector.
  527. * \par Fixed-Point Behavior
  528. * Care must be taken when using the Q31 version of the Clarke transform.
  529. * In particular, the overflow and saturation behavior of the accumulator used must be considered.
  530. * Refer to the function specific documentation below for usage guidelines.
  531. */
  532. /**
  533. * @addtogroup clarke
  534. * @{
  535. */
  536. /**
  537. *
  538. * @brief Floating-point Clarke transform
  539. * @param[in] Ia input three-phase coordinate <code>a</code>
  540. * @param[in] Ib input three-phase coordinate <code>b</code>
  541. * @param[out] pIalpha points to output two-phase orthogonal vector axis alpha
  542. * @param[out] pIbeta points to output two-phase orthogonal vector axis beta
  543. * @return none
  544. */
  545. __STATIC_FORCEINLINE void arm_clarke_f32(
  546. float32_t Ia,
  547. float32_t Ib,
  548. float32_t * pIalpha,
  549. float32_t * pIbeta)
  550. {
  551. /* Calculate pIalpha using the equation, pIalpha = Ia */
  552. *pIalpha = Ia;
  553. /* Calculate pIbeta using the equation, pIbeta = (1/sqrt(3)) * Ia + (2/sqrt(3)) * Ib */
  554. *pIbeta = (0.57735026919f * Ia + 1.15470053838f * Ib);
  555. }
  556. /**
  557. @brief Clarke transform for Q31 version
  558. @param[in] Ia input three-phase coordinate <code>a</code>
  559. @param[in] Ib input three-phase coordinate <code>b</code>
  560. @param[out] pIalpha points to output two-phase orthogonal vector axis alpha
  561. @param[out] pIbeta points to output two-phase orthogonal vector axis beta
  562. @return none
  563. \par Scaling and Overflow Behavior
  564. The function is implemented using an internal 32-bit accumulator.
  565. The accumulator maintains 1.31 format by truncating lower 31 bits of the intermediate multiplication in 2.62 format.
  566. There is saturation on the addition, hence there is no risk of overflow.
  567. */
  568. __STATIC_FORCEINLINE void arm_clarke_q31(
  569. q31_t Ia,
  570. q31_t Ib,
  571. q31_t * pIalpha,
  572. q31_t * pIbeta)
  573. {
  574. q31_t product1, product2; /* Temporary variables used to store intermediate results */
  575. /* Calculating pIalpha from Ia by equation pIalpha = Ia */
  576. *pIalpha = Ia;
  577. /* Intermediate product is calculated by (1/(sqrt(3)) * Ia) */
  578. product1 = (q31_t) (((q63_t) Ia * 0x24F34E8B) >> 30);
  579. /* Intermediate product is calculated by (2/sqrt(3) * Ib) */
  580. product2 = (q31_t) (((q63_t) Ib * 0x49E69D16) >> 30);
  581. /* pIbeta is calculated by adding the intermediate products */
  582. *pIbeta = __QADD(product1, product2);
  583. }
  584. /**
  585. * @} end of clarke group
  586. */
  587. /**
  588. * @ingroup groupController
  589. */
  590. /**
  591. * @defgroup inv_clarke Vector Inverse Clarke Transform
  592. * Inverse Clarke transform converts the two-coordinate time invariant vector into instantaneous stator phases.
  593. *
  594. * The function operates on a single sample of data and each call to the function returns the processed output.
  595. * The library provides separate functions for Q31 and floating-point data types.
  596. * \par Algorithm
  597. * \image html clarkeInvFormula.gif
  598. * where <code>pIa</code> and <code>pIb</code> are the instantaneous stator phases and
  599. * <code>Ialpha</code> and <code>Ibeta</code> are the two coordinates of time invariant vector.
  600. * \par Fixed-Point Behavior
  601. * Care must be taken when using the Q31 version of the Clarke transform.
  602. * In particular, the overflow and saturation behavior of the accumulator used must be considered.
  603. * Refer to the function specific documentation below for usage guidelines.
  604. */
  605. /**
  606. * @addtogroup inv_clarke
  607. * @{
  608. */
  609. /**
  610. * @brief Floating-point Inverse Clarke transform
  611. * @param[in] Ialpha input two-phase orthogonal vector axis alpha
  612. * @param[in] Ibeta input two-phase orthogonal vector axis beta
  613. * @param[out] pIa points to output three-phase coordinate <code>a</code>
  614. * @param[out] pIb points to output three-phase coordinate <code>b</code>
  615. * @return none
  616. */
  617. __STATIC_FORCEINLINE void arm_inv_clarke_f32(
  618. float32_t Ialpha,
  619. float32_t Ibeta,
  620. float32_t * pIa,
  621. float32_t * pIb)
  622. {
  623. /* Calculating pIa from Ialpha by equation pIa = Ialpha */
  624. *pIa = Ialpha;
  625. /* Calculating pIb from Ialpha and Ibeta by equation pIb = -(1/2) * Ialpha + (sqrt(3)/2) * Ibeta */
  626. *pIb = -0.5f * Ialpha + 0.8660254039f * Ibeta;
  627. }
  628. /**
  629. @brief Inverse Clarke transform for Q31 version
  630. @param[in] Ialpha input two-phase orthogonal vector axis alpha
  631. @param[in] Ibeta input two-phase orthogonal vector axis beta
  632. @param[out] pIa points to output three-phase coordinate <code>a</code>
  633. @param[out] pIb points to output three-phase coordinate <code>b</code>
  634. @return none
  635. \par Scaling and Overflow Behavior
  636. The function is implemented using an internal 32-bit accumulator.
  637. The accumulator maintains 1.31 format by truncating lower 31 bits of the intermediate multiplication in 2.62 format.
  638. There is saturation on the subtraction, hence there is no risk of overflow.
  639. */
  640. __STATIC_FORCEINLINE void arm_inv_clarke_q31(
  641. q31_t Ialpha,
  642. q31_t Ibeta,
  643. q31_t * pIa,
  644. q31_t * pIb)
  645. {
  646. q31_t product1, product2; /* Temporary variables used to store intermediate results */
  647. /* Calculating pIa from Ialpha by equation pIa = Ialpha */
  648. *pIa = Ialpha;
  649. /* Intermediate product is calculated by (1/(2*sqrt(3)) * Ia) */
  650. product1 = (q31_t) (((q63_t) (Ialpha) * (0x40000000)) >> 31);
  651. /* Intermediate product is calculated by (1/sqrt(3) * pIb) */
  652. product2 = (q31_t) (((q63_t) (Ibeta) * (0x6ED9EBA1)) >> 31);
  653. /* pIb is calculated by subtracting the products */
  654. *pIb = __QSUB(product2, product1);
  655. }
  656. /**
  657. * @} end of inv_clarke group
  658. */
  659. #ifdef __cplusplus
  660. }
  661. #endif
  662. #endif /* ifndef _CONTROLLER_FUNCTIONS_H_ */