controller_functions.h 28 KB

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