controller_functions.h 27 KB

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