PMSM_Controller.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * File: PMSM_Controller.c
  3. *
  4. * Code generated for Simulink model 'PMSM_Controller'.
  5. *
  6. * Model version : 1.885
  7. * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020
  8. * C/C++ source code generated on : Sat Jan 8 15:44:11 2022
  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 "PMSM_Controller.h"
  18. #include "PMSM_Controller_private.h"
  19. static void rate_scheduler(RT_MODEL *const rtM);
  20. uint16_T plook_u16u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace,
  21. uint32_T maxIndex)
  22. {
  23. uint16_T bpIndex;
  24. /* Prelookup - Index only
  25. Index Search method: 'even'
  26. Extrapolation method: 'Clip'
  27. Use previous index: 'off'
  28. Use last breakpoint for index at or above upper limit: 'on'
  29. Remove protection against out-of-range input in generated code: 'off'
  30. */
  31. if (u <= bp0) {
  32. bpIndex = 0U;
  33. } else {
  34. bpIndex = (uint16_T)((uint32_T)(uint16_T)((uint32_T)u - bp0) / bpSpace);
  35. if (bpIndex < maxIndex) {
  36. } else {
  37. bpIndex = (uint16_T)maxIndex;
  38. }
  39. }
  40. return bpIndex;
  41. }
  42. int32_T div_nde_s32_floor(int32_T numerator, int32_T denominator)
  43. {
  44. return (((numerator < 0) != (denominator < 0)) && (numerator % denominator !=
  45. 0) ? -1 : 0) + numerator / denominator;
  46. }
  47. /*
  48. * This function updates active task flag for each subrate.
  49. * The function is called at model base rate, hence the
  50. * generated code self-manages all its subrates.
  51. */
  52. static void rate_scheduler(RT_MODEL *const rtM)
  53. {
  54. /* Compute which subrates run during the next base time step. Subrates
  55. * are an integer multiple of the base rate counter. Therefore, the subtask
  56. * counter is reset when it reaches its limit (zero means run).
  57. */
  58. (rtM->Timing.TaskCounters.TID[1])++;
  59. if ((rtM->Timing.TaskCounters.TID[1]) > 9) {/* Sample time: [6.2E-5s, 0.0s] */
  60. rtM->Timing.TaskCounters.TID[1] = 0;
  61. }
  62. }
  63. real_T rt_modd_snf(real_T u0, real_T u1)
  64. {
  65. real_T q;
  66. real_T y;
  67. boolean_T yEq;
  68. y = u0;
  69. if (u1 == 0.0) {
  70. if (u0 == 0.0) {
  71. y = u1;
  72. }
  73. } else if (rtIsNaN(u0) || rtIsNaN(u1) || rtIsInf(u0)) {
  74. y = (rtNaN);
  75. } else if (u0 == 0.0) {
  76. y = 0.0 / u1;
  77. } else if (rtIsInf(u1)) {
  78. if ((u1 < 0.0) != (u0 < 0.0)) {
  79. y = u1;
  80. }
  81. } else {
  82. y = fmod(u0, u1);
  83. yEq = (y == 0.0);
  84. if ((!yEq) && (u1 > floor(u1))) {
  85. q = fabs(u0 / u1);
  86. yEq = !(fabs(q - floor(q + 0.5)) > DBL_EPSILON * q);
  87. }
  88. if (yEq) {
  89. y = u1 * 0.0;
  90. } else {
  91. if ((u0 < 0.0) != (u1 < 0.0)) {
  92. y += u1;
  93. }
  94. }
  95. }
  96. return y;
  97. }
  98. real_T rt_roundd_snf(real_T u)
  99. {
  100. real_T y;
  101. if (fabs(u) < 4.503599627370496E+15) {
  102. if (u >= 0.5) {
  103. y = floor(u + 0.5);
  104. } else if (u > -0.5) {
  105. y = u * 0.0;
  106. } else {
  107. y = ceil(u - 0.5);
  108. }
  109. } else {
  110. y = u;
  111. }
  112. return y;
  113. }
  114. /* Model step function */
  115. void PMSM_Controller_step(RT_MODEL *const rtM, int16_T rtU_Vdc, uint16_T rtU_Ts,
  116. int16_T rtU_adc_a, int16_T rtU_adc_b, real_T rtU_rotor_angle, int16_T
  117. rtU_rotor_speed, int16_T rtU_input_target, uint16_T rtY_pwm_counts[8], uint8_T
  118. *rtY_sector)
  119. {
  120. DW *rtDW = rtM->dwork;
  121. int32_T rtb_Sum1_tmp;
  122. int32_T rtb_Sum3_idx_0;
  123. int32_T rtb_Sum3_idx_1;
  124. int32_T tmp;
  125. int32_T tmp_0;
  126. int16_T rtb_Divide;
  127. int16_T rtb_Divide4;
  128. int16_T rtb_Divide7;
  129. int16_T rtb_Multiply_idx_0;
  130. int16_T rtb_Multiply_idx_1;
  131. int16_T rtb_SignPreIntegrator;
  132. int16_T rtb_SignPreSat;
  133. int16_T rtb_SignPreSat_k;
  134. int16_T rtb_Sum1;
  135. int16_T rtb_Sum2;
  136. int16_T rtb_Sum3_l;
  137. uint16_T rtb_DataTypeConversion3;
  138. uint16_T rtb_MultiportSwitch_idx_0;
  139. uint16_T rtb_MultiportSwitch_idx_1;
  140. uint16_T rtb_o_samp_p1;
  141. uint8_T rtb_DataTypeConversion_a;
  142. boolean_T rtb_NotEqual;
  143. /* Gain: '<S1>/Multiply' incorporates:
  144. * Inport: '<Root>/adc_a'
  145. * Inport: '<Root>/adc_b'
  146. */
  147. rtb_Multiply_idx_0 = (int16_T)((19761 * rtU_adc_a) >> 17);
  148. rtb_Multiply_idx_1 = (int16_T)((19761 * rtU_adc_b) >> 17);
  149. /* Outputs for Atomic SubSystem: '<S1>/controller' */
  150. /* PreLookup: '<S23>/a_elecAngle_XA' incorporates:
  151. * Constant: '<S1>/Constant1'
  152. * Gain: '<S1>/Gain'
  153. * Inport: '<Root>/rotor_angle'
  154. * Math: '<S1>/Math Function'
  155. */
  156. rtb_o_samp_p1 = plook_u16u16_evencka((uint16_T)(57.295779513082323 *
  157. rt_modd_snf(rtU_rotor_angle, 6.2831853071795862)), 0U, 1U, 360U);
  158. /* Sum: '<S15>/Add1' incorporates:
  159. * Product: '<S177>/Divide4'
  160. * Sum: '<S15>/Add'
  161. */
  162. rtb_Divide4 = (int16_T)-(rtb_Multiply_idx_0 + rtb_Multiply_idx_1);
  163. /* Gain: '<S20>/Gain1' incorporates:
  164. * Gain: '<S20>/Gain'
  165. * Product: '<S177>/Divide'
  166. * Product: '<S177>/Divide4'
  167. * Sum: '<S20>/Add'
  168. * Sum: '<S20>/Add1'
  169. */
  170. rtb_Divide = (int16_T)(((((rtb_Multiply_idx_0 << 1) - rtb_Multiply_idx_1) -
  171. rtb_Divide4) * 21845) >> 16);
  172. /* Gain: '<S20>/Gain2' incorporates:
  173. * Product: '<S177>/Divide4'
  174. * Sum: '<S20>/Add2'
  175. */
  176. rtb_Divide4 = (int16_T)(((rtb_Multiply_idx_1 - rtb_Divide4) * 18919) >> 15);
  177. /* Sum: '<S22>/Sum1' incorporates:
  178. * Interpolation_n-D: '<S23>/r_cos_M1'
  179. * Interpolation_n-D: '<S23>/r_sin_M1'
  180. * Product: '<S177>/Divide'
  181. * Product: '<S177>/Divide4'
  182. * Product: '<S22>/Divide2'
  183. * Product: '<S22>/Divide3'
  184. */
  185. rtb_Sum1_tmp = ((rtb_Divide * rtConstP.r_cos_M1_Table[rtb_o_samp_p1]) >> 14) +
  186. ((rtb_Divide4 * rtConstP.r_sin_M1_Table[rtb_o_samp_p1]) >> 14);
  187. if (rtb_Sum1_tmp > 32767) {
  188. rtb_Sum1_tmp = 32767;
  189. } else {
  190. if (rtb_Sum1_tmp < -32768) {
  191. rtb_Sum1_tmp = -32768;
  192. }
  193. }
  194. /* Sum: '<S21>/Sum2' incorporates:
  195. * Sum: '<S22>/Sum1'
  196. * UnitDelay: '<S21>/UnitDelay1'
  197. */
  198. rtb_Sum1_tmp -= rtDW->UnitDelay1_DSTATE[0] >> 16;
  199. if (rtb_Sum1_tmp > 32767) {
  200. rtb_Sum1_tmp = 32767;
  201. } else {
  202. if (rtb_Sum1_tmp < -32768) {
  203. rtb_Sum1_tmp = -32768;
  204. }
  205. }
  206. /* Sum: '<S21>/Sum3' incorporates:
  207. * Constant: '<S15>/Constant'
  208. * Product: '<S21>/Divide3'
  209. * Sum: '<S21>/Sum2'
  210. * UnitDelay: '<S21>/UnitDelay1'
  211. */
  212. rtb_Sum3_idx_0 = 13107 * rtb_Sum1_tmp + rtDW->UnitDelay1_DSTATE[0];
  213. /* Sum: '<S22>/Sum6' incorporates:
  214. * Interpolation_n-D: '<S23>/r_cos_M1'
  215. * Interpolation_n-D: '<S23>/r_sin_M1'
  216. * Product: '<S177>/Divide'
  217. * Product: '<S177>/Divide4'
  218. * Product: '<S22>/Divide1'
  219. * Product: '<S22>/Divide4'
  220. */
  221. rtb_Sum1_tmp = ((rtb_Divide4 * rtConstP.r_cos_M1_Table[rtb_o_samp_p1]) >> 14)
  222. - ((rtb_Divide * rtConstP.r_sin_M1_Table[rtb_o_samp_p1]) >> 14);
  223. if (rtb_Sum1_tmp > 32767) {
  224. rtb_Sum1_tmp = 32767;
  225. } else {
  226. if (rtb_Sum1_tmp < -32768) {
  227. rtb_Sum1_tmp = -32768;
  228. }
  229. }
  230. /* Sum: '<S21>/Sum2' incorporates:
  231. * Sum: '<S22>/Sum6'
  232. * UnitDelay: '<S21>/UnitDelay1'
  233. */
  234. rtb_Sum1_tmp -= rtDW->UnitDelay1_DSTATE[1] >> 16;
  235. if (rtb_Sum1_tmp > 32767) {
  236. rtb_Sum1_tmp = 32767;
  237. } else {
  238. if (rtb_Sum1_tmp < -32768) {
  239. rtb_Sum1_tmp = -32768;
  240. }
  241. }
  242. /* Sum: '<S21>/Sum3' incorporates:
  243. * Constant: '<S15>/Constant'
  244. * Product: '<S21>/Divide3'
  245. * Sum: '<S21>/Sum2'
  246. * UnitDelay: '<S21>/UnitDelay1'
  247. */
  248. rtb_Sum3_idx_1 = 13107 * rtb_Sum1_tmp + rtDW->UnitDelay1_DSTATE[1];
  249. /* DataTypeConversion: '<S21>/Data Type Conversion' incorporates:
  250. * Sum: '<S21>/Sum3'
  251. */
  252. rtb_Multiply_idx_0 = (int16_T)(rtb_Sum3_idx_0 >> 16);
  253. /* Sum: '<S69>/Sum' incorporates:
  254. * DiscreteIntegrator: '<S60>/Integrator'
  255. * Gain: '<S65>/Proportional Gain'
  256. * Signum: '<S51>/SignPreSat'
  257. * Sum: '<S16>/Sum1'
  258. */
  259. rtb_Multiply_idx_1 = (int16_T)((((int16_T)(9 * (int16_T)-rtb_Multiply_idx_0) <<
  260. 15) + (int32_T)((rtDW->Integrator_DSTATE * 1704243LL) >> 23)) >> 15);
  261. /* Saturate: '<S67>/Saturation' incorporates:
  262. * Product: '<S177>/Divide'
  263. * Signum: '<S51>/SignPreSat'
  264. */
  265. if (rtb_Multiply_idx_1 > 8960) {
  266. rtb_Divide = 8960;
  267. } else if (rtb_Multiply_idx_1 < -8960) {
  268. rtb_Divide = -8960;
  269. } else {
  270. rtb_Divide = rtb_Multiply_idx_1;
  271. }
  272. /* End of Saturate: '<S67>/Saturation' */
  273. /* Sum: '<S16>/Sum3' incorporates:
  274. * Inport: '<Root>/input_target'
  275. * Inport: '<Root>/rotor_speed'
  276. */
  277. rtb_Sum3_l = (int16_T)(rtU_input_target - rtU_rotor_speed);
  278. /* Sum: '<S169>/Sum' incorporates:
  279. * DiscreteIntegrator: '<S160>/Integrator'
  280. * Gain: '<S165>/Proportional Gain'
  281. * Signum: '<S151>/SignPreSat'
  282. */
  283. rtb_SignPreSat = (int16_T)((rtDW->Integrator_DSTATE_i * 26629 < 0 ? -1 : 0) +
  284. (rtb_Sum3_l << 4));
  285. /* Saturate: '<S167>/Saturation' incorporates:
  286. * Signum: '<S151>/SignPreSat'
  287. */
  288. if (rtb_SignPreSat > 800) {
  289. rtb_Divide4 = 800;
  290. } else if (rtb_SignPreSat < -800) {
  291. rtb_Divide4 = -800;
  292. } else {
  293. rtb_Divide4 = rtb_SignPreSat;
  294. }
  295. /* End of Saturate: '<S167>/Saturation' */
  296. /* Sum: '<S16>/Sum2' incorporates:
  297. * DataTypeConversion: '<S21>/Data Type Conversion'
  298. * Sum: '<S21>/Sum3'
  299. */
  300. rtb_Sum2 = (int16_T)(rtb_Divide4 - (int16_T)(rtb_Sum3_idx_1 >> 16));
  301. /* Sum: '<S119>/Sum' incorporates:
  302. * DiscreteIntegrator: '<S110>/Integrator'
  303. * Gain: '<S115>/Proportional Gain'
  304. * Signum: '<S101>/SignPreSat'
  305. * Sum: '<S16>/Sum2'
  306. */
  307. rtb_SignPreSat_k = (int16_T)((((int16_T)(10 * rtb_Sum2) << 15) + (int32_T)
  308. ((rtDW->Integrator_DSTATE_p * 1704243LL) >> 23)) >> 15);
  309. /* Saturate: '<S117>/Saturation' incorporates:
  310. * Signum: '<S101>/SignPreSat'
  311. * Signum: '<S51>/SignPreIntegrator'
  312. */
  313. if (rtb_SignPreSat_k > 8960) {
  314. rtb_SignPreIntegrator = 8960;
  315. } else if (rtb_SignPreSat_k < -8960) {
  316. rtb_SignPreIntegrator = -8960;
  317. } else {
  318. rtb_SignPreIntegrator = rtb_SignPreSat_k;
  319. }
  320. /* End of Saturate: '<S117>/Saturation' */
  321. /* Sum: '<S18>/Sum1' incorporates:
  322. * Interpolation_n-D: '<S23>/r_cos_M1'
  323. * Interpolation_n-D: '<S23>/r_sin_M1'
  324. * Product: '<S177>/Divide'
  325. * Product: '<S18>/Divide2'
  326. * Product: '<S18>/Divide3'
  327. * Signum: '<S51>/SignPreIntegrator'
  328. */
  329. rtb_Sum1_tmp = ((rtb_Divide * rtConstP.r_sin_M1_Table[rtb_o_samp_p1]) >> 14) +
  330. ((rtb_SignPreIntegrator * rtConstP.r_cos_M1_Table[rtb_o_samp_p1]) >> 14);
  331. if (rtb_Sum1_tmp > 32767) {
  332. rtb_Sum1_tmp = 32767;
  333. } else {
  334. if (rtb_Sum1_tmp < -32768) {
  335. rtb_Sum1_tmp = -32768;
  336. }
  337. }
  338. /* Sum: '<S18>/Sum6' incorporates:
  339. * Interpolation_n-D: '<S23>/r_cos_M1'
  340. * Interpolation_n-D: '<S23>/r_sin_M1'
  341. * Product: '<S177>/Divide'
  342. * Product: '<S18>/Divide1'
  343. * Product: '<S18>/Divide4'
  344. * Signum: '<S51>/SignPreIntegrator'
  345. */
  346. tmp_0 = ((rtb_Divide * rtConstP.r_cos_M1_Table[rtb_o_samp_p1]) >> 14) -
  347. ((rtb_SignPreIntegrator * rtConstP.r_sin_M1_Table[rtb_o_samp_p1]) >> 14);
  348. if (tmp_0 > 32767) {
  349. tmp_0 = 32767;
  350. } else {
  351. if (tmp_0 < -32768) {
  352. tmp_0 = -32768;
  353. }
  354. }
  355. /* Product: '<S177>/Divide7' incorporates:
  356. * Constant: '<S177>/Constant3'
  357. * Sum: '<S18>/Sum1'
  358. */
  359. rtb_Divide7 = (int16_T)((2365 * (int16_T)rtb_Sum1_tmp) >> 10);
  360. /* MATLAB Function: '<S177>/sector_select' incorporates:
  361. * Product: '<S177>/Divide7'
  362. * Sum: '<S18>/Sum1'
  363. * Sum: '<S18>/Sum6'
  364. */
  365. if ((int16_T)rtb_Sum1_tmp >= 0) {
  366. if ((int16_T)tmp_0 >= 0) {
  367. if (rtb_Divide7 > ((int16_T)tmp_0 << 2)) {
  368. /* DataTypeConversion: '<S177>/Data Type Conversion' */
  369. rtb_DataTypeConversion_a = 2U;
  370. } else {
  371. /* DataTypeConversion: '<S177>/Data Type Conversion' */
  372. rtb_DataTypeConversion_a = 1U;
  373. }
  374. } else {
  375. tmp = -rtb_Divide7;
  376. if (-rtb_Divide7 > 32767) {
  377. tmp = 32767;
  378. }
  379. if (tmp > ((int16_T)tmp_0 << 2)) {
  380. /* DataTypeConversion: '<S177>/Data Type Conversion' */
  381. rtb_DataTypeConversion_a = 3U;
  382. } else {
  383. /* DataTypeConversion: '<S177>/Data Type Conversion' */
  384. rtb_DataTypeConversion_a = 2U;
  385. }
  386. }
  387. } else if ((int16_T)tmp_0 >= 0) {
  388. tmp = -rtb_Divide7;
  389. if (-rtb_Divide7 > 32767) {
  390. tmp = 32767;
  391. }
  392. if (tmp > ((int16_T)tmp_0 << 2)) {
  393. /* DataTypeConversion: '<S177>/Data Type Conversion' */
  394. rtb_DataTypeConversion_a = 5U;
  395. } else {
  396. /* DataTypeConversion: '<S177>/Data Type Conversion' */
  397. rtb_DataTypeConversion_a = 6U;
  398. }
  399. } else if (rtb_Divide7 > ((int16_T)tmp_0 << 2)) {
  400. /* DataTypeConversion: '<S177>/Data Type Conversion' */
  401. rtb_DataTypeConversion_a = 4U;
  402. } else {
  403. /* DataTypeConversion: '<S177>/Data Type Conversion' */
  404. rtb_DataTypeConversion_a = 5U;
  405. }
  406. /* End of MATLAB Function: '<S177>/sector_select' */
  407. /* Product: '<S177>/Divide' incorporates:
  408. * DataTypeConversion: '<S177>/Data Type Conversion6'
  409. * Inport: '<Root>/Ts'
  410. * Inport: '<Root>/Vdc'
  411. */
  412. rtb_Divide = (int16_T)div_nde_s32_floor((int16_T)rtU_Ts << 10, rtU_Vdc);
  413. /* Product: '<S177>/Divide8' incorporates:
  414. * Constant: '<S177>/Constant'
  415. * Product: '<S177>/Divide2'
  416. * Product: '<S177>/Divide4'
  417. * Sum: '<S18>/Sum1'
  418. */
  419. rtb_Sum1_tmp = 2365 * (int16_T)((3547 * (int16_T)rtb_Sum1_tmp) >> 10);
  420. /* Product: '<S177>/Divide1' incorporates:
  421. * Product: '<S177>/Divide'
  422. * Product: '<S177>/Divide8'
  423. */
  424. rtb_Sum1 = (int16_T)(((int16_T)(rtb_Sum1_tmp >> 11) * rtb_Divide) >> 12);
  425. /* Product: '<S177>/Divide3' incorporates:
  426. * Constant: '<S177>/Constant1'
  427. * Signum: '<S51>/SignPreIntegrator'
  428. * Sum: '<S18>/Sum6'
  429. */
  430. rtb_SignPreIntegrator = (int16_T)((3547 * (int16_T)tmp_0) >> 10);
  431. /* Product: '<S177>/Divide4' */
  432. rtb_Divide4 = (int16_T)(rtb_Sum1_tmp >> 12);
  433. /* Product: '<S177>/Divide5' incorporates:
  434. * Product: '<S177>/Divide'
  435. * Product: '<S177>/Divide4'
  436. * Signum: '<S51>/SignPreIntegrator'
  437. * Sum: '<S177>/Add'
  438. */
  439. rtb_Divide7 = (int16_T)(((int16_T)(rtb_SignPreIntegrator + rtb_Divide4) *
  440. rtb_Divide) >> 12);
  441. /* Product: '<S177>/Divide6' incorporates:
  442. * Product: '<S177>/Divide'
  443. * Product: '<S177>/Divide4'
  444. * Signum: '<S51>/SignPreIntegrator'
  445. * Sum: '<S177>/Add1'
  446. */
  447. rtb_SignPreIntegrator = (int16_T)(((int16_T)(rtb_Divide4 -
  448. rtb_SignPreIntegrator) * rtb_Divide) >> 12);
  449. /* MATLAB Function: '<S177>/phase_time' incorporates:
  450. * DataTypeConversion: '<S177>/Data Type Conversion1'
  451. * DataTypeConversion: '<S177>/Data Type Conversion2'
  452. * Inport: '<Root>/Ts'
  453. */
  454. switch ((int8_T)rtb_DataTypeConversion_a) {
  455. case 1:
  456. rtb_Sum1_tmp = -rtb_SignPreIntegrator;
  457. if (-rtb_SignPreIntegrator > 32767) {
  458. rtb_Sum1_tmp = 32767;
  459. }
  460. tmp_0 = (int16_T)rtU_Ts - (int16_T)rtb_Sum1_tmp;
  461. if (tmp_0 > 32767) {
  462. tmp_0 = 32767;
  463. } else {
  464. if (tmp_0 < -32768) {
  465. tmp_0 = -32768;
  466. }
  467. }
  468. tmp_0 -= rtb_Sum1;
  469. if (tmp_0 > 32767) {
  470. tmp_0 = 32767;
  471. } else {
  472. if (tmp_0 < -32768) {
  473. tmp_0 = -32768;
  474. }
  475. }
  476. rtb_Divide = (int16_T)rt_roundd_snf((real_T)tmp_0 / 4.0);
  477. rtb_Sum1 = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_Sum1 / 2.0) +
  478. rtb_Divide);
  479. rtb_Sum1_tmp = (int32_T)rt_roundd_snf((real_T)(int16_T)rtb_Sum1_tmp / 2.0) +
  480. rtb_Sum1;
  481. if (rtb_Sum1_tmp > 32767) {
  482. rtb_Sum1_tmp = 32767;
  483. } else {
  484. if (rtb_Sum1_tmp < -32768) {
  485. rtb_Sum1_tmp = -32768;
  486. }
  487. }
  488. rtb_Divide7 = (int16_T)rtb_Sum1_tmp;
  489. break;
  490. case 2:
  491. rtb_Sum1_tmp = (int16_T)rtU_Ts - rtb_Divide7;
  492. if (rtb_Sum1_tmp > 32767) {
  493. rtb_Sum1_tmp = 32767;
  494. } else {
  495. if (rtb_Sum1_tmp < -32768) {
  496. rtb_Sum1_tmp = -32768;
  497. }
  498. }
  499. rtb_Sum1_tmp -= rtb_SignPreIntegrator;
  500. if (rtb_Sum1_tmp > 32767) {
  501. rtb_Sum1_tmp = 32767;
  502. } else {
  503. if (rtb_Sum1_tmp < -32768) {
  504. rtb_Sum1_tmp = -32768;
  505. }
  506. }
  507. rtb_Divide = (int16_T)rt_roundd_snf((real_T)rtb_Sum1_tmp / 4.0);
  508. rtb_Divide7 = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_Divide7 / 2.0) +
  509. rtb_Divide);
  510. rtb_Sum1_tmp = (int32_T)rt_roundd_snf((real_T)rtb_SignPreIntegrator / 2.0) +
  511. rtb_Divide7;
  512. if (rtb_Sum1_tmp > 32767) {
  513. rtb_Sum1_tmp = 32767;
  514. } else {
  515. if (rtb_Sum1_tmp < -32768) {
  516. rtb_Sum1_tmp = -32768;
  517. }
  518. }
  519. rtb_Sum1 = (int16_T)rtb_Sum1_tmp;
  520. break;
  521. case 3:
  522. rtb_Sum1_tmp = -rtb_Divide7;
  523. if (-rtb_Divide7 > 32767) {
  524. rtb_Sum1_tmp = 32767;
  525. }
  526. tmp_0 = (int16_T)rtU_Ts - rtb_Sum1;
  527. if (tmp_0 > 32767) {
  528. tmp_0 = 32767;
  529. } else {
  530. if (tmp_0 < -32768) {
  531. tmp_0 = -32768;
  532. }
  533. }
  534. tmp_0 -= (int16_T)rtb_Sum1_tmp;
  535. if (tmp_0 > 32767) {
  536. tmp_0 = 32767;
  537. } else {
  538. if (tmp_0 < -32768) {
  539. tmp_0 = -32768;
  540. }
  541. }
  542. rtb_Divide7 = (int16_T)rt_roundd_snf((real_T)tmp_0 / 4.0);
  543. rtb_Divide = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)rtb_Sum1_tmp /
  544. 2.0) + rtb_Divide7);
  545. rtb_Sum1_tmp = (int32_T)rt_roundd_snf((real_T)rtb_Sum1 / 2.0) + rtb_Divide;
  546. if (rtb_Sum1_tmp > 32767) {
  547. rtb_Sum1_tmp = 32767;
  548. } else {
  549. if (rtb_Sum1_tmp < -32768) {
  550. rtb_Sum1_tmp = -32768;
  551. }
  552. }
  553. rtb_Sum1 = (int16_T)rtb_Sum1_tmp;
  554. break;
  555. case 4:
  556. rtb_Sum1_tmp = -rtb_Sum1;
  557. if (-rtb_Sum1 > 32767) {
  558. rtb_Sum1_tmp = 32767;
  559. }
  560. tmp_0 = (int16_T)rtU_Ts - (int16_T)rtb_Sum1_tmp;
  561. if (tmp_0 > 32767) {
  562. tmp_0 = 32767;
  563. } else {
  564. if (tmp_0 < -32768) {
  565. tmp_0 = -32768;
  566. }
  567. }
  568. tmp_0 -= rtb_SignPreIntegrator;
  569. if (tmp_0 > 32767) {
  570. tmp_0 = 32767;
  571. } else {
  572. if (tmp_0 < -32768) {
  573. tmp_0 = -32768;
  574. }
  575. }
  576. rtb_Divide7 = (int16_T)rt_roundd_snf((real_T)tmp_0 / 4.0);
  577. rtb_Sum1 = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_SignPreIntegrator /
  578. 2.0) + rtb_Divide7);
  579. rtb_Sum1_tmp = (int32_T)rt_roundd_snf((real_T)(int16_T)rtb_Sum1_tmp / 2.0) +
  580. rtb_Sum1;
  581. if (rtb_Sum1_tmp > 32767) {
  582. rtb_Sum1_tmp = 32767;
  583. } else {
  584. if (rtb_Sum1_tmp < -32768) {
  585. rtb_Sum1_tmp = -32768;
  586. }
  587. }
  588. rtb_Divide = (int16_T)rtb_Sum1_tmp;
  589. break;
  590. case 5:
  591. rtb_Sum1_tmp = -rtb_Divide7;
  592. if (-rtb_Divide7 > 32767) {
  593. rtb_Sum1_tmp = 32767;
  594. }
  595. tmp_0 = -rtb_SignPreIntegrator;
  596. if (-rtb_SignPreIntegrator > 32767) {
  597. tmp_0 = 32767;
  598. }
  599. tmp = (int16_T)rtU_Ts - (int16_T)rtb_Sum1_tmp;
  600. if (tmp > 32767) {
  601. tmp = 32767;
  602. } else {
  603. if (tmp < -32768) {
  604. tmp = -32768;
  605. }
  606. }
  607. tmp -= (int16_T)tmp_0;
  608. if (tmp > 32767) {
  609. tmp = 32767;
  610. } else {
  611. if (tmp < -32768) {
  612. tmp = -32768;
  613. }
  614. }
  615. rtb_Sum1 = (int16_T)rt_roundd_snf((real_T)tmp / 4.0);
  616. rtb_Divide7 = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)tmp_0 / 2.0)
  617. + rtb_Sum1);
  618. rtb_Sum1_tmp = (int32_T)rt_roundd_snf((real_T)(int16_T)rtb_Sum1_tmp / 2.0) +
  619. rtb_Divide7;
  620. if (rtb_Sum1_tmp > 32767) {
  621. rtb_Sum1_tmp = 32767;
  622. } else {
  623. if (rtb_Sum1_tmp < -32768) {
  624. rtb_Sum1_tmp = -32768;
  625. }
  626. }
  627. rtb_Divide = (int16_T)rtb_Sum1_tmp;
  628. break;
  629. default:
  630. rtb_Sum1_tmp = -rtb_Sum1;
  631. if (-rtb_Sum1 > 32767) {
  632. rtb_Sum1_tmp = 32767;
  633. }
  634. tmp_0 = (int16_T)rtU_Ts - rtb_Divide7;
  635. if (tmp_0 > 32767) {
  636. tmp_0 = 32767;
  637. } else {
  638. if (tmp_0 < -32768) {
  639. tmp_0 = -32768;
  640. }
  641. }
  642. tmp_0 -= (int16_T)rtb_Sum1_tmp;
  643. if (tmp_0 > 32767) {
  644. tmp_0 = 32767;
  645. } else {
  646. if (tmp_0 < -32768) {
  647. tmp_0 = -32768;
  648. }
  649. }
  650. rtb_Sum1 = (int16_T)rt_roundd_snf((real_T)tmp_0 / 4.0);
  651. rtb_Divide = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)rtb_Sum1_tmp /
  652. 2.0) + rtb_Sum1);
  653. rtb_Sum1_tmp = (int32_T)rt_roundd_snf((real_T)rtb_Divide7 / 2.0) +
  654. rtb_Divide;
  655. if (rtb_Sum1_tmp > 32767) {
  656. rtb_Sum1_tmp = 32767;
  657. } else {
  658. if (rtb_Sum1_tmp < -32768) {
  659. rtb_Sum1_tmp = -32768;
  660. }
  661. }
  662. rtb_Divide7 = (int16_T)rtb_Sum1_tmp;
  663. break;
  664. }
  665. /* DataTypeConversion: '<S177>/Data Type Conversion3' incorporates:
  666. * MATLAB Function: '<S177>/phase_time'
  667. */
  668. rtb_DataTypeConversion3 = (uint16_T)rtb_Divide7;
  669. /* MultiPortSwitch: '<S178>/Multiport Switch' incorporates:
  670. * DataTypeConversion: '<S177>/Data Type Conversion3'
  671. * DataTypeConversion: '<S177>/Data Type Conversion4'
  672. * DataTypeConversion: '<S177>/Data Type Conversion5'
  673. * MATLAB Function: '<S177>/phase_time'
  674. */
  675. switch (rtb_DataTypeConversion_a) {
  676. case 1:
  677. rtb_MultiportSwitch_idx_0 = (uint16_T)rtb_Divide7;
  678. rtb_MultiportSwitch_idx_1 = (uint16_T)rtb_Sum1;
  679. break;
  680. case 2:
  681. rtb_MultiportSwitch_idx_0 = (uint16_T)rtb_Sum1;
  682. rtb_MultiportSwitch_idx_1 = (uint16_T)rtb_Divide7;
  683. break;
  684. case 3:
  685. rtb_MultiportSwitch_idx_0 = (uint16_T)rtb_Sum1;
  686. rtb_MultiportSwitch_idx_1 = (uint16_T)rtb_Divide;
  687. break;
  688. case 4:
  689. rtb_MultiportSwitch_idx_0 = (uint16_T)rtb_Divide;
  690. rtb_MultiportSwitch_idx_1 = (uint16_T)rtb_Sum1;
  691. break;
  692. case 5:
  693. rtb_MultiportSwitch_idx_0 = (uint16_T)rtb_Divide;
  694. rtb_MultiportSwitch_idx_1 = (uint16_T)rtb_Divide7;
  695. break;
  696. default:
  697. rtb_MultiportSwitch_idx_0 = (uint16_T)rtb_Divide7;
  698. rtb_MultiportSwitch_idx_1 = (uint16_T)rtb_Divide;
  699. break;
  700. }
  701. /* End of MultiPortSwitch: '<S178>/Multiport Switch' */
  702. /* Outport: '<Root>/pwm_counts' incorporates:
  703. * Constant: '<S178>/Constant'
  704. * Constant: '<S178>/Constant1'
  705. * Constant: '<S178>/Constant2'
  706. * Constant: '<S178>/Constant3'
  707. * MATLAB Function: '<S178>/Shunt_Three_Sample_Point'
  708. * Outport: '<Root>/sector'
  709. */
  710. Shunt_Three_Sample_Point(rtb_MultiportSwitch_idx_0, rtb_MultiportSwitch_idx_1,
  711. rtb_DataTypeConversion_a, 4000, 10, 10, 10, &rtb_o_samp_p1, &rtY_pwm_counts
  712. [7], rtY_sector);
  713. /* DeadZone: '<S103>/DeadZone' incorporates:
  714. * Signum: '<S101>/SignPreSat'
  715. */
  716. if (rtb_SignPreSat_k > 8960) {
  717. rtb_SignPreSat_k = (int16_T)(rtb_SignPreSat_k - 8960);
  718. } else if (rtb_SignPreSat_k >= -8960) {
  719. rtb_SignPreSat_k = 0;
  720. } else {
  721. rtb_SignPreSat_k = (int16_T)(rtb_SignPreSat_k - -8960);
  722. }
  723. /* End of DeadZone: '<S103>/DeadZone' */
  724. /* RelationalOperator: '<S101>/NotEqual' incorporates:
  725. * Gain: '<S101>/ZeroGain'
  726. * Signum: '<S101>/SignPreSat'
  727. */
  728. rtb_NotEqual = (0 != rtb_SignPreSat_k);
  729. /* Signum: '<S101>/SignPreSat' */
  730. if (rtb_SignPreSat_k < 0) {
  731. rtb_SignPreSat_k = -1;
  732. } else {
  733. rtb_SignPreSat_k = (int16_T)(rtb_SignPreSat_k > 0);
  734. }
  735. /* End of Signum: '<S101>/SignPreSat' */
  736. /* Gain: '<S107>/Integral Gain' incorporates:
  737. * Gain: '<S57>/Integral Gain'
  738. * Sum: '<S16>/Sum2'
  739. */
  740. rtb_Divide7 = (int16_T)(1080 * rtb_Sum2);
  741. /* Signum: '<S101>/SignPreIntegrator' incorporates:
  742. * Gain: '<S57>/Integral Gain'
  743. */
  744. if (rtb_Divide7 < 0) {
  745. rtb_Divide4 = -1;
  746. } else {
  747. rtb_Divide4 = (int16_T)(rtb_Divide7 > 0);
  748. }
  749. /* End of Signum: '<S101>/SignPreIntegrator' */
  750. /* Switch: '<S101>/Switch' incorporates:
  751. * Constant: '<S101>/Constant1'
  752. * DataTypeConversion: '<S101>/DataTypeConv1'
  753. * DataTypeConversion: '<S101>/DataTypeConv2'
  754. * Gain: '<S57>/Integral Gain'
  755. * Logic: '<S101>/AND3'
  756. * RelationalOperator: '<S101>/Equal1'
  757. */
  758. if (rtb_NotEqual && (rtb_SignPreSat_k == rtb_Divide4)) {
  759. rtb_Sum2 = 0;
  760. } else {
  761. rtb_Sum2 = rtb_Divide7;
  762. }
  763. /* End of Switch: '<S101>/Switch' */
  764. /* DeadZone: '<S153>/DeadZone' incorporates:
  765. * Signum: '<S151>/SignPreSat'
  766. */
  767. if (rtb_SignPreSat > 800) {
  768. rtb_SignPreSat = (int16_T)(rtb_SignPreSat - 800);
  769. } else if (rtb_SignPreSat >= -800) {
  770. rtb_SignPreSat = 0;
  771. } else {
  772. rtb_SignPreSat = (int16_T)(rtb_SignPreSat - -800);
  773. }
  774. /* End of DeadZone: '<S153>/DeadZone' */
  775. /* RelationalOperator: '<S151>/NotEqual' incorporates:
  776. * Gain: '<S151>/ZeroGain'
  777. * Signum: '<S151>/SignPreSat'
  778. */
  779. rtb_NotEqual = (0 != rtb_SignPreSat);
  780. /* Signum: '<S151>/SignPreSat' */
  781. if (rtb_SignPreSat < 0) {
  782. rtb_SignPreSat = -1;
  783. } else {
  784. rtb_SignPreSat = (int16_T)(rtb_SignPreSat > 0);
  785. }
  786. /* End of Signum: '<S151>/SignPreSat' */
  787. /* Gain: '<S157>/Integral Gain' incorporates:
  788. * Gain: '<S57>/Integral Gain'
  789. */
  790. rtb_Divide7 = (int16_T)((int16_T)(200 * rtb_Sum3_l) << 4);
  791. /* Signum: '<S151>/SignPreIntegrator' incorporates:
  792. * Gain: '<S57>/Integral Gain'
  793. */
  794. if (rtb_Divide7 < 0) {
  795. rtb_Divide4 = -1;
  796. } else {
  797. rtb_Divide4 = (int16_T)(rtb_Divide7 > 0);
  798. }
  799. /* End of Signum: '<S151>/SignPreIntegrator' */
  800. /* Switch: '<S151>/Switch' incorporates:
  801. * Constant: '<S151>/Constant1'
  802. * DataTypeConversion: '<S151>/DataTypeConv1'
  803. * DataTypeConversion: '<S151>/DataTypeConv2'
  804. * Gain: '<S57>/Integral Gain'
  805. * Logic: '<S151>/AND3'
  806. * RelationalOperator: '<S151>/Equal1'
  807. */
  808. if (rtb_NotEqual && (rtb_SignPreSat == rtb_Divide4)) {
  809. rtb_Sum3_l = 0;
  810. } else {
  811. rtb_Sum3_l = rtb_Divide7;
  812. }
  813. /* End of Switch: '<S151>/Switch' */
  814. /* DeadZone: '<S53>/DeadZone' incorporates:
  815. * Signum: '<S51>/SignPreSat'
  816. */
  817. if (rtb_Multiply_idx_1 > 8960) {
  818. rtb_Multiply_idx_1 = (int16_T)(rtb_Multiply_idx_1 - 8960);
  819. } else if (rtb_Multiply_idx_1 >= -8960) {
  820. rtb_Multiply_idx_1 = 0;
  821. } else {
  822. rtb_Multiply_idx_1 = (int16_T)(rtb_Multiply_idx_1 - -8960);
  823. }
  824. /* End of DeadZone: '<S53>/DeadZone' */
  825. /* RelationalOperator: '<S51>/NotEqual' incorporates:
  826. * Gain: '<S51>/ZeroGain'
  827. * Signum: '<S51>/SignPreSat'
  828. */
  829. rtb_NotEqual = (0 != rtb_Multiply_idx_1);
  830. /* Signum: '<S51>/SignPreSat' */
  831. if (rtb_Multiply_idx_1 < 0) {
  832. rtb_Multiply_idx_1 = -1;
  833. } else {
  834. rtb_Multiply_idx_1 = (int16_T)(rtb_Multiply_idx_1 > 0);
  835. }
  836. /* End of Signum: '<S51>/SignPreSat' */
  837. /* Gain: '<S57>/Integral Gain' incorporates:
  838. * Sum: '<S16>/Sum1'
  839. */
  840. rtb_Divide7 = (int16_T)(1071 * (int16_T)-rtb_Multiply_idx_0);
  841. /* Update for UnitDelay: '<S21>/UnitDelay1' incorporates:
  842. * Sum: '<S21>/Sum3'
  843. */
  844. rtDW->UnitDelay1_DSTATE[0] = rtb_Sum3_idx_0;
  845. rtDW->UnitDelay1_DSTATE[1] = rtb_Sum3_idx_1;
  846. /* Signum: '<S51>/SignPreIntegrator' incorporates:
  847. * Gain: '<S57>/Integral Gain'
  848. */
  849. if (rtb_Divide7 < 0) {
  850. rtb_Divide4 = -1;
  851. } else {
  852. rtb_Divide4 = (int16_T)(rtb_Divide7 > 0);
  853. }
  854. /* End of Signum: '<S51>/SignPreIntegrator' */
  855. /* Switch: '<S51>/Switch' incorporates:
  856. * Constant: '<S51>/Constant1'
  857. * DataTypeConversion: '<S51>/DataTypeConv1'
  858. * DataTypeConversion: '<S51>/DataTypeConv2'
  859. * Logic: '<S51>/AND3'
  860. * RelationalOperator: '<S51>/Equal1'
  861. */
  862. if (rtb_NotEqual && (rtb_Multiply_idx_1 == rtb_Divide4)) {
  863. rtb_Divide7 = 0;
  864. }
  865. /* End of Switch: '<S51>/Switch' */
  866. /* Update for DiscreteIntegrator: '<S60>/Integrator' */
  867. rtDW->Integrator_DSTATE += rtb_Divide7;
  868. /* Update for DiscreteIntegrator: '<S160>/Integrator' incorporates:
  869. * Switch: '<S151>/Switch'
  870. */
  871. rtDW->Integrator_DSTATE_i += rtb_Sum3_l;
  872. /* Update for DiscreteIntegrator: '<S110>/Integrator' incorporates:
  873. * Switch: '<S101>/Switch'
  874. */
  875. rtDW->Integrator_DSTATE_p += rtb_Sum2;
  876. /* End of Outputs for SubSystem: '<S1>/controller' */
  877. /* Outport: '<Root>/pwm_counts' incorporates:
  878. * DataTypeConversion: '<S177>/Data Type Conversion4'
  879. * DataTypeConversion: '<S177>/Data Type Conversion5'
  880. * MATLAB Function: '<S177>/phase_time'
  881. */
  882. rtY_pwm_counts[0] = rtb_DataTypeConversion3;
  883. /* Outputs for Atomic SubSystem: '<S1>/controller' */
  884. rtY_pwm_counts[1] = (uint16_T)rtb_Sum1;
  885. rtY_pwm_counts[2] = (uint16_T)rtb_Divide;
  886. /* End of Outputs for SubSystem: '<S1>/controller' */
  887. rtY_pwm_counts[3] = rtb_DataTypeConversion3;
  888. /* Outputs for Atomic SubSystem: '<S1>/controller' */
  889. rtY_pwm_counts[4] = (uint16_T)rtb_Sum1;
  890. rtY_pwm_counts[5] = (uint16_T)rtb_Divide;
  891. /* End of Outputs for SubSystem: '<S1>/controller' */
  892. rtY_pwm_counts[6] = rtb_o_samp_p1;
  893. rate_scheduler(rtM);
  894. }
  895. /* Model initialize function */
  896. void PMSM_Controller_initialize(RT_MODEL *const rtM)
  897. {
  898. /* Registration code */
  899. /* initialize non-finites */
  900. rt_InitInfAndNaN(sizeof(real_T));
  901. UNUSED_PARAMETER(rtM);
  902. }
  903. /*
  904. * File trailer for generated code.
  905. *
  906. * [EOF]
  907. */