SMO_arctan_PLL.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * File: SMO_arctan_PLL.c
  3. *
  4. * Code generated for Simulink model 'SMO_arctan_PLL'.
  5. *
  6. * Model version : 1.812
  7. * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020
  8. * C/C++ source code generated on : Tue Apr 11 20:18:35 2023
  9. *
  10. * Target selection: ert.tlc
  11. * Embedded hardware selection: ARM Compatible->ARM Cortex-M
  12. * Code generation objectives:
  13. * 1. Execution efficiency
  14. * 2. RAM efficiency
  15. * Validation result: Not run
  16. */
  17. #include "SMO_arctan_PLL.h"
  18. #define NumBitsPerChar 8U
  19. extern real_T rt_remd_snf(real_T u0, real_T u1);
  20. static real_T rtGetNaN(void);
  21. static real32_T rtGetNaNF(void);
  22. extern real_T rtInf;
  23. extern real_T rtMinusInf;
  24. extern real_T rtNaN;
  25. extern real32_T rtInfF;
  26. extern real32_T rtMinusInfF;
  27. extern real32_T rtNaNF;
  28. static void rt_InitInfAndNaN(size_t realSize);
  29. static boolean_T rtIsInf(real_T value);
  30. static boolean_T rtIsInfF(real32_T value);
  31. static boolean_T rtIsNaN(real_T value);
  32. static boolean_T rtIsNaNF(real32_T value);
  33. typedef struct {
  34. struct {
  35. uint32_T wordH;
  36. uint32_T wordL;
  37. } words;
  38. } BigEndianIEEEDouble;
  39. typedef struct {
  40. struct {
  41. uint32_T wordL;
  42. uint32_T wordH;
  43. } words;
  44. } LittleEndianIEEEDouble;
  45. typedef struct {
  46. union {
  47. real32_T wordLreal;
  48. uint32_T wordLuint;
  49. } wordL;
  50. } IEEESingle;
  51. real_T rtInf;
  52. real_T rtMinusInf;
  53. real_T rtNaN;
  54. real32_T rtInfF;
  55. real32_T rtMinusInfF;
  56. real32_T rtNaNF;
  57. static real_T rtGetInf(void);
  58. static real32_T rtGetInfF(void);
  59. static real_T rtGetMinusInf(void);
  60. static real32_T rtGetMinusInfF(void);
  61. /*===========*
  62. * Constants *
  63. *===========*/
  64. #define RT_PI 3.14159265358979323846
  65. #define RT_PIF 3.1415927F
  66. #define RT_LN_10 2.30258509299404568402
  67. #define RT_LN_10F 2.3025851F
  68. #define RT_LOG10E 0.43429448190325182765
  69. #define RT_LOG10EF 0.43429449F
  70. #define RT_E 2.7182818284590452354
  71. #define RT_EF 2.7182817F
  72. /*
  73. * UNUSED_PARAMETER(x)
  74. * Used to specify that a function parameter (argument) is required but not
  75. * accessed by the function body.
  76. */
  77. #ifndef UNUSED_PARAMETER
  78. #if defined(__LCC__)
  79. #define UNUSED_PARAMETER(x) /* do nothing */
  80. #else
  81. /*
  82. * This is the semi-ANSI standard way of indicating that an
  83. * unused function parameter is required.
  84. */
  85. #define UNUSED_PARAMETER(x) (void) (x)
  86. #endif
  87. #endif
  88. /*
  89. * Initialize rtNaN needed by the generated code.
  90. * NaN is initialized as non-signaling. Assumes IEEE.
  91. */
  92. static real_T rtGetNaN(void)
  93. {
  94. size_t bitsPerReal = sizeof(real_T) * (NumBitsPerChar);
  95. real_T nan = 0.0;
  96. if (bitsPerReal == 32U) {
  97. nan = rtGetNaNF();
  98. } else {
  99. union {
  100. LittleEndianIEEEDouble bitVal;
  101. real_T fltVal;
  102. } tmpVal;
  103. tmpVal.bitVal.words.wordH = 0xFFF80000U;
  104. tmpVal.bitVal.words.wordL = 0x00000000U;
  105. nan = tmpVal.fltVal;
  106. }
  107. return nan;
  108. }
  109. /*
  110. * Initialize rtNaNF needed by the generated code.
  111. * NaN is initialized as non-signaling. Assumes IEEE.
  112. */
  113. static real32_T rtGetNaNF(void)
  114. {
  115. IEEESingle nanF = { { 0 } };
  116. nanF.wordL.wordLuint = 0xFFC00000U;
  117. return nanF.wordL.wordLreal;
  118. }
  119. /*
  120. * Initialize the rtInf, rtMinusInf, and rtNaN needed by the
  121. * generated code. NaN is initialized as non-signaling. Assumes IEEE.
  122. */
  123. static void rt_InitInfAndNaN(size_t realSize)
  124. {
  125. (void) (realSize);
  126. rtNaN = rtGetNaN();
  127. rtNaNF = rtGetNaNF();
  128. rtInf = rtGetInf();
  129. rtInfF = rtGetInfF();
  130. rtMinusInf = rtGetMinusInf();
  131. rtMinusInfF = rtGetMinusInfF();
  132. }
  133. /* Test if value is infinite */
  134. static boolean_T rtIsInf(real_T value)
  135. {
  136. return (boolean_T)((value==rtInf || value==rtMinusInf) ? 1U : 0U);
  137. }
  138. /* Test if single-precision value is infinite */
  139. static boolean_T rtIsInfF(real32_T value)
  140. {
  141. return (boolean_T)(((value)==rtInfF || (value)==rtMinusInfF) ? 1U : 0U);
  142. }
  143. /* Test if value is not a number */
  144. static boolean_T rtIsNaN(real_T value)
  145. {
  146. boolean_T result = (boolean_T) 0;
  147. size_t bitsPerReal = sizeof(real_T) * (NumBitsPerChar);
  148. if (bitsPerReal == 32U) {
  149. result = rtIsNaNF((real32_T)value);
  150. } else {
  151. union {
  152. LittleEndianIEEEDouble bitVal;
  153. real_T fltVal;
  154. } tmpVal;
  155. tmpVal.fltVal = value;
  156. result = (boolean_T)((tmpVal.bitVal.words.wordH & 0x7FF00000) == 0x7FF00000 &&
  157. ( (tmpVal.bitVal.words.wordH & 0x000FFFFF) != 0 ||
  158. (tmpVal.bitVal.words.wordL != 0) ));
  159. }
  160. return result;
  161. }
  162. /* Test if single-precision value is not a number */
  163. static boolean_T rtIsNaNF(real32_T value)
  164. {
  165. IEEESingle tmp;
  166. tmp.wordL.wordLreal = value;
  167. return (boolean_T)( (tmp.wordL.wordLuint & 0x7F800000) == 0x7F800000 &&
  168. (tmp.wordL.wordLuint & 0x007FFFFF) != 0 );
  169. }
  170. /*
  171. * Initialize rtInf needed by the generated code.
  172. * Inf is initialized as non-signaling. Assumes IEEE.
  173. */
  174. static real_T rtGetInf(void)
  175. {
  176. size_t bitsPerReal = sizeof(real_T) * (NumBitsPerChar);
  177. real_T inf = 0.0;
  178. if (bitsPerReal == 32U) {
  179. inf = rtGetInfF();
  180. } else {
  181. union {
  182. LittleEndianIEEEDouble bitVal;
  183. real_T fltVal;
  184. } tmpVal;
  185. tmpVal.bitVal.words.wordH = 0x7FF00000U;
  186. tmpVal.bitVal.words.wordL = 0x00000000U;
  187. inf = tmpVal.fltVal;
  188. }
  189. return inf;
  190. }
  191. /*
  192. * Initialize rtInfF needed by the generated code.
  193. * Inf is initialized as non-signaling. Assumes IEEE.
  194. */
  195. static real32_T rtGetInfF(void)
  196. {
  197. IEEESingle infF;
  198. infF.wordL.wordLuint = 0x7F800000U;
  199. return infF.wordL.wordLreal;
  200. }
  201. /*
  202. * Initialize rtMinusInf needed by the generated code.
  203. * Inf is initialized as non-signaling. Assumes IEEE.
  204. */
  205. static real_T rtGetMinusInf(void)
  206. {
  207. size_t bitsPerReal = sizeof(real_T) * (NumBitsPerChar);
  208. real_T minf = 0.0;
  209. if (bitsPerReal == 32U) {
  210. minf = rtGetMinusInfF();
  211. } else {
  212. union {
  213. LittleEndianIEEEDouble bitVal;
  214. real_T fltVal;
  215. } tmpVal;
  216. tmpVal.bitVal.words.wordH = 0xFFF00000U;
  217. tmpVal.bitVal.words.wordL = 0x00000000U;
  218. minf = tmpVal.fltVal;
  219. }
  220. return minf;
  221. }
  222. /*
  223. * Initialize rtMinusInfF needed by the generated code.
  224. * Inf is initialized as non-signaling. Assumes IEEE.
  225. */
  226. static real32_T rtGetMinusInfF(void)
  227. {
  228. IEEESingle minfF;
  229. minfF.wordL.wordLuint = 0xFF800000U;
  230. return minfF.wordL.wordLreal;
  231. }
  232. real_T rt_remd_snf(real_T u0, real_T u1)
  233. {
  234. real_T q;
  235. real_T y;
  236. if (rtIsNaN(u0) || rtIsNaN(u1) || rtIsInf(u0)) {
  237. y = (rtNaN);
  238. } else if (rtIsInf(u1)) {
  239. y = u0;
  240. } else if ((u1 != 0.0) && (u1 != trunc(u1))) {
  241. q = fabs(u0 / u1);
  242. if (!(fabs(q - floor(q + 0.5)) > DBL_EPSILON * q)) {
  243. y = 0.0 * u0;
  244. } else {
  245. y = fmod(u0, u1);
  246. }
  247. } else {
  248. y = fmod(u0, u1);
  249. }
  250. return y;
  251. }
  252. /* Model step function */
  253. void SMO_arctan_PLL_step(RT_MODEL *const rtM, real_T rtU_Ialfabeta[2], real_T
  254. rtU_Ualfabeta[2], real_T *rtY_theta, real_T *rtY_we)
  255. {
  256. DW *rtDW = rtM->dwork;
  257. real_T rtb_Add1;
  258. real_T rtb_Add1_h;
  259. real_T rtb_Product4;
  260. real_T rtb_Sum;
  261. real_T rtb_Sum2;
  262. real_T rtb_Sum5;
  263. real_T rtb_Switch_e;
  264. /* Sum: '<S7>/Sum2' incorporates:
  265. * DiscreteIntegrator: '<S7>/Discrete-Time Integrator'
  266. * Inport: '<Root>/Ialfa,beta'
  267. */
  268. rtb_Sum2 = rtDW->DiscreteTimeIntegrator_DSTATE_e - rtU_Ialfabeta[0];
  269. /* Switch: '<S58>/Switch' incorporates:
  270. * Abs: '<S58>/Abs'
  271. * Constant: '<S7>/Constant1'
  272. * Constant: '<S7>/Constant3'
  273. * Product: '<S58>/Divide'
  274. * Product: '<S58>/Divide1'
  275. * Product: '<S58>/Divide2'
  276. * Sum: '<S58>/Add'
  277. */
  278. if (fabs(rtb_Sum2) - 120.0 > 0.0) {
  279. /* Signum: '<S58>/Sign' */
  280. if (rtb_Sum2 < 0.0) {
  281. rtb_Sum2 = -1.0;
  282. } else if (rtb_Sum2 > 0.0) {
  283. rtb_Sum2 = 1.0;
  284. } else if (rtb_Sum2 == 0.0) {
  285. rtb_Sum2 = 0.0;
  286. } else {
  287. rtb_Sum2 = (rtNaN);
  288. }
  289. /* End of Signum: '<S58>/Sign' */
  290. rtb_Sum2 *= 100.0;
  291. } else {
  292. rtb_Sum2 = rtb_Sum2 / 120.0 * 100.0;
  293. }
  294. /* End of Switch: '<S58>/Switch' */
  295. /* Sum: '<S7>/Sum5' incorporates:
  296. * DiscreteIntegrator: '<S7>/Discrete-Time Integrator1'
  297. * Inport: '<Root>/Ialfa,beta'
  298. */
  299. rtb_Sum5 = rtDW->DiscreteTimeIntegrator1_DSTATE - rtU_Ialfabeta[1];
  300. /* Switch: '<S57>/Switch' incorporates:
  301. * Abs: '<S57>/Abs'
  302. * Constant: '<S7>/Constant4'
  303. * Constant: '<S7>/Constant5'
  304. * Product: '<S57>/Divide'
  305. * Product: '<S57>/Divide1'
  306. * Product: '<S57>/Divide2'
  307. * Sum: '<S57>/Add'
  308. */
  309. if (fabs(rtb_Sum5) - 120.0 > 0.0) {
  310. /* Signum: '<S57>/Sign' */
  311. if (rtb_Sum5 < 0.0) {
  312. rtb_Sum5 = -1.0;
  313. } else if (rtb_Sum5 > 0.0) {
  314. rtb_Sum5 = 1.0;
  315. } else if (rtb_Sum5 == 0.0) {
  316. rtb_Sum5 = 0.0;
  317. } else {
  318. rtb_Sum5 = (rtNaN);
  319. }
  320. /* End of Signum: '<S57>/Sign' */
  321. rtb_Switch_e = rtb_Sum5 * 100.0;
  322. } else {
  323. rtb_Switch_e = rtb_Sum5 / 120.0 * 100.0;
  324. }
  325. /* End of Switch: '<S57>/Switch' */
  326. /* Sqrt: '<S6>/Sqrt' incorporates:
  327. * Constant: '<S6>/Constant'
  328. * Math: '<S6>/Math Function'
  329. * Math: '<S6>/Math Function1'
  330. * Sum: '<S6>/Add'
  331. */
  332. rtb_Sum5 = sqrt((rtb_Sum2 * rtb_Sum2 + rtb_Switch_e * rtb_Switch_e) + 1.0E-6);
  333. /* Sum: '<S6>/Sum' incorporates:
  334. * DiscreteIntegrator: '<S6>/Discrete-Time Integrator'
  335. * Gain: '<S6>/Gain'
  336. * Product: '<S6>/Divide'
  337. * Product: '<S6>/Divide2'
  338. * Product: '<S6>/Product'
  339. * Product: '<S6>/Product1'
  340. * Trigonometry: '<S6>/Trigonometric Function'
  341. * Trigonometry: '<S6>/Trigonometric Function1'
  342. */
  343. rtb_Sum5 = -(rtb_Sum2 / rtb_Sum5) * cos(rtDW->DiscreteTimeIntegrator_DSTATE) -
  344. rtb_Switch_e / rtb_Sum5 * sin(rtDW->DiscreteTimeIntegrator_DSTATE);
  345. /* Sum: '<S49>/Sum' incorporates:
  346. * DiscreteIntegrator: '<S40>/Integrator'
  347. * Gain: '<S45>/Proportional Gain'
  348. */
  349. rtb_Sum = 12566.370614359172 * rtb_Sum5 + rtDW->Integrator_DSTATE;
  350. /* Sum: '<S5>/Add1' incorporates:
  351. * Constant: '<S5>/Filter_Constant'
  352. * Constant: '<S5>/One'
  353. * Product: '<S5>/Product'
  354. * Product: '<S5>/Product1'
  355. * UnitDelay: '<S5>/Unit Delay'
  356. */
  357. rtb_Add1 = rtb_Sum * 0.01885 + 0.98115 * rtDW->UnitDelay_DSTATE;
  358. /* Sum: '<S4>/Add1' incorporates:
  359. * Constant: '<S4>/Filter_Constant'
  360. * Constant: '<S4>/One'
  361. * Product: '<S4>/Product'
  362. * Product: '<S4>/Product1'
  363. * UnitDelay: '<S4>/Unit Delay'
  364. */
  365. rtb_Add1_h = rtb_Add1 * 0.1 + 0.9 * rtDW->UnitDelay_DSTATE_h;
  366. /* Outport: '<Root>/we' incorporates:
  367. * Constant: '<S1>/P'
  368. * Gain: '<S1>/Gain3'
  369. * Product: '<S1>/Divide1'
  370. */
  371. *rtY_we = 9.5492965855137211 * rtb_Add1_h / 4.0;
  372. /* Product: '<S7>/Product4' incorporates:
  373. * Constant: '<S7>/Constant2'
  374. */
  375. rtb_Product4 = rtb_Add1 * 0.000178;
  376. /* Sum: '<S7>/ ' incorporates:
  377. * DiscreteIntegrator: '<S7>/Discrete-Time Integrator'
  378. * DiscreteIntegrator: '<S7>/Discrete-Time Integrator1'
  379. * Gain: '<S7>/ 1//L'
  380. * Gain: '<S7>/ 1//L '
  381. * Gain: '<S7>/Gain5'
  382. * Gain: '<S7>/R//L '
  383. * Inport: '<Root>/Ualfa,beta'
  384. * Product: '<S7>/Product3'
  385. */
  386. rtb_Switch_e = ((5617.9775280898875 * rtU_Ualfabeta[1] - 61.797752808988761 *
  387. rtDW->DiscreteTimeIntegrator1_DSTATE) - 5617.9775280898875 *
  388. rtb_Switch_e) + 5617.9775280898875 * rtb_Product4 *
  389. rtDW->DiscreteTimeIntegrator_DSTATE_e;
  390. /* Outport: '<Root>/theta' incorporates:
  391. * Constant: '<S2>/Constant1'
  392. * DiscreteIntegrator: '<S6>/Discrete-Time Integrator'
  393. * Math: '<S2>/Rem1'
  394. * Sum: '<S2>/Add1'
  395. */
  396. *rtY_theta = rt_remd_snf(rtDW->DiscreteTimeIntegrator_DSTATE +
  397. 6.2831853071795862, 6.2831853071795862);
  398. /* Update for DiscreteIntegrator: '<S6>/Discrete-Time Integrator' */
  399. rtDW->DiscreteTimeIntegrator_DSTATE += 6.0E-5 * rtb_Sum;
  400. /* Update for DiscreteIntegrator: '<S7>/Discrete-Time Integrator' incorporates:
  401. * DiscreteIntegrator: '<S7>/Discrete-Time Integrator1'
  402. * Gain: '<S7>/1//L'
  403. * Gain: '<S7>/1//L '
  404. * Gain: '<S7>/Gain4'
  405. * Gain: '<S7>/R//L'
  406. * Inport: '<Root>/Ualfa,beta'
  407. * Product: '<S7>/Product2'
  408. * Sum: '<S7>/ '
  409. */
  410. rtDW->DiscreteTimeIntegrator_DSTATE_e += (((5617.9775280898875 *
  411. rtU_Ualfabeta[0] - 61.797752808988761 *
  412. rtDW->DiscreteTimeIntegrator_DSTATE_e) - 5617.9775280898875 * rtb_Sum2) -
  413. 5617.9775280898875 * rtb_Product4 * rtDW->DiscreteTimeIntegrator1_DSTATE) *
  414. 6.0E-5;
  415. /* Update for DiscreteIntegrator: '<S7>/Discrete-Time Integrator1' */
  416. rtDW->DiscreteTimeIntegrator1_DSTATE += 6.0E-5 * rtb_Switch_e;
  417. /* Update for DiscreteIntegrator: '<S40>/Integrator' incorporates:
  418. * Gain: '<S37>/Integral Gain'
  419. */
  420. rtDW->Integrator_DSTATE += 3.9478417604357429E+7 * rtb_Sum5 * 6.0E-5;
  421. /* Update for UnitDelay: '<S5>/Unit Delay' */
  422. rtDW->UnitDelay_DSTATE = rtb_Add1;
  423. /* Update for UnitDelay: '<S4>/Unit Delay' */
  424. rtDW->UnitDelay_DSTATE_h = rtb_Add1_h;
  425. }
  426. /* Model initialize function */
  427. void SMO_arctan_PLL_initialize(RT_MODEL *const rtM)
  428. {
  429. /* Registration code */
  430. /* initialize non-finites */
  431. rt_InitInfAndNaN(sizeof(real_T));
  432. UNUSED_PARAMETER(rtM);
  433. }
  434. /*
  435. * File trailer for generated code.
  436. *
  437. * [EOF]
  438. */