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