1/*
2 * File: PMSM_Controller.c
3 *
4 * Code generated for Simulink model 'PMSM_Controller'.
5 *
6 * Model version : 1.1445
7 * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020
8 * C/C++ source code generated on : Sat May 21 17:50:12 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
20/* Named constants for Chart: '<S4>/Control_Mode_Manager' */
21#define IN_ACTIVE ((uint8_T)1U)
22#define IN_NO_ACTIVE_CHILD ((uint8_T)0U)
23#define IN_OPEN ((uint8_T)2U)
24#define IN_SPEED_MODE ((uint8_T)1U)
25#define IN_TORQUE_MODE ((uint8_T)2U)
26#define OPEN_MODE ((uint8_T)0U)
27#define SPD_MODE ((uint8_T)1U)
28#define TRQ_MODE ((uint8_T)2U)
29#ifndef UCHAR_MAX
30#include <limits.h>
31#endif
32
33#if ( UCHAR_MAX != (0xFFU) ) || ( SCHAR_MAX != (0x7F) )
34#error Code was generated for compiler with different sized uchar/char. \
35Consider adjusting Test hardware word size settings on the \
36Hardware Implementation pane to match your compiler word sizes as \
37defined in limits.h of the compiler. Alternatively, you can \
38select the Test hardware is the same as production hardware option and \
39select the Enable portable word sizes option on the Code Generation > \
40Verification pane for ERT based targets, which will disable the \
41preprocessor word size checks.
42#endif
43
44#if ( USHRT_MAX != (0xFFFFU) ) || ( SHRT_MAX != (0x7FFF) )
45#error Code was generated for compiler with different sized ushort/short. \
46Consider adjusting Test hardware word size settings on the \
47Hardware Implementation pane to match your compiler word sizes as \
48defined in limits.h of the compiler. Alternatively, you can \
49select the Test hardware is the same as production hardware option and \
50select the Enable portable word sizes option on the Code Generation > \
51Verification pane for ERT based targets, which will disable the \
52preprocessor word size checks.
53#endif
54
55#if ( UINT_MAX != (0xFFFFFFFFU) ) || ( INT_MAX != (0x7FFFFFFF) )
56#error Code was generated for compiler with different sized uint/int. \
57Consider adjusting Test hardware word size settings on the \
58Hardware Implementation pane to match your compiler word sizes as \
59defined in limits.h of the compiler. Alternatively, you can \
60select the Test hardware is the same as production hardware option and \
61select the Enable portable word sizes option on the Code Generation > \
62Verification pane for ERT based targets, which will disable the \
63preprocessor word size checks.
64#endif
65
66#if ( ULONG_MAX != (0xFFFFFFFFU) ) || ( LONG_MAX != (0x7FFFFFFF) )
67#error Code was generated for compiler with different sized ulong/long. \
68Consider adjusting Test hardware word size settings on the \
69Hardware Implementation pane to match your compiler word sizes as \
70defined in limits.h of the compiler. Alternatively, you can \
71select the Test hardware is the same as production hardware option and \
72select the Enable portable word sizes option on the Code Generation > \
73Verification pane for ERT based targets, which will disable the \
74preprocessor word size checks.
75#endif
76
77/* Skipping ulong_long/long_long check: insufficient preprocessor integer range. */
78extern int16_T rt_sqrt_Us32En10_Ys16E_7VJYwqF9(int32_T u);
79extern uint16_T rt_sqrt_Uu16En14_Yu16E_WMwW1mku(uint16_T u);
80uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T
81 maxIndex);
82extern void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T
83 rty_y[2], DW_Low_Pass_Filter *localDW);
84extern void PI_backCalc_fixdt_Init(DW_PI_backCalc_fixdt *localDW);
85extern int32_T PI_backCalc_fixdt(int32_T rtu_err, int16_T rtu_P, int16_T rtu_I,
86 int16_T rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_init,
87 uint8_T rtu_reset, DW_PI_backCalc_fixdt *localDW, ZCE_PI_backCalc_fixdt
88 *localZCE);
89extern void PI_backCalc_fixdt_p_Init(DW_PI_backCalc_fixdt_i *localDW);
90extern int32_T PI_backCalc_fixdt_o(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I,
91 int16_T rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_init,
92 uint8_T rtu_reset, DW_PI_backCalc_fixdt_i *localDW, ZCE_PI_backCalc_fixdt_e
93 *localZCE);
94extern void RateInit(int16_T rtu_initVal, int16_T rtu_target, int16_T rtu_step,
95 int16_T *rty_s_step, int16_T *rty_High, int16_T *rty_Low);
96uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T
97 maxIndex)
98{
99 uint16_T bpIndex;
100
101 /* Prelookup - Index only
102 Index Search method: 'even'
103 Extrapolation method: 'Clip'
104 Use previous index: 'off'
105 Use last breakpoint for index at or above upper limit: 'on'
106 Remove protection against out-of-range input in generated code: 'off'
107 */
108 if (u <= bp0) {
109 bpIndex = 0U;
110 } else {
111 bpIndex = (uint16_T)((uint32_T)(uint16_T)(u - bp0) / bpSpace);
112 if (bpIndex < maxIndex) {
113 } else {
114 bpIndex = (uint16_T)maxIndex;
115 }
116 }
117
118 return bpIndex;
119}
120
121/*
122 * Output and update for atomic system:
123 * '<S48>/Low_Pass_Filter'
124 * '<S6>/Low_Pass_Filter'
125 */
126void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T rty_y[2],
127 DW_Low_Pass_Filter *localDW)
128{
129 int32_T rtb_Sum3_m;
130
131 /* Sum: '<S57>/Sum2' incorporates:
132 * UnitDelay: '<S57>/UnitDelay1'
133 */
134 rtb_Sum3_m = rtu_u[0] - (localDW->UnitDelay1_DSTATE[0] >> 16);
135 if (rtb_Sum3_m > 32767) {
136 rtb_Sum3_m = 32767;
137 } else {
138 if (rtb_Sum3_m < -32768) {
139 rtb_Sum3_m = -32768;
140 }
141 }
142
143 rty_y[0] = (int16_T)rtb_Sum3_m;
144
145 /* Sum: '<S57>/Sum3' incorporates:
146 * Product: '<S57>/Divide3'
147 * UnitDelay: '<S57>/UnitDelay1'
148 */
149 rtb_Sum3_m = rtu_coef * rty_y[0] + localDW->UnitDelay1_DSTATE[0];
150
151 /* DataTypeConversion: '<S57>/Data Type Conversion' */
152 rty_y[0] = (int16_T)(rtb_Sum3_m >> 16);
153
154 /* Update for UnitDelay: '<S57>/UnitDelay1' */
155 localDW->UnitDelay1_DSTATE[0] = rtb_Sum3_m;
156
157 /* Sum: '<S57>/Sum2' incorporates:
158 * UnitDelay: '<S57>/UnitDelay1'
159 */
160 rtb_Sum3_m = rtu_u[1] - (localDW->UnitDelay1_DSTATE[1] >> 16);
161 if (rtb_Sum3_m > 32767) {
162 rtb_Sum3_m = 32767;
163 } else {
164 if (rtb_Sum3_m < -32768) {
165 rtb_Sum3_m = -32768;
166 }
167 }
168
169 rty_y[1] = (int16_T)rtb_Sum3_m;
170
171 /* Sum: '<S57>/Sum3' incorporates:
172 * Product: '<S57>/Divide3'
173 * UnitDelay: '<S57>/UnitDelay1'
174 */
175 rtb_Sum3_m = rtu_coef * rty_y[1] + localDW->UnitDelay1_DSTATE[1];
176
177 /* DataTypeConversion: '<S57>/Data Type Conversion' */
178 rty_y[1] = (int16_T)(rtb_Sum3_m >> 16);
179
180 /* Update for UnitDelay: '<S57>/UnitDelay1' */
181 localDW->UnitDelay1_DSTATE[1] = rtb_Sum3_m;
182}
183
184/* System initialize for atomic system: '<S87>/PI_Speed' */
185void PI_backCalc_fixdt_Init(DW_PI_backCalc_fixdt *localDW)
186{
187 /* InitializeConditions for Delay: '<S90>/Resettable Delay' */
188 localDW->icLoad = 1U;
189}
190
191/* Output and update for atomic system: '<S87>/PI_Speed' */
192int32_T PI_backCalc_fixdt(int32_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T
193 rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_init, uint8_T
194 rtu_reset, DW_PI_backCalc_fixdt *localDW, ZCE_PI_backCalc_fixdt *localZCE)
195{
196 int32_T rty_pi_out_0;
197 int64_T tmp;
198 int64_T tmp_0;
199
200 /* Product: '<S89>/Divide4' */
201 tmp_0 = (int64_T)rtu_err * rtu_P;
202 if (tmp_0 > 2147483647LL) {
203 tmp_0 = 2147483647LL;
204 } else {
205 if (tmp_0 < -2147483648LL) {
206 tmp_0 = -2147483648LL;
207 }
208 }
209
210 /* Delay: '<S90>/Resettable Delay' incorporates:
211 * DataTypeConversion: '<S90>/Data Type Conversion2'
212 */
213 if ((rtu_reset > 0) && (localZCE->ResettableDelay_Reset_ZCE_f != POS_ZCSIG)) {
214 localDW->icLoad = 1U;
215 }
216
217 localZCE->ResettableDelay_Reset_ZCE_f = (ZCSigState)(rtu_reset > 0);
218 if (localDW->icLoad != 0) {
219 localDW->ResettableDelay_DSTATE = rtu_init << 7;
220 }
221
222 /* Product: '<S89>/Divide1' incorporates:
223 * Product: '<S89>/Divide4'
224 */
225 tmp = ((int64_T)(int32_T)tmp_0 * rtu_I) >> 14;
226 if (tmp > 2147483647LL) {
227 tmp = 2147483647LL;
228 } else {
229 if (tmp < -2147483648LL) {
230 tmp = -2147483648LL;
231 }
232 }
233
234 /* Sum: '<S89>/Sum2' incorporates:
235 * Product: '<S89>/Divide1'
236 * UnitDelay: '<S89>/UnitDelay'
237 */
238 tmp = (int64_T)(int32_T)tmp + localDW->UnitDelay_DSTATE;
239 if (tmp > 2147483647LL) {
240 tmp = 2147483647LL;
241 } else {
242 if (tmp < -2147483648LL) {
243 tmp = -2147483648LL;
244 }
245 }
246
247 /* Sum: '<S90>/Sum1' incorporates:
248 * Delay: '<S90>/Resettable Delay'
249 * Sum: '<S89>/Sum2'
250 */
251 tmp = (((int64_T)localDW->ResettableDelay_DSTATE << 2) + (int32_T)tmp) >> 2;
252 if (tmp > 2147483647LL) {
253 tmp = 2147483647LL;
254 } else {
255 if (tmp < -2147483648LL) {
256 tmp = -2147483648LL;
257 }
258 }
259
260 /* Sum: '<S89>/Sum6' incorporates:
261 * DataTypeConversion: '<S90>/Data Type Conversion1'
262 * Product: '<S89>/Divide4'
263 * Sum: '<S90>/Sum1'
264 */
265 tmp_0 = (int64_T)((int32_T)tmp << 2) + (int32_T)tmp_0;
266 if (tmp_0 > 2147483647LL) {
267 tmp_0 = 2147483647LL;
268 } else {
269 if (tmp_0 < -2147483648LL) {
270 tmp_0 = -2147483648LL;
271 }
272 }
273
274 /* RelationalOperator: '<S91>/LowerRelop1' incorporates:
275 * Switch: '<S91>/Switch2'
276 */
277 rty_pi_out_0 = rtu_satMax << 9;
278
279 /* Switch: '<S91>/Switch2' incorporates:
280 * RelationalOperator: '<S91>/LowerRelop1'
281 * Sum: '<S89>/Sum6'
282 */
283 if ((int32_T)tmp_0 <= rty_pi_out_0) {
284 /* RelationalOperator: '<S91>/UpperRelop' incorporates:
285 * Switch: '<S91>/Switch'
286 */
287 rty_pi_out_0 = rtu_satMin << 9;
288
289 /* Switch: '<S91>/Switch' incorporates:
290 * RelationalOperator: '<S91>/UpperRelop'
291 */
292 if ((int32_T)tmp_0 >= rty_pi_out_0) {
293 rty_pi_out_0 = (int32_T)tmp_0;
294 }
295 }
296
297 /* Update for UnitDelay: '<S89>/UnitDelay' incorporates:
298 * Product: '<S89>/Divide2'
299 * Sum: '<S89>/Sum3'
300 * Sum: '<S89>/Sum6'
301 */
302 localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)(rty_pi_out_0 - (int32_T)tmp_0)
303 * rtu_Kb) >> 14);
304
305 /* Update for Delay: '<S90>/Resettable Delay' incorporates:
306 * Sum: '<S90>/Sum1'
307 */
308 localDW->icLoad = 0U;
309 localDW->ResettableDelay_DSTATE = (int32_T)tmp;
310 return rty_pi_out_0;
311}
312
313/*
314 * System initialize for atomic system:
315 * '<S95>/PI_backCalc_fixdt'
316 * '<S95>/PI_backCalc_fixdt1'
317 */
318void PI_backCalc_fixdt_p_Init(DW_PI_backCalc_fixdt_i *localDW)
319{
320 /* InitializeConditions for Delay: '<S102>/Resettable Delay' */
321 localDW->icLoad = 1U;
322}
323
324/*
325 * Output and update for atomic system:
326 * '<S95>/PI_backCalc_fixdt'
327 * '<S95>/PI_backCalc_fixdt1'
328 */
329int32_T PI_backCalc_fixdt_o(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I,
330 int16_T rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_init,
331 uint8_T rtu_reset, DW_PI_backCalc_fixdt_i *localDW, ZCE_PI_backCalc_fixdt_e
332 *localZCE)
333{
334 int32_T rty_pi_out_0;
335 int64_T tmp;
336 int64_T tmp_0;
337 int32_T rtb_Divide4_n;
338
339 /* Product: '<S100>/Divide4' */
340 rtb_Divide4_n = (rtu_err * rtu_P) >> 1;
341
342 /* Delay: '<S102>/Resettable Delay' incorporates:
343 * DataTypeConversion: '<S102>/Data Type Conversion2'
344 */
345 if ((rtu_reset > 0) && (localZCE->ResettableDelay_Reset_ZCE != POS_ZCSIG)) {
346 localDW->icLoad = 1U;
347 }
348
349 localZCE->ResettableDelay_Reset_ZCE = (ZCSigState)(rtu_reset > 0);
350 if (localDW->icLoad != 0) {
351 localDW->ResettableDelay_DSTATE = rtu_init << 7;
352 }
353
354 /* Product: '<S100>/Divide1' incorporates:
355 * Product: '<S100>/Divide4'
356 */
357 tmp_0 = ((int64_T)rtb_Divide4_n * rtu_I) >> 14;
358 if (tmp_0 > 2147483647LL) {
359 tmp_0 = 2147483647LL;
360 } else {
361 if (tmp_0 < -2147483648LL) {
362 tmp_0 = -2147483648LL;
363 }
364 }
365
366 /* Sum: '<S100>/Sum2' incorporates:
367 * Product: '<S100>/Divide1'
368 * UnitDelay: '<S100>/UnitDelay'
369 */
370 tmp_0 = (int64_T)(int32_T)tmp_0 + localDW->UnitDelay_DSTATE;
371 if (tmp_0 > 2147483647LL) {
372 tmp_0 = 2147483647LL;
373 } else {
374 if (tmp_0 < -2147483648LL) {
375 tmp_0 = -2147483648LL;
376 }
377 }
378
379 /* Sum: '<S102>/Sum1' incorporates:
380 * Delay: '<S102>/Resettable Delay'
381 * Sum: '<S100>/Sum2'
382 */
383 tmp_0 = (((int64_T)localDW->ResettableDelay_DSTATE << 2) + (int32_T)tmp_0) >>
384 2;
385 if (tmp_0 > 2147483647LL) {
386 tmp_0 = 2147483647LL;
387 } else {
388 if (tmp_0 < -2147483648LL) {
389 tmp_0 = -2147483648LL;
390 }
391 }
392
393 /* Sum: '<S100>/Sum6' incorporates:
394 * DataTypeConversion: '<S102>/Data Type Conversion1'
395 * Product: '<S100>/Divide4'
396 * Sum: '<S102>/Sum1'
397 */
398 tmp = (int64_T)((int32_T)tmp_0 << 2) + rtb_Divide4_n;
399 if (tmp > 2147483647LL) {
400 tmp = 2147483647LL;
401 } else {
402 if (tmp < -2147483648LL) {
403 tmp = -2147483648LL;
404 }
405 }
406
407 /* RelationalOperator: '<S103>/LowerRelop1' incorporates:
408 * Switch: '<S103>/Switch2'
409 */
410 rty_pi_out_0 = rtu_satMax << 9;
411
412 /* Switch: '<S103>/Switch2' incorporates:
413 * RelationalOperator: '<S103>/LowerRelop1'
414 * Sum: '<S100>/Sum6'
415 */
416 if ((int32_T)tmp <= rty_pi_out_0) {
417 /* RelationalOperator: '<S103>/UpperRelop' incorporates:
418 * Switch: '<S103>/Switch'
419 */
420 rty_pi_out_0 = rtu_satMin << 9;
421
422 /* Switch: '<S103>/Switch' incorporates:
423 * RelationalOperator: '<S103>/UpperRelop'
424 */
425 if ((int32_T)tmp >= rty_pi_out_0) {
426 rty_pi_out_0 = (int32_T)tmp;
427 }
428 }
429
430 /* Update for UnitDelay: '<S100>/UnitDelay' incorporates:
431 * Product: '<S100>/Divide2'
432 * Sum: '<S100>/Sum3'
433 * Sum: '<S100>/Sum6'
434 */
435 localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)(rty_pi_out_0 - (int32_T)tmp) *
436 rtu_Kb) >> 14);
437
438 /* Update for Delay: '<S102>/Resettable Delay' incorporates:
439 * Sum: '<S102>/Sum1'
440 */
441 localDW->icLoad = 0U;
442 localDW->ResettableDelay_DSTATE = (int32_T)tmp_0;
443 return rty_pi_out_0;
444}
445
446/*
447 * Output and update for action system:
448 * '<S108>/RateInit'
449 * '<S115>/RateInit'
450 */
451void RateInit(int16_T rtu_initVal, int16_T rtu_target, int16_T rtu_step, int16_T
452 *rty_s_step, int16_T *rty_High, int16_T *rty_Low)
453{
454 int16_T rtb_Add_b;
455
456 /* Sum: '<S109>/Add' */
457 rtb_Add_b = (int16_T)((rtu_target - rtu_initVal) >> 1);
458
459 /* Signum: '<S109>/Sign' incorporates:
460 * Sum: '<S109>/Add'
461 */
462 if (rtb_Add_b < 0) {
463 rtb_Add_b = -1;
464 } else {
465 rtb_Add_b = (int16_T)(rtb_Add_b > 0);
466 }
467
468 /* End of Signum: '<S109>/Sign' */
469
470 /* Product: '<S109>/Divide' */
471 *rty_s_step = (int16_T)(rtu_step * rtb_Add_b);
472
473 /* MinMax: '<S109>/Max' */
474 if (rtu_target > rtu_initVal) {
475 *rty_High = rtu_target;
476 } else {
477 *rty_High = rtu_initVal;
478 }
479
480 /* End of MinMax: '<S109>/Max' */
481
482 /* MinMax: '<S109>/Max1' */
483 if (rtu_initVal < rtu_target) {
484 *rty_Low = rtu_initVal;
485 } else {
486 *rty_Low = rtu_target;
487 }
488
489 /* End of MinMax: '<S109>/Max1' */
490}
491
492int16_T rt_sqrt_Us32En10_Ys16E_7VJYwqF9(int32_T u)
493{
494 int32_T iBit;
495 int16_T shiftMask;
496 int16_T tmp01_y;
497 int16_T y;
498
499 /* Fixed-Point Sqrt Computation by the bisection method. */
500 if (u > 0) {
501 y = 0;
502 shiftMask = 16384;
503 for (iBit = 0; iBit < 15; iBit++) {
504 tmp01_y = (int16_T)(y | shiftMask);
505 if (tmp01_y * tmp01_y <= u) {
506 y = tmp01_y;
507 }
508
509 shiftMask = (int16_T)((uint32_T)shiftMask >> 1U);
510 }
511 } else {
512 y = 0;
513 }
514
515 return y;
516}
517
518uint16_T rt_sqrt_Uu16En14_Yu16E_WMwW1mku(uint16_T u)
519{
520 int32_T iBit;
521 uint32_T tmp03_u;
522 uint16_T shiftMask;
523 uint16_T tmp01_y;
524 uint16_T y;
525
526 /* Fixed-Point Sqrt Computation by the bisection method. */
527 if (u > 0) {
528 y = 0U;
529 shiftMask = 32768U;
530 tmp03_u = (uint32_T)u << 14;
531 for (iBit = 0; iBit < 16; iBit++) {
532 tmp01_y = (uint16_T)(y | shiftMask);
533 if ((uint32_T)tmp01_y * tmp01_y <= tmp03_u) {
534 y = tmp01_y;
535 }
536
537 shiftMask = (uint16_T)((uint32_T)shiftMask >> 1U);
538 }
539 } else {
540 y = 0U;
541 }
542
543 return y;
544}
545
546/* Model step function */
547void PMSM_Controller_step(RT_MODEL *const rtM)
548{
549 DW *rtDW = rtM->dwork;
550 PrevZCX *rtPrevZCX = rtM->prevZCSigState;
551 ExtU *rtU = (ExtU *) rtM->inputs;
552 ExtY *rtY = (ExtY *) rtM->outputs;
553 int64_T tmp;
554 uint64_T tmp_3;
555 int32_T rtb_Gain_b0;
556 int32_T rtb_Gain_p2;
557 int32_T rtb_Sum1;
558 int32_T rtb_Switch;
559 int32_T rtb_Switch3;
560 int32_T tmp_0;
561 int32_T tmp_1;
562 int32_T tmp_2;
563 uint32_T qY;
564 uint32_T rtb_Switch2;
565 int16_T rtb_DataTypeConversion_b[2];
566 int16_T rtb_UnitDelay1[2];
567 int16_T rtb_Divide1_m;
568 int16_T rtb_Divide3_k;
569 int16_T rtb_Sum1_a;
570 int16_T rtb_Sum3_jm;
571 int16_T rtb_Sum6_k;
572 int16_T rtb_Sum6_p;
573 int16_T rtb_Switch_f_idx_0;
574 int16_T rtb_Switch_f_idx_1;
575 int16_T rtb_r_cos_M1;
576 uint16_T rtb_BitwiseOperator2;
577 uint16_T rtb_LogicalOperator3;
578 int8_T UnitDelay3;
579 int8_T rtb_Sum2;
580 int8_T rtb_Sum2_tmp;
581 uint8_T rtb_Add_gf;
582 uint8_T rtb_DataTypeConversion_np;
583 uint8_T rtb_Sum_i;
584 uint8_T rtb_UnitDelay_bc;
585 uint8_T rtb_z_ctrlMod;
586 boolean_T rtb_Equal_k;
587 boolean_T rtb_LogicalOperator12;
588 boolean_T rtb_LogicalOperator2_h;
589 boolean_T rtb_LogicalOperator4_e;
590 boolean_T rtb_RelationalOperator4_f;
591 boolean_T rtb_n_commDeacv;
592
593 /* Outputs for Atomic SubSystem: '<Root>/PMSM_Controller' */
594 /* UnitDelay: '<S6>/UnitDelay1' */
595 rtb_UnitDelay1[0] = rtDW->UnitDelay1_DSTATE_f[0];
596 rtb_UnitDelay1[1] = rtDW->UnitDelay1_DSTATE_f[1];
597
598 /* S-Function (sfix_bitop): '<S4>/Bitwise Operator2' incorporates:
599 * Inport: '<Root>/FOC_Flags'
600 */
601 rtb_BitwiseOperator2 = (uint16_T)(rtU->FOC_Flags & 1);
602
603 /* UnitDelay: '<S37>/UnitDelay' */
604 rtb_UnitDelay_bc = rtDW->UnitDelay_DSTATE_j;
605
606 /* Logic: '<S9>/Edge_Detect' incorporates:
607 * Delay: '<S9>/Delay'
608 * Delay: '<S9>/Delay1'
609 * Delay: '<S9>/Delay2'
610 * Inport: '<Root>/hall_A'
611 * Inport: '<Root>/hall_B'
612 * Inport: '<Root>/hall_C'
613 */
614 rtb_Equal_k = (boolean_T)((rtU->hall_A != 0) ^ (rtDW->Delay_DSTATE_d != 0) ^
615 (rtU->hall_B != 0) ^ (rtDW->Delay1_DSTATE != 0) ^ (rtU->hall_C != 0)) ^
616 (rtDW->Delay2_DSTATE != 0);
617
618 /* Sum: '<S11>/Add' incorporates:
619 * Gain: '<S11>/Gain'
620 * Gain: '<S11>/Gain1'
621 * Inport: '<Root>/hall_A'
622 * Inport: '<Root>/hall_B'
623 * Inport: '<Root>/hall_C'
624 */
625 rtb_Add_gf = (uint8_T)((uint32_T)(uint8_T)((uint32_T)(uint8_T)(rtU->hall_C <<
626 2) + (uint8_T)(rtU->hall_B << 1)) + rtU->hall_A);
627
628 /* If: '<S3>/If2' incorporates:
629 * If: '<S14>/If2'
630 * Inport: '<S20>/z_counterRawPrev'
631 * UnitDelay: '<S14>/UnitDelay3'
632 */
633 if (rtb_Equal_k) {
634 /* Outputs for IfAction SubSystem: '<S3>/Direction_Detection' incorporates:
635 * ActionPort: '<S8>/Action Port'
636 */
637 /* UnitDelay: '<S8>/UnitDelay3' */
638 UnitDelay3 = rtDW->Switch2_i;
639
640 /* End of Outputs for SubSystem: '<S3>/Direction_Detection' */
641
642 /* Selector: '<S11>/Selector' incorporates:
643 * Constant: '<S11>/vec_hallToPos'
644 */
645 rtb_Sum2_tmp = rtConstP.vec_hallToPos_Value[rtb_Add_gf];
646
647 /* Outputs for IfAction SubSystem: '<S3>/Direction_Detection' incorporates:
648 * ActionPort: '<S8>/Action Port'
649 */
650 /* Sum: '<S8>/Sum2' incorporates:
651 * Constant: '<S11>/vec_hallToPos'
652 * Selector: '<S11>/Selector'
653 * UnitDelay: '<S8>/UnitDelay2'
654 */
655 rtb_Sum2 = (int8_T)(rtb_Sum2_tmp - rtDW->UnitDelay2_DSTATE_j);
656
657 /* Switch: '<S8>/Switch2' incorporates:
658 * Constant: '<S8>/Constant20'
659 * Constant: '<S8>/Constant8'
660 * Logic: '<S8>/Logical Operator3'
661 * RelationalOperator: '<S8>/Relational Operator1'
662 * RelationalOperator: '<S8>/Relational Operator6'
663 */
664 if ((rtb_Sum2 == 1) || (rtb_Sum2 == -5)) {
665 /* Switch: '<S8>/Switch2' incorporates:
666 * Constant: '<S8>/Constant24'
667 */
668 rtDW->Switch2_i = 1;
669 } else {
670 /* Switch: '<S8>/Switch2' incorporates:
671 * Constant: '<S8>/Constant23'
672 */
673 rtDW->Switch2_i = -1;
674 }
675
676 /* End of Switch: '<S8>/Switch2' */
677
678 /* Update for UnitDelay: '<S8>/UnitDelay2' */
679 rtDW->UnitDelay2_DSTATE_j = rtb_Sum2_tmp;
680
681 /* End of Outputs for SubSystem: '<S3>/Direction_Detection' */
682
683 /* Outputs for IfAction SubSystem: '<S14>/Raw_Motor_Speed_Estimation' incorporates:
684 * ActionPort: '<S20>/Action Port'
685 */
686 /* RelationalOperator: '<S20>/Relational Operator4' */
687 rtb_RelationalOperator4_f = (rtDW->Switch2_i != UnitDelay3);
688 rtDW->z_counterRawPrev = rtDW->UnitDelay3_DSTATE;
689
690 /* Switch: '<S20>/Switch3' incorporates:
691 * Constant: '<S20>/Constant4'
692 * Inport: '<S20>/z_counterRawPrev'
693 * Logic: '<S20>/Logical Operator1'
694 * Switch: '<S20>/Switch2'
695 * UnitDelay: '<S14>/UnitDelay3'
696 * UnitDelay: '<S20>/UnitDelay1'
697 */
698 if (rtb_RelationalOperator4_f && rtDW->UnitDelay1_DSTATE_iv) {
699 rtb_Switch3 = 0;
700 } else {
701 if (rtb_RelationalOperator4_f) {
702 /* Switch: '<S20>/Switch2' incorporates:
703 * UnitDelay: '<S14>/UnitDelay4'
704 */
705 rtb_Switch2 = rtDW->UnitDelay4_DSTATE;
706 } else {
707 /* Sum: '<S20>/Sum13' incorporates:
708 * Switch: '<S20>/Switch2'
709 * UnitDelay: '<S20>/UnitDelay2'
710 * UnitDelay: '<S20>/UnitDelay3'
711 * UnitDelay: '<S20>/UnitDelay5'
712 */
713 tmp_3 = (((uint64_T)rtDW->UnitDelay2_DSTATE + rtDW->UnitDelay3_DSTATE_l)
714 + rtDW->UnitDelay5_DSTATE) + rtDW->z_counterRawPrev;
715 if (tmp_3 > 4294967295ULL) {
716 tmp_3 = 4294967295ULL;
717 }
718
719 /* Switch: '<S20>/Switch2' incorporates:
720 * Product: '<S20>/Divide13'
721 * Sum: '<S20>/Sum13'
722 */
723 rtb_Switch2 = 160000000U / (uint32_T)tmp_3;
724 }
725
726 rtb_Switch3 = (int32_T)rtb_Switch2;
727 }
728
729 /* End of Switch: '<S20>/Switch3' */
730
731 /* Product: '<S20>/Divide11' incorporates:
732 * Switch: '<S20>/Switch3'
733 */
734 rtDW->Divide11 = rtb_Switch3 * rtDW->Switch2_i;
735
736 /* Update for UnitDelay: '<S20>/UnitDelay1' */
737 rtDW->UnitDelay1_DSTATE_iv = rtb_RelationalOperator4_f;
738
739 /* Update for UnitDelay: '<S20>/UnitDelay2' incorporates:
740 * UnitDelay: '<S20>/UnitDelay3'
741 */
742 rtDW->UnitDelay2_DSTATE = rtDW->UnitDelay3_DSTATE_l;
743
744 /* Update for UnitDelay: '<S20>/UnitDelay3' incorporates:
745 * UnitDelay: '<S20>/UnitDelay5'
746 */
747 rtDW->UnitDelay3_DSTATE_l = rtDW->UnitDelay5_DSTATE;
748
749 /* Update for UnitDelay: '<S20>/UnitDelay5' */
750 rtDW->UnitDelay5_DSTATE = rtDW->z_counterRawPrev;
751
752 /* End of Outputs for SubSystem: '<S14>/Raw_Motor_Speed_Estimation' */
753 }
754
755 /* End of If: '<S3>/If2' */
756
757 /* Switch: '<S14>/Switch2' incorporates:
758 * Constant: '<S14>/Constant4'
759 * Constant: '<S14>/z_maxCntRst'
760 * Gain: '<S14>/Gain'
761 * Inport: '<Root>/us_Count'
762 * Product: '<S20>/Divide11'
763 * RelationalOperator: '<S14>/Relational Operator2'
764 */
765 if (rtU->us_Count >= (rtP.n_hall_count_ps << 1)) {
766 rtb_Switch3 = 0;
767 } else {
768 rtb_Switch3 = rtDW->Divide11;
769 }
770
771 /* End of Switch: '<S14>/Switch2' */
772
773 /* Abs: '<S14>/Abs5' incorporates:
774 * Switch: '<S14>/Switch2'
775 */
776 if (rtb_Switch3 < 0) {
777 rtb_Switch2 = (uint32_T)-rtb_Switch3;
778 } else {
779 rtb_Switch2 = (uint32_T)rtb_Switch3;
780 }
781
782 /* End of Abs: '<S14>/Abs5' */
783
784 /* If: '<S14>/If1' */
785 if (rtb_Equal_k) {
786 /* Outputs for IfAction SubSystem: '<S14>/AdvCtrlDetect' incorporates:
787 * ActionPort: '<S19>/Action Port'
788 */
789 /* Relay: '<S19>/n_commDeacv' incorporates:
790 * Abs: '<S14>/Abs5'
791 */
792 rtDW->n_commDeacv_Mode = ((rtb_Switch2 >= 480U) || ((rtb_Switch2 > 240U) &&
793 rtDW->n_commDeacv_Mode));
794
795 /* RelationalOperator: '<S21>/Compare' incorporates:
796 * Constant: '<S21>/Constant'
797 * Relay: '<S19>/n_commDeacv'
798 * Sum: '<S19>/Sum13'
799 * UnitDelay: '<S19>/UnitDelay2'
800 * UnitDelay: '<S19>/UnitDelay3'
801 * UnitDelay: '<S19>/UnitDelay5'
802 */
803 rtDW->Compare = ((uint16_T)((uint32_T)(uint16_T)((uint32_T)(uint16_T)
804 ((uint32_T)rtDW->UnitDelay2_DSTATE_f + rtDW->UnitDelay3_DSTATE_lh) +
805 rtDW->UnitDelay5_DSTATE_f) + rtDW->n_commDeacv_Mode) >= 4);
806
807 /* Update for UnitDelay: '<S19>/UnitDelay2' incorporates:
808 * UnitDelay: '<S19>/UnitDelay3'
809 */
810 rtDW->UnitDelay2_DSTATE_f = rtDW->UnitDelay3_DSTATE_lh;
811
812 /* Update for UnitDelay: '<S19>/UnitDelay3' incorporates:
813 * UnitDelay: '<S19>/UnitDelay5'
814 */
815 rtDW->UnitDelay3_DSTATE_lh = rtDW->UnitDelay5_DSTATE_f;
816
817 /* Update for UnitDelay: '<S19>/UnitDelay5' incorporates:
818 * Logic: '<S19>/Logical Operator3'
819 * Relay: '<S19>/n_commDeacv'
820 */
821 rtDW->UnitDelay5_DSTATE_f = rtDW->n_commDeacv_Mode;
822
823 /* End of Outputs for SubSystem: '<S14>/AdvCtrlDetect' */
824 }
825
826 /* End of If: '<S14>/If1' */
827
828 /* Switch: '<S37>/Switch3' incorporates:
829 * Abs: '<S14>/Abs5'
830 * Abs: '<S37>/Abs4'
831 * Constant: '<S37>/CTRL_COMM4'
832 * Inport: '<Root>/b_motEna'
833 * Logic: '<S37>/Logical Operator1'
834 * RelationalOperator: '<S14>/Relational Operator9'
835 * RelationalOperator: '<S37>/Relational Operator7'
836 * S-Function (sfix_bitop): '<S37>/Bitwise Operator1'
837 * UnitDelay: '<S6>/UnitDelay1'
838 */
839 if ((rtb_UnitDelay_bc & 4U) != 0U) {
840 rtb_Equal_k = true;
841 } else {
842 if (rtDW->UnitDelay1_DSTATE_f[1] < 0) {
843 /* Abs: '<S37>/Abs4' incorporates:
844 * UnitDelay: '<S6>/UnitDelay1'
845 */
846 rtb_Sum6_p = (int16_T)-rtDW->UnitDelay1_DSTATE_f[1];
847 } else {
848 /* Abs: '<S37>/Abs4' incorporates:
849 * UnitDelay: '<S6>/UnitDelay1'
850 */
851 rtb_Sum6_p = rtDW->UnitDelay1_DSTATE_f[1];
852 }
853
854 rtb_Equal_k = (rtU->b_motEna && (rtb_Switch2 < 48U) && (rtb_Sum6_p > 9920));
855 }
856
857 /* End of Switch: '<S37>/Switch3' */
858
859 /* Sum: '<S37>/Sum' incorporates:
860 * Constant: '<S37>/CTRL_COMM'
861 * Constant: '<S37>/CTRL_COMM1'
862 * DataTypeConversion: '<S37>/Data Type Conversion3'
863 * Gain: '<S37>/g_Hb'
864 * Gain: '<S37>/g_Hb1'
865 * RelationalOperator: '<S37>/Relational Operator1'
866 * RelationalOperator: '<S37>/Relational Operator3'
867 */
868 rtb_Sum_i = (uint8_T)(((uint32_T)((rtb_Add_gf == 7) << 1) + (rtb_Add_gf == 0))
869 + (rtb_Equal_k << 2));
870
871 /* RelationalOperator: '<S37>/Relational Operator2' incorporates:
872 * Constant: '<S37>/CTRL_COMM2'
873 */
874 rtb_RelationalOperator4_f = (rtb_Sum_i != 0);
875
876 /* RelationalOperator: '<S42>/Relational Operator' incorporates:
877 * UnitDelay: '<S42>/UnitDelay'
878 */
879 rtb_n_commDeacv = (rtb_RelationalOperator4_f != rtDW->UnitDelay_DSTATE_n);
880
881 /* If: '<S38>/If2' incorporates:
882 * Inport: '<S40>/yPrev'
883 * Logic: '<S38>/Logical Operator1'
884 * Logic: '<S38>/Logical Operator2'
885 * Logic: '<S38>/Logical Operator3'
886 * Logic: '<S38>/Logical Operator4'
887 * UnitDelay: '<S38>/UnitDelay'
888 */
889 if (rtb_RelationalOperator4_f && (!rtDW->UnitDelay_DSTATE_k)) {
890 /* Outputs for IfAction SubSystem: '<S38>/Qualification' incorporates:
891 * ActionPort: '<S43>/Action Port'
892 */
893 /* Switch: '<S47>/Switch1' incorporates:
894 * Constant: '<S47>/Constant23'
895 * UnitDelay: '<S47>/UnitDelay'
896 */
897 if (rtb_n_commDeacv) {
898 rtb_LogicalOperator3 = 0U;
899 } else {
900 rtb_LogicalOperator3 = rtDW->UnitDelay_DSTATE_p;
901 }
902
903 /* End of Switch: '<S47>/Switch1' */
904
905 /* Switch: '<S43>/Switch2' incorporates:
906 * Constant: '<S37>/t_errQual'
907 * Constant: '<S43>/Constant6'
908 * RelationalOperator: '<S43>/Relational Operator2'
909 * Sum: '<S46>/Sum1'
910 */
911 rtb_n_commDeacv = (((uint16_T)(rtb_LogicalOperator3 + 1U) > 1600) ||
912 rtDW->UnitDelay_DSTATE_k);
913
914 /* MinMax: '<S46>/MinMax' incorporates:
915 * Constant: '<S43>/Constant6'
916 * Sum: '<S46>/Sum1'
917 */
918 if ((uint16_T)(rtb_LogicalOperator3 + 1U) < 1600) {
919 /* Update for UnitDelay: '<S47>/UnitDelay' */
920 rtDW->UnitDelay_DSTATE_p = (uint16_T)(rtb_LogicalOperator3 + 1U);
921 } else {
922 /* Update for UnitDelay: '<S47>/UnitDelay' */
923 rtDW->UnitDelay_DSTATE_p = 1600U;
924 }
925
926 /* End of MinMax: '<S46>/MinMax' */
927 /* End of Outputs for SubSystem: '<S38>/Qualification' */
928 } else if ((!rtb_RelationalOperator4_f) && rtDW->UnitDelay_DSTATE_k) {
929 /* Outputs for IfAction SubSystem: '<S38>/Dequalification' incorporates:
930 * ActionPort: '<S41>/Action Port'
931 */
932 /* Switch: '<S45>/Switch1' incorporates:
933 * Constant: '<S45>/Constant23'
934 * UnitDelay: '<S45>/UnitDelay'
935 */
936 if (rtb_n_commDeacv) {
937 rtb_LogicalOperator3 = 0U;
938 } else {
939 rtb_LogicalOperator3 = rtDW->UnitDelay_DSTATE_f;
940 }
941
942 /* End of Switch: '<S45>/Switch1' */
943
944 /* Switch: '<S41>/Switch2' incorporates:
945 * Constant: '<S37>/t_errDequal'
946 * Constant: '<S41>/Constant6'
947 * RelationalOperator: '<S41>/Relational Operator2'
948 * Sum: '<S44>/Sum1'
949 */
950 rtb_n_commDeacv = (((uint16_T)(rtb_LogicalOperator3 + 1U) <= 12000) &&
951 rtDW->UnitDelay_DSTATE_k);
952
953 /* MinMax: '<S44>/MinMax' incorporates:
954 * Constant: '<S41>/Constant6'
955 * Sum: '<S44>/Sum1'
956 */
957 if ((uint16_T)(rtb_LogicalOperator3 + 1U) < 12000) {
958 /* Update for UnitDelay: '<S45>/UnitDelay' */
959 rtDW->UnitDelay_DSTATE_f = (uint16_T)(rtb_LogicalOperator3 + 1U);
960 } else {
961 /* Update for UnitDelay: '<S45>/UnitDelay' */
962 rtDW->UnitDelay_DSTATE_f = 12000U;
963 }
964
965 /* End of MinMax: '<S44>/MinMax' */
966 /* End of Outputs for SubSystem: '<S38>/Dequalification' */
967 } else {
968 /* Outputs for IfAction SubSystem: '<S38>/Default' incorporates:
969 * ActionPort: '<S40>/Action Port'
970 */
971 rtb_n_commDeacv = rtDW->UnitDelay_DSTATE_k;
972
973 /* End of Outputs for SubSystem: '<S38>/Default' */
974 }
975
976 /* End of If: '<S38>/If2' */
977
978 /* Logic: '<S25>/Logical Operator12' incorporates:
979 * Inport: '<Root>/b_motEna'
980 * Logic: '<S25>/Logical Operator7'
981 */
982 rtb_LogicalOperator12 = ((!rtb_n_commDeacv) && rtU->b_motEna);
983
984 /* Logic: '<S25>/Logical Operator4' incorporates:
985 * Constant: '<S25>/constant8'
986 * Inport: '<Root>/n_ctrlModReq'
987 * Logic: '<S25>/Logical Operator11'
988 * Logic: '<S25>/Logical Operator8'
989 * RelationalOperator: '<S25>/Relational Operator10'
990 */
991 rtb_LogicalOperator4_e = ((rtb_BitwiseOperator2 != 0) || (!rtDW->Compare) || (
992 !rtb_LogicalOperator12) || (rtU->n_ctrlModReq == 0));
993
994 /* Abs: '<S4>/Abs2' incorporates:
995 * Switch: '<S14>/Switch2'
996 */
997 if (rtb_Switch3 < 0) {
998 rtb_LogicalOperator3 = (uint16_T)((uint32_T)-rtb_Switch3 >> 4);
999 } else {
1000 rtb_LogicalOperator3 = (uint16_T)((uint32_T)rtb_Switch3 >> 4);
1001 }
1002
1003 /* End of Abs: '<S4>/Abs2' */
1004
1005 /* Relay: '<S25>/n_SpeedCtrl' */
1006 rtDW->n_SpeedCtrl_Mode = ((rtb_LogicalOperator3 >= 300) ||
1007 ((rtb_LogicalOperator3 > 200) && rtDW->n_SpeedCtrl_Mode));
1008
1009 /* Logic: '<S25>/Logical Operator10' incorporates:
1010 * Inport: '<Root>/b_cruiseEna'
1011 * Relay: '<S25>/n_SpeedCtrl'
1012 */
1013 rtb_Equal_k = (rtDW->n_SpeedCtrl_Mode && rtU->b_cruiseEna);
1014
1015 /* Logic: '<S25>/Logical Operator2' incorporates:
1016 * Constant: '<S25>/constant'
1017 * Inport: '<Root>/n_ctrlModReq'
1018 * Logic: '<S25>/Logical Operator5'
1019 * RelationalOperator: '<S25>/Relational Operator4'
1020 */
1021 rtb_LogicalOperator2_h = ((rtU->n_ctrlModReq == 2) && (!rtb_Equal_k));
1022
1023 /* Logic: '<S25>/Logical Operator1' incorporates:
1024 * Constant: '<S25>/constant1'
1025 * Inport: '<Root>/n_ctrlModReq'
1026 * RelationalOperator: '<S25>/Relational Operator1'
1027 */
1028 rtb_Equal_k = ((rtU->n_ctrlModReq == 1) || rtb_Equal_k);
1029
1030 /* Chart: '<S4>/Control_Mode_Manager' incorporates:
1031 * Logic: '<S25>/Logical Operator3'
1032 * Logic: '<S25>/Logical Operator6'
1033 * Logic: '<S25>/Logical Operator9'
1034 */
1035 if (rtDW->is_active_c5_PMSM_Controller == 0U) {
1036 rtDW->is_active_c5_PMSM_Controller = 1U;
1037 rtDW->is_c5_PMSM_Controller = IN_OPEN;
1038 rtb_z_ctrlMod = OPEN_MODE;
1039 } else if (rtDW->is_c5_PMSM_Controller == 1) {
1040 if (rtb_LogicalOperator4_e) {
1041 rtDW->is_ACTIVE = IN_NO_ACTIVE_CHILD;
1042 rtDW->is_c5_PMSM_Controller = IN_OPEN;
1043 rtb_z_ctrlMod = OPEN_MODE;
1044 } else if (rtDW->is_ACTIVE == 1) {
1045 rtb_z_ctrlMod = SPD_MODE;
1046 if (!rtb_Equal_k) {
1047 if (rtb_LogicalOperator2_h) {
1048 rtDW->is_ACTIVE = IN_TORQUE_MODE;
1049 rtb_z_ctrlMod = TRQ_MODE;
1050 } else {
1051 rtDW->is_ACTIVE = IN_SPEED_MODE;
1052 }
1053 }
1054 } else {
1055 /* case IN_TORQUE_MODE: */
1056 rtb_z_ctrlMod = TRQ_MODE;
1057 if (!rtb_LogicalOperator2_h) {
1058 rtDW->is_ACTIVE = IN_SPEED_MODE;
1059 rtb_z_ctrlMod = SPD_MODE;
1060 }
1061 }
1062 } else {
1063 /* case IN_OPEN: */
1064 rtb_z_ctrlMod = OPEN_MODE;
1065 if ((!rtb_LogicalOperator4_e) && (rtb_LogicalOperator2_h || rtb_Equal_k)) {
1066 rtDW->is_c5_PMSM_Controller = IN_ACTIVE;
1067 if (rtb_LogicalOperator2_h) {
1068 rtDW->is_ACTIVE = IN_TORQUE_MODE;
1069 rtb_z_ctrlMod = TRQ_MODE;
1070 } else {
1071 rtDW->is_ACTIVE = IN_SPEED_MODE;
1072 rtb_z_ctrlMod = SPD_MODE;
1073 }
1074 }
1075 }
1076
1077 /* End of Chart: '<S4>/Control_Mode_Manager' */
1078
1079 /* Gain: '<S53>/Multiply' incorporates:
1080 * Inport: '<Root>/adc_Pha'
1081 * Inport: '<Root>/adc_Phb'
1082 */
1083 rtb_Gain_b0 = (12351 * rtU->adc_Pha) >> 12;
1084 if (rtb_Gain_b0 > 32767) {
1085 rtb_Gain_b0 = 32767;
1086 } else {
1087 if (rtb_Gain_b0 < -32768) {
1088 rtb_Gain_b0 = -32768;
1089 }
1090 }
1091
1092 tmp_2 = (12351 * rtU->adc_Phb) >> 12;
1093 if (tmp_2 > 32767) {
1094 tmp_2 = 32767;
1095 } else {
1096 if (tmp_2 < -32768) {
1097 tmp_2 = -32768;
1098 }
1099 }
1100
1101 /* Sum: '<S48>/Add' incorporates:
1102 * Gain: '<S53>/Multiply'
1103 */
1104 tmp_0 = (int16_T)rtb_Gain_b0 + (int16_T)tmp_2;
1105 if (tmp_0 > 32767) {
1106 tmp_0 = 32767;
1107 } else {
1108 if (tmp_0 < -32768) {
1109 tmp_0 = -32768;
1110 }
1111 }
1112
1113 /* Sum: '<S48>/Add1' incorporates:
1114 * Sum: '<S48>/Add'
1115 */
1116 tmp_1 = -tmp_0;
1117 if (-tmp_0 > 32767) {
1118 tmp_1 = 32767;
1119 }
1120
1121 /* Sum: '<S56>/Add3' incorporates:
1122 * Gain: '<S53>/Multiply'
1123 * Sum: '<S48>/Add1'
1124 */
1125 tmp_0 = (int16_T)tmp_2 + (int16_T)tmp_1;
1126
1127 /* Gain: '<S56>/Gain' incorporates:
1128 * Gain: '<S53>/Multiply'
1129 */
1130 if ((int16_T)rtb_Gain_b0 > 16383) {
1131 rtb_Sum6_p = MAX_int16_T;
1132 } else if ((int16_T)rtb_Gain_b0 <= -16384) {
1133 rtb_Sum6_p = MIN_int16_T;
1134 } else {
1135 rtb_Sum6_p = (int16_T)((int16_T)rtb_Gain_b0 << 1);
1136 }
1137
1138 /* End of Gain: '<S56>/Gain' */
1139
1140 /* Sum: '<S56>/Add3' */
1141 if (tmp_0 > 16383) {
1142 rtb_Divide1_m = MAX_int16_T;
1143 } else if (tmp_0 <= -16384) {
1144 rtb_Divide1_m = MIN_int16_T;
1145 } else {
1146 rtb_Divide1_m = (int16_T)(tmp_0 << 1);
1147 }
1148
1149 /* Sum: '<S56>/Add' */
1150 rtb_Gain_b0 = ((rtb_Sum6_p << 1) - rtb_Divide1_m) >> 1;
1151 if (rtb_Gain_b0 > 32767) {
1152 rtb_Gain_b0 = 32767;
1153 } else {
1154 if (rtb_Gain_b0 < -32768) {
1155 rtb_Gain_b0 = -32768;
1156 }
1157 }
1158
1159 /* Gain: '<S56>/Gain1' incorporates:
1160 * Product: '<S58>/Divide1'
1161 * Sum: '<S56>/Add'
1162 */
1163 rtb_Divide1_m = (int16_T)((21845 * rtb_Gain_b0) >> 16);
1164
1165 /* Switch: '<S10>/Switch3' incorporates:
1166 * Constant: '<S10>/Constant16'
1167 * Constant: '<S10>/Constant2'
1168 * Constant: '<S11>/vec_hallToPos'
1169 * RelationalOperator: '<S10>/Relational Operator7'
1170 * Selector: '<S11>/Selector'
1171 * Sum: '<S10>/Sum1'
1172 */
1173 if (rtDW->Switch2_i == 1) {
1174 rtb_Sum2 = rtConstP.vec_hallToPos_Value[rtb_Add_gf];
1175 } else {
1176 rtb_Sum2 = (int8_T)(rtConstP.vec_hallToPos_Value[rtb_Add_gf] + 1);
1177 }
1178
1179 /* End of Switch: '<S10>/Switch3' */
1180
1181 /* MinMax: '<S10>/MinMax' incorporates:
1182 * Inport: '<Root>/us_Count'
1183 */
1184 if (rtU->us_Count < rtDW->z_counterRawPrev) {
1185 qY = rtU->us_Count;
1186 } else {
1187 qY = rtDW->z_counterRawPrev;
1188 }
1189
1190 /* End of MinMax: '<S10>/MinMax' */
1191
1192 /* Sum: '<S10>/Sum3' incorporates:
1193 * Product: '<S10>/Divide1'
1194 * Product: '<S10>/Divide3'
1195 */
1196 rtb_Sum3_jm = (int16_T)(((int16_T)((int16_T)(((uint64_T)qY << 14) /
1197 rtDW->z_counterRawPrev) * rtDW->Switch2_i) + (rtb_Sum2 << 14)) >> 2);
1198
1199 /* MinMax: '<S10>/MinMax1' incorporates:
1200 * Constant: '<S10>/Constant1'
1201 * Sum: '<S10>/Sum3'
1202 * Switch: '<S10>/Switch2'
1203 */
1204 if (rtb_Sum3_jm <= 0) {
1205 rtb_Sum3_jm = 0;
1206 }
1207
1208 /* End of MinMax: '<S10>/MinMax1' */
1209
1210 /* Sum: '<S15>/Add2' incorporates:
1211 * Constant: '<S15>/Constant2'
1212 * Product: '<S10>/Divide2'
1213 */
1214 rtb_Sum3_jm = (int16_T)((((15 * rtb_Sum3_jm) >> 4) + (rtP.i_hall_offset << 2))
1215 >> 2);
1216
1217 /* DataTypeConversion: '<S15>/Data Type Conversion' incorporates:
1218 * Sum: '<S15>/Add2'
1219 */
1220 rtb_r_cos_M1 = (int16_T)(rtb_Sum3_jm >> 4);
1221
1222 /* If: '<S15>/If' incorporates:
1223 * Constant: '<S15>/Constant1'
1224 * Constant: '<S15>/Constant3'
1225 * Inport: '<S16>/In1'
1226 * Inport: '<S18>/In1'
1227 * Merge: '<S15>/Merge'
1228 * Sum: '<S15>/Add'
1229 * Sum: '<S15>/Add1'
1230 * Sum: '<S15>/Add2'
1231 */
1232 if (rtb_r_cos_M1 >= 360) {
1233 /* Outputs for IfAction SubSystem: '<S15>/If Action Subsystem' incorporates:
1234 * ActionPort: '<S16>/Action Port'
1235 */
1236 rtb_Sum3_jm = (int16_T)(rtb_Sum3_jm - 5760);
1237
1238 /* End of Outputs for SubSystem: '<S15>/If Action Subsystem' */
1239 } else {
1240 if (rtb_r_cos_M1 < 0) {
1241 /* Outputs for IfAction SubSystem: '<S15>/If Action Subsystem2' incorporates:
1242 * ActionPort: '<S18>/Action Port'
1243 */
1244 rtb_Sum3_jm = (int16_T)(rtb_Sum3_jm + 5760);
1245
1246 /* End of Outputs for SubSystem: '<S15>/If Action Subsystem2' */
1247 }
1248 }
1249
1250 /* End of If: '<S15>/If' */
1251
1252 /* If: '<S3>/If' incorporates:
1253 * Inport: '<Root>/FOC_Flags'
1254 */
1255 if ((rtU->FOC_Flags == 0) || (rtU->FOC_Flags == 2)) {
1256 /* Outputs for IfAction SubSystem: '<S3>/If Action Subsystem' incorporates:
1257 * ActionPort: '<S12>/Action Port'
1258 */
1259 /* Merge: '<S3>/Merge' incorporates:
1260 * Inport: '<S12>/In1'
1261 * Merge: '<S15>/Merge'
1262 */
1263 rtDW->Merge_i = rtb_Sum3_jm;
1264
1265 /* End of Outputs for SubSystem: '<S3>/If Action Subsystem' */
1266 } else {
1267 if (rtU->FOC_Flags == 1) {
1268 /* Outputs for IfAction SubSystem: '<S3>/If Action Subsystem1' incorporates:
1269 * ActionPort: '<S13>/Action Port'
1270 */
1271 /* Merge: '<S3>/Merge' incorporates:
1272 * Inport: '<Root>/theta_Open'
1273 * Inport: '<S13>/In1'
1274 */
1275 rtDW->Merge_i = rtU->theta_Open;
1276
1277 /* End of Outputs for SubSystem: '<S3>/If Action Subsystem1' */
1278 }
1279 }
1280
1281 /* End of If: '<S3>/If' */
1282
1283 /* PreLookup: '<S59>/a_elecAngle_XA' incorporates:
1284 * Merge: '<S3>/Merge'
1285 */
1286 rtb_LogicalOperator3 = plook_u16s16_evencka(rtDW->Merge_i, 0, 16U, 360U);
1287
1288 /* Sum: '<S56>/Add2' incorporates:
1289 * Gain: '<S53>/Multiply'
1290 * Sum: '<S48>/Add1'
1291 */
1292 rtb_Gain_b0 = (int16_T)tmp_2 - (int16_T)tmp_1;
1293 if (rtb_Gain_b0 > 32767) {
1294 rtb_Gain_b0 = 32767;
1295 } else {
1296 if (rtb_Gain_b0 < -32768) {
1297 rtb_Gain_b0 = -32768;
1298 }
1299 }
1300
1301 /* Gain: '<S56>/Gain2' incorporates:
1302 * Sum: '<S56>/Add2'
1303 * Sum: '<S58>/Sum6'
1304 */
1305 rtb_Sum6_p = (int16_T)((18919 * rtb_Gain_b0) >> 15);
1306
1307 /* Sum: '<S58>/Sum1' incorporates:
1308 * Interpolation_n-D: '<S59>/r_cos_M1'
1309 * Interpolation_n-D: '<S59>/r_sin_M1'
1310 * Product: '<S58>/Divide1'
1311 * Product: '<S58>/Divide2'
1312 * Product: '<S58>/Divide3'
1313 * Sum: '<S58>/Sum6'
1314 */
1315 rtb_Gain_b0 = ((rtb_Divide1_m * rtConstP.pooled8[rtb_LogicalOperator3]) >> 14)
1316 + (int16_T)((rtb_Sum6_p * rtConstP.pooled7[rtb_LogicalOperator3]) >> 14);
1317 if (rtb_Gain_b0 > 32767) {
1318 rtb_Gain_b0 = 32767;
1319 } else {
1320 if (rtb_Gain_b0 < -32768) {
1321 rtb_Gain_b0 = -32768;
1322 }
1323 }
1324
1325 /* SignalConversion generated from: '<S48>/Low_Pass_Filter' incorporates:
1326 * Sum: '<S58>/Sum1'
1327 */
1328 rtb_DataTypeConversion_b[0] = (int16_T)rtb_Gain_b0;
1329
1330 /* Sum: '<S58>/Sum6' incorporates:
1331 * Interpolation_n-D: '<S59>/r_cos_M1'
1332 * Interpolation_n-D: '<S59>/r_sin_M1'
1333 * Product: '<S58>/Divide1'
1334 * Product: '<S58>/Divide4'
1335 */
1336 rtb_Gain_b0 = (int16_T)((rtb_Sum6_p * rtConstP.pooled8[rtb_LogicalOperator3]) >>
1337 14) - ((rtb_Divide1_m * rtConstP.pooled7[rtb_LogicalOperator3]) >> 14);
1338 if (rtb_Gain_b0 > 32767) {
1339 rtb_Gain_b0 = 32767;
1340 } else {
1341 if (rtb_Gain_b0 < -32768) {
1342 rtb_Gain_b0 = -32768;
1343 }
1344 }
1345
1346 /* SignalConversion generated from: '<S48>/Low_Pass_Filter' incorporates:
1347 * Sum: '<S58>/Sum6'
1348 */
1349 rtb_DataTypeConversion_b[1] = (int16_T)rtb_Gain_b0;
1350
1351 /* Outputs for Atomic SubSystem: '<S48>/Low_Pass_Filter' */
1352 /* Constant: '<S48>/Constant' incorporates:
1353 * Outport: '<Root>/f_Idq'
1354 */
1355 Low_Pass_Filter(rtb_DataTypeConversion_b, rtP.f_lpf_coeff, rtY->f_Idq,
1356 &rtDW->Low_Pass_Filter_d);
1357
1358 /* End of Outputs for SubSystem: '<S48>/Low_Pass_Filter' */
1359
1360 /* Outputs for Atomic SubSystem: '<S6>/Low_Pass_Filter' */
1361 /* Constant: '<S6>/Constant' */
1362 Low_Pass_Filter(rtb_UnitDelay1, 655, rtb_DataTypeConversion_b,
1363 &rtDW->Low_Pass_Filter_h);
1364
1365 /* End of Outputs for SubSystem: '<S6>/Low_Pass_Filter' */
1366
1367 /* Switch: '<S24>/Switch' incorporates:
1368 * Constant: '<S24>/Constant3'
1369 * Inport: '<Root>/spd_Target'
1370 */
1371 if (rtU->spd_Target > 240) {
1372 /* Switch: '<S24>/Switch1' incorporates:
1373 * Constant: '<S24>/Constant1'
1374 * DataTypeConversion: '<S24>/Data Type Conversion'
1375 * Switch: '<S24>/Switch'
1376 */
1377 if (rtb_LogicalOperator12) {
1378 rtb_Switch = rtU->spd_Target;
1379 } else {
1380 rtb_Switch = 0;
1381 }
1382
1383 /* End of Switch: '<S24>/Switch1' */
1384 } else {
1385 rtb_Switch = 0;
1386 }
1387
1388 /* End of Switch: '<S24>/Switch' */
1389
1390 /* Switch: '<S24>/Switch3' incorporates:
1391 * Constant: '<S24>/Constant4'
1392 * DataTypeConversion: '<S24>/Data Type Conversion2'
1393 * Inport: '<Root>/vdq_Open'
1394 */
1395 if (rtb_LogicalOperator12) {
1396 rtb_Sum6_p = rtU->vdq_Open[1];
1397 } else {
1398 rtb_Sum6_p = 0;
1399 }
1400
1401 /* End of Switch: '<S24>/Switch3' */
1402
1403 /* Sum: '<S7>/Sum3' incorporates:
1404 * UnitDelay: '<S7>/UnitDelay1'
1405 */
1406 qY = rtDW->UnitDelay1_DSTATE + /*MW:OvSatOk*/ 1U;
1407 if (rtDW->UnitDelay1_DSTATE + 1U < 1U) {
1408 qY = MAX_uint32_T;
1409 }
1410
1411 /* RelationalOperator: '<S2>/Equal' incorporates:
1412 * Constant: '<S2>/Constant1'
1413 * Math: '<S2>/Rem'
1414 * Sum: '<S7>/Sum3'
1415 */
1416 rtb_Equal_k = (qY % 40U == 0U);
1417
1418 /* If: '<S26>/If' incorporates:
1419 * DataTypeConversion: '<S26>/Data Type Conversion1'
1420 * DataTypeConversion: '<S26>/Data Type Conversion2'
1421 * Inport: '<Root>/idq_Target'
1422 * Inport: '<S27>/vq_in'
1423 * Inport: '<S30>/r_currTgt'
1424 * Switch: '<S24>/Switch3'
1425 */
1426 if (rtb_BitwiseOperator2 == 1) {
1427 /* Switch: '<S24>/Switch2' incorporates:
1428 * Constant: '<S24>/Constant2'
1429 * DataTypeConversion: '<S24>/Data Type Conversion1'
1430 * Inport: '<Root>/vdq_Open'
1431 * Inport: '<S27>/vd_in'
1432 */
1433 if (rtb_LogicalOperator12) {
1434 /* Outputs for IfAction SubSystem: '<S26>/If Action Subsystem' incorporates:
1435 * ActionPort: '<S27>/Action Port'
1436 */
1437 rtDW->Merge[0] = rtU->vdq_Open[0];
1438
1439 /* End of Outputs for SubSystem: '<S26>/If Action Subsystem' */
1440 } else {
1441 /* Outputs for IfAction SubSystem: '<S26>/If Action Subsystem' incorporates:
1442 * ActionPort: '<S27>/Action Port'
1443 */
1444 rtDW->Merge[0] = 0;
1445
1446 /* End of Outputs for SubSystem: '<S26>/If Action Subsystem' */
1447 }
1448
1449 /* End of Switch: '<S24>/Switch2' */
1450
1451 /* Outputs for IfAction SubSystem: '<S26>/If Action Subsystem' incorporates:
1452 * ActionPort: '<S27>/Action Port'
1453 */
1454 rtDW->Merge[1] = rtb_Sum6_p;
1455
1456 /* End of Outputs for SubSystem: '<S26>/If Action Subsystem' */
1457 } else if ((rtb_z_ctrlMod == 0) && rtb_Equal_k) {
1458 /* Outputs for IfAction SubSystem: '<S26>/open_mode' incorporates:
1459 * ActionPort: '<S29>/Action Port'
1460 */
1461 /* RelationalOperator: '<S31>/Relational Operator' incorporates:
1462 * Switch: '<S24>/Switch3'
1463 * UnitDelay: '<S31>/UnitDelay'
1464 */
1465 rtb_LogicalOperator12 = (rtb_Sum6_p != rtDW->UnitDelay_DSTATE_e);
1466
1467 /* If: '<S32>/If' */
1468 if (rtb_LogicalOperator12) {
1469 /* Outputs for IfAction SubSystem: '<S32>/RateInit' incorporates:
1470 * ActionPort: '<S33>/Action Port'
1471 */
1472 /* Sum: '<S33>/Add' incorporates:
1473 * Switch: '<S24>/Switch3'
1474 * UnitDelay: '<S6>/UnitDelay1'
1475 */
1476 rtb_Divide1_m = (int16_T)((rtb_Sum6_p - rtDW->UnitDelay1_DSTATE_f[1]) >> 1);
1477
1478 /* Signum: '<S33>/Sign' incorporates:
1479 * Sum: '<S33>/Add'
1480 */
1481 if (rtb_Divide1_m < 0) {
1482 rtb_Divide1_m = -1;
1483 } else {
1484 rtb_Divide1_m = (int16_T)(rtb_Divide1_m > 0);
1485 }
1486
1487 /* End of Signum: '<S33>/Sign' */
1488
1489 /* Product: '<S33>/Divide' incorporates:
1490 * Constant: '<S29>/Constant5'
1491 */
1492 rtDW->Divide = (int16_T)(rtP.dz_OpenStepVol * rtb_Divide1_m);
1493
1494 /* MinMax: '<S33>/Max' incorporates:
1495 * Switch: '<S24>/Switch3'
1496 * UnitDelay: '<S6>/UnitDelay1'
1497 */
1498 if (rtb_Sum6_p > rtDW->UnitDelay1_DSTATE_f[1]) {
1499 /* MinMax: '<S33>/Max' */
1500 rtDW->Max_p = rtb_Sum6_p;
1501 } else {
1502 /* MinMax: '<S33>/Max' */
1503 rtDW->Max_p = rtDW->UnitDelay1_DSTATE_f[1];
1504 }
1505
1506 /* End of MinMax: '<S33>/Max' */
1507
1508 /* MinMax: '<S33>/Max1' incorporates:
1509 * Switch: '<S24>/Switch3'
1510 * UnitDelay: '<S6>/UnitDelay1'
1511 */
1512 if (rtDW->UnitDelay1_DSTATE_f[1] < rtb_Sum6_p) {
1513 /* MinMax: '<S33>/Max1' */
1514 rtDW->Max1_g = rtDW->UnitDelay1_DSTATE_f[1];
1515 } else {
1516 /* MinMax: '<S33>/Max1' */
1517 rtDW->Max1_g = rtb_Sum6_p;
1518 }
1519
1520 /* End of MinMax: '<S33>/Max1' */
1521 /* End of Outputs for SubSystem: '<S32>/RateInit' */
1522
1523 /* Switch: '<S36>/Switch1' incorporates:
1524 * UnitDelay: '<S6>/UnitDelay1'
1525 */
1526 rtb_r_cos_M1 = rtDW->UnitDelay1_DSTATE_f[1];
1527 } else {
1528 /* Switch: '<S36>/Switch1' incorporates:
1529 * UnitDelay: '<S36>/UnitDelay'
1530 */
1531 rtb_r_cos_M1 = rtDW->UnitDelay_DSTATE_fv;
1532 }
1533
1534 /* End of If: '<S32>/If' */
1535
1536 /* Switch: '<S32>/Switch' incorporates:
1537 * Constant: '<S32>/Constant'
1538 * Product: '<S33>/Divide'
1539 * RelationalOperator: '<S32>/Equal'
1540 * Switch: '<S24>/Switch3'
1541 * UnitDelay: '<S32>/Unit Delay'
1542 */
1543 if (rtb_Sum6_p != rtDW->UnitDelay_DSTATE_i) {
1544 rtb_Divide1_m = rtDW->Divide;
1545 } else {
1546 rtb_Divide1_m = 0;
1547 }
1548
1549 /* End of Switch: '<S32>/Switch' */
1550
1551 /* Sum: '<S35>/Add2' */
1552 rtb_Gain_b0 = ((rtb_r_cos_M1 << 2) + rtb_Divide1_m) >> 2;
1553 if (rtb_Gain_b0 > 32767) {
1554 rtb_Gain_b0 = 32767;
1555 } else {
1556 if (rtb_Gain_b0 < -32768) {
1557 rtb_Gain_b0 = -32768;
1558 }
1559 }
1560
1561 /* Switch: '<S34>/Switch2' incorporates:
1562 * MinMax: '<S33>/Max'
1563 * MinMax: '<S33>/Max1'
1564 * RelationalOperator: '<S34>/LowerRelop1'
1565 * RelationalOperator: '<S34>/UpperRelop'
1566 * Sum: '<S35>/Add2'
1567 * Switch: '<S34>/Switch'
1568 */
1569 if ((int16_T)rtb_Gain_b0 > rtDW->Max_p) {
1570 rtb_r_cos_M1 = rtDW->Max_p;
1571 } else if ((int16_T)rtb_Gain_b0 < rtDW->Max1_g) {
1572 /* Switch: '<S34>/Switch' incorporates:
1573 * MinMax: '<S33>/Max1'
1574 * Switch: '<S34>/Switch2'
1575 */
1576 rtb_r_cos_M1 = rtDW->Max1_g;
1577 } else {
1578 rtb_r_cos_M1 = (int16_T)rtb_Gain_b0;
1579 }
1580
1581 /* End of Switch: '<S34>/Switch2' */
1582
1583 /* Merge: '<S26>/Merge' incorporates:
1584 * Constant: '<S29>/Constant3'
1585 * SignalConversion generated from: '<S29>/open_voltage'
1586 */
1587 rtDW->Merge[0] = 0;
1588
1589 /* Switch: '<S29>/Switch' incorporates:
1590 * Switch: '<S24>/Switch'
1591 */
1592 if (rtb_Switch > 0) {
1593 /* Merge: '<S26>/Merge' incorporates:
1594 * SignalConversion generated from: '<S29>/open_voltage'
1595 * Switch: '<S34>/Switch2'
1596 */
1597 rtDW->Merge[1] = rtb_r_cos_M1;
1598 } else {
1599 /* Merge: '<S26>/Merge' incorporates:
1600 * Constant: '<S29>/Constant1'
1601 * SignalConversion generated from: '<S29>/open_voltage'
1602 */
1603 rtDW->Merge[1] = 0;
1604 }
1605
1606 /* End of Switch: '<S29>/Switch' */
1607
1608 /* Update for UnitDelay: '<S31>/UnitDelay' incorporates:
1609 * Switch: '<S24>/Switch3'
1610 */
1611 rtDW->UnitDelay_DSTATE_e = rtb_Sum6_p;
1612
1613 /* Switch: '<S36>/Switch2' */
1614 if (rtb_LogicalOperator12) {
1615 /* Update for UnitDelay: '<S36>/UnitDelay' incorporates:
1616 * UnitDelay: '<S6>/UnitDelay1'
1617 */
1618 rtDW->UnitDelay_DSTATE_fv = rtDW->UnitDelay1_DSTATE_f[1];
1619 } else {
1620 /* Update for UnitDelay: '<S36>/UnitDelay' incorporates:
1621 * Sum: '<S35>/Add2'
1622 */
1623 rtDW->UnitDelay_DSTATE_fv = (int16_T)rtb_Gain_b0;
1624 }
1625
1626 /* End of Switch: '<S36>/Switch2' */
1627
1628 /* Update for UnitDelay: '<S32>/Unit Delay' incorporates:
1629 * Switch: '<S34>/Switch2'
1630 */
1631 rtDW->UnitDelay_DSTATE_i = rtb_r_cos_M1;
1632
1633 /* End of Outputs for SubSystem: '<S26>/open_mode' */
1634 } else if (rtb_z_ctrlMod == 2) {
1635 /* Outputs for IfAction SubSystem: '<S26>/torque_mode' incorporates:
1636 * ActionPort: '<S30>/Action Port'
1637 */
1638 rtDW->r_currTgt = rtU->idq_Target;
1639
1640 /* Merge: '<S26>/Merge1' incorporates:
1641 * Inport: '<Root>/idq_Target'
1642 * Inport: '<S30>/r_currTgt'
1643 * Inport: '<S30>/r_spdTgt'
1644 * Switch: '<S24>/Switch'
1645 */
1646 rtDW->Merge1 = rtb_Switch;
1647
1648 /* End of Outputs for SubSystem: '<S26>/torque_mode' */
1649 } else {
1650 /* Outputs for IfAction SubSystem: '<S26>/If Action Subsystem1' incorporates:
1651 * ActionPort: '<S28>/Action Port'
1652 */
1653 /* Merge: '<S26>/Merge1' incorporates:
1654 * Inport: '<S28>/In1'
1655 * Switch: '<S24>/Switch'
1656 */
1657 rtDW->Merge1 = rtb_Switch;
1658
1659 /* End of Outputs for SubSystem: '<S26>/If Action Subsystem1' */
1660 }
1661
1662 /* End of If: '<S26>/If' */
1663
1664 /* Switch: '<S60>/Switch2' incorporates:
1665 * Inport: '<Root>/spd_Limit'
1666 * Merge: '<S26>/Merge1'
1667 * RelationalOperator: '<S60>/LowerRelop1'
1668 * RelationalOperator: '<S60>/UpperRelop'
1669 * Switch: '<S60>/Switch'
1670 */
1671 if (rtDW->Merge1 > rtU->spd_Limit) {
1672 rtb_Switch = rtU->spd_Limit;
1673 } else if (rtDW->Merge1 < 0) {
1674 /* Switch: '<S60>/Switch' incorporates:
1675 * Constant: '<S50>/Constant'
1676 * Switch: '<S60>/Switch2'
1677 */
1678 rtb_Switch = 0;
1679 } else {
1680 rtb_Switch = rtDW->Merge1;
1681 }
1682
1683 /* End of Switch: '<S60>/Switch2' */
1684
1685 /* If: '<S54>/If' incorporates:
1686 * Logic: '<S72>/Logical Operator'
1687 * Switch: '<S72>/Switch2'
1688 */
1689 if ((rtb_z_ctrlMod != 0) && rtb_Equal_k) {
1690 /* Outputs for IfAction SubSystem: '<S54>/Do_Calc' incorporates:
1691 * ActionPort: '<S71>/Action Port'
1692 */
1693 /* DataTypeConversion: '<S71>/Data Type Conversion' incorporates:
1694 * RelationalOperator: '<S71>/Equal'
1695 * UnitDelay: '<S71>/Unit Delay'
1696 */
1697 rtb_DataTypeConversion_np = (uint8_T)(rtDW->UnitDelay_DSTATE_p2 !=
1698 rtb_z_ctrlMod);
1699
1700 /* If: '<S74>/If' incorporates:
1701 * Constant: '<S87>/Constant1'
1702 * Constant: '<S87>/Constant11'
1703 * Constant: '<S87>/Constant4'
1704 * Gain: '<S71>/Gain'
1705 * Sum: '<S87>/Sum1'
1706 * Switch: '<S14>/Switch2'
1707 * Switch: '<S60>/Switch2'
1708 * UnitDelay: '<S71>/Unit Delay1'
1709 */
1710 if (rtb_z_ctrlMod == 1) {
1711 rtb_Sum2 = 0;
1712
1713 /* Outputs for IfAction SubSystem: '<S74>/speed_mode' incorporates:
1714 * ActionPort: '<S87>/Action Port'
1715 */
1716 /* MinMax: '<S87>/Min' incorporates:
1717 * Constant: '<S87>/Constant6'
1718 * UnitDelay: '<S87>/Unit Delay'
1719 */
1720 if (4800 < rtDW->UnitDelay_DSTATE_l) {
1721 rtb_Sum6_p = 4800;
1722 } else {
1723 rtb_Sum6_p = rtDW->UnitDelay_DSTATE_l;
1724 }
1725
1726 /* End of MinMax: '<S87>/Min' */
1727
1728 /* MinMax: '<S87>/Min1' incorporates:
1729 * Constant: '<S87>/Constant2'
1730 * Gain: '<S87>/Gain'
1731 * UnitDelay: '<S87>/Unit Delay'
1732 */
1733 if ((int16_T)-rtDW->UnitDelay_DSTATE_l > -4800) {
1734 rtb_Divide1_m = (int16_T)-rtDW->UnitDelay_DSTATE_l;
1735 } else {
1736 rtb_Divide1_m = -4800;
1737 }
1738
1739 /* End of MinMax: '<S87>/Min1' */
1740
1741 /* Outputs for Atomic SubSystem: '<S87>/PI_Speed' */
1742 rtb_Sum1 = PI_backCalc_fixdt(rtb_Switch - rtb_Switch3, rtP.cf_nKp,
1743 rtP.cf_nKi, rtP.cf_nKb, rtb_Sum6_p, rtb_Divide1_m, (int16_T)
1744 (rtDW->UnitDelay1_DSTATE_g >> 1), rtb_DataTypeConversion_np,
1745 &rtDW->PI_Speed, &rtPrevZCX->PI_Speed);
1746
1747 /* End of Outputs for SubSystem: '<S87>/PI_Speed' */
1748
1749 /* Merge: '<S74>/Merge' incorporates:
1750 * Constant: '<S87>/Constant1'
1751 * Constant: '<S87>/Constant11'
1752 * Constant: '<S87>/Constant4'
1753 * DataTypeConversion: '<S87>/Data Type Conversion'
1754 * Gain: '<S71>/Gain'
1755 * Sum: '<S87>/Sum1'
1756 * Switch: '<S14>/Switch2'
1757 * Switch: '<S60>/Switch2'
1758 * Switch: '<S91>/Switch2'
1759 * UnitDelay: '<S71>/Unit Delay1'
1760 */
1761 rtDW->Merge_f = (int16_T)(rtb_Sum1 >> 9);
1762
1763 /* End of Outputs for SubSystem: '<S74>/speed_mode' */
1764 } else {
1765 rtb_Sum2 = 1;
1766
1767 /* Outputs for IfAction SubSystem: '<S74>/torque_mode' incorporates:
1768 * ActionPort: '<S88>/Action Port'
1769 */
1770 /* Sum: '<S88>/Sum1' incorporates:
1771 * Switch: '<S14>/Switch2'
1772 * Switch: '<S60>/Switch2'
1773 */
1774 rtb_Sum1 = rtb_Switch - rtb_Switch3;
1775
1776 /* Delay: '<S88>/Delay' incorporates:
1777 * Inport: '<S30>/r_currTgt'
1778 */
1779 if (rtDW->icLoad != 0) {
1780 rtDW->Delay_DSTATE = rtDW->r_currTgt;
1781 }
1782
1783 /* MinMax: '<S88>/Min' incorporates:
1784 * Delay: '<S88>/Delay'
1785 * Inport: '<S30>/r_currTgt'
1786 */
1787 if (rtDW->r_currTgt < rtDW->Delay_DSTATE) {
1788 rtb_Sum6_p = rtDW->r_currTgt;
1789 } else {
1790 rtb_Sum6_p = rtDW->Delay_DSTATE;
1791 }
1792
1793 /* End of MinMax: '<S88>/Min' */
1794
1795 /* Outputs for Atomic SubSystem: '<S88>/PI_TrqSpdLim' */
1796 /* Delay: '<S93>/Resettable Delay' incorporates:
1797 * DataTypeConversion: '<S93>/Data Type Conversion2'
1798 * Inport: '<S30>/r_currTgt'
1799 */
1800 if ((rtb_DataTypeConversion_np > 0) &&
1801 (rtPrevZCX->ResettableDelay_Reset_ZCE_a != 1)) {
1802 rtDW->icLoad_k = 1U;
1803 }
1804
1805 rtPrevZCX->ResettableDelay_Reset_ZCE_a = (ZCSigState)
1806 (rtb_DataTypeConversion_np > 0);
1807 if (rtDW->icLoad_k != 0) {
1808 rtDW->ResettableDelay_DSTATE = rtDW->r_currTgt << 7;
1809 }
1810
1811 /* Product: '<S92>/Divide1' incorporates:
1812 * Constant: '<S88>/Constant1'
1813 * Sum: '<S88>/Sum1'
1814 */
1815 tmp = (int64_T)rtb_Sum1 * rtP.cf_TrqLimKi;
1816 if (tmp > 2147483647LL) {
1817 tmp = 2147483647LL;
1818 } else {
1819 if (tmp < -2147483648LL) {
1820 tmp = -2147483648LL;
1821 }
1822 }
1823
1824 /* Sum: '<S92>/Sum2' incorporates:
1825 * Product: '<S92>/Divide1'
1826 * UnitDelay: '<S92>/Unit Delay'
1827 */
1828 if (((int32_T)tmp < 0) && (rtDW->UnitDelay_DSTATE < MIN_int32_T - (int32_T)
1829 tmp)) {
1830 rtb_Gain_b0 = MIN_int32_T;
1831 } else if (((int32_T)tmp > 0) && (rtDW->UnitDelay_DSTATE > MAX_int32_T
1832 - (int32_T)tmp)) {
1833 rtb_Gain_b0 = MAX_int32_T;
1834 } else {
1835 rtb_Gain_b0 = (int32_T)tmp + rtDW->UnitDelay_DSTATE;
1836 }
1837
1838 /* End of Sum: '<S92>/Sum2' */
1839
1840 /* Sum: '<S93>/Sum1' incorporates:
1841 * Delay: '<S93>/Resettable Delay'
1842 */
1843 tmp = (((int64_T)rtDW->ResettableDelay_DSTATE << 2) + rtb_Gain_b0) >> 2;
1844 if (tmp > 2147483647LL) {
1845 tmp = 2147483647LL;
1846 } else {
1847 if (tmp < -2147483648LL) {
1848 tmp = -2147483648LL;
1849 }
1850 }
1851
1852 rtb_Switch = (int32_T)tmp;
1853
1854 /* End of Sum: '<S93>/Sum1' */
1855
1856 /* Product: '<S92>/Divide4' incorporates:
1857 * Constant: '<S88>/Constant4'
1858 * Sum: '<S88>/Sum1'
1859 */
1860 tmp = (int64_T)rtb_Sum1 * rtP.cf_TrqLimKp;
1861 if (tmp > 2147483647LL) {
1862 tmp = 2147483647LL;
1863 } else {
1864 if (tmp < -2147483648LL) {
1865 tmp = -2147483648LL;
1866 }
1867 }
1868
1869 /* Sum: '<S92>/Sum6' incorporates:
1870 * DataTypeConversion: '<S93>/Data Type Conversion1'
1871 * Product: '<S92>/Divide4'
1872 * Sum: '<S93>/Sum1'
1873 */
1874 tmp = (int64_T)(rtb_Switch << 2) + (int32_T)tmp;
1875 if (tmp > 2147483647LL) {
1876 tmp = 2147483647LL;
1877 } else {
1878 if (tmp < -2147483648LL) {
1879 tmp = -2147483648LL;
1880 }
1881 }
1882
1883 rtb_Sum1 = (int32_T)tmp;
1884
1885 /* End of Sum: '<S92>/Sum6' */
1886
1887 /* RelationalOperator: '<S94>/LowerRelop1' incorporates:
1888 * MinMax: '<S88>/Min'
1889 * Switch: '<S94>/Switch2'
1890 */
1891 rtb_Gain_b0 = rtb_Sum6_p << 9;
1892
1893 /* Switch: '<S94>/Switch2' incorporates:
1894 * RelationalOperator: '<S94>/LowerRelop1'
1895 * Sum: '<S92>/Sum6'
1896 */
1897 if (rtb_Sum1 <= rtb_Gain_b0) {
1898 /* Gain: '<S92>/Gain' incorporates:
1899 * MinMax: '<S88>/Min'
1900 */
1901 rtb_Gain_b0 = -32768 * rtb_Sum6_p;
1902
1903 /* Switch: '<S94>/Switch' incorporates:
1904 * Gain: '<S92>/Gain'
1905 * RelationalOperator: '<S94>/UpperRelop'
1906 * Switch: '<S94>/Switch2'
1907 */
1908 if (((int64_T)rtb_Sum1 << 6) < rtb_Gain_b0) {
1909 rtb_Gain_b0 >>= 6;
1910 } else {
1911 rtb_Gain_b0 = rtb_Sum1;
1912 }
1913
1914 /* End of Switch: '<S94>/Switch' */
1915 }
1916
1917 /* Update for UnitDelay: '<S92>/Unit Delay' incorporates:
1918 * Constant: '<S88>/Constant2'
1919 * Product: '<S92>/Divide2'
1920 * Sum: '<S92>/Sum3'
1921 * Sum: '<S92>/Sum6'
1922 * Switch: '<S94>/Switch2'
1923 */
1924 rtDW->UnitDelay_DSTATE = (int32_T)(((int64_T)(rtb_Gain_b0 - rtb_Sum1) *
1925 rtP.cf_TrqLimKb) >> 10);
1926
1927 /* Update for Delay: '<S93>/Resettable Delay' incorporates:
1928 * Sum: '<S93>/Sum1'
1929 */
1930 rtDW->icLoad_k = 0U;
1931 rtDW->ResettableDelay_DSTATE = rtb_Switch;
1932
1933 /* End of Outputs for SubSystem: '<S88>/PI_TrqSpdLim' */
1934
1935 /* Merge: '<S74>/Merge' incorporates:
1936 * DataTypeConversion: '<S88>/Data Type Conversion'
1937 * ManualSwitch: '<S88>/Manual Switch'
1938 * Switch: '<S94>/Switch2'
1939 */
1940 rtDW->Merge_f = (int16_T)(rtb_Gain_b0 >> 9);
1941
1942 /* End of Outputs for SubSystem: '<S74>/torque_mode' */
1943 }
1944
1945 /* Outputs for IfAction SubSystem: '<S76>/MTPA_Calc' incorporates:
1946 * ActionPort: '<S81>/Action Port'
1947 */
1948 /* If: '<S76>/If' incorporates:
1949 * Constant: '<S81>/Constant3'
1950 * Merge: '<S76>/Merge'
1951 * Switch: '<S81>/Switch'
1952 */
1953 rtDW->Merge_c[0] = 0;
1954 rtDW->Merge_c[1] = rtDW->Merge_f;
1955
1956 /* End of Outputs for SubSystem: '<S76>/MTPA_Calc' */
1957
1958 /* Sum: '<S77>/Add' incorporates:
1959 * Inport: '<Root>/iDC_Limit'
1960 * Inport: '<Root>/vDC'
1961 * Math: '<S86>/Math Function3'
1962 * Merge: '<S76>/Merge'
1963 * Product: '<S50>/Divide'
1964 * Product: '<S77>/Divide'
1965 * Switch: '<S75>/Switch'
1966 */
1967 rtb_Switch = rtU->iDC_Limit * rtU->vDC - rtDW->Merge_c[0] *
1968 rtb_DataTypeConversion_b[0];
1969
1970 /* Product: '<S77>/Divide3' incorporates:
1971 * Constant: '<S77>/Constant5'
1972 * Math: '<S86>/Math Function3'
1973 */
1974 rtb_Gain_b0 = rtb_Switch / 9600;
1975 if (rtb_Gain_b0 > 32767) {
1976 rtb_Gain_b0 = 32767;
1977 } else {
1978 if (rtb_Gain_b0 < -32768) {
1979 rtb_Gain_b0 = -32768;
1980 }
1981 }
1982
1983 /* Product: '<S77>/Divide1' incorporates:
1984 * Math: '<S86>/Math Function3'
1985 */
1986 tmp_2 = rtb_Switch;
1987
1988 /* MinMax: '<S77>/Min2' incorporates:
1989 * Product: '<S77>/Divide3'
1990 */
1991 if (rtb_DataTypeConversion_b[1] > (int16_T)rtb_Gain_b0) {
1992 rtb_Divide1_m = rtb_DataTypeConversion_b[1];
1993 } else {
1994 rtb_Divide1_m = (int16_T)rtb_Gain_b0;
1995 }
1996
1997 /* End of MinMax: '<S77>/Min2' */
1998
1999 /* Product: '<S77>/Divide1' */
2000 rtb_Gain_b0 = tmp_2 / rtb_Divide1_m;
2001 if (rtb_Gain_b0 > 32767) {
2002 rtb_Gain_b0 = 32767;
2003 } else {
2004 if (rtb_Gain_b0 < -32768) {
2005 rtb_Gain_b0 = -32768;
2006 }
2007 }
2008
2009 /* Signum: '<S77>/Sign' incorporates:
2010 * Merge: '<S76>/Merge'
2011 * Switch: '<S75>/Switch'
2012 */
2013 if (rtDW->Merge_c[1] < 0) {
2014 rtb_Divide1_m = -1;
2015 } else {
2016 rtb_Divide1_m = (int16_T)(rtDW->Merge_c[1] > 0);
2017 }
2018
2019 /* End of Signum: '<S77>/Sign' */
2020
2021 /* Product: '<S77>/Divide2' incorporates:
2022 * Product: '<S77>/Divide1'
2023 */
2024 rtb_Sum6_p = (int16_T)((int16_T)rtb_Gain_b0 * rtb_Divide1_m);
2025
2026 /* Switch: '<S85>/Switch2' incorporates:
2027 * Constant: '<S77>/Constant3'
2028 * Product: '<S77>/Divide2'
2029 * RelationalOperator: '<S85>/LowerRelop1'
2030 * RelationalOperator: '<S85>/UpperRelop'
2031 * Switch: '<S85>/Switch'
2032 */
2033 if (rtb_Sum6_p > 4800) {
2034 rtb_Sum6_p = 4800;
2035 } else {
2036 if (rtb_Sum6_p < -4800) {
2037 /* Switch: '<S85>/Switch' incorporates:
2038 * Gain: '<S77>/Gain1'
2039 * Switch: '<S85>/Switch2'
2040 */
2041 rtb_Sum6_p = -4800;
2042 }
2043 }
2044
2045 /* End of Switch: '<S85>/Switch2' */
2046
2047 /* Switch: '<S77>/Switch' incorporates:
2048 * Merge: '<S76>/Merge'
2049 * MinMax: '<S77>/Min1'
2050 * Switch: '<S75>/Switch'
2051 * Switch: '<S85>/Switch2'
2052 */
2053 if (rtb_Divide1_m > 0) {
2054 /* MinMax: '<S77>/Min' incorporates:
2055 * Merge: '<S76>/Merge'
2056 * Switch: '<S75>/Switch'
2057 * Switch: '<S85>/Switch2'
2058 */
2059 if (rtb_Sum6_p < rtDW->Merge_c[1]) {
2060 /* Switch: '<S77>/Switch' */
2061 rtDW->Switch = rtb_Sum6_p;
2062 } else {
2063 /* Switch: '<S77>/Switch' */
2064 rtDW->Switch = rtDW->Merge_c[1];
2065 }
2066
2067 /* End of MinMax: '<S77>/Min' */
2068 } else if (rtb_Sum6_p > rtDW->Merge_c[1]) {
2069 /* MinMax: '<S77>/Min1' incorporates:
2070 * Switch: '<S77>/Switch'
2071 * Switch: '<S85>/Switch2'
2072 */
2073 rtDW->Switch = rtb_Sum6_p;
2074 } else {
2075 /* Switch: '<S77>/Switch' incorporates:
2076 * Merge: '<S76>/Merge'
2077 * Switch: '<S75>/Switch'
2078 */
2079 rtDW->Switch = rtDW->Merge_c[1];
2080 }
2081
2082 /* End of Switch: '<S77>/Switch' */
2083
2084 /* Switch: '<S84>/Switch2' incorporates:
2085 * Merge: '<S76>/Merge'
2086 * RelationalOperator: '<S84>/LowerRelop1'
2087 * RelationalOperator: '<S84>/UpperRelop'
2088 * Switch: '<S75>/Switch'
2089 * Switch: '<S84>/Switch'
2090 */
2091 if (rtDW->Merge_c[0] > 4800) {
2092 /* Switch: '<S84>/Switch2' incorporates:
2093 * Constant: '<S77>/Constant1'
2094 */
2095 rtDW->Switch2 = 4800;
2096 } else if (rtDW->Merge_c[0] < -4800) {
2097 /* Switch: '<S84>/Switch' incorporates:
2098 * Gain: '<S77>/Gain1'
2099 * Switch: '<S84>/Switch2'
2100 */
2101 rtDW->Switch2 = -4800;
2102 } else {
2103 /* Switch: '<S84>/Switch2' */
2104 rtDW->Switch2 = rtDW->Merge_c[0];
2105 }
2106
2107 /* End of Switch: '<S84>/Switch2' */
2108
2109 /* Update for UnitDelay: '<S71>/Unit Delay' */
2110 rtDW->UnitDelay_DSTATE_p2 = rtb_z_ctrlMod;
2111
2112 /* Update for UnitDelay: '<S71>/Unit Delay1' incorporates:
2113 * Merge: '<S74>/Merge'
2114 */
2115 rtDW->UnitDelay1_DSTATE_g = rtDW->Merge_f;
2116
2117 /* If: '<S74>/If' */
2118 switch (rtb_Sum2) {
2119 case 0:
2120 /* Update for IfAction SubSystem: '<S74>/speed_mode' incorporates:
2121 * ActionPort: '<S87>/Action Port'
2122 */
2123 /* Update for UnitDelay: '<S87>/Unit Delay' incorporates:
2124 * Math: '<S86>/Math Function2'
2125 * Math: '<S86>/Math Function3'
2126 * Merge: '<S76>/Merge'
2127 * Product: '<S77>/Divide1'
2128 * Sqrt: '<S86>/Sqrt1'
2129 * Sum: '<S86>/Add'
2130 * Switch: '<S75>/Switch'
2131 */
2132 rtDW->UnitDelay_DSTATE_l = rt_sqrt_Us32En10_Ys16E_7VJYwqF9(rtDW->Merge_c[0]
2133 * rtDW->Merge_c[0] + (int16_T)rtb_Gain_b0 * (int16_T)rtb_Gain_b0);
2134
2135 /* End of Update for SubSystem: '<S74>/speed_mode' */
2136 break;
2137
2138 case 1:
2139 /* Update for IfAction SubSystem: '<S74>/torque_mode' incorporates:
2140 * ActionPort: '<S88>/Action Port'
2141 */
2142 /* Update for Delay: '<S88>/Delay' incorporates:
2143 * Math: '<S86>/Math Function2'
2144 * Math: '<S86>/Math Function3'
2145 * Merge: '<S76>/Merge'
2146 * Product: '<S77>/Divide1'
2147 * Sqrt: '<S86>/Sqrt1'
2148 * Sum: '<S86>/Add'
2149 * Switch: '<S75>/Switch'
2150 */
2151 rtDW->icLoad = 0U;
2152 rtDW->Delay_DSTATE = rt_sqrt_Us32En10_Ys16E_7VJYwqF9(rtDW->Merge_c[0] *
2153 rtDW->Merge_c[0] + (int16_T)rtb_Gain_b0 * (int16_T)rtb_Gain_b0);
2154
2155 /* End of Update for SubSystem: '<S74>/torque_mode' */
2156 break;
2157 }
2158
2159 /* End of Outputs for SubSystem: '<S54>/Do_Calc' */
2160 }
2161
2162 /* End of If: '<S54>/If' */
2163
2164 /* RelationalOperator: '<S106>/Relational Operator' incorporates:
2165 * Switch: '<S84>/Switch2'
2166 * UnitDelay: '<S106>/UnitDelay'
2167 */
2168 rtb_Equal_k = (rtDW->Switch2 != rtDW->UnitDelay_DSTATE_h);
2169
2170 /* Sum: '<S97>/Add' incorporates:
2171 * Product: '<S61>/Divide1'
2172 * Switch: '<S84>/Switch2'
2173 * UnitDelay: '<S97>/Unit Delay1'
2174 */
2175 rtb_r_cos_M1 = (int16_T)(rtDW->Switch2 - rtDW->UnitDelay1_DSTATE_i);
2176
2177 /* Abs: '<S97>/Abs' incorporates:
2178 * Product: '<S61>/Divide1'
2179 */
2180 if (rtb_r_cos_M1 < 0) {
2181 rtb_r_cos_M1 = (int16_T)-rtb_r_cos_M1;
2182 }
2183
2184 /* End of Abs: '<S97>/Abs' */
2185
2186 /* Outputs for Enabled SubSystem: '<S97>/Enabled Subsystem' incorporates:
2187 * EnablePort: '<S107>/Enable'
2188 */
2189 /* If: '<S108>/If' incorporates:
2190 * Gain: '<S97>/Gain'
2191 * Product: '<S61>/Divide1'
2192 * UnitDelay: '<S97>/Unit Delay1'
2193 */
2194 if (rtb_Equal_k) {
2195 /* Outputs for IfAction SubSystem: '<S108>/RateInit' incorporates:
2196 * ActionPort: '<S109>/Action Port'
2197 */
2198 RateInit(rtDW->UnitDelay1_DSTATE_i, rtDW->Switch2, (int16_T)((13107 *
2199 rtb_r_cos_M1) >> 13), &rtDW->Divide_n, &rtDW->Max_g,
2200 &rtDW->Max1_j);
2201
2202 /* End of Outputs for SubSystem: '<S108>/RateInit' */
2203
2204 /* Switch: '<S112>/Switch1' incorporates:
2205 * Gain: '<S97>/Gain'
2206 * Product: '<S61>/Divide1'
2207 * UnitDelay: '<S97>/Unit Delay1'
2208 */
2209 rtb_Divide1_m = rtDW->UnitDelay1_DSTATE_i;
2210 } else {
2211 /* Switch: '<S112>/Switch1' incorporates:
2212 * UnitDelay: '<S112>/UnitDelay'
2213 */
2214 rtb_Divide1_m = rtDW->UnitDelay_DSTATE_b;
2215 }
2216
2217 /* End of If: '<S108>/If' */
2218 /* End of Outputs for SubSystem: '<S97>/Enabled Subsystem' */
2219
2220 /* Switch: '<S108>/Switch' incorporates:
2221 * Constant: '<S108>/Constant'
2222 * Product: '<S109>/Divide'
2223 * RelationalOperator: '<S108>/Equal'
2224 * Switch: '<S84>/Switch2'
2225 * UnitDelay: '<S108>/Unit Delay'
2226 */
2227 if (rtDW->Switch2 != rtDW->UnitDelay_DSTATE_g) {
2228 rtb_Sum6_p = rtDW->Divide_n;
2229 } else {
2230 rtb_Sum6_p = 0;
2231 }
2232
2233 /* End of Switch: '<S108>/Switch' */
2234
2235 /* Sum: '<S111>/Add2' */
2236 rtb_Gain_b0 = ((rtb_Divide1_m << 5) + rtb_Sum6_p) >> 5;
2237 if (rtb_Gain_b0 > 32767) {
2238 rtb_Gain_b0 = 32767;
2239 } else {
2240 if (rtb_Gain_b0 < -32768) {
2241 rtb_Gain_b0 = -32768;
2242 }
2243 }
2244
2245 /* Switch: '<S110>/Switch2' incorporates:
2246 * MinMax: '<S109>/Max'
2247 * MinMax: '<S109>/Max1'
2248 * RelationalOperator: '<S110>/LowerRelop1'
2249 * RelationalOperator: '<S110>/UpperRelop'
2250 * Sum: '<S111>/Add2'
2251 * Switch: '<S110>/Switch'
2252 */
2253 if ((int16_T)rtb_Gain_b0 > rtDW->Max_g) {
2254 rtb_Divide1_m = rtDW->Max_g;
2255 } else if ((int16_T)rtb_Gain_b0 < rtDW->Max1_j) {
2256 /* Switch: '<S110>/Switch' incorporates:
2257 * MinMax: '<S109>/Max1'
2258 * Switch: '<S110>/Switch2'
2259 */
2260 rtb_Divide1_m = rtDW->Max1_j;
2261 } else {
2262 rtb_Divide1_m = (int16_T)rtb_Gain_b0;
2263 }
2264
2265 /* End of Switch: '<S110>/Switch2' */
2266
2267 /* RelationalOperator: '<S113>/Relational Operator' incorporates:
2268 * Switch: '<S77>/Switch'
2269 * UnitDelay: '<S113>/UnitDelay'
2270 */
2271 rtb_LogicalOperator12 = (rtDW->Switch != rtDW->UnitDelay_DSTATE_o);
2272
2273 /* Sum: '<S98>/Add' incorporates:
2274 * Product: '<S61>/Divide1'
2275 * Switch: '<S77>/Switch'
2276 * UnitDelay: '<S98>/Unit Delay1'
2277 */
2278 rtb_r_cos_M1 = (int16_T)(rtDW->Switch - rtDW->UnitDelay1_DSTATE_b);
2279
2280 /* Abs: '<S98>/Abs' incorporates:
2281 * Product: '<S61>/Divide1'
2282 */
2283 if (rtb_r_cos_M1 < 0) {
2284 rtb_r_cos_M1 = (int16_T)-rtb_r_cos_M1;
2285 }
2286
2287 /* End of Abs: '<S98>/Abs' */
2288
2289 /* Outputs for Enabled SubSystem: '<S98>/Enabled Subsystem' incorporates:
2290 * EnablePort: '<S114>/Enable'
2291 */
2292 /* If: '<S115>/If' incorporates:
2293 * Gain: '<S98>/Gain'
2294 * Product: '<S61>/Divide1'
2295 * UnitDelay: '<S98>/Unit Delay1'
2296 */
2297 if (rtb_LogicalOperator12) {
2298 /* Outputs for IfAction SubSystem: '<S115>/RateInit' incorporates:
2299 * ActionPort: '<S116>/Action Port'
2300 */
2301 RateInit(rtDW->UnitDelay1_DSTATE_b, rtDW->Switch, (int16_T)((13107 *
2302 rtb_r_cos_M1) >> 13), &rtDW->Divide_l, &rtDW->Max, &rtDW->Max1);
2303
2304 /* End of Outputs for SubSystem: '<S115>/RateInit' */
2305
2306 /* Switch: '<S119>/Switch1' incorporates:
2307 * Gain: '<S98>/Gain'
2308 * Product: '<S61>/Divide1'
2309 * UnitDelay: '<S98>/Unit Delay1'
2310 */
2311 rtb_r_cos_M1 = rtDW->UnitDelay1_DSTATE_b;
2312 } else {
2313 /* Switch: '<S119>/Switch1' incorporates:
2314 * UnitDelay: '<S119>/UnitDelay'
2315 */
2316 rtb_r_cos_M1 = rtDW->UnitDelay_DSTATE_d;
2317 }
2318
2319 /* End of If: '<S115>/If' */
2320 /* End of Outputs for SubSystem: '<S98>/Enabled Subsystem' */
2321
2322 /* Switch: '<S115>/Switch' incorporates:
2323 * Constant: '<S115>/Constant'
2324 * Product: '<S116>/Divide'
2325 * RelationalOperator: '<S115>/Equal'
2326 * Switch: '<S77>/Switch'
2327 * UnitDelay: '<S115>/Unit Delay'
2328 */
2329 if (rtDW->Switch != rtDW->UnitDelay_DSTATE_a) {
2330 rtb_Sum6_p = rtDW->Divide_l;
2331 } else {
2332 rtb_Sum6_p = 0;
2333 }
2334
2335 /* End of Switch: '<S115>/Switch' */
2336
2337 /* Sum: '<S118>/Add2' */
2338 tmp_2 = ((rtb_r_cos_M1 << 5) + rtb_Sum6_p) >> 5;
2339 if (tmp_2 > 32767) {
2340 tmp_2 = 32767;
2341 } else {
2342 if (tmp_2 < -32768) {
2343 tmp_2 = -32768;
2344 }
2345 }
2346
2347 /* Switch: '<S117>/Switch2' incorporates:
2348 * MinMax: '<S116>/Max'
2349 * MinMax: '<S116>/Max1'
2350 * RelationalOperator: '<S117>/LowerRelop1'
2351 * RelationalOperator: '<S117>/UpperRelop'
2352 * Sum: '<S118>/Add2'
2353 * Switch: '<S117>/Switch'
2354 */
2355 if ((int16_T)tmp_2 > rtDW->Max) {
2356 rtb_Sum6_p = rtDW->Max;
2357 } else if ((int16_T)tmp_2 < rtDW->Max1) {
2358 /* Switch: '<S117>/Switch' incorporates:
2359 * MinMax: '<S116>/Max1'
2360 * Switch: '<S117>/Switch2'
2361 */
2362 rtb_Sum6_p = rtDW->Max1;
2363 } else {
2364 rtb_Sum6_p = (int16_T)tmp_2;
2365 }
2366
2367 /* End of Switch: '<S117>/Switch2' */
2368
2369 /* DataTypeConversion: '<S55>/Data Type Conversion' incorporates:
2370 * Logic: '<S55>/Logical Operator'
2371 * RelationalOperator: '<S55>/Equal'
2372 * UnitDelay: '<S55>/Unit Delay'
2373 */
2374 rtb_DataTypeConversion_np = (uint8_T)((rtb_z_ctrlMod != 0) &&
2375 (rtDW->UnitDelay_DSTATE_bm != rtb_z_ctrlMod));
2376
2377 /* If: '<S55>/If1' incorporates:
2378 * Constant: '<S95>/Constant1'
2379 * Constant: '<S95>/Constant3'
2380 * Constant: '<S95>/Constant4'
2381 * Constant: '<S95>/Constant6'
2382 * Constant: '<S95>/Constant7'
2383 * Constant: '<S95>/Constant8'
2384 * Gain: '<S95>/Gain1'
2385 * Gain: '<S95>/Gain2'
2386 * Inport: '<S96>/In1'
2387 * Merge: '<S26>/Merge'
2388 * Merge: '<S55>/Merge'
2389 * Outport: '<Root>/f_Idq'
2390 * Product: '<S95>/Divide'
2391 * Sum: '<S95>/Sum'
2392 * Sum: '<S95>/Sum1'
2393 * Switch: '<S110>/Switch2'
2394 * Switch: '<S117>/Switch2'
2395 * UnitDelay: '<S6>/UnitDelay1'
2396 */
2397 if (rtb_z_ctrlMod != 0) {
2398 /* Outputs for IfAction SubSystem: '<S55>/CurrentLoop' incorporates:
2399 * ActionPort: '<S95>/Action Port'
2400 */
2401 /* Product: '<S95>/Divide' incorporates:
2402 * Inport: '<Root>/vDC'
2403 */
2404 rtb_r_cos_M1 = (int16_T)((rtU->vDC * 15) >> 4);
2405
2406 /* Outputs for Atomic SubSystem: '<S95>/PI_backCalc_fixdt' */
2407 rtb_Switch = PI_backCalc_fixdt_o((int16_T)(rtb_Divide1_m - rtY->f_Idq[0]),
2408 rtP.cf_idKp, rtP.cf_idKi, rtP.cf_idKb, rtb_r_cos_M1, (int16_T)
2409 -rtb_r_cos_M1, rtDW->UnitDelay1_DSTATE_f[0], rtb_DataTypeConversion_np,
2410 &rtDW->PI_backCalc_fixdt_o3, &rtPrevZCX->PI_backCalc_fixdt_o3);
2411
2412 /* End of Outputs for SubSystem: '<S95>/PI_backCalc_fixdt' */
2413
2414 /* Outputs for Atomic SubSystem: '<S95>/PI_backCalc_fixdt1' */
2415 rtb_Sum1 = PI_backCalc_fixdt_o((int16_T)(rtb_Sum6_p - rtY->f_Idq[1]),
2416 rtP.cf_iqKp, rtP.cf_iqKi, rtP.cf_iqKb, rtb_r_cos_M1, (int16_T)
2417 -rtb_r_cos_M1, rtDW->UnitDelay1_DSTATE_f[1], rtb_DataTypeConversion_np,
2418 &rtDW->PI_backCalc_fixdt1, &rtPrevZCX->PI_backCalc_fixdt1);
2419
2420 /* End of Outputs for SubSystem: '<S95>/PI_backCalc_fixdt1' */
2421
2422 /* Sum: '<S95>/Sum2' incorporates:
2423 * Constant: '<S95>/Constant1'
2424 * Constant: '<S95>/Constant3'
2425 * Constant: '<S95>/Constant4'
2426 * Constant: '<S95>/Constant6'
2427 * Constant: '<S95>/Constant7'
2428 * Constant: '<S95>/Constant8'
2429 * DataTypeConversion: '<S95>/Data Type Conversion'
2430 * DataTypeConversion: '<S95>/Data Type Conversion1'
2431 * Gain: '<S95>/Gain1'
2432 * Gain: '<S95>/Gain2'
2433 * Merge: '<S55>/Merge'
2434 * Outport: '<Root>/f_Idq'
2435 * Product: '<S95>/Divide'
2436 * Sum: '<S95>/Sum'
2437 * Sum: '<S95>/Sum1'
2438 * Switch: '<S103>/Switch2'
2439 * Switch: '<S105>/Switch2'
2440 * Switch: '<S110>/Switch2'
2441 * Switch: '<S117>/Switch2'
2442 * UnitDelay: '<S6>/UnitDelay1'
2443 */
2444 rtb_DataTypeConversion_b[0] = (int16_T)(rtb_Switch >> 9);
2445 rtb_DataTypeConversion_b[1] = (int16_T)(rtb_Sum1 >> 9);
2446
2447 /* End of Outputs for SubSystem: '<S55>/CurrentLoop' */
2448 } else {
2449 /* Outputs for IfAction SubSystem: '<S55>/OpenLoop' incorporates:
2450 * ActionPort: '<S96>/Action Port'
2451 */
2452 rtb_DataTypeConversion_b[0] = rtDW->Merge[0];
2453 rtb_DataTypeConversion_b[1] = rtDW->Merge[1];
2454
2455 /* End of Outputs for SubSystem: '<S55>/OpenLoop' */
2456 }
2457
2458 /* End of If: '<S55>/If1' */
2459
2460 /* Gain: '<S52>/Gain' incorporates:
2461 * Inport: '<Root>/vDC'
2462 * Product: '<S61>/Divide1'
2463 */
2464 rtb_r_cos_M1 = (int16_T)((15565 * rtU->vDC) >> 13);
2465
2466 /* Math: '<S52>/Math Function1' incorporates:
2467 * Product: '<S61>/Divide1'
2468 */
2469 rtb_Switch = (rtb_r_cos_M1 * rtb_r_cos_M1) >> 6;
2470
2471 /* Sum: '<S52>/Sum of Elements' incorporates:
2472 * Math: '<S52>/Math Function'
2473 * Merge: '<S55>/Merge'
2474 */
2475 tmp = (int64_T)((rtb_DataTypeConversion_b[0] * rtb_DataTypeConversion_b[0]) >>
2476 4) + ((rtb_DataTypeConversion_b[1] * rtb_DataTypeConversion_b
2477 [1]) >> 4);
2478 if (tmp > 2147483647LL) {
2479 tmp = 2147483647LL;
2480 } else {
2481 if (tmp < -2147483648LL) {
2482 tmp = -2147483648LL;
2483 }
2484 }
2485
2486 /* Product: '<S52>/Divide' incorporates:
2487 * Math: '<S52>/Math Function1'
2488 * Sum: '<S52>/Sum of Elements'
2489 */
2490 tmp = ((int64_T)(int32_T)tmp << 14) / rtb_Switch;
2491 if (tmp < 0LL) {
2492 tmp = 0LL;
2493 } else {
2494 if (tmp > 65535LL) {
2495 tmp = 65535LL;
2496 }
2497 }
2498
2499 /* Sqrt: '<S52>/Sqrt' incorporates:
2500 * Product: '<S52>/Divide'
2501 */
2502 rtb_BitwiseOperator2 = rt_sqrt_Uu16En14_Yu16E_WMwW1mku((uint16_T)tmp);
2503
2504 /* Switch: '<S52>/Switch' incorporates:
2505 * Merge: '<S55>/Merge'
2506 * Sqrt: '<S52>/Sqrt'
2507 */
2508 if (rtb_BitwiseOperator2 > 16384) {
2509 /* Switch: '<S52>/Switch' incorporates:
2510 * Merge: '<S55>/Merge'
2511 * MultiPortSwitch: '<S52>/Multiport Switch'
2512 * Product: '<S52>/Divide1'
2513 */
2514 rtb_Switch_f_idx_0 = (int16_T)((rtb_DataTypeConversion_b[0] << 14) /
2515 rtb_BitwiseOperator2);
2516 rtb_Switch_f_idx_1 = (int16_T)((rtb_DataTypeConversion_b[1] << 14) /
2517 rtb_BitwiseOperator2);
2518 } else {
2519 rtb_Switch_f_idx_0 = rtb_DataTypeConversion_b[0];
2520 rtb_Switch_f_idx_1 = rtb_DataTypeConversion_b[1];
2521 }
2522
2523 /* End of Switch: '<S52>/Switch' */
2524
2525 /* Sum: '<S61>/Sum1' incorporates:
2526 * Interpolation_n-D: '<S59>/r_cos_M1'
2527 * Interpolation_n-D: '<S59>/r_sin_M1'
2528 * Product: '<S61>/Divide2'
2529 * Product: '<S61>/Divide3'
2530 */
2531 tmp_0 = (int16_T)((rtb_Switch_f_idx_0 * rtConstP.pooled7[rtb_LogicalOperator3])
2532 >> 14) + (int16_T)((rtb_Switch_f_idx_1 *
2533 rtConstP.pooled8[rtb_LogicalOperator3]) >> 14);
2534 if (tmp_0 > 32767) {
2535 tmp_0 = 32767;
2536 } else {
2537 if (tmp_0 < -32768) {
2538 tmp_0 = -32768;
2539 }
2540 }
2541
2542 /* Sum: '<S61>/Sum6' incorporates:
2543 * Interpolation_n-D: '<S59>/r_cos_M1'
2544 * Interpolation_n-D: '<S59>/r_sin_M1'
2545 * Product: '<S61>/Divide1'
2546 * Product: '<S61>/Divide4'
2547 */
2548 tmp_1 = (int16_T)((rtb_Switch_f_idx_0 * rtConstP.pooled8[rtb_LogicalOperator3])
2549 >> 14) - (int16_T)((rtb_Switch_f_idx_1 *
2550 rtConstP.pooled7[rtb_LogicalOperator3]) >> 14);
2551 if (tmp_1 > 32767) {
2552 tmp_1 = 32767;
2553 } else {
2554 if (tmp_1 < -32768) {
2555 tmp_1 = -32768;
2556 }
2557 }
2558
2559 /* Product: '<S62>/Divide7' incorporates:
2560 * Constant: '<S62>/Constant3'
2561 * Sum: '<S61>/Sum1'
2562 */
2563 rtb_r_cos_M1 = (int16_T)((2365 * (int16_T)tmp_0) >> 11);
2564
2565 /* MATLAB Function: '<S62>/sector_select' incorporates:
2566 * Product: '<S62>/Divide7'
2567 * Sum: '<S61>/Sum1'
2568 * Sum: '<S61>/Sum6'
2569 */
2570 if ((int16_T)tmp_0 >= 0) {
2571 if ((int16_T)tmp_1 >= 0) {
2572 if (rtb_r_cos_M1 > ((int16_T)tmp_1 << 1)) {
2573 /* DataTypeConversion: '<S62>/Data Type Conversion' */
2574 rtb_DataTypeConversion_np = 2U;
2575 } else {
2576 /* DataTypeConversion: '<S62>/Data Type Conversion' */
2577 rtb_DataTypeConversion_np = 1U;
2578 }
2579 } else {
2580 rtb_Gain_p2 = -rtb_r_cos_M1;
2581 if (-rtb_r_cos_M1 > 32767) {
2582 rtb_Gain_p2 = 32767;
2583 }
2584
2585 if (rtb_Gain_p2 > ((int16_T)tmp_1 << 1)) {
2586 /* DataTypeConversion: '<S62>/Data Type Conversion' */
2587 rtb_DataTypeConversion_np = 3U;
2588 } else {
2589 /* DataTypeConversion: '<S62>/Data Type Conversion' */
2590 rtb_DataTypeConversion_np = 2U;
2591 }
2592 }
2593 } else if ((int16_T)tmp_1 >= 0) {
2594 rtb_Gain_p2 = -rtb_r_cos_M1;
2595 if (-rtb_r_cos_M1 > 32767) {
2596 rtb_Gain_p2 = 32767;
2597 }
2598
2599 if (rtb_Gain_p2 > ((int16_T)tmp_1 << 1)) {
2600 /* DataTypeConversion: '<S62>/Data Type Conversion' */
2601 rtb_DataTypeConversion_np = 5U;
2602 } else {
2603 /* DataTypeConversion: '<S62>/Data Type Conversion' */
2604 rtb_DataTypeConversion_np = 6U;
2605 }
2606 } else if (rtb_r_cos_M1 > ((int16_T)tmp_1 << 1)) {
2607 /* DataTypeConversion: '<S62>/Data Type Conversion' */
2608 rtb_DataTypeConversion_np = 4U;
2609 } else {
2610 /* DataTypeConversion: '<S62>/Data Type Conversion' */
2611 rtb_DataTypeConversion_np = 5U;
2612 }
2613
2614 /* End of MATLAB Function: '<S62>/sector_select' */
2615
2616 /* Gain: '<S62>/Gain' incorporates:
2617 * Inport: '<Root>/vDC'
2618 */
2619 rtb_Gain_p2 = 18919 * rtU->vDC;
2620
2621 /* Product: '<S62>/Divide' incorporates:
2622 * Gain: '<S62>/Gain'
2623 * Sum: '<S61>/Sum6'
2624 */
2625 rtb_Sum6_k = (int16_T)(((int64_T)(int16_T)tmp_1 << 26) / rtb_Gain_p2);
2626
2627 /* Product: '<S62>/Divide1' incorporates:
2628 * Gain: '<S62>/Gain'
2629 * Sum: '<S61>/Sum1'
2630 */
2631 rtb_Sum1_a = (int16_T)(((int64_T)(int16_T)tmp_0 << 26) / rtb_Gain_p2);
2632
2633 /* MultiPortSwitch: '<S63>/Multiport Switch' incorporates:
2634 * DataTypeConversion: '<S62>/Data Type Conversion1'
2635 */
2636 switch (rtb_DataTypeConversion_np) {
2637 case 1:
2638 /* Product: '<S65>/Divide3' incorporates:
2639 * Product: '<S62>/Divide1'
2640 * Product: '<S65>/Divide2'
2641 */
2642 rtb_Divide3_k = (int16_T)(((int16_T)((rtb_Sum1_a * 9459) >> 13) * 375) >> 9);
2643
2644 /* Product: '<S65>/Divide1' incorporates:
2645 * Constant: '<S65>/Constant'
2646 * Product: '<S62>/Divide'
2647 * Product: '<S62>/Divide1'
2648 * Product: '<S65>/Divide'
2649 * Sum: '<S65>/Add'
2650 */
2651 rtb_Sum1_a = (int16_T)(((int16_T)(rtb_Sum6_k - ((rtb_Sum1_a * 9459) >> 14)) *
2652 375) >> 9);
2653
2654 /* Product: '<S65>/Divide4' incorporates:
2655 * Sum: '<S65>/Add1'
2656 * Sum: '<S65>/Add2'
2657 */
2658 rtb_r_cos_M1 = (int16_T)((int16_T)(3000 - (int16_T)(rtb_Sum1_a +
2659 rtb_Divide3_k)) >> 1);
2660
2661 /* Sum: '<S65>/Add3' */
2662 rtb_Sum6_k = (int16_T)(rtb_r_cos_M1 + rtb_Divide3_k);
2663
2664 /* Outport: '<Root>/pwm_Duty' incorporates:
2665 * Sum: '<S65>/Add4'
2666 */
2667 rtY->pwm_Duty[0] = (int16_T)(rtb_Sum6_k + rtb_Sum1_a);
2668 rtY->pwm_Duty[1] = rtb_Sum6_k;
2669 rtY->pwm_Duty[2] = rtb_r_cos_M1;
2670 break;
2671
2672 case 2:
2673 /* Product: '<S66>/Divide1' incorporates:
2674 * Constant: '<S66>/Constant'
2675 * Product: '<S62>/Divide'
2676 * Product: '<S62>/Divide1'
2677 * Product: '<S66>/Divide'
2678 * Sum: '<S66>/Add'
2679 */
2680 rtb_Divide3_k = (int16_T)(((int16_T)(((rtb_Sum1_a * 9459) >> 14) +
2681 rtb_Sum6_k) * 375) >> 9);
2682
2683 /* Product: '<S66>/Divide3' incorporates:
2684 * Constant: '<S66>/Constant'
2685 * Product: '<S62>/Divide'
2686 * Product: '<S62>/Divide1'
2687 * Product: '<S66>/Divide2'
2688 * Sum: '<S66>/Add5'
2689 */
2690 rtb_Sum1_a = (int16_T)(((int16_T)(((rtb_Sum1_a * 9459) >> 14) - rtb_Sum6_k) *
2691 375) >> 9);
2692
2693 /* Product: '<S66>/Divide4' incorporates:
2694 * Sum: '<S66>/Add1'
2695 * Sum: '<S66>/Add2'
2696 */
2697 rtb_r_cos_M1 = (int16_T)((int16_T)(3000 - (int16_T)(rtb_Sum1_a +
2698 rtb_Divide3_k)) >> 1);
2699
2700 /* Sum: '<S66>/Add3' */
2701 rtb_Sum6_k = (int16_T)(rtb_r_cos_M1 + rtb_Divide3_k);
2702
2703 /* Outport: '<Root>/pwm_Duty' incorporates:
2704 * Sum: '<S66>/Add4'
2705 */
2706 rtY->pwm_Duty[0] = rtb_Sum6_k;
2707 rtY->pwm_Duty[1] = (int16_T)(rtb_Sum6_k + rtb_Sum1_a);
2708 rtY->pwm_Duty[2] = rtb_r_cos_M1;
2709 break;
2710
2711 case 3:
2712 /* Product: '<S67>/Divide1' incorporates:
2713 * Constant: '<S67>/Constant'
2714 * Product: '<S62>/Divide'
2715 * Product: '<S62>/Divide1'
2716 * Product: '<S67>/Divide'
2717 * Sum: '<S67>/Add'
2718 */
2719 rtb_Sum6_k = (int16_T)(((int16_T)(-rtb_Sum6_k - ((rtb_Sum1_a * 9459) >> 14))
2720 * 375) >> 9);
2721
2722 /* Product: '<S67>/Divide3' incorporates:
2723 * Product: '<S62>/Divide1'
2724 * Product: '<S67>/Divide2'
2725 */
2726 rtb_Sum1_a = (int16_T)(((int16_T)((rtb_Sum1_a * 9459) >> 13) * 375) >> 9);
2727
2728 /* Product: '<S67>/Divide4' incorporates:
2729 * Sum: '<S67>/Add1'
2730 * Sum: '<S67>/Add2'
2731 */
2732 rtb_r_cos_M1 = (int16_T)((int16_T)(3000 - (int16_T)(rtb_Sum1_a + rtb_Sum6_k))
2733 >> 1);
2734
2735 /* Sum: '<S67>/Add3' */
2736 rtb_Sum6_k += rtb_r_cos_M1;
2737
2738 /* Outport: '<Root>/pwm_Duty' incorporates:
2739 * Sum: '<S67>/Add4'
2740 */
2741 rtY->pwm_Duty[0] = rtb_r_cos_M1;
2742 rtY->pwm_Duty[1] = (int16_T)(rtb_Sum6_k + rtb_Sum1_a);
2743 rtY->pwm_Duty[2] = rtb_Sum6_k;
2744 break;
2745
2746 case 4:
2747 /* Product: '<S68>/Divide1' incorporates:
2748 * Constant: '<S68>/Constant'
2749 * Product: '<S62>/Divide'
2750 * Product: '<S62>/Divide1'
2751 * Product: '<S68>/Divide'
2752 * Sum: '<S68>/Add'
2753 */
2754 rtb_Sum6_k = (int16_T)(((int16_T)(((rtb_Sum1_a * 9459) >> 14) - rtb_Sum6_k) *
2755 375) >> 9);
2756
2757 /* Product: '<S68>/Divide3' incorporates:
2758 * Product: '<S62>/Divide1'
2759 * Product: '<S68>/Divide2'
2760 * Sum: '<S68>/Add5'
2761 */
2762 rtb_Sum1_a = (int16_T)(((int16_T)(-((int16_T)((rtb_Sum1_a * 9459) >> 13) <<
2763 2) >> 2) * 375) >> 9);
2764
2765 /* Product: '<S68>/Divide4' incorporates:
2766 * Sum: '<S68>/Add1'
2767 * Sum: '<S68>/Add2'
2768 */
2769 rtb_r_cos_M1 = (int16_T)((int16_T)(3000 - (int16_T)(rtb_Sum1_a + rtb_Sum6_k))
2770 >> 1);
2771
2772 /* Sum: '<S68>/Add3' */
2773 rtb_Sum6_k += rtb_r_cos_M1;
2774
2775 /* Outport: '<Root>/pwm_Duty' incorporates:
2776 * Sum: '<S68>/Add4'
2777 */
2778 rtY->pwm_Duty[0] = rtb_r_cos_M1;
2779 rtY->pwm_Duty[1] = rtb_Sum6_k;
2780 rtY->pwm_Duty[2] = (int16_T)(rtb_Sum6_k + rtb_Sum1_a);
2781 break;
2782
2783 case 5:
2784 /* Product: '<S69>/Divide3' incorporates:
2785 * Constant: '<S69>/Constant'
2786 * Product: '<S62>/Divide'
2787 * Product: '<S62>/Divide1'
2788 * Product: '<S69>/Divide2'
2789 * Sum: '<S69>/Add5'
2790 */
2791 rtb_Divide3_k = (int16_T)(((int16_T)(rtb_Sum6_k - ((rtb_Sum1_a * 9459) >> 14))
2792 * 375) >> 9);
2793
2794 /* Product: '<S69>/Divide1' incorporates:
2795 * Constant: '<S69>/Constant'
2796 * Product: '<S62>/Divide'
2797 * Product: '<S62>/Divide1'
2798 * Product: '<S69>/Divide'
2799 * Sum: '<S69>/Add'
2800 */
2801 rtb_Sum1_a = (int16_T)(((int16_T)(-rtb_Sum6_k - ((rtb_Sum1_a * 9459) >> 14))
2802 * 375) >> 9);
2803
2804 /* Product: '<S69>/Divide4' incorporates:
2805 * Sum: '<S69>/Add1'
2806 * Sum: '<S69>/Add2'
2807 */
2808 rtb_r_cos_M1 = (int16_T)((int16_T)(3000 - (int16_T)(rtb_Sum1_a +
2809 rtb_Divide3_k)) >> 1);
2810
2811 /* Sum: '<S69>/Add3' */
2812 rtb_Sum6_k = (int16_T)(rtb_r_cos_M1 + rtb_Divide3_k);
2813
2814 /* Outport: '<Root>/pwm_Duty' incorporates:
2815 * Sum: '<S69>/Add4'
2816 */
2817 rtY->pwm_Duty[0] = rtb_Sum6_k;
2818 rtY->pwm_Duty[1] = rtb_r_cos_M1;
2819 rtY->pwm_Duty[2] = (int16_T)(rtb_Sum6_k + rtb_Sum1_a);
2820 break;
2821
2822 default:
2823 /* Product: '<S70>/Divide3' incorporates:
2824 * Product: '<S62>/Divide1'
2825 * Product: '<S70>/Divide2'
2826 * Sum: '<S70>/Add5'
2827 */
2828 rtb_Divide3_k = (int16_T)(((int16_T)(-((int16_T)((rtb_Sum1_a * 9459) >> 13) <<
2829 2) >> 2) * 375) >> 9);
2830
2831 /* Product: '<S70>/Divide1' incorporates:
2832 * Constant: '<S70>/Constant'
2833 * Product: '<S62>/Divide'
2834 * Product: '<S62>/Divide1'
2835 * Product: '<S70>/Divide'
2836 * Sum: '<S70>/Add'
2837 */
2838 rtb_Sum1_a = (int16_T)(((int16_T)(((rtb_Sum1_a * 9459) >> 14) + rtb_Sum6_k) *
2839 375) >> 9);
2840
2841 /* Product: '<S70>/Divide4' incorporates:
2842 * Sum: '<S70>/Add1'
2843 * Sum: '<S70>/Add2'
2844 */
2845 rtb_r_cos_M1 = (int16_T)((int16_T)(3000 - (int16_T)(rtb_Sum1_a +
2846 rtb_Divide3_k)) >> 1);
2847
2848 /* Sum: '<S70>/Add3' */
2849 rtb_Sum6_k = (int16_T)(rtb_r_cos_M1 + rtb_Divide3_k);
2850
2851 /* Outport: '<Root>/pwm_Duty' incorporates:
2852 * Sum: '<S70>/Add4'
2853 */
2854 rtY->pwm_Duty[0] = (int16_T)(rtb_Sum6_k + rtb_Sum1_a);
2855 rtY->pwm_Duty[1] = rtb_r_cos_M1;
2856 rtY->pwm_Duty[2] = rtb_Sum6_k;
2857 break;
2858 }
2859
2860 /* End of MultiPortSwitch: '<S63>/Multiport Switch' */
2861
2862 /* Switch: '<S119>/Switch2' */
2863 if (rtb_LogicalOperator12) {
2864 /* Update for UnitDelay: '<S119>/UnitDelay' incorporates:
2865 * UnitDelay: '<S98>/Unit Delay1'
2866 */
2867 rtDW->UnitDelay_DSTATE_d = rtDW->UnitDelay1_DSTATE_b;
2868 } else {
2869 /* Update for UnitDelay: '<S119>/UnitDelay' incorporates:
2870 * Sum: '<S118>/Add2'
2871 */
2872 rtDW->UnitDelay_DSTATE_d = (int16_T)tmp_2;
2873 }
2874
2875 /* End of Switch: '<S119>/Switch2' */
2876
2877 /* Switch: '<S112>/Switch2' */
2878 if (rtb_Equal_k) {
2879 /* Update for UnitDelay: '<S112>/UnitDelay' incorporates:
2880 * UnitDelay: '<S97>/Unit Delay1'
2881 */
2882 rtDW->UnitDelay_DSTATE_b = rtDW->UnitDelay1_DSTATE_i;
2883 } else {
2884 /* Update for UnitDelay: '<S112>/UnitDelay' incorporates:
2885 * Sum: '<S111>/Add2'
2886 */
2887 rtDW->UnitDelay_DSTATE_b = (int16_T)rtb_Gain_b0;
2888 }
2889
2890 /* End of Switch: '<S112>/Switch2' */
2891
2892 /* Switch: '<S37>/Switch1' incorporates:
2893 * RelationalOperator: '<S39>/Relational Operator'
2894 * UnitDelay: '<S39>/UnitDelay'
2895 */
2896 if (rtb_n_commDeacv != rtDW->UnitDelay_DSTATE_bv) {
2897 rtb_UnitDelay_bc = rtb_Sum_i;
2898 }
2899
2900 /* End of Switch: '<S37>/Switch1' */
2901
2902 /* Update for UnitDelay: '<S37>/UnitDelay' */
2903 rtDW->UnitDelay_DSTATE_j = rtb_UnitDelay_bc;
2904
2905 /* Update for Delay: '<S9>/Delay' incorporates:
2906 * Inport: '<Root>/hall_A'
2907 */
2908 rtDW->Delay_DSTATE_d = rtU->hall_A;
2909
2910 /* Update for Delay: '<S9>/Delay1' incorporates:
2911 * Inport: '<Root>/hall_B'
2912 */
2913 rtDW->Delay1_DSTATE = rtU->hall_B;
2914
2915 /* Update for Delay: '<S9>/Delay2' incorporates:
2916 * Inport: '<Root>/hall_C'
2917 */
2918 rtDW->Delay2_DSTATE = rtU->hall_C;
2919
2920 /* Update for UnitDelay: '<S14>/UnitDelay3' incorporates:
2921 * Inport: '<Root>/us_Count'
2922 */
2923 rtDW->UnitDelay3_DSTATE = rtU->us_Count;
2924
2925 /* Update for UnitDelay: '<S14>/UnitDelay4' incorporates:
2926 * Abs: '<S14>/Abs5'
2927 */
2928 rtDW->UnitDelay4_DSTATE = rtb_Switch2;
2929
2930 /* Update for UnitDelay: '<S38>/UnitDelay' */
2931 rtDW->UnitDelay_DSTATE_k = rtb_n_commDeacv;
2932
2933 /* Update for UnitDelay: '<S42>/UnitDelay' */
2934 rtDW->UnitDelay_DSTATE_n = rtb_RelationalOperator4_f;
2935
2936 /* Update for UnitDelay: '<S7>/UnitDelay1' incorporates:
2937 * Sum: '<S7>/Sum3'
2938 */
2939 rtDW->UnitDelay1_DSTATE = qY;
2940
2941 /* Update for UnitDelay: '<S106>/UnitDelay' incorporates:
2942 * Switch: '<S84>/Switch2'
2943 */
2944 rtDW->UnitDelay_DSTATE_h = rtDW->Switch2;
2945
2946 /* Update for UnitDelay: '<S97>/Unit Delay1' incorporates:
2947 * Switch: '<S110>/Switch2'
2948 */
2949 rtDW->UnitDelay1_DSTATE_i = rtb_Divide1_m;
2950
2951 /* Update for UnitDelay: '<S108>/Unit Delay' incorporates:
2952 * Switch: '<S110>/Switch2'
2953 */
2954 rtDW->UnitDelay_DSTATE_g = rtb_Divide1_m;
2955
2956 /* Update for UnitDelay: '<S113>/UnitDelay' incorporates:
2957 * Switch: '<S77>/Switch'
2958 */
2959 rtDW->UnitDelay_DSTATE_o = rtDW->Switch;
2960
2961 /* Update for UnitDelay: '<S98>/Unit Delay1' incorporates:
2962 * Switch: '<S117>/Switch2'
2963 */
2964 rtDW->UnitDelay1_DSTATE_b = rtb_Sum6_p;
2965
2966 /* Update for UnitDelay: '<S115>/Unit Delay' incorporates:
2967 * Switch: '<S117>/Switch2'
2968 */
2969 rtDW->UnitDelay_DSTATE_a = rtb_Sum6_p;
2970
2971 /* Update for UnitDelay: '<S55>/Unit Delay' */
2972 rtDW->UnitDelay_DSTATE_bm = rtb_z_ctrlMod;
2973
2974 /* Update for UnitDelay: '<S39>/UnitDelay' */
2975 rtDW->UnitDelay_DSTATE_bv = rtb_n_commDeacv;
2976
2977 /* Update for UnitDelay: '<S6>/UnitDelay1' incorporates:
2978 * Switch: '<S52>/Switch'
2979 */
2980 rtDW->UnitDelay1_DSTATE_f[0] = rtb_Switch_f_idx_0;
2981
2982 /* End of Outputs for SubSystem: '<Root>/PMSM_Controller' */
2983
2984 /* Outport: '<Root>/f_Vdq' incorporates:
2985 * UnitDelay: '<S6>/UnitDelay1'
2986 */
2987 rtY->f_Vdq[0] = rtb_UnitDelay1[0];
2988
2989 /* Outputs for Atomic SubSystem: '<Root>/PMSM_Controller' */
2990 /* Update for UnitDelay: '<S6>/UnitDelay1' incorporates:
2991 * Switch: '<S52>/Switch'
2992 */
2993 rtDW->UnitDelay1_DSTATE_f[1] = rtb_Switch_f_idx_1;
2994
2995 /* End of Outputs for SubSystem: '<Root>/PMSM_Controller' */
2996
2997 /* Outport: '<Root>/f_Vdq' incorporates:
2998 * UnitDelay: '<S6>/UnitDelay1'
2999 */
3000 rtY->f_Vdq[1] = rtb_UnitDelay1[1];
3001
3002 /* Outport: '<Root>/n_Sector' */
3003 rtY->n_Sector = rtb_DataTypeConversion_np;
3004
3005 /* Outport: '<Root>/n_MotError' */
3006 rtY->n_MotError = rtb_UnitDelay_bc;
3007
3008 /* Outport: '<Root>/f_MotAngle' incorporates:
3009 * Merge: '<S3>/Merge'
3010 */
3011 rtY->f_MotAngle = rtDW->Merge_i;
3012
3013 /* Outport: '<Root>/f_MotRPM' incorporates:
3014 * Switch: '<S14>/Switch2'
3015 */
3016 rtY->f_MotRPM = rtb_Switch3;
3017
3018 /* Outport: '<Root>/f_hallAngle' incorporates:
3019 * Merge: '<S15>/Merge'
3020 */
3021 rtY->f_hallAngle = rtb_Sum3_jm;
3022
3023 /* Outport: '<Root>/n_hallStat' */
3024 rtY->n_hallStat = rtb_Add_gf;
3025
3026 /* Outport: '<Root>/n_runingMode' */
3027 rtY->n_runingMode = rtb_z_ctrlMod;
3028}
3029
3030/* Model initialize function */
3031void PMSM_Controller_initialize(RT_MODEL *const rtM)
3032{
3033 DW *rtDW = rtM->dwork;
3034 PrevZCX *rtPrevZCX = rtM->prevZCSigState;
3035 ExtY *rtY = (ExtY *) rtM->outputs;
3036 rtPrevZCX->ResettableDelay_Reset_ZCE_a = POS_ZCSIG;
3037 rtPrevZCX->PI_backCalc_fixdt1.ResettableDelay_Reset_ZCE = POS_ZCSIG;
3038 rtPrevZCX->PI_backCalc_fixdt_o3.ResettableDelay_Reset_ZCE = POS_ZCSIG;
3039 rtPrevZCX->PI_Speed.ResettableDelay_Reset_ZCE_f = POS_ZCSIG;
3040
3041 /* SystemInitialize for Atomic SubSystem: '<Root>/PMSM_Controller' */
3042 /* SystemInitialize for IfAction SubSystem: '<S14>/Raw_Motor_Speed_Estimation' */
3043 /* InitializeConditions for UnitDelay: '<S20>/UnitDelay2' */
3044 rtDW->UnitDelay2_DSTATE = rtP.n_hall_count_ps;
3045
3046 /* SystemInitialize for Outport: '<S20>/z_counter' incorporates:
3047 * Inport: '<S20>/z_counterRawPrev'
3048 */
3049 rtDW->z_counterRawPrev = rtP.n_hall_count_ps;
3050
3051 /* End of SystemInitialize for SubSystem: '<S14>/Raw_Motor_Speed_Estimation' */
3052
3053 /* SystemInitialize for IfAction SubSystem: '<S54>/Do_Calc' */
3054 /* SystemInitialize for IfAction SubSystem: '<S74>/speed_mode' */
3055 /* SystemInitialize for Atomic SubSystem: '<S87>/PI_Speed' */
3056 PI_backCalc_fixdt_Init(&rtDW->PI_Speed);
3057
3058 /* End of SystemInitialize for SubSystem: '<S87>/PI_Speed' */
3059 /* End of SystemInitialize for SubSystem: '<S74>/speed_mode' */
3060
3061 /* SystemInitialize for IfAction SubSystem: '<S74>/torque_mode' */
3062 /* InitializeConditions for Delay: '<S88>/Delay' */
3063 rtDW->icLoad = 1U;
3064
3065 /* SystemInitialize for Atomic SubSystem: '<S88>/PI_TrqSpdLim' */
3066 /* InitializeConditions for Delay: '<S93>/Resettable Delay' */
3067 rtDW->icLoad_k = 1U;
3068
3069 /* End of SystemInitialize for SubSystem: '<S88>/PI_TrqSpdLim' */
3070 /* End of SystemInitialize for SubSystem: '<S74>/torque_mode' */
3071 /* End of SystemInitialize for SubSystem: '<S54>/Do_Calc' */
3072
3073 /* SystemInitialize for IfAction SubSystem: '<S55>/CurrentLoop' */
3074 /* SystemInitialize for Atomic SubSystem: '<S95>/PI_backCalc_fixdt' */
3075 PI_backCalc_fixdt_p_Init(&rtDW->PI_backCalc_fixdt_o3);
3076
3077 /* End of SystemInitialize for SubSystem: '<S95>/PI_backCalc_fixdt' */
3078
3079 /* SystemInitialize for Atomic SubSystem: '<S95>/PI_backCalc_fixdt1' */
3080 PI_backCalc_fixdt_p_Init(&rtDW->PI_backCalc_fixdt1);
3081
3082 /* End of SystemInitialize for SubSystem: '<S95>/PI_backCalc_fixdt1' */
3083 /* End of SystemInitialize for SubSystem: '<S55>/CurrentLoop' */
3084 /* End of SystemInitialize for SubSystem: '<Root>/PMSM_Controller' */
3085
3086 /* SystemInitialize for Outport: '<Root>/f_MotAngle' incorporates:
3087 * Merge: '<S3>/Merge'
3088 */
3089 rtY->f_MotAngle = rtDW->Merge_i;
3090}
3091
3092/*
3093 * File trailer for generated code.
3094 *
3095 * [EOF]
3096 */
3097