1/*
2 * File: PMSM_Controller.c
3 *
4 * Code generated for Simulink model 'PMSM_Controller'.
5 *
6 * Model version : 1.1200
7 * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020
8 * C/C++ source code generated on : Mon Apr 4 09:18:28 2022
9 *
10 * Target selection: ert.tlc
11 * Embedded hardware selection: ARM Compatible->ARM Cortex-M
12 * Code generation objectives:
13 * 1. Execution efficiency
14 * 2. RAM efficiency
15 * Validation result: Not run
16 */
17
18#include "PMSM_Controller.h"
19#include "PMSM_Controller_private.h"
20
21/* Named constants for Chart: '<S3>/Control_Mode_Manager' */
22#define IN_ACTIVE ((uint8_T)1U)
23#define IN_NO_ACTIVE_CHILD ((uint8_T)0U)
24#define IN_OPEN ((uint8_T)2U)
25#define IN_SPEED_MODE ((uint8_T)1U)
26#define IN_TORQUE_MODE ((uint8_T)2U)
27#define OPEN_MODE ((uint8_T)0U)
28#define SPD_MODE ((uint8_T)1U)
29#define TRQ_MODE ((uint8_T)2U)
30
31uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T
32 maxIndex)
33{
34 uint16_T bpIndex;
35
36 /* Prelookup - Index only
37 Index Search method: 'even'
38 Extrapolation method: 'Clip'
39 Use previous index: 'off'
40 Use last breakpoint for index at or above upper limit: 'on'
41 Remove protection against out-of-range input in generated code: 'off'
42 */
43 if (u <= bp0) {
44 bpIndex = 0U;
45 } else {
46 bpIndex = (uint16_T)((uint32_T)(uint16_T)(u - bp0) / bpSpace);
47 if (bpIndex < maxIndex) {
48 } else {
49 bpIndex = (uint16_T)maxIndex;
50 }
51 }
52
53 return bpIndex;
54}
55
56uint8_T plook_u8u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace, uint32_T
57 maxIndex)
58{
59 uint16_T fbpIndex;
60 uint8_T bpIndex;
61
62 /* Prelookup - Index only
63 Index Search method: 'even'
64 Extrapolation method: 'Clip'
65 Use previous index: 'off'
66 Use last breakpoint for index at or above upper limit: 'on'
67 Remove protection against out-of-range input in generated code: 'off'
68 */
69 if (u <= bp0) {
70 bpIndex = 0U;
71 } else {
72 fbpIndex = (uint16_T)((uint32_T)(uint16_T)((uint32_T)u - bp0) / bpSpace);
73 if (fbpIndex < maxIndex) {
74 bpIndex = (uint8_T)fbpIndex;
75 } else {
76 bpIndex = (uint8_T)maxIndex;
77 }
78 }
79
80 return bpIndex;
81}
82
83int32_T div_nde_s32_floor(int32_T numerator, int32_T denominator)
84{
85 return (((numerator < 0) != (denominator < 0)) && (numerator % denominator !=
86 0) ? -1 : 0) + numerator / denominator;
87}
88
89/*
90 * System initialize for atomic system:
91 * '<S39>/Counter'
92 * '<S38>/Counter'
93 */
94void Counter_Init(DW_Counter *localDW, uint16_T rtp_z_cntInit)
95{
96 /* InitializeConditions for UnitDelay: '<S44>/UnitDelay' */
97 localDW->UnitDelay_DSTATE = rtp_z_cntInit;
98}
99
100/*
101 * Output and update for atomic system:
102 * '<S39>/Counter'
103 * '<S38>/Counter'
104 */
105uint16_T Counter(uint16_T rtu_inc, uint16_T rtu_max, boolean_T rtu_rst,
106 DW_Counter *localDW)
107{
108 uint16_T rty_cnt_0;
109 uint16_T rtu_rst_0;
110
111 /* Switch: '<S44>/Switch1' incorporates:
112 * Constant: '<S44>/Constant23'
113 * UnitDelay: '<S44>/UnitDelay'
114 */
115 if (rtu_rst) {
116 rtu_rst_0 = 0U;
117 } else {
118 rtu_rst_0 = localDW->UnitDelay_DSTATE;
119 }
120
121 /* End of Switch: '<S44>/Switch1' */
122
123 /* Sum: '<S43>/Sum1' */
124 rty_cnt_0 = (uint16_T)((uint32_T)rtu_inc + rtu_rst_0);
125
126 /* MinMax: '<S43>/MinMax' */
127 if (rty_cnt_0 < rtu_max) {
128 /* Update for UnitDelay: '<S44>/UnitDelay' */
129 localDW->UnitDelay_DSTATE = rty_cnt_0;
130 } else {
131 /* Update for UnitDelay: '<S44>/UnitDelay' */
132 localDW->UnitDelay_DSTATE = rtu_max;
133 }
134
135 /* End of MinMax: '<S43>/MinMax' */
136 return rty_cnt_0;
137}
138
139/*
140 * Output and update for atomic system:
141 * '<S35>/either_edge'
142 * '<S34>/either_edge'
143 */
144boolean_T either_edge(boolean_T rtu_u, DW_either_edge *localDW)
145{
146 boolean_T rty_y_0;
147
148 /* RelationalOperator: '<S40>/Relational Operator' incorporates:
149 * UnitDelay: '<S40>/UnitDelay'
150 */
151 rty_y_0 = (rtu_u != localDW->UnitDelay_DSTATE);
152
153 /* Update for UnitDelay: '<S40>/UnitDelay' */
154 localDW->UnitDelay_DSTATE = rtu_u;
155 return rty_y_0;
156}
157
158/* System initialize for atomic system: '<S34>/Debounce_Filter' */
159void Debounce_Filter_Init(DW_Debounce_Filter *localDW)
160{
161 /* SystemInitialize for IfAction SubSystem: '<S35>/Qualification' */
162 /* SystemInitialize for Atomic SubSystem: '<S39>/Counter' */
163 Counter_Init(&localDW->Counter_f, 0);
164
165 /* End of SystemInitialize for SubSystem: '<S39>/Counter' */
166 /* End of SystemInitialize for SubSystem: '<S35>/Qualification' */
167
168 /* SystemInitialize for IfAction SubSystem: '<S35>/Dequalification' */
169 /* SystemInitialize for Atomic SubSystem: '<S38>/Counter' */
170 Counter_Init(&localDW->Counter_d, 0);
171
172 /* End of SystemInitialize for SubSystem: '<S38>/Counter' */
173 /* End of SystemInitialize for SubSystem: '<S35>/Dequalification' */
174}
175
176/* Output and update for atomic system: '<S34>/Debounce_Filter' */
177void Debounce_Filter(boolean_T rtu_u, uint16_T rtu_tAcv, uint16_T rtu_tDeacv,
178 boolean_T *rty_y, DW_Debounce_Filter *localDW)
179{
180 uint16_T rtb_Sum1_n;
181 boolean_T rtb_RelationalOperator_e;
182
183 /* Outputs for Atomic SubSystem: '<S35>/either_edge' */
184 rtb_RelationalOperator_e = either_edge(rtu_u, &localDW->either_edge_j);
185
186 /* End of Outputs for SubSystem: '<S35>/either_edge' */
187
188 /* If: '<S35>/If2' incorporates:
189 * Constant: '<S38>/Constant6'
190 * Constant: '<S39>/Constant6'
191 * Inport: '<S37>/yPrev'
192 * Logic: '<S35>/Logical Operator1'
193 * Logic: '<S35>/Logical Operator2'
194 * Logic: '<S35>/Logical Operator3'
195 * Logic: '<S35>/Logical Operator4'
196 * UnitDelay: '<S35>/UnitDelay'
197 */
198 if (rtu_u && (!localDW->UnitDelay_DSTATE)) {
199 /* Outputs for IfAction SubSystem: '<S35>/Qualification' incorporates:
200 * ActionPort: '<S39>/Action Port'
201 */
202 /* Outputs for Atomic SubSystem: '<S39>/Counter' */
203 rtb_Sum1_n = Counter(1, rtu_tAcv, rtb_RelationalOperator_e,
204 &localDW->Counter_f);
205
206 /* End of Outputs for SubSystem: '<S39>/Counter' */
207
208 /* Switch: '<S39>/Switch2' incorporates:
209 * Constant: '<S39>/Constant6'
210 * RelationalOperator: '<S39>/Relational Operator2'
211 */
212 *rty_y = ((rtb_Sum1_n > rtu_tAcv) || localDW->UnitDelay_DSTATE);
213
214 /* End of Outputs for SubSystem: '<S35>/Qualification' */
215 } else if ((!rtu_u) && localDW->UnitDelay_DSTATE) {
216 /* Outputs for IfAction SubSystem: '<S35>/Dequalification' incorporates:
217 * ActionPort: '<S38>/Action Port'
218 */
219 /* Outputs for Atomic SubSystem: '<S38>/Counter' */
220 rtb_Sum1_n = Counter(1, rtu_tDeacv, rtb_RelationalOperator_e,
221 &localDW->Counter_d);
222
223 /* End of Outputs for SubSystem: '<S38>/Counter' */
224
225 /* Switch: '<S38>/Switch2' incorporates:
226 * Constant: '<S38>/Constant6'
227 * RelationalOperator: '<S38>/Relational Operator2'
228 */
229 *rty_y = ((rtb_Sum1_n <= rtu_tDeacv) && localDW->UnitDelay_DSTATE);
230
231 /* End of Outputs for SubSystem: '<S35>/Dequalification' */
232 } else {
233 /* Outputs for IfAction SubSystem: '<S35>/Default' incorporates:
234 * ActionPort: '<S37>/Action Port'
235 */
236 *rty_y = localDW->UnitDelay_DSTATE;
237
238 /* End of Outputs for SubSystem: '<S35>/Default' */
239 }
240
241 /* End of If: '<S35>/If2' */
242
243 /* Update for UnitDelay: '<S35>/UnitDelay' */
244 localDW->UnitDelay_DSTATE = *rty_y;
245}
246
247/* Output and update for atomic system: '<S45>/Low_Pass_Filter' */
248void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T rty_y[2],
249 DW_Low_Pass_Filter *localDW)
250{
251 int32_T tmp;
252
253 /* Sum: '<S54>/Sum2' incorporates:
254 * UnitDelay: '<S54>/UnitDelay1'
255 */
256 tmp = rtu_u[0] - localDW->UnitDelay1_DSTATE[0];
257 if (tmp > 32767) {
258 tmp = 32767;
259 } else {
260 if (tmp < -32768) {
261 tmp = -32768;
262 }
263 }
264
265 /* Product: '<S54>/Divide3' incorporates:
266 * Sum: '<S54>/Sum2'
267 */
268 rty_y[0] = (int16_T)((rtu_coef * tmp) >> 16);
269
270 /* Sum: '<S54>/Sum3' incorporates:
271 * UnitDelay: '<S54>/UnitDelay1'
272 */
273 rty_y[0] += localDW->UnitDelay1_DSTATE[0];
274
275 /* Update for UnitDelay: '<S54>/UnitDelay1' incorporates:
276 * Sum: '<S54>/Sum3'
277 */
278 localDW->UnitDelay1_DSTATE[0] = rty_y[0];
279
280 /* Sum: '<S54>/Sum2' incorporates:
281 * UnitDelay: '<S54>/UnitDelay1'
282 */
283 tmp = rtu_u[1] - localDW->UnitDelay1_DSTATE[1];
284 if (tmp > 32767) {
285 tmp = 32767;
286 } else {
287 if (tmp < -32768) {
288 tmp = -32768;
289 }
290 }
291
292 /* Product: '<S54>/Divide3' incorporates:
293 * Sum: '<S54>/Sum2'
294 */
295 rty_y[1] = (int16_T)((rtu_coef * tmp) >> 16);
296
297 /* Sum: '<S54>/Sum3' incorporates:
298 * UnitDelay: '<S54>/UnitDelay1'
299 */
300 rty_y[1] += localDW->UnitDelay1_DSTATE[1];
301
302 /* Update for UnitDelay: '<S54>/UnitDelay1' incorporates:
303 * Sum: '<S54>/Sum3'
304 */
305 localDW->UnitDelay1_DSTATE[1] = rty_y[1];
306}
307
308/* Output and update for atomic system: '<S58>/PI_iq' */
309void PI_iq(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
310 int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt,
311 int16_T *rty_pi_out, DW_PI_iq *localDW)
312{
313 int64_T tmp;
314 int32_T rtb_Divide4_k;
315 int32_T rtb_Sum1_hy;
316
317 /* Product: '<S63>/Divide4' */
318 rtb_Divide4_k = (rtu_err * rtu_P) >> 6;
319
320 /* Product: '<S63>/Divide1' incorporates:
321 * Product: '<S63>/Divide4'
322 */
323 tmp = ((int64_T)rtb_Divide4_k * rtu_I) >> 10;
324 if (tmp > 2147483647LL) {
325 tmp = 2147483647LL;
326 } else {
327 if (tmp < -2147483648LL) {
328 tmp = -2147483648LL;
329 }
330 }
331
332 /* Sum: '<S63>/Sum2' incorporates:
333 * Product: '<S63>/Divide1'
334 * UnitDelay: '<S63>/UnitDelay'
335 */
336 tmp = (((int64_T)rtu_ext_limProt << 4) + (int32_T)tmp) +
337 localDW->UnitDelay_DSTATE;
338 if (tmp > 2147483647LL) {
339 tmp = 2147483647LL;
340 } else {
341 if (tmp < -2147483648LL) {
342 tmp = -2147483648LL;
343 }
344 }
345
346 /* Sum: '<S65>/Sum1' incorporates:
347 * Sum: '<S63>/Sum2'
348 * UnitDelay: '<S65>/UnitDelay'
349 */
350 rtb_Sum1_hy = (int32_T)tmp + localDW->UnitDelay_DSTATE_i;
351
352 /* Sum: '<S63>/Sum6' incorporates:
353 * Product: '<S63>/Divide4'
354 * Sum: '<S65>/Sum1'
355 */
356 tmp = (int64_T)rtb_Divide4_k + rtb_Sum1_hy;
357 if (tmp > 2147483647LL) {
358 tmp = 2147483647LL;
359 } else {
360 if (tmp < -2147483648LL) {
361 tmp = -2147483648LL;
362 }
363 }
364
365 /* Switch: '<S66>/Switch2' incorporates:
366 * RelationalOperator: '<S66>/LowerRelop1'
367 * RelationalOperator: '<S66>/UpperRelop'
368 * Sum: '<S63>/Sum6'
369 * Switch: '<S66>/Switch'
370 */
371 if ((int32_T)tmp > (rtu_satMax << 4)) {
372 *rty_pi_out = rtu_satMax;
373 } else if ((int32_T)tmp < (rtu_satMin << 4)) {
374 /* Switch: '<S66>/Switch' */
375 *rty_pi_out = rtu_satMin;
376 } else {
377 *rty_pi_out = (int16_T)((int32_T)tmp >> 4);
378 }
379
380 /* End of Switch: '<S66>/Switch2' */
381
382 /* Update for UnitDelay: '<S63>/UnitDelay' incorporates:
383 * Product: '<S63>/Divide2'
384 * Sum: '<S63>/Sum3'
385 * Sum: '<S63>/Sum6'
386 */
387 localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((*rty_pi_out << 4) - (int32_T)
388 tmp) * rtu_Kb) >> 10);
389
390 /* Update for UnitDelay: '<S65>/UnitDelay' incorporates:
391 * Sum: '<S65>/Sum1'
392 */
393 localDW->UnitDelay_DSTATE_i = rtb_Sum1_hy;
394}
395
396/* Output and update for atomic system: '<S57>/PI_id' */
397void PI_id(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
398 int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt,
399 int16_T *rty_pi_out, DW_PI_id *localDW)
400{
401 int64_T tmp;
402 int32_T rtb_Divide4_e;
403 int32_T rtb_Sum1_l;
404
405 /* Product: '<S59>/Divide4' */
406 rtb_Divide4_e = (rtu_err * rtu_P) >> 6;
407
408 /* Product: '<S59>/Divide1' incorporates:
409 * Product: '<S59>/Divide4'
410 */
411 tmp = ((int64_T)rtb_Divide4_e * rtu_I) >> 13;
412 if (tmp > 2147483647LL) {
413 tmp = 2147483647LL;
414 } else {
415 if (tmp < -2147483648LL) {
416 tmp = -2147483648LL;
417 }
418 }
419
420 /* Sum: '<S59>/Sum2' incorporates:
421 * Product: '<S59>/Divide1'
422 * UnitDelay: '<S59>/UnitDelay'
423 */
424 tmp = ((((int64_T)(int32_T)tmp << 3) + ((int64_T)rtu_ext_limProt << 3)) +
425 localDW->UnitDelay_DSTATE_j) >> 3;
426 if (tmp > 2147483647LL) {
427 tmp = 2147483647LL;
428 } else {
429 if (tmp < -2147483648LL) {
430 tmp = -2147483648LL;
431 }
432 }
433
434 /* Sum: '<S61>/Sum1' incorporates:
435 * Sum: '<S59>/Sum2'
436 * UnitDelay: '<S61>/UnitDelay'
437 */
438 rtb_Sum1_l = (int32_T)tmp + localDW->UnitDelay_DSTATE;
439
440 /* Sum: '<S59>/Sum6' incorporates:
441 * Product: '<S59>/Divide4'
442 * Sum: '<S61>/Sum1'
443 */
444 tmp = ((int64_T)rtb_Sum1_l << 3) + rtb_Divide4_e;
445 if (tmp > 2147483647LL) {
446 tmp = 2147483647LL;
447 } else {
448 if (tmp < -2147483648LL) {
449 tmp = -2147483648LL;
450 }
451 }
452
453 /* Switch: '<S62>/Switch2' incorporates:
454 * RelationalOperator: '<S62>/LowerRelop1'
455 * RelationalOperator: '<S62>/UpperRelop'
456 * Sum: '<S59>/Sum6'
457 * Switch: '<S62>/Switch'
458 */
459 if ((int32_T)tmp > (rtu_satMax << 4)) {
460 *rty_pi_out = rtu_satMax;
461 } else if ((int32_T)tmp < (rtu_satMin << 4)) {
462 /* Switch: '<S62>/Switch' */
463 *rty_pi_out = rtu_satMin;
464 } else {
465 *rty_pi_out = (int16_T)((int32_T)tmp >> 4);
466 }
467
468 /* End of Switch: '<S62>/Switch2' */
469
470 /* Update for UnitDelay: '<S59>/UnitDelay' incorporates:
471 * Product: '<S59>/Divide2'
472 * Sum: '<S59>/Sum3'
473 * Sum: '<S59>/Sum6'
474 */
475 localDW->UnitDelay_DSTATE_j = (int32_T)(((int64_T)((*rty_pi_out << 4) -
476 (int32_T)tmp) * rtu_Kb) >> 10);
477
478 /* Update for UnitDelay: '<S61>/UnitDelay' incorporates:
479 * Sum: '<S61>/Sum1'
480 */
481 localDW->UnitDelay_DSTATE = rtb_Sum1_l;
482}
483
484/* System initialize for atomic system: '<S73>/pi_speed' */
485void pi_speed_Init(DW_pi_speed *localDW)
486{
487 /* InitializeConditions for Delay: '<S77>/Resettable Delay' */
488 localDW->icLoad = 1U;
489}
490
491/* Output and update for atomic system: '<S73>/pi_speed' */
492int16_T pi_speed(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
493 int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt,
494 uint8_T rtu_reset, const ConstB_pi_speed *localC, DW_pi_speed
495 *localDW, ZCE_pi_speed *localZCE)
496{
497 int16_T rty_pi_out_0;
498 int64_T tmp;
499 int32_T rtb_Divide4_hl;
500 int32_T rtb_Sum1_b1;
501
502 /* Product: '<S76>/Divide4' */
503 rtb_Divide4_hl = (rtu_err * rtu_P) >> 2;
504
505 /* Delay: '<S77>/Resettable Delay' incorporates:
506 * DataTypeConversion: '<S77>/Data Type Conversion2'
507 */
508 if ((rtu_reset > 0) && (localZCE->ResettableDelay_Reset_ZCE != POS_ZCSIG)) {
509 localDW->icLoad = 1U;
510 }
511
512 localZCE->ResettableDelay_Reset_ZCE = (ZCSigState)(rtu_reset > 0);
513 if (localDW->icLoad != 0) {
514 localDW->ResettableDelay_DSTATE = localC->DataTypeConversion2;
515 }
516
517 /* Product: '<S76>/Divide1' incorporates:
518 * Product: '<S76>/Divide4'
519 */
520 tmp = ((int64_T)rtb_Divide4_hl * rtu_I) >> 10;
521 if (tmp > 2147483647LL) {
522 tmp = 2147483647LL;
523 } else {
524 if (tmp < -2147483648LL) {
525 tmp = -2147483648LL;
526 }
527 }
528
529 /* Sum: '<S76>/Sum2' incorporates:
530 * Product: '<S76>/Divide1'
531 * UnitDelay: '<S76>/UnitDelay'
532 */
533 tmp = (((int64_T)(int32_T)tmp + rtu_ext_limProt) + ((int64_T)
534 localDW->UnitDelay_DSTATE << 2)) >> 2;
535 if (tmp > 2147483647LL) {
536 tmp = 2147483647LL;
537 } else {
538 if (tmp < -2147483648LL) {
539 tmp = -2147483648LL;
540 }
541 }
542
543 /* Sum: '<S77>/Sum1' incorporates:
544 * Delay: '<S77>/Resettable Delay'
545 * Sum: '<S76>/Sum2'
546 */
547 rtb_Sum1_b1 = (int32_T)tmp + localDW->ResettableDelay_DSTATE;
548
549 /* Sum: '<S76>/Sum6' incorporates:
550 * DataTypeConversion: '<S77>/Data Type Conversion1'
551 * Product: '<S76>/Divide4'
552 * Sum: '<S77>/Sum1'
553 */
554 tmp = ((int64_T)(rtb_Sum1_b1 >> 2) << 4) + rtb_Divide4_hl;
555 if (tmp > 2147483647LL) {
556 tmp = 2147483647LL;
557 } else {
558 if (tmp < -2147483648LL) {
559 tmp = -2147483648LL;
560 }
561 }
562
563 /* Switch: '<S78>/Switch2' incorporates:
564 * RelationalOperator: '<S78>/LowerRelop1'
565 * RelationalOperator: '<S78>/UpperRelop'
566 * Sum: '<S76>/Sum6'
567 * Switch: '<S78>/Switch'
568 */
569 if ((int32_T)tmp > (rtu_satMax << 4)) {
570 rty_pi_out_0 = rtu_satMax;
571 } else if ((int32_T)tmp < (rtu_satMin << 4)) {
572 /* Switch: '<S78>/Switch' */
573 rty_pi_out_0 = rtu_satMin;
574 } else {
575 rty_pi_out_0 = (int16_T)((int32_T)tmp >> 4);
576 }
577
578 /* End of Switch: '<S78>/Switch2' */
579
580 /* Update for UnitDelay: '<S76>/UnitDelay' incorporates:
581 * Product: '<S76>/Divide2'
582 * Sum: '<S76>/Sum3'
583 * Sum: '<S76>/Sum6'
584 */
585 localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((rty_pi_out_0 << 4) -
586 (int32_T)tmp) * rtu_Kb) >> 12);
587
588 /* Update for Delay: '<S77>/Resettable Delay' incorporates:
589 * Sum: '<S77>/Sum1'
590 */
591 localDW->icLoad = 0U;
592 localDW->ResettableDelay_DSTATE = rtb_Sum1_b1;
593 return rty_pi_out_0;
594}
595
596real_T rt_roundd_snf(real_T u)
597{
598 real_T y;
599 if (fabs(u) < 4.503599627370496E+15) {
600 if (u >= 0.5) {
601 y = floor(u + 0.5);
602 } else if (u > -0.5) {
603 y = u * 0.0;
604 } else {
605 y = ceil(u - 0.5);
606 }
607 } else {
608 y = u;
609 }
610
611 return y;
612}
613
614/* Model step function */
615void PMSM_Controller_step(RT_MODEL *const rtM)
616{
617 DW *rtDW = rtM->dwork;
618 PrevZCX *rtPrevZCX = rtM->prevZCSigState;
619 ExtU *rtU = (ExtU *) rtM->inputs;
620 ExtY *rtY = (ExtY *) rtM->outputs;
621 int32_T sigIdx;
622 int32_T tmp;
623 int32_T tmp_0;
624 int32_T tmp_1;
625 uint32_T tmp_2;
626 int16_T rtb_DataTypeConversion[2];
627 int16_T rtb_TmpSignalConversionAtLow_Pa[2];
628 int16_T rtb_Abs5;
629 int16_T rtb_Abs5_h;
630 int16_T rtb_Divide1_fi;
631 int16_T rtb_Divide4_c;
632 int16_T rtb_Gain1;
633 int16_T rtb_Gain4;
634 int16_T rtb_Max;
635 int16_T rtb_Switch2_ip;
636 int16_T rtb_Switch3_c;
637 int16_T rtb_Switch_b;
638 int16_T rtb_Switch_dr;
639 int16_T rtb_Switch_oi;
640 uint16_T rtb_LogicalOperator3;
641 uint16_T rtb_MultiportSwitch_idx_0;
642 uint16_T rtb_MultiportSwitch_idx_1;
643 uint16_T rtb_Switch2_idx_1;
644 uint16_T rtb_Switch2_idx_2;
645 int8_T UnitDelay3;
646 int8_T rtb_Sum2;
647 int8_T rtb_Sum2_tmp;
648 uint8_T rtb_Add_k;
649 uint8_T rtb_DataTypeConversion1_c;
650 uint8_T rtb_Switch2_fu;
651 uint8_T rtb_UnitDelay;
652 uint8_T rtb_z_ctrlMod;
653 boolean_T rtb_LogicalOperator2;
654 boolean_T rtb_LogicalOperator4;
655 boolean_T rtb_LogicalOperator_p;
656 boolean_T rtb_RelationalOperator4_f;
657 boolean_T rtb_n_commDeacv;
658
659 /* Outputs for Atomic SubSystem: '<Root>/PMSM_Controller' */
660 /* Logic: '<S7>/Edge_Detect' incorporates:
661 * Delay: '<S7>/Delay'
662 * Delay: '<S7>/Delay1'
663 * Delay: '<S7>/Delay2'
664 * Inport: '<Root>/hall_a'
665 * Inport: '<Root>/hall_b'
666 * Inport: '<Root>/hall_c'
667 */
668 rtb_LogicalOperator_p = (boolean_T)((rtU->hall_a != 0) ^ (rtDW->Delay_DSTATE
669 != 0) ^ (rtU->hall_b != 0) ^ (rtDW->Delay1_DSTATE != 0) ^ (rtU->hall_c != 0))
670 ^ (rtDW->Delay2_DSTATE != 0);
671
672 /* Sum: '<S9>/Add' incorporates:
673 * Gain: '<S9>/Gain'
674 * Gain: '<S9>/Gain1'
675 * Inport: '<Root>/hall_a'
676 * Inport: '<Root>/hall_b'
677 * Inport: '<Root>/hall_c'
678 */
679 rtb_Add_k = (uint8_T)((uint32_T)(uint8_T)((uint32_T)(uint8_T)(rtU->hall_c << 2)
680 + (uint8_T)(rtU->hall_b << 1)) + rtU->hall_a);
681
682 /* If: '<S2>/If2' incorporates:
683 * If: '<S10>/If2'
684 * Inport: '<S15>/z_counterRawPrev'
685 * UnitDelay: '<S10>/UnitDelay3'
686 */
687 if (rtb_LogicalOperator_p) {
688 /* Outputs for IfAction SubSystem: '<S2>/Direction_Detection' incorporates:
689 * ActionPort: '<S6>/Action Port'
690 */
691 /* UnitDelay: '<S6>/UnitDelay3' */
692 UnitDelay3 = rtDW->Switch2_i;
693
694 /* End of Outputs for SubSystem: '<S2>/Direction_Detection' */
695
696 /* Selector: '<S9>/Selector' incorporates:
697 * Constant: '<S9>/vec_hallToPos'
698 */
699 rtb_Sum2_tmp = rtConstP.vec_hallToPos_Value[rtb_Add_k];
700
701 /* Outputs for IfAction SubSystem: '<S2>/Direction_Detection' incorporates:
702 * ActionPort: '<S6>/Action Port'
703 */
704 /* Sum: '<S6>/Sum2' incorporates:
705 * Constant: '<S9>/vec_hallToPos'
706 * Selector: '<S9>/Selector'
707 * UnitDelay: '<S6>/UnitDelay2'
708 */
709 rtb_Sum2 = (int8_T)(rtb_Sum2_tmp - rtDW->UnitDelay2_DSTATE_j);
710
711 /* Switch: '<S6>/Switch2' incorporates:
712 * Constant: '<S6>/Constant20'
713 * Constant: '<S6>/Constant8'
714 * Logic: '<S6>/Logical Operator3'
715 * RelationalOperator: '<S6>/Relational Operator1'
716 * RelationalOperator: '<S6>/Relational Operator6'
717 */
718 if ((rtb_Sum2 == 1) || (rtb_Sum2 == -5)) {
719 /* Switch: '<S6>/Switch2' incorporates:
720 * Constant: '<S6>/Constant24'
721 */
722 rtDW->Switch2_i = 1;
723 } else {
724 /* Switch: '<S6>/Switch2' incorporates:
725 * Constant: '<S6>/Constant23'
726 */
727 rtDW->Switch2_i = -1;
728 }
729
730 /* End of Switch: '<S6>/Switch2' */
731
732 /* Update for UnitDelay: '<S6>/UnitDelay2' */
733 rtDW->UnitDelay2_DSTATE_j = rtb_Sum2_tmp;
734
735 /* End of Outputs for SubSystem: '<S2>/Direction_Detection' */
736
737 /* Outputs for IfAction SubSystem: '<S10>/Raw_Motor_Speed_Estimation' incorporates:
738 * ActionPort: '<S15>/Action Port'
739 */
740 /* RelationalOperator: '<S15>/Relational Operator4' */
741 rtb_RelationalOperator4_f = (rtDW->Switch2_i != UnitDelay3);
742 rtDW->z_counterRawPrev = rtDW->UnitDelay3_DSTATE;
743
744 /* Switch: '<S15>/Switch3' incorporates:
745 * Constant: '<S15>/Constant4'
746 * Inport: '<S15>/z_counterRawPrev'
747 * Logic: '<S15>/Logical Operator1'
748 * Switch: '<S15>/Switch2'
749 * UnitDelay: '<S10>/UnitDelay3'
750 * UnitDelay: '<S15>/UnitDelay1'
751 */
752 if (rtb_RelationalOperator4_f && rtDW->UnitDelay1_DSTATE_i) {
753 rtb_Switch3_c = 0;
754 } else if (rtb_RelationalOperator4_f) {
755 /* Switch: '<S15>/Switch3' incorporates:
756 * Switch: '<S15>/Switch2'
757 * UnitDelay: '<S10>/UnitDelay4'
758 */
759 rtb_Switch3_c = rtDW->UnitDelay4_DSTATE;
760 } else {
761 /* Product: '<S15>/Divide13' incorporates:
762 * Sum: '<S15>/Sum13'
763 * Switch: '<S15>/Switch2'
764 * UnitDelay: '<S15>/UnitDelay2'
765 * UnitDelay: '<S15>/UnitDelay3'
766 * UnitDelay: '<S15>/UnitDelay5'
767 */
768 tmp_2 = 8000000U / (((rtDW->UnitDelay2_DSTATE + rtDW->UnitDelay3_DSTATE_l)
769 + rtDW->UnitDelay5_DSTATE) + rtDW->z_counterRawPrev);
770 if (tmp_2 > 32767U) {
771 tmp_2 = 32767U;
772 }
773
774 /* Switch: '<S15>/Switch3' incorporates:
775 * Product: '<S15>/Divide13'
776 * Switch: '<S15>/Switch2'
777 */
778 rtb_Switch3_c = (int16_T)tmp_2;
779 }
780
781 /* End of Switch: '<S15>/Switch3' */
782
783 /* Product: '<S15>/Divide11' incorporates:
784 * Switch: '<S15>/Switch3'
785 */
786 rtDW->Divide11 = (int16_T)(rtb_Switch3_c * rtDW->Switch2_i);
787
788 /* Update for UnitDelay: '<S15>/UnitDelay1' */
789 rtDW->UnitDelay1_DSTATE_i = rtb_RelationalOperator4_f;
790
791 /* Update for UnitDelay: '<S15>/UnitDelay2' incorporates:
792 * UnitDelay: '<S15>/UnitDelay3'
793 */
794 rtDW->UnitDelay2_DSTATE = rtDW->UnitDelay3_DSTATE_l;
795
796 /* Update for UnitDelay: '<S15>/UnitDelay3' incorporates:
797 * UnitDelay: '<S15>/UnitDelay5'
798 */
799 rtDW->UnitDelay3_DSTATE_l = rtDW->UnitDelay5_DSTATE;
800
801 /* Update for UnitDelay: '<S15>/UnitDelay5' */
802 rtDW->UnitDelay5_DSTATE = rtDW->z_counterRawPrev;
803
804 /* End of Outputs for SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
805 }
806
807 /* End of If: '<S2>/If2' */
808
809 /* Switch: '<S8>/Switch3' incorporates:
810 * Constant: '<S8>/Constant16'
811 * Constant: '<S8>/Constant2'
812 * Constant: '<S9>/vec_hallToPos'
813 * RelationalOperator: '<S8>/Relational Operator7'
814 * Selector: '<S9>/Selector'
815 * Sum: '<S8>/Sum1'
816 */
817 if (rtDW->Switch2_i == 1) {
818 rtb_Sum2 = rtConstP.vec_hallToPos_Value[rtb_Add_k];
819 } else {
820 rtb_Sum2 = (int8_T)(rtConstP.vec_hallToPos_Value[rtb_Add_k] + 1);
821 }
822
823 /* End of Switch: '<S8>/Switch3' */
824
825 /* MinMax: '<S8>/MinMax' incorporates:
826 * Inport: '<Root>/hw_count'
827 */
828 if (rtU->hw_count < rtDW->z_counterRawPrev) {
829 tmp_2 = rtU->hw_count;
830 } else {
831 tmp_2 = rtDW->z_counterRawPrev;
832 }
833
834 /* End of MinMax: '<S8>/MinMax' */
835
836 /* Sum: '<S8>/Sum3' incorporates:
837 * Product: '<S8>/Divide1'
838 * Product: '<S8>/Divide3'
839 */
840 rtb_Switch3_c = (int16_T)(((int16_T)((int16_T)(((uint64_T)tmp_2 << 14) /
841 rtDW->z_counterRawPrev) * rtDW->Switch2_i) + (rtb_Sum2 << 14)) >> 2);
842
843 /* MinMax: '<S8>/MinMax1' incorporates:
844 * Constant: '<S8>/Constant1'
845 * Sum: '<S8>/Sum3'
846 * Switch: '<S8>/Switch2'
847 */
848 if (rtb_Switch3_c <= 0) {
849 rtb_Switch3_c = 0;
850 }
851
852 /* End of MinMax: '<S8>/MinMax1' */
853
854 /* Sum: '<S11>/Add2' incorporates:
855 * Constant: '<S11>/Constant2'
856 * Product: '<S8>/Divide2'
857 */
858 rtb_Switch3_c = (int16_T)((((15 * rtb_Switch3_c) >> 4) + 3840) >> 2);
859
860 /* If: '<S11>/If' incorporates:
861 * Constant: '<S11>/Constant3'
862 * DataTypeConversion: '<S11>/Data Type Conversion'
863 * Inport: '<S12>/In1'
864 * Merge: '<S11>/Merge'
865 * Sum: '<S11>/Add'
866 * Sum: '<S11>/Add2'
867 */
868 if ((int16_T)(rtb_Switch3_c >> 4) >= 360) {
869 /* Outputs for IfAction SubSystem: '<S11>/If Action Subsystem' incorporates:
870 * ActionPort: '<S12>/Action Port'
871 */
872 rtb_Switch3_c = (int16_T)(rtb_Switch3_c - 5760);
873
874 /* End of Outputs for SubSystem: '<S11>/If Action Subsystem' */
875 }
876
877 /* End of If: '<S11>/If' */
878
879 /* Switch: '<S10>/Switch2' incorporates:
880 * Constant: '<S10>/Constant4'
881 * Inport: '<Root>/hw_count'
882 * Product: '<S15>/Divide11'
883 * RelationalOperator: '<S10>/Relational Operator2'
884 */
885 if (rtU->hw_count >= 400000U) {
886 rtb_Switch2_ip = 0;
887 } else {
888 rtb_Switch2_ip = rtDW->Divide11;
889 }
890
891 /* End of Switch: '<S10>/Switch2' */
892
893 /* Abs: '<S10>/Abs5' incorporates:
894 * Switch: '<S10>/Switch2'
895 */
896 if (rtb_Switch2_ip < 0) {
897 rtb_Abs5 = (int16_T)-rtb_Switch2_ip;
898 } else {
899 rtb_Abs5 = rtb_Switch2_ip;
900 }
901
902 /* End of Abs: '<S10>/Abs5' */
903
904 /* If: '<S10>/If1' */
905 if (rtb_LogicalOperator_p) {
906 /* Outputs for IfAction SubSystem: '<S10>/Subsystem' incorporates:
907 * ActionPort: '<S16>/Action Port'
908 */
909 /* Relay: '<S16>/n_commDeacv' incorporates:
910 * Abs: '<S10>/Abs5'
911 */
912 rtDW->n_commDeacv_Mode = ((rtb_Abs5 >= 120) || ((rtb_Abs5 > 60) &&
913 rtDW->n_commDeacv_Mode));
914
915 /* RelationalOperator: '<S18>/Compare' incorporates:
916 * Constant: '<S18>/Constant'
917 * Relay: '<S16>/n_commDeacv'
918 * Sum: '<S16>/Sum13'
919 * UnitDelay: '<S16>/UnitDelay2'
920 * UnitDelay: '<S16>/UnitDelay3'
921 * UnitDelay: '<S16>/UnitDelay5'
922 */
923 rtDW->Compare = ((uint16_T)((uint32_T)(uint16_T)((uint32_T)(uint16_T)
924 ((uint32_T)rtDW->UnitDelay2_DSTATE_f + rtDW->UnitDelay3_DSTATE_lh) +
925 rtDW->UnitDelay5_DSTATE_f) + rtDW->n_commDeacv_Mode) >= 4);
926
927 /* Update for UnitDelay: '<S16>/UnitDelay2' incorporates:
928 * UnitDelay: '<S16>/UnitDelay3'
929 */
930 rtDW->UnitDelay2_DSTATE_f = rtDW->UnitDelay3_DSTATE_lh;
931
932 /* Update for UnitDelay: '<S16>/UnitDelay3' incorporates:
933 * UnitDelay: '<S16>/UnitDelay5'
934 */
935 rtDW->UnitDelay3_DSTATE_lh = rtDW->UnitDelay5_DSTATE_f;
936
937 /* Update for UnitDelay: '<S16>/UnitDelay5' incorporates:
938 * Logic: '<S16>/Logical Operator3'
939 * Relay: '<S16>/n_commDeacv'
940 */
941 rtDW->UnitDelay5_DSTATE_f = rtDW->n_commDeacv_Mode;
942
943 /* End of Outputs for SubSystem: '<S10>/Subsystem' */
944 }
945
946 /* End of If: '<S10>/If1' */
947
948 /* Switch: '<S2>/Switch' incorporates:
949 * Inport: '<Root>/b_hall_calibrate'
950 * Inport: '<Root>/open_theta'
951 * Merge: '<S11>/Merge'
952 */
953 if (rtU->b_hall_calibrate) {
954 rtb_Switch_b = (int16_T)(rtU->open_theta << 4);
955 } else {
956 rtb_Switch_b = rtb_Switch3_c;
957 }
958
959 /* End of Switch: '<S2>/Switch' */
960
961 /* Abs: '<S3>/Abs2' incorporates:
962 * Switch: '<S10>/Switch2'
963 */
964 if (rtb_Switch2_ip < 0) {
965 rtb_LogicalOperator3 = (uint16_T)((uint32_T)-rtb_Switch2_ip >> 2);
966 } else {
967 rtb_LogicalOperator3 = (uint16_T)((uint32_T)rtb_Switch2_ip >> 2);
968 }
969
970 /* End of Abs: '<S3>/Abs2' */
971
972 /* UnitDelay: '<S34>/UnitDelay' */
973 rtb_UnitDelay = rtDW->UnitDelay_DSTATE_j;
974
975 /* Outport: '<Root>/VqPrev' incorporates:
976 * UnitDelay: '<S5>/UnitDelay2'
977 */
978 rtY->VqPrev = rtDW->UnitDelay2_DSTATE_p;
979
980 /* Switch: '<S34>/Switch3' incorporates:
981 * Abs: '<S10>/Abs5'
982 * Abs: '<S34>/Abs4'
983 * Constant: '<S34>/CTRL_COMM4'
984 * Inport: '<Root>/b_motEna'
985 * Logic: '<S34>/Logical Operator1'
986 * RelationalOperator: '<S10>/Relational Operator9'
987 * RelationalOperator: '<S34>/Relational Operator7'
988 * S-Function (sfix_bitop): '<S34>/Bitwise Operator1'
989 * UnitDelay: '<S5>/UnitDelay2'
990 */
991 if ((rtb_UnitDelay & 4U) != 0U) {
992 rtb_LogicalOperator_p = true;
993 } else {
994 if (rtDW->UnitDelay2_DSTATE_p < 0) {
995 /* Abs: '<S34>/Abs4' incorporates:
996 * UnitDelay: '<S5>/UnitDelay2'
997 */
998 rtb_Divide1_fi = (int16_T)-rtDW->UnitDelay2_DSTATE_p;
999 } else {
1000 /* Abs: '<S34>/Abs4' incorporates:
1001 * UnitDelay: '<S5>/UnitDelay2'
1002 */
1003 rtb_Divide1_fi = rtDW->UnitDelay2_DSTATE_p;
1004 }
1005
1006 rtb_LogicalOperator_p = (rtU->b_motEna && (rtb_Abs5 < 12) && (rtb_Divide1_fi
1007 > 960));
1008 }
1009
1010 /* End of Switch: '<S34>/Switch3' */
1011
1012 /* Sum: '<S34>/Sum' incorporates:
1013 * Constant: '<S34>/CTRL_COMM'
1014 * Constant: '<S34>/CTRL_COMM1'
1015 * DataTypeConversion: '<S34>/Data Type Conversion3'
1016 * Gain: '<S34>/g_Hb'
1017 * Gain: '<S34>/g_Hb1'
1018 * RelationalOperator: '<S34>/Relational Operator1'
1019 * RelationalOperator: '<S34>/Relational Operator3'
1020 */
1021 rtb_DataTypeConversion1_c = (uint8_T)(((uint32_T)((rtb_Add_k == 7) << 1) +
1022 (rtb_Add_k == 0)) + (rtb_LogicalOperator_p << 2));
1023
1024 /* Outputs for Atomic SubSystem: '<S34>/Debounce_Filter' */
1025 /* RelationalOperator: '<S34>/Relational Operator2' incorporates:
1026 * Constant: '<S34>/CTRL_COMM2'
1027 * Constant: '<S34>/t_errDequal'
1028 * Constant: '<S34>/t_errQual'
1029 */
1030 Debounce_Filter(rtb_DataTypeConversion1_c != 0, 1600, 12000,
1031 &rtb_RelationalOperator4_f, &rtDW->Debounce_Filter_i);
1032
1033 /* End of Outputs for SubSystem: '<S34>/Debounce_Filter' */
1034
1035 /* Logic: '<S21>/Logical Operator12' incorporates:
1036 * Inport: '<Root>/b_motEna'
1037 * Logic: '<S21>/Logical Operator7'
1038 */
1039 rtb_n_commDeacv = ((!rtb_RelationalOperator4_f) && rtU->b_motEna);
1040
1041 /* Logic: '<S21>/Logical Operator4' incorporates:
1042 * Constant: '<S21>/constant8'
1043 * Inport: '<Root>/b_hall_calibrate'
1044 * Inport: '<Root>/n_ctrlModReq'
1045 * Logic: '<S21>/Logical Operator11'
1046 * Logic: '<S21>/Logical Operator8'
1047 * RelationalOperator: '<S21>/Relational Operator10'
1048 */
1049 rtb_LogicalOperator4 = (rtU->b_hall_calibrate || (!rtDW->Compare) ||
1050 (!rtb_n_commDeacv) || (rtU->n_ctrlModReq == 0));
1051
1052 /* Relay: '<S21>/n_SpeedCtrl' */
1053 rtDW->n_SpeedCtrl_Mode = ((rtb_LogicalOperator3 >= 300) ||
1054 ((rtb_LogicalOperator3 > 200) && rtDW->n_SpeedCtrl_Mode));
1055 rtb_LogicalOperator_p = rtDW->n_SpeedCtrl_Mode;
1056
1057 /* Logic: '<S21>/Logical Operator10' incorporates:
1058 * Inport: '<Root>/b_cruiseEna'
1059 */
1060 rtb_LogicalOperator_p = (rtb_LogicalOperator_p && rtU->b_cruiseEna);
1061
1062 /* Logic: '<S21>/Logical Operator2' incorporates:
1063 * Constant: '<S21>/constant'
1064 * Inport: '<Root>/n_ctrlModReq'
1065 * Logic: '<S21>/Logical Operator5'
1066 * RelationalOperator: '<S21>/Relational Operator4'
1067 */
1068 rtb_LogicalOperator2 = ((rtU->n_ctrlModReq == 2) && (!rtb_LogicalOperator_p));
1069
1070 /* Logic: '<S21>/Logical Operator1' incorporates:
1071 * Constant: '<S21>/constant1'
1072 * Inport: '<Root>/n_ctrlModReq'
1073 * RelationalOperator: '<S21>/Relational Operator1'
1074 */
1075 rtb_LogicalOperator_p = ((rtU->n_ctrlModReq == 1) || rtb_LogicalOperator_p);
1076
1077 /* Chart: '<S3>/Control_Mode_Manager' incorporates:
1078 * Logic: '<S21>/Logical Operator3'
1079 * Logic: '<S21>/Logical Operator6'
1080 * Logic: '<S21>/Logical Operator9'
1081 */
1082 if (rtDW->is_active_c5_PMSM_Controller == 0U) {
1083 rtDW->is_active_c5_PMSM_Controller = 1U;
1084 rtDW->is_c5_PMSM_Controller = IN_OPEN;
1085 rtb_z_ctrlMod = OPEN_MODE;
1086 } else if (rtDW->is_c5_PMSM_Controller == 1) {
1087 if (rtb_LogicalOperator4) {
1088 rtDW->is_ACTIVE = IN_NO_ACTIVE_CHILD;
1089 rtDW->is_c5_PMSM_Controller = IN_OPEN;
1090 rtb_z_ctrlMod = OPEN_MODE;
1091 } else if (rtDW->is_ACTIVE == 1) {
1092 rtb_z_ctrlMod = SPD_MODE;
1093 if (!rtb_LogicalOperator_p) {
1094 if (rtb_LogicalOperator2) {
1095 rtDW->is_ACTIVE = IN_TORQUE_MODE;
1096 rtb_z_ctrlMod = TRQ_MODE;
1097 } else {
1098 rtDW->is_ACTIVE = IN_SPEED_MODE;
1099 }
1100 }
1101 } else {
1102 /* case IN_TORQUE_MODE: */
1103 rtb_z_ctrlMod = TRQ_MODE;
1104 if (!rtb_LogicalOperator2) {
1105 rtDW->is_ACTIVE = IN_SPEED_MODE;
1106 rtb_z_ctrlMod = SPD_MODE;
1107 }
1108 }
1109 } else {
1110 /* case IN_OPEN: */
1111 rtb_z_ctrlMod = OPEN_MODE;
1112 if ((!rtb_LogicalOperator4) && (rtb_LogicalOperator2 ||
1113 rtb_LogicalOperator_p)) {
1114 rtDW->is_c5_PMSM_Controller = IN_ACTIVE;
1115 if (rtb_LogicalOperator2) {
1116 rtDW->is_ACTIVE = IN_TORQUE_MODE;
1117 rtb_z_ctrlMod = TRQ_MODE;
1118 } else {
1119 rtDW->is_ACTIVE = IN_SPEED_MODE;
1120 rtb_z_ctrlMod = SPD_MODE;
1121 }
1122 }
1123 }
1124
1125 /* End of Chart: '<S3>/Control_Mode_Manager' */
1126
1127 /* Switch: '<S22>/Switch' incorporates:
1128 * Constant: '<S22>/Constant3'
1129 * Inport: '<Root>/input_target'
1130 */
1131 if (rtU->input_target > 60) {
1132 /* Switch: '<S22>/Switch1' incorporates:
1133 * Constant: '<S22>/Constant1'
1134 * DataTypeConversion: '<S22>/Data Type Conversion'
1135 * Switch: '<S22>/Switch'
1136 */
1137 if (rtb_n_commDeacv) {
1138 rtb_Switch_oi = rtU->input_target;
1139 } else {
1140 rtb_Switch_oi = 0;
1141 }
1142
1143 /* End of Switch: '<S22>/Switch1' */
1144 } else {
1145 rtb_Switch_oi = 0;
1146 }
1147
1148 /* End of Switch: '<S22>/Switch' */
1149
1150 /* Switch: '<S22>/Switch3' incorporates:
1151 * Constant: '<S22>/Constant4'
1152 * DataTypeConversion: '<S22>/Data Type Conversion2'
1153 * Inport: '<Root>/vq_open_target'
1154 */
1155 if (rtb_n_commDeacv) {
1156 rtb_Abs5_h = rtU->vq_open_target;
1157 } else {
1158 rtb_Abs5_h = 0;
1159 }
1160
1161 /* End of Switch: '<S22>/Switch3' */
1162
1163 /* If: '<S23>/If' incorporates:
1164 * Inport: '<Root>/b_hall_calibrate'
1165 * Inport: '<S27>/vq_in'
1166 * Switch: '<S22>/Switch3'
1167 */
1168 if (rtU->b_hall_calibrate) {
1169 /* Switch: '<S22>/Switch2' incorporates:
1170 * Constant: '<S22>/Constant2'
1171 * DataTypeConversion: '<S22>/Data Type Conversion1'
1172 * Inport: '<Root>/vd_open_target'
1173 * Inport: '<S27>/vd_in'
1174 */
1175 if (rtb_n_commDeacv) {
1176 /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
1177 * ActionPort: '<S27>/Action Port'
1178 */
1179 rtDW->Merge[0] = rtU->vd_open_target;
1180
1181 /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
1182 } else {
1183 /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
1184 * ActionPort: '<S27>/Action Port'
1185 */
1186 rtDW->Merge[0] = 0;
1187
1188 /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
1189 }
1190
1191 /* End of Switch: '<S22>/Switch2' */
1192
1193 /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
1194 * ActionPort: '<S27>/Action Port'
1195 */
1196 rtDW->Merge[1] = rtb_Abs5_h;
1197
1198 /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
1199 } else if (rtb_z_ctrlMod == 0) {
1200 /* Outputs for IfAction SubSystem: '<S23>/open_mode' incorporates:
1201 * ActionPort: '<S28>/Action Port'
1202 */
1203 /* RelationalOperator: '<S28>/Equal1' incorporates:
1204 * Switch: '<S22>/Switch3'
1205 * UnitDelay: '<S28>/Unit Delay'
1206 */
1207 rtb_LogicalOperator_p = (rtDW->UnitDelay_DSTATE != rtb_Abs5_h);
1208
1209 /* If: '<S30>/If' */
1210 if (rtb_LogicalOperator_p) {
1211 /* Outputs for IfAction SubSystem: '<S30>/Subsystem' incorporates:
1212 * ActionPort: '<S32>/Action Port'
1213 */
1214 /* Sum: '<S32>/Add' incorporates:
1215 * Signum: '<S32>/Sign'
1216 * Switch: '<S22>/Switch3'
1217 * UnitDelay: '<S5>/UnitDelay2'
1218 */
1219 rtb_Divide1_fi = (int16_T)((rtb_Abs5_h - rtDW->UnitDelay2_DSTATE_p) >> 2);
1220
1221 /* Signum: '<S32>/Sign' */
1222 if (rtb_Divide1_fi < 0) {
1223 rtb_Divide1_fi = -1;
1224 } else {
1225 rtb_Divide1_fi = (int16_T)(rtb_Divide1_fi > 0);
1226 }
1227
1228 /* End of Signum: '<S32>/Sign' */
1229
1230 /* Product: '<S32>/Divide' incorporates:
1231 * Constant: '<S28>/Constant5'
1232 */
1233 rtDW->Divide = (int16_T)(rtb_Divide1_fi * 6);
1234
1235 /* Switch: '<S32>/Switch' incorporates:
1236 * Switch: '<S32>/Switch1'
1237 */
1238 if (rtb_Divide1_fi > 0) {
1239 /* Switch: '<S32>/Switch' incorporates:
1240 * Switch: '<S22>/Switch3'
1241 */
1242 rtDW->Switch = rtb_Abs5_h;
1243
1244 /* Switch: '<S32>/Switch1' incorporates:
1245 * UnitDelay: '<S5>/UnitDelay2'
1246 */
1247 rtDW->Switch1 = rtDW->UnitDelay2_DSTATE_p;
1248 } else {
1249 /* Switch: '<S32>/Switch' incorporates:
1250 * UnitDelay: '<S5>/UnitDelay2'
1251 */
1252 rtDW->Switch = rtDW->UnitDelay2_DSTATE_p;
1253
1254 /* Switch: '<S32>/Switch1' incorporates:
1255 * Switch: '<S22>/Switch3'
1256 */
1257 rtDW->Switch1 = rtb_Abs5_h;
1258 }
1259
1260 /* End of Switch: '<S32>/Switch' */
1261 /* End of Outputs for SubSystem: '<S30>/Subsystem' */
1262
1263 /* Switch: '<S33>/Switch1' incorporates:
1264 * UnitDelay: '<S5>/UnitDelay2'
1265 */
1266 rtb_Switch_dr = rtDW->UnitDelay2_DSTATE_p;
1267 } else {
1268 /* Switch: '<S33>/Switch1' incorporates:
1269 * UnitDelay: '<S33>/UnitDelay'
1270 */
1271 rtb_Switch_dr = rtDW->UnitDelay_DSTATE_d;
1272 }
1273
1274 /* End of If: '<S30>/If' */
1275
1276 /* Sum: '<S30>/Add2' incorporates:
1277 * Product: '<S32>/Divide'
1278 */
1279 sigIdx = ((rtb_Switch_dr << 1) + rtDW->Divide) >> 1;
1280 if (sigIdx > 32767) {
1281 sigIdx = 32767;
1282 } else {
1283 if (sigIdx < -32768) {
1284 sigIdx = -32768;
1285 }
1286 }
1287
1288 /* Switch: '<S28>/Switch' incorporates:
1289 * Switch: '<S22>/Switch'
1290 */
1291 if (rtb_Switch_oi > 0) {
1292 /* Switch: '<S31>/Switch2' incorporates:
1293 * RelationalOperator: '<S31>/LowerRelop1'
1294 * RelationalOperator: '<S31>/UpperRelop'
1295 * Sum: '<S30>/Add2'
1296 * Switch: '<S31>/Switch'
1297 * Switch: '<S32>/Switch'
1298 * Switch: '<S32>/Switch1'
1299 */
1300 if ((int16_T)sigIdx > rtDW->Switch) {
1301 /* Merge: '<S23>/Merge' incorporates:
1302 * Switch: '<S28>/Switch'
1303 */
1304 rtDW->Merge[1] = rtDW->Switch;
1305 } else if ((int16_T)sigIdx < rtDW->Switch1) {
1306 /* Merge: '<S23>/Merge' incorporates:
1307 * Switch: '<S28>/Switch'
1308 * Switch: '<S31>/Switch'
1309 * Switch: '<S32>/Switch1'
1310 */
1311 rtDW->Merge[1] = rtDW->Switch1;
1312 } else {
1313 /* Merge: '<S23>/Merge' incorporates:
1314 * Switch: '<S28>/Switch'
1315 */
1316 rtDW->Merge[1] = (int16_T)sigIdx;
1317 }
1318
1319 /* End of Switch: '<S31>/Switch2' */
1320 } else {
1321 /* Merge: '<S23>/Merge' incorporates:
1322 * Constant: '<S28>/Constant1'
1323 */
1324 rtDW->Merge[1] = 0;
1325 }
1326
1327 /* End of Switch: '<S28>/Switch' */
1328
1329 /* Merge: '<S23>/Merge' incorporates:
1330 * Constant: '<S28>/Constant3'
1331 * SignalConversion generated from: '<S28>/open_voltage'
1332 */
1333 rtDW->Merge[0] = 0;
1334
1335 /* Update for UnitDelay: '<S28>/Unit Delay' incorporates:
1336 * Switch: '<S22>/Switch3'
1337 */
1338 rtDW->UnitDelay_DSTATE = rtb_Abs5_h;
1339
1340 /* Switch: '<S33>/Switch2' */
1341 if (rtb_LogicalOperator_p) {
1342 /* Update for UnitDelay: '<S33>/UnitDelay' incorporates:
1343 * UnitDelay: '<S5>/UnitDelay2'
1344 */
1345 rtDW->UnitDelay_DSTATE_d = rtDW->UnitDelay2_DSTATE_p;
1346 } else {
1347 /* Update for UnitDelay: '<S33>/UnitDelay' incorporates:
1348 * Sum: '<S30>/Add2'
1349 */
1350 rtDW->UnitDelay_DSTATE_d = (int16_T)sigIdx;
1351 }
1352
1353 /* End of Switch: '<S33>/Switch2' */
1354 /* End of Outputs for SubSystem: '<S23>/open_mode' */
1355 } else {
1356 /* Outputs for IfAction SubSystem: '<S23>/torque_mode' incorporates:
1357 * ActionPort: '<S29>/Action Port'
1358 */
1359 /* Product: '<S29>/Divide1' incorporates:
1360 * Inport: '<Root>/i_dc_limit'
1361 * Inport: '<Root>/speed_limit'
1362 * Product: '<S29>/Divide4'
1363 * Switch: '<S22>/Switch'
1364 */
1365 sigIdx = ((uint16_T)((rtU->i_dc_limit << 8) / rtU->speed_limit) *
1366 rtb_Switch_oi) >> 8;
1367 if (sigIdx > 32767) {
1368 sigIdx = 32767;
1369 } else {
1370 if (sigIdx < -32768) {
1371 sigIdx = -32768;
1372 }
1373 }
1374
1375 /* Product: '<S29>/Divide1' */
1376 rtDW->Divide1 = (int16_T)sigIdx;
1377
1378 /* End of Outputs for SubSystem: '<S23>/torque_mode' */
1379 }
1380
1381 /* End of If: '<S23>/If' */
1382
1383 /* Outputs for Atomic SubSystem: '<S34>/either_edge' */
1384 rtb_LogicalOperator_p = either_edge(rtb_RelationalOperator4_f,
1385 &rtDW->either_edge_f);
1386
1387 /* End of Outputs for SubSystem: '<S34>/either_edge' */
1388
1389 /* Switch: '<S34>/Switch1' */
1390 if (rtb_LogicalOperator_p) {
1391 rtb_UnitDelay = rtb_DataTypeConversion1_c;
1392 }
1393
1394 /* End of Switch: '<S34>/Switch1' */
1395
1396 /* Gain: '<S51>/Multiply' incorporates:
1397 * DataTypeConversion: '<S54>/Data Type Conversion'
1398 * Inport: '<Root>/adc_a'
1399 * Inport: '<Root>/adc_b'
1400 */
1401 sigIdx = (12351 * rtU->adc_a) >> 11;
1402 if (sigIdx > 32767) {
1403 sigIdx = 32767;
1404 } else {
1405 if (sigIdx < -32768) {
1406 sigIdx = -32768;
1407 }
1408 }
1409
1410 rtb_DataTypeConversion[0] = (int16_T)sigIdx;
1411 tmp_1 = (12351 * rtU->adc_b) >> 11;
1412 if (tmp_1 > 32767) {
1413 tmp_1 = 32767;
1414 } else {
1415 if (tmp_1 < -32768) {
1416 tmp_1 = -32768;
1417 }
1418 }
1419
1420 rtb_DataTypeConversion[1] = (int16_T)tmp_1;
1421
1422 /* Sum: '<S45>/Add' incorporates:
1423 * Gain: '<S51>/Multiply'
1424 */
1425 tmp = (int16_T)sigIdx + (int16_T)tmp_1;
1426 if (tmp > 32767) {
1427 tmp = 32767;
1428 } else {
1429 if (tmp < -32768) {
1430 tmp = -32768;
1431 }
1432 }
1433
1434 /* Sum: '<S45>/Add1' incorporates:
1435 * Sum: '<S45>/Add'
1436 */
1437 tmp_0 = -tmp;
1438 if (-tmp > 32767) {
1439 tmp_0 = 32767;
1440 }
1441
1442 /* Sum: '<S53>/Add3' incorporates:
1443 * Gain: '<S51>/Multiply'
1444 * Sum: '<S45>/Add1'
1445 */
1446 tmp = (int16_T)tmp_1 + (int16_T)tmp_0;
1447 if (tmp > 32767) {
1448 tmp = 32767;
1449 } else {
1450 if (tmp < -32768) {
1451 tmp = -32768;
1452 }
1453 }
1454
1455 /* Sum: '<S53>/Add' incorporates:
1456 * Gain: '<S51>/Multiply'
1457 * Sum: '<S53>/Add3'
1458 */
1459 sigIdx = (((int16_T)sigIdx << 1) - tmp) >> 1;
1460 if (sigIdx > 32767) {
1461 sigIdx = 32767;
1462 } else {
1463 if (sigIdx < -32768) {
1464 sigIdx = -32768;
1465 }
1466 }
1467
1468 /* Gain: '<S53>/Gain1' incorporates:
1469 * Product: '<S55>/Divide1'
1470 * Sum: '<S53>/Add'
1471 */
1472 rtb_Divide1_fi = (int16_T)((21845 * sigIdx) >> 15);
1473
1474 /* Gain: '<S53>/Gain2' incorporates:
1475 * Gain: '<S51>/Multiply'
1476 * Sum: '<S45>/Add1'
1477 * Sum: '<S53>/Add2'
1478 */
1479 sigIdx = ((((int16_T)tmp_1 - (int16_T)tmp_0) >> 1) * 18919) >> 14;
1480 if (sigIdx > 32767) {
1481 sigIdx = 32767;
1482 } else {
1483 if (sigIdx < -32768) {
1484 sigIdx = -32768;
1485 }
1486 }
1487
1488 /* PreLookup: '<S56>/a_elecAngle_XA' incorporates:
1489 * Switch: '<S2>/Switch'
1490 */
1491 rtb_LogicalOperator3 = plook_u16s16_evencka(rtb_Switch_b, 0, 4U, 1440U);
1492
1493 /* Interpolation_n-D: '<S56>/r_cos_M1' */
1494 rtb_Switch_dr = rtConstP.r_cos_M1_Table[rtb_LogicalOperator3];
1495
1496 /* Interpolation_n-D: '<S56>/r_sin_M1' incorporates:
1497 * Product: '<S67>/Divide4'
1498 */
1499 rtb_Abs5_h = rtConstP.r_sin_M1_Table[rtb_LogicalOperator3];
1500
1501 /* Sum: '<S55>/Sum1' incorporates:
1502 * Gain: '<S53>/Gain2'
1503 * Interpolation_n-D: '<S56>/r_cos_M1'
1504 * Interpolation_n-D: '<S56>/r_sin_M1'
1505 * Product: '<S55>/Divide1'
1506 * Product: '<S55>/Divide2'
1507 * Product: '<S55>/Divide3'
1508 */
1509 tmp_1 = (int16_T)((rtb_Divide1_fi *
1510 rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) +
1511 (int16_T)(((int16_T)sigIdx * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >>
1512 14);
1513 if (tmp_1 > 32767) {
1514 tmp_1 = 32767;
1515 } else {
1516 if (tmp_1 < -32768) {
1517 tmp_1 = -32768;
1518 }
1519 }
1520
1521 /* SignalConversion generated from: '<S45>/Low_Pass_Filter' incorporates:
1522 * Sum: '<S55>/Sum1'
1523 */
1524 rtb_TmpSignalConversionAtLow_Pa[0] = (int16_T)tmp_1;
1525
1526 /* Sum: '<S55>/Sum6' incorporates:
1527 * Gain: '<S53>/Gain2'
1528 * Interpolation_n-D: '<S56>/r_cos_M1'
1529 * Interpolation_n-D: '<S56>/r_sin_M1'
1530 * Product: '<S55>/Divide1'
1531 * Product: '<S55>/Divide4'
1532 */
1533 sigIdx = (int16_T)(((int16_T)sigIdx *
1534 rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) -
1535 (int16_T)((rtb_Divide1_fi * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >>
1536 14);
1537 if (sigIdx > 32767) {
1538 sigIdx = 32767;
1539 } else {
1540 if (sigIdx < -32768) {
1541 sigIdx = -32768;
1542 }
1543 }
1544
1545 /* SignalConversion generated from: '<S45>/Low_Pass_Filter' incorporates:
1546 * Sum: '<S55>/Sum6'
1547 */
1548 rtb_TmpSignalConversionAtLow_Pa[1] = (int16_T)sigIdx;
1549
1550 /* Outputs for Atomic SubSystem: '<S45>/Low_Pass_Filter' */
1551 /* Constant: '<S45>/Constant' */
1552 Low_Pass_Filter(rtb_TmpSignalConversionAtLow_Pa, 26214, rtb_DataTypeConversion,
1553 &rtDW->Low_Pass_Filter_d);
1554
1555 /* End of Outputs for SubSystem: '<S45>/Low_Pass_Filter' */
1556
1557 /* Outport: '<Root>/VdPrev' incorporates:
1558 * UnitDelay: '<S5>/UnitDelay1'
1559 */
1560 rtY->VdPrev = rtDW->UnitDelay1_DSTATE;
1561
1562 /* Interpolation_n-D: '<S46>/Vq_max_M1' incorporates:
1563 * Abs: '<S46>/Abs5'
1564 * PreLookup: '<S46>/Vq_max_XA'
1565 * UnitDelay: '<S5>/UnitDelay1'
1566 */
1567 if (rtDW->UnitDelay1_DSTATE < 0) {
1568 rtb_Divide1_fi = (int16_T)-rtDW->UnitDelay1_DSTATE;
1569 } else {
1570 rtb_Divide1_fi = rtDW->UnitDelay1_DSTATE;
1571 }
1572
1573 rtb_Divide1_fi = rtConstP.Vq_max_M1_Table[plook_u16s16_evencka(rtb_Divide1_fi,
1574 0, 64U, 45U)];
1575
1576 /* End of Interpolation_n-D: '<S46>/Vq_max_M1' */
1577
1578 /* Product: '<S46>/Divide4' incorporates:
1579 * Inport: '<Root>/i_dc_limit'
1580 * Product: '<S24>/Divide3'
1581 */
1582 sigIdx = rtDW->Divide3 << 16;
1583 sigIdx = (sigIdx == MIN_int32_T) && (rtU->i_dc_limit == -1) ? MAX_int32_T :
1584 sigIdx / rtU->i_dc_limit;
1585 if (sigIdx < 0) {
1586 sigIdx = 0;
1587 } else {
1588 if (sigIdx > 65535) {
1589 sigIdx = 65535;
1590 }
1591 }
1592
1593 /* PreLookup: '<S46>/iq_maxSca_XA' incorporates:
1594 * Product: '<S46>/Divide4'
1595 */
1596 rtb_DataTypeConversion1_c = plook_u8u16_evencka((uint16_T)sigIdx, 0U, 1311U,
1597 49U);
1598
1599 /* Outport: '<Root>/PWM' incorporates:
1600 * Interpolation_n-D: '<S46>/iq_maxSca_M1'
1601 */
1602 rtY->PWM[6] = rtConstP.iq_maxSca_M1_Table[rtb_DataTypeConversion1_c];
1603
1604 /* Product: '<S46>/Divide1' incorporates:
1605 * Inport: '<Root>/i_dc_limit'
1606 * Interpolation_n-D: '<S46>/iq_maxSca_M1'
1607 */
1608 rtb_Divide4_c = (int16_T)
1609 ((rtConstP.iq_maxSca_M1_Table[rtb_DataTypeConversion1_c] * rtU->i_dc_limit) >>
1610 16);
1611
1612 /* Switch: '<S52>/Switch2' */
1613 rtb_Switch2_fu = (uint8_T)(rtb_z_ctrlMod != 0);
1614
1615 /* Delay: '<S80>/Delay' */
1616 rtb_RelationalOperator4_f = rtDW->Delay_DSTATE_n[0];
1617
1618 /* DataTypeConversion: '<S52>/Data Type Conversion1' incorporates:
1619 * Delay: '<S80>/Delay'
1620 * Logic: '<S52>/Logical Operator'
1621 * Logic: '<S80>/Logical Operator'
1622 * UnitDelay: '<S80>/Unit Delay'
1623 */
1624 rtb_DataTypeConversion1_c = (uint8_T)((rtb_Switch2_fu != 0) && ((boolean_T)
1625 (rtDW->UnitDelay_DSTATE_f ^ rtDW->Delay_DSTATE_n[0])));
1626
1627 /* If: '<S50>/If' incorporates:
1628 * Constant: '<S73>/Constant1'
1629 * Constant: '<S73>/Constant11'
1630 * Constant: '<S73>/Constant2'
1631 * Constant: '<S73>/Constant4'
1632 * Gain: '<S46>/Gain1'
1633 * Product: '<S46>/Divide1'
1634 * Sum: '<S73>/Add2'
1635 * Switch: '<S10>/Switch2'
1636 * Switch: '<S78>/Switch2'
1637 */
1638 if ((rtb_DataTypeConversion1_c > 0) && (rtb_z_ctrlMod == 1)) {
1639 /* Outputs for IfAction SubSystem: '<S50>/speed_mode' incorporates:
1640 * ActionPort: '<S73>/Action Port'
1641 */
1642 /* Switch: '<S75>/Switch2' incorporates:
1643 * Inport: '<Root>/speed_limit'
1644 * RelationalOperator: '<S75>/LowerRelop1'
1645 * RelationalOperator: '<S75>/UpperRelop'
1646 * Switch: '<S22>/Switch'
1647 * Switch: '<S75>/Switch'
1648 * Switch: '<S78>/Switch2'
1649 */
1650 if (rtb_Switch_oi > rtU->speed_limit) {
1651 rtb_Switch_oi = rtU->speed_limit;
1652 } else {
1653 if (rtb_Switch_oi < 0) {
1654 /* Switch: '<S75>/Switch' incorporates:
1655 * Constant: '<S73>/Constant5'
1656 * Switch: '<S78>/Switch2'
1657 */
1658 rtb_Switch_oi = 0;
1659 }
1660 }
1661
1662 /* End of Switch: '<S75>/Switch2' */
1663
1664 /* Outputs for Atomic SubSystem: '<S73>/pi_speed' */
1665 rtb_Switch_oi = pi_speed((int16_T)(rtb_Switch_oi - rtb_Switch2_ip), 3174, 10,
1666 20, rtb_Divide4_c, (int16_T)-rtb_Divide4_c, 0, rtb_Switch2_fu,
1667 &rtConstB.pi_speed_g, &rtDW->pi_speed_g, &rtPrevZCX->pi_speed_g);
1668
1669 /* End of Outputs for SubSystem: '<S73>/pi_speed' */
1670
1671 /* Merge: '<S50>/Merge' incorporates:
1672 * Constant: '<S73>/Constant1'
1673 * Constant: '<S73>/Constant11'
1674 * Constant: '<S73>/Constant2'
1675 * Constant: '<S73>/Constant4'
1676 * Gain: '<S46>/Gain1'
1677 * Product: '<S46>/Divide1'
1678 * SignalConversion generated from: '<S73>/iq_target'
1679 * Sum: '<S73>/Add2'
1680 * Switch: '<S10>/Switch2'
1681 * Switch: '<S78>/Switch2'
1682 */
1683 rtDW->Merge_b = rtb_Switch_oi;
1684
1685 /* End of Outputs for SubSystem: '<S50>/speed_mode' */
1686 } else {
1687 if ((rtb_DataTypeConversion1_c > 0) && (rtb_z_ctrlMod == 2)) {
1688 /* Outputs for IfAction SubSystem: '<S50>/torque_mode' incorporates:
1689 * ActionPort: '<S74>/Action Port'
1690 */
1691 /* Product: '<S74>/Divide' incorporates:
1692 * Constant: '<S74>/Constant2'
1693 * Sum: '<S74>/Sum2'
1694 * Switch: '<S10>/Switch2'
1695 * Switch: '<S22>/Switch'
1696 */
1697 sigIdx = ((int16_T)(rtb_Switch_oi - rtb_Switch2_ip) * 819) >> 6;
1698 if (sigIdx > 32767) {
1699 sigIdx = 32767;
1700 } else {
1701 if (sigIdx < -32768) {
1702 sigIdx = -32768;
1703 }
1704 }
1705
1706 /* Product: '<S74>/Divide1' incorporates:
1707 * Sum: '<S74>/Sum3'
1708 * Switch: '<S10>/Switch2'
1709 * Switch: '<S22>/Switch'
1710 */
1711 tmp_1 = ((int16_T)(rtb_Switch2_ip - rtb_Switch_oi) * -51) >> 5;
1712 if (tmp_1 > 32767) {
1713 tmp_1 = 32767;
1714 } else {
1715 if (tmp_1 < -32768) {
1716 tmp_1 = -32768;
1717 }
1718 }
1719
1720 rtb_Switch_oi = (int16_T)tmp_1;
1721
1722 /* End of Product: '<S74>/Divide1' */
1723
1724 /* MinMax: '<S74>/Max' incorporates:
1725 * Product: '<S74>/Divide'
1726 * Product: '<S74>/Divide1'
1727 */
1728 if ((int16_T)sigIdx > rtb_Switch_oi) {
1729 rtb_Max = (int16_T)sigIdx;
1730 } else {
1731 rtb_Max = rtb_Switch_oi;
1732 }
1733
1734 /* End of MinMax: '<S74>/Max' */
1735
1736 /* MinMax: '<S74>/Max3' incorporates:
1737 * MinMax: '<S74>/Max'
1738 * Product: '<S46>/Divide1'
1739 * Switch: '<S79>/Switch2'
1740 */
1741 if (rtb_Divide4_c < rtb_Max) {
1742 rtb_Max = rtb_Divide4_c;
1743 }
1744
1745 /* End of MinMax: '<S74>/Max3' */
1746
1747 /* Switch: '<S79>/Switch2' incorporates:
1748 * Product: '<S29>/Divide1'
1749 * RelationalOperator: '<S79>/LowerRelop1'
1750 */
1751 if (rtDW->Divide1 <= rtb_Max) {
1752 /* MinMax: '<S74>/Max1' incorporates:
1753 * Product: '<S74>/Divide'
1754 * Product: '<S74>/Divide1'
1755 */
1756 if ((int16_T)sigIdx < rtb_Switch_oi) {
1757 rtb_Switch_oi = (int16_T)sigIdx;
1758 }
1759
1760 /* End of MinMax: '<S74>/Max1' */
1761
1762 /* MinMax: '<S74>/Max2' incorporates:
1763 * Gain: '<S46>/Gain1'
1764 * MinMax: '<S74>/Max1'
1765 * Product: '<S46>/Divide1'
1766 */
1767 if (rtb_Switch_oi <= (int16_T)-rtb_Divide4_c) {
1768 rtb_Switch_oi = (int16_T)-rtb_Divide4_c;
1769 }
1770
1771 /* End of MinMax: '<S74>/Max2' */
1772
1773 /* Switch: '<S79>/Switch' incorporates:
1774 * MinMax: '<S74>/Max2'
1775 * RelationalOperator: '<S79>/UpperRelop'
1776 */
1777 if (rtDW->Divide1 < rtb_Switch_oi) {
1778 rtb_Max = rtb_Switch_oi;
1779 } else {
1780 rtb_Max = rtDW->Divide1;
1781 }
1782
1783 /* End of Switch: '<S79>/Switch' */
1784 }
1785
1786 /* End of Switch: '<S79>/Switch2' */
1787
1788 /* Merge: '<S50>/Merge' incorporates:
1789 * SignalConversion generated from: '<S74>/torque_iq'
1790 * Switch: '<S79>/Switch2'
1791 */
1792 rtDW->Merge_b = rtb_Max;
1793
1794 /* End of Outputs for SubSystem: '<S50>/torque_mode' */
1795 }
1796 }
1797
1798 /* End of If: '<S50>/If' */
1799
1800 /* If: '<S47>/If' incorporates:
1801 * Constant: '<S47>/Constant3'
1802 * Constant: '<S57>/Constant3'
1803 * Constant: '<S57>/Constant4'
1804 * Constant: '<S57>/Constant6'
1805 * Constant: '<S57>/Constant9'
1806 * Constant: '<S58>/Constant1'
1807 * Constant: '<S58>/Constant7'
1808 * Constant: '<S58>/Constant8'
1809 * Gain: '<S46>/Gain3'
1810 * Gain: '<S46>/Gain5'
1811 * If: '<S47>/If1'
1812 * Inport: '<Root>/vbus_voltage'
1813 * Interpolation_n-D: '<S46>/Vq_max_M1'
1814 * Sum: '<S57>/Add'
1815 * Sum: '<S58>/Add1'
1816 * Switch: '<S60>/Switch2'
1817 * Switch: '<S64>/Switch2'
1818 */
1819 if (rtb_Switch2_fu == 1) {
1820 /* Outputs for IfAction SubSystem: '<S47>/iq_ctrl' incorporates:
1821 * ActionPort: '<S58>/Action Port'
1822 */
1823 /* Switch: '<S64>/Switch2' incorporates:
1824 * Merge: '<S50>/Merge'
1825 * Product: '<S46>/Divide1'
1826 * RelationalOperator: '<S64>/LowerRelop1'
1827 */
1828 if (rtDW->Merge_b <= rtb_Divide4_c) {
1829 /* Switch: '<S64>/Switch' incorporates:
1830 * Gain: '<S46>/Gain1'
1831 * RelationalOperator: '<S64>/UpperRelop'
1832 * Switch: '<S64>/Switch2'
1833 */
1834 if (rtDW->Merge_b < (int16_T)-rtb_Divide4_c) {
1835 rtb_Divide4_c = (int16_T)-rtb_Divide4_c;
1836 } else {
1837 rtb_Divide4_c = rtDW->Merge_b;
1838 }
1839
1840 /* End of Switch: '<S64>/Switch' */
1841 }
1842
1843 /* End of Switch: '<S64>/Switch2' */
1844
1845 /* Outputs for Atomic SubSystem: '<S58>/PI_iq' */
1846 PI_iq((int16_T)(rtb_Divide4_c - rtb_DataTypeConversion[1]), 4096, 51, 1024,
1847 rtb_Divide1_fi, (int16_T)-rtb_Divide1_fi, 0, &rtDW->Switch2_m,
1848 &rtDW->PI_iq_g);
1849
1850 /* End of Outputs for SubSystem: '<S58>/PI_iq' */
1851 /* End of Outputs for SubSystem: '<S47>/iq_ctrl' */
1852
1853 /* Outputs for IfAction SubSystem: '<S47>/id_ctrl' incorporates:
1854 * ActionPort: '<S57>/Action Port'
1855 */
1856 /* Switch: '<S60>/Switch2' incorporates:
1857 * Constant: '<S47>/Constant3'
1858 * Constant: '<S58>/Constant1'
1859 * Constant: '<S58>/Constant7'
1860 * Constant: '<S58>/Constant8'
1861 * Gain: '<S46>/Gain4'
1862 * Gain: '<S46>/Gain5'
1863 * Inport: '<Root>/i_dc_limit'
1864 * Interpolation_n-D: '<S46>/Vq_max_M1'
1865 * Product: '<S24>/Divide3'
1866 * RelationalOperator: '<S60>/LowerRelop1'
1867 * RelationalOperator: '<S60>/UpperRelop'
1868 * Sum: '<S58>/Add1'
1869 * Switch: '<S60>/Switch'
1870 * Switch: '<S64>/Switch2'
1871 */
1872 if (rtDW->Divide3 > rtU->i_dc_limit) {
1873 rtb_Switch_oi = rtU->i_dc_limit;
1874 } else if (rtDW->Divide3 < (int16_T)-rtU->i_dc_limit) {
1875 /* Switch: '<S60>/Switch' incorporates:
1876 * Gain: '<S46>/Gain4'
1877 * Switch: '<S60>/Switch2'
1878 */
1879 rtb_Switch_oi = (int16_T)-rtU->i_dc_limit;
1880 } else {
1881 rtb_Switch_oi = rtDW->Divide3;
1882 }
1883
1884 /* End of Switch: '<S60>/Switch2' */
1885
1886 /* Outputs for Atomic SubSystem: '<S57>/PI_id' */
1887 PI_id((int16_T)(rtb_Switch_oi - rtb_DataTypeConversion[0]), 4096, 51, 1024,
1888 rtU->vbus_voltage, (int16_T)-rtU->vbus_voltage, 0, &rtDW->Switch2,
1889 &rtDW->PI_id_b);
1890
1891 /* End of Outputs for SubSystem: '<S57>/PI_id' */
1892 /* End of Outputs for SubSystem: '<S47>/id_ctrl' */
1893 }
1894
1895 /* End of If: '<S47>/If' */
1896
1897 /* Switch: '<S5>/Switch1' incorporates:
1898 * Switch: '<S5>/Switch'
1899 * Switch: '<S62>/Switch2'
1900 * Switch: '<S66>/Switch2'
1901 */
1902 if (rtb_z_ctrlMod != 0) {
1903 rtb_Switch_oi = rtDW->Switch2_m;
1904 rtb_Divide1_fi = rtDW->Switch2;
1905 } else {
1906 rtb_Switch_oi = rtDW->Merge[1];
1907 rtb_Divide1_fi = rtDW->Merge[0];
1908 }
1909
1910 /* End of Switch: '<S5>/Switch1' */
1911
1912 /* Sum: '<S48>/Sum1' incorporates:
1913 * Interpolation_n-D: '<S56>/r_cos_M1'
1914 * Product: '<S48>/Divide2'
1915 * Product: '<S48>/Divide3'
1916 * Product: '<S67>/Divide4'
1917 * Switch: '<S5>/Switch'
1918 * Switch: '<S5>/Switch1'
1919 */
1920 sigIdx = (int16_T)((rtb_Divide1_fi * rtb_Abs5_h) >> 14) + (int16_T)
1921 ((rtb_Switch_oi * rtb_Switch_dr) >> 14);
1922 if (sigIdx > 32767) {
1923 sigIdx = 32767;
1924 } else {
1925 if (sigIdx < -32768) {
1926 sigIdx = -32768;
1927 }
1928 }
1929
1930 /* Sum: '<S48>/Sum6' incorporates:
1931 * Interpolation_n-D: '<S56>/r_cos_M1'
1932 * Product: '<S48>/Divide1'
1933 * Product: '<S48>/Divide4'
1934 * Product: '<S67>/Divide4'
1935 * Switch: '<S5>/Switch'
1936 * Switch: '<S5>/Switch1'
1937 */
1938 tmp_1 = (int16_T)((rtb_Divide1_fi * rtb_Switch_dr) >> 14) - (int16_T)
1939 ((rtb_Switch_oi * rtb_Abs5_h) >> 14);
1940 if (tmp_1 > 32767) {
1941 tmp_1 = 32767;
1942 } else {
1943 if (tmp_1 < -32768) {
1944 tmp_1 = -32768;
1945 }
1946 }
1947
1948 /* Product: '<S67>/Divide3' incorporates:
1949 * Constant: '<S67>/Constant1'
1950 * Product: '<S67>/Divide'
1951 * Sum: '<S48>/Sum6'
1952 */
1953 rtb_Switch_dr = (int16_T)((3547 * (int16_T)tmp_1) >> 12);
1954
1955 /* Product: '<S67>/Divide2' incorporates:
1956 * Constant: '<S67>/Constant'
1957 * Sum: '<S48>/Sum1'
1958 */
1959 rtb_Max = (int16_T)((3547 * (int16_T)sigIdx) >> 12);
1960
1961 /* Product: '<S67>/Divide4' incorporates:
1962 * Constant: '<S67>/Constant2'
1963 * Product: '<S67>/Divide2'
1964 */
1965 rtb_Abs5_h = (int16_T)((2365 * rtb_Max) >> 12);
1966
1967 /* Sum: '<S67>/Add' incorporates:
1968 * Product: '<S67>/Divide'
1969 * Product: '<S67>/Divide4'
1970 */
1971 rtb_Gain1 = (int16_T)((rtb_Switch_dr + rtb_Abs5_h) >> 1);
1972
1973 /* Sum: '<S67>/Add1' incorporates:
1974 * Product: '<S67>/Divide'
1975 * Product: '<S67>/Divide4'
1976 */
1977 rtb_Gain4 = (int16_T)((rtb_Abs5_h - rtb_Switch_dr) >> 1);
1978
1979 /* Product: '<S67>/Divide7' incorporates:
1980 * Constant: '<S67>/Constant3'
1981 * Sum: '<S48>/Sum1'
1982 */
1983 rtb_Abs5_h = (int16_T)((2365 * (int16_T)sigIdx) >> 12);
1984
1985 /* MATLAB Function: '<S67>/sector_select' incorporates:
1986 * Product: '<S67>/Divide7'
1987 * Sum: '<S48>/Sum1'
1988 * Sum: '<S48>/Sum6'
1989 */
1990 if ((int16_T)sigIdx >= 0) {
1991 if ((int16_T)tmp_1 >= 0) {
1992 if (rtb_Abs5_h > (int16_T)tmp_1) {
1993 /* DataTypeConversion: '<S67>/Data Type Conversion' */
1994 rtb_DataTypeConversion1_c = 2U;
1995 } else {
1996 /* DataTypeConversion: '<S67>/Data Type Conversion' */
1997 rtb_DataTypeConversion1_c = 1U;
1998 }
1999 } else if (-rtb_Abs5_h > (int16_T)tmp_1) {
2000 /* DataTypeConversion: '<S67>/Data Type Conversion' */
2001 rtb_DataTypeConversion1_c = 3U;
2002 } else {
2003 /* DataTypeConversion: '<S67>/Data Type Conversion' */
2004 rtb_DataTypeConversion1_c = 2U;
2005 }
2006 } else if ((int16_T)tmp_1 >= 0) {
2007 if (-rtb_Abs5_h > (int16_T)tmp_1) {
2008 /* DataTypeConversion: '<S67>/Data Type Conversion' */
2009 rtb_DataTypeConversion1_c = 5U;
2010 } else {
2011 /* DataTypeConversion: '<S67>/Data Type Conversion' */
2012 rtb_DataTypeConversion1_c = 6U;
2013 }
2014 } else if (rtb_Abs5_h > (int16_T)tmp_1) {
2015 /* DataTypeConversion: '<S67>/Data Type Conversion' */
2016 rtb_DataTypeConversion1_c = 4U;
2017 } else {
2018 /* DataTypeConversion: '<S67>/Data Type Conversion' */
2019 rtb_DataTypeConversion1_c = 5U;
2020 }
2021
2022 /* End of MATLAB Function: '<S67>/sector_select' */
2023
2024 /* Product: '<S67>/Divide' incorporates:
2025 * Inport: '<Root>/vbus_voltage'
2026 */
2027 rtb_Switch_dr = (int16_T)div_nde_s32_floor(40960000, rtU->vbus_voltage);
2028
2029 /* Product: '<S67>/Divide1' incorporates:
2030 * Product: '<S67>/Divide'
2031 * Product: '<S67>/Divide2'
2032 * Product: '<S67>/Divide8'
2033 */
2034 rtb_Abs5_h = (int16_T)((((2365 * rtb_Max) >> 13) * rtb_Switch_dr) >> 10);
2035
2036 /* Product: '<S67>/Divide5' incorporates:
2037 * Product: '<S67>/Divide'
2038 * Sum: '<S67>/Add'
2039 */
2040 rtb_Divide4_c = (int16_T)((rtb_Gain1 * rtb_Switch_dr) >> 11);
2041
2042 /* Product: '<S67>/Divide6' incorporates:
2043 * Product: '<S67>/Divide'
2044 * Sum: '<S67>/Add1'
2045 */
2046 rtb_Gain1 = (int16_T)((rtb_Gain4 * rtb_Switch_dr) >> 11);
2047
2048 /* MATLAB Function: '<S67>/phase_time' incorporates:
2049 * DataTypeConversion: '<S67>/Data Type Conversion1'
2050 */
2051 switch ((int8_T)rtb_DataTypeConversion1_c) {
2052 case 1:
2053 sigIdx = -rtb_Gain1;
2054 if (-rtb_Gain1 > 32767) {
2055 sigIdx = 32767;
2056 }
2057
2058 tmp_1 = 10000 - (int16_T)sigIdx;
2059 if (10000 - (int16_T)sigIdx > 32767) {
2060 tmp_1 = 32767;
2061 }
2062
2063 tmp_1 -= rtb_Abs5_h;
2064 if (tmp_1 > 32767) {
2065 tmp_1 = 32767;
2066 } else {
2067 if (tmp_1 < -32768) {
2068 tmp_1 = -32768;
2069 }
2070 }
2071
2072 rtb_Switch_dr = (int16_T)rt_roundd_snf((real_T)tmp_1 / 4.0);
2073 rtb_Abs5_h = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_Abs5_h / 2.0) +
2074 rtb_Switch_dr);
2075 sigIdx = (int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx / 2.0) + rtb_Abs5_h;
2076 if (sigIdx > 32767) {
2077 sigIdx = 32767;
2078 } else {
2079 if (sigIdx < -32768) {
2080 sigIdx = -32768;
2081 }
2082 }
2083
2084 rtb_Divide4_c = (int16_T)sigIdx;
2085 break;
2086
2087 case 2:
2088 sigIdx = 10000 - rtb_Divide4_c;
2089 if (10000 - rtb_Divide4_c > 32767) {
2090 sigIdx = 32767;
2091 }
2092
2093 sigIdx -= rtb_Gain1;
2094 if (sigIdx > 32767) {
2095 sigIdx = 32767;
2096 } else {
2097 if (sigIdx < -32768) {
2098 sigIdx = -32768;
2099 }
2100 }
2101
2102 rtb_Switch_dr = (int16_T)rt_roundd_snf((real_T)sigIdx / 4.0);
2103 rtb_Divide4_c = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_Divide4_c / 2.0)
2104 + rtb_Switch_dr);
2105 sigIdx = (int32_T)rt_roundd_snf((real_T)rtb_Gain1 / 2.0) + rtb_Divide4_c;
2106 if (sigIdx > 32767) {
2107 sigIdx = 32767;
2108 } else {
2109 if (sigIdx < -32768) {
2110 sigIdx = -32768;
2111 }
2112 }
2113
2114 rtb_Abs5_h = (int16_T)sigIdx;
2115 break;
2116
2117 case 3:
2118 sigIdx = -rtb_Divide4_c;
2119 if (-rtb_Divide4_c > 32767) {
2120 sigIdx = 32767;
2121 }
2122
2123 tmp_1 = 10000 - rtb_Abs5_h;
2124 if (10000 - rtb_Abs5_h > 32767) {
2125 tmp_1 = 32767;
2126 }
2127
2128 tmp_1 -= (int16_T)sigIdx;
2129 if (tmp_1 > 32767) {
2130 tmp_1 = 32767;
2131 } else {
2132 if (tmp_1 < -32768) {
2133 tmp_1 = -32768;
2134 }
2135 }
2136
2137 rtb_Divide4_c = (int16_T)rt_roundd_snf((real_T)tmp_1 / 4.0);
2138 rtb_Switch_dr = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx /
2139 2.0) + rtb_Divide4_c);
2140 sigIdx = (int32_T)rt_roundd_snf((real_T)rtb_Abs5_h / 2.0) + rtb_Switch_dr;
2141 if (sigIdx > 32767) {
2142 sigIdx = 32767;
2143 } else {
2144 if (sigIdx < -32768) {
2145 sigIdx = -32768;
2146 }
2147 }
2148
2149 rtb_Abs5_h = (int16_T)sigIdx;
2150 break;
2151
2152 case 4:
2153 sigIdx = -rtb_Abs5_h;
2154 if (-rtb_Abs5_h > 32767) {
2155 sigIdx = 32767;
2156 }
2157
2158 tmp_1 = 10000 - (int16_T)sigIdx;
2159 if (10000 - (int16_T)sigIdx > 32767) {
2160 tmp_1 = 32767;
2161 }
2162
2163 tmp_1 -= rtb_Gain1;
2164 if (tmp_1 > 32767) {
2165 tmp_1 = 32767;
2166 } else {
2167 if (tmp_1 < -32768) {
2168 tmp_1 = -32768;
2169 }
2170 }
2171
2172 rtb_Divide4_c = (int16_T)rt_roundd_snf((real_T)tmp_1 / 4.0);
2173 rtb_Abs5_h = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_Gain1 / 2.0) +
2174 rtb_Divide4_c);
2175 sigIdx = (int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx / 2.0) + rtb_Abs5_h;
2176 if (sigIdx > 32767) {
2177 sigIdx = 32767;
2178 } else {
2179 if (sigIdx < -32768) {
2180 sigIdx = -32768;
2181 }
2182 }
2183
2184 rtb_Switch_dr = (int16_T)sigIdx;
2185 break;
2186
2187 case 5:
2188 sigIdx = -rtb_Divide4_c;
2189 if (-rtb_Divide4_c > 32767) {
2190 sigIdx = 32767;
2191 }
2192
2193 tmp_1 = -rtb_Gain1;
2194 if (-rtb_Gain1 > 32767) {
2195 tmp_1 = 32767;
2196 }
2197
2198 tmp = 10000 - (int16_T)sigIdx;
2199 if (10000 - (int16_T)sigIdx > 32767) {
2200 tmp = 32767;
2201 }
2202
2203 tmp -= (int16_T)tmp_1;
2204 if (tmp > 32767) {
2205 tmp = 32767;
2206 } else {
2207 if (tmp < -32768) {
2208 tmp = -32768;
2209 }
2210 }
2211
2212 rtb_Abs5_h = (int16_T)rt_roundd_snf((real_T)tmp / 4.0);
2213 rtb_Divide4_c = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)tmp_1 /
2214 2.0) + rtb_Abs5_h);
2215 sigIdx = (int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx / 2.0) +
2216 rtb_Divide4_c;
2217 if (sigIdx > 32767) {
2218 sigIdx = 32767;
2219 } else {
2220 if (sigIdx < -32768) {
2221 sigIdx = -32768;
2222 }
2223 }
2224
2225 rtb_Switch_dr = (int16_T)sigIdx;
2226 break;
2227
2228 default:
2229 sigIdx = -rtb_Abs5_h;
2230 if (-rtb_Abs5_h > 32767) {
2231 sigIdx = 32767;
2232 }
2233
2234 tmp_1 = 10000 - rtb_Divide4_c;
2235 if (10000 - rtb_Divide4_c > 32767) {
2236 tmp_1 = 32767;
2237 }
2238
2239 tmp_1 -= (int16_T)sigIdx;
2240 if (tmp_1 > 32767) {
2241 tmp_1 = 32767;
2242 } else {
2243 if (tmp_1 < -32768) {
2244 tmp_1 = -32768;
2245 }
2246 }
2247
2248 rtb_Abs5_h = (int16_T)rt_roundd_snf((real_T)tmp_1 / 4.0);
2249 rtb_Switch_dr = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx /
2250 2.0) + rtb_Abs5_h);
2251 sigIdx = (int32_T)rt_roundd_snf((real_T)rtb_Divide4_c / 2.0) + rtb_Switch_dr;
2252 if (sigIdx > 32767) {
2253 sigIdx = 32767;
2254 } else {
2255 if (sigIdx < -32768) {
2256 sigIdx = -32768;
2257 }
2258 }
2259
2260 rtb_Divide4_c = (int16_T)sigIdx;
2261 break;
2262 }
2263
2264 /* Switch: '<S69>/Switch2' incorporates:
2265 * Constant: '<S67>/Constant6'
2266 * MATLAB Function: '<S67>/phase_time'
2267 * RelationalOperator: '<S69>/LowerRelop1'
2268 * RelationalOperator: '<S69>/UpperRelop'
2269 * Switch: '<S69>/Switch'
2270 */
2271 if (rtb_Divide4_c > 5000) {
2272 rtb_LogicalOperator3 = 5000U;
2273 } else if (rtb_Divide4_c < 0) {
2274 /* Switch: '<S69>/Switch' incorporates:
2275 * Constant: '<S67>/Constant5'
2276 */
2277 rtb_LogicalOperator3 = 0U;
2278 } else {
2279 rtb_LogicalOperator3 = (uint16_T)rtb_Divide4_c;
2280 }
2281
2282 if (rtb_Abs5_h > 5000) {
2283 rtb_Switch2_idx_1 = 5000U;
2284 } else if (rtb_Abs5_h < 0) {
2285 /* Switch: '<S69>/Switch' incorporates:
2286 * Constant: '<S67>/Constant5'
2287 */
2288 rtb_Switch2_idx_1 = 0U;
2289 } else {
2290 rtb_Switch2_idx_1 = (uint16_T)rtb_Abs5_h;
2291 }
2292
2293 if (rtb_Switch_dr > 5000) {
2294 rtb_Switch2_idx_2 = 5000U;
2295 } else if (rtb_Switch_dr < 0) {
2296 /* Switch: '<S69>/Switch' incorporates:
2297 * Constant: '<S67>/Constant5'
2298 */
2299 rtb_Switch2_idx_2 = 0U;
2300 } else {
2301 rtb_Switch2_idx_2 = (uint16_T)rtb_Switch_dr;
2302 }
2303
2304 /* End of Switch: '<S69>/Switch2' */
2305
2306 /* MultiPortSwitch: '<S68>/Multiport Switch' */
2307 switch (rtb_DataTypeConversion1_c) {
2308 case 1:
2309 rtb_MultiportSwitch_idx_0 = rtb_LogicalOperator3;
2310 rtb_MultiportSwitch_idx_1 = rtb_Switch2_idx_1;
2311 break;
2312
2313 case 2:
2314 rtb_MultiportSwitch_idx_0 = rtb_Switch2_idx_1;
2315 rtb_MultiportSwitch_idx_1 = rtb_LogicalOperator3;
2316 break;
2317
2318 case 3:
2319 rtb_MultiportSwitch_idx_0 = rtb_Switch2_idx_1;
2320 rtb_MultiportSwitch_idx_1 = rtb_Switch2_idx_2;
2321 break;
2322
2323 case 4:
2324 rtb_MultiportSwitch_idx_0 = rtb_Switch2_idx_2;
2325 rtb_MultiportSwitch_idx_1 = rtb_Switch2_idx_1;
2326 break;
2327
2328 case 5:
2329 rtb_MultiportSwitch_idx_0 = rtb_Switch2_idx_2;
2330 rtb_MultiportSwitch_idx_1 = rtb_LogicalOperator3;
2331 break;
2332
2333 default:
2334 rtb_MultiportSwitch_idx_0 = rtb_LogicalOperator3;
2335 rtb_MultiportSwitch_idx_1 = rtb_Switch2_idx_2;
2336 break;
2337 }
2338
2339 /* End of MultiPortSwitch: '<S68>/Multiport Switch' */
2340
2341 /* Outport: '<Root>/PWM' incorporates:
2342 * Constant: '<S68>/Constant'
2343 * Constant: '<S68>/Constant1'
2344 * Constant: '<S68>/Constant2'
2345 * Constant: '<S68>/Constant3'
2346 * MATLAB Function: '<S68>/Shunt_Three_Sample_Point'
2347 * Outport: '<Root>/sector'
2348 */
2349 Shunt_Three_Sample_Point(rtb_MultiportSwitch_idx_0, rtb_MultiportSwitch_idx_1,
2350 rtb_DataTypeConversion1_c, 5000, 10, 10, 10, &rtY->PWM[6], &rtY->PWM[7],
2351 &rtY->sector);
2352
2353 /* Update for Delay: '<S7>/Delay' incorporates:
2354 * Inport: '<Root>/hall_a'
2355 */
2356 rtDW->Delay_DSTATE = rtU->hall_a;
2357
2358 /* Update for Delay: '<S7>/Delay1' incorporates:
2359 * Inport: '<Root>/hall_b'
2360 */
2361 rtDW->Delay1_DSTATE = rtU->hall_b;
2362
2363 /* Update for Delay: '<S7>/Delay2' incorporates:
2364 * Inport: '<Root>/hall_c'
2365 */
2366 rtDW->Delay2_DSTATE = rtU->hall_c;
2367
2368 /* Update for UnitDelay: '<S10>/UnitDelay3' incorporates:
2369 * Inport: '<Root>/hw_count'
2370 */
2371 rtDW->UnitDelay3_DSTATE = rtU->hw_count;
2372
2373 /* Update for UnitDelay: '<S10>/UnitDelay4' incorporates:
2374 * Abs: '<S10>/Abs5'
2375 */
2376 rtDW->UnitDelay4_DSTATE = rtb_Abs5;
2377
2378 /* Update for UnitDelay: '<S34>/UnitDelay' */
2379 rtDW->UnitDelay_DSTATE_j = rtb_UnitDelay;
2380
2381 /* Update for UnitDelay: '<S5>/UnitDelay2' incorporates:
2382 * Switch: '<S5>/Switch1'
2383 */
2384 rtDW->UnitDelay2_DSTATE_p = rtb_Switch_oi;
2385
2386 /* Update for UnitDelay: '<S5>/UnitDelay1' incorporates:
2387 * Switch: '<S5>/Switch'
2388 */
2389 rtDW->UnitDelay1_DSTATE = rtb_Divide1_fi;
2390
2391 /* Update for UnitDelay: '<S80>/Unit Delay' incorporates:
2392 * Delay: '<S80>/Delay'
2393 */
2394 rtDW->UnitDelay_DSTATE_f = rtDW->Delay_DSTATE_n[0];
2395
2396 /* Update for Delay: '<S80>/Delay' incorporates:
2397 * Logic: '<S80>/Logical Operator1'
2398 */
2399 for (sigIdx = 0; sigIdx < 19; sigIdx++) {
2400 rtDW->Delay_DSTATE_n[sigIdx] = rtDW->Delay_DSTATE_n[sigIdx + 1];
2401 }
2402
2403 rtDW->Delay_DSTATE_n[19] = !rtb_RelationalOperator4_f;
2404
2405 /* End of Update for Delay: '<S80>/Delay' */
2406 /* End of Outputs for SubSystem: '<Root>/PMSM_Controller' */
2407
2408 /* Outport: '<Root>/PWM' */
2409 rtY->PWM[0] = rtb_LogicalOperator3;
2410 rtY->PWM[3] = rtb_LogicalOperator3;
2411 rtY->PWM[1] = rtb_Switch2_idx_1;
2412 rtY->PWM[4] = rtb_Switch2_idx_1;
2413 rtY->PWM[2] = rtb_Switch2_idx_2;
2414 rtY->PWM[5] = rtb_Switch2_idx_2;
2415
2416 /* Outport: '<Root>/n_MotError' */
2417 rtY->n_MotError = rtb_UnitDelay;
2418
2419 /* Outport: '<Root>/iq' */
2420 rtY->iq = rtb_DataTypeConversion[1];
2421
2422 /* Outport: '<Root>/id' */
2423 rtY->id = rtb_DataTypeConversion[0];
2424
2425 /* Outport: '<Root>/angle' incorporates:
2426 * Switch: '<S2>/Switch'
2427 */
2428 rtY->angle = rtb_Switch_b;
2429
2430 /* Outport: '<Root>/rpm' incorporates:
2431 * Switch: '<S10>/Switch2'
2432 */
2433 rtY->rpm = rtb_Switch2_ip;
2434
2435 /* Outport: '<Root>/hall_angle' incorporates:
2436 * Merge: '<S11>/Merge'
2437 */
2438 rtY->hall_angle = rtb_Switch3_c;
2439
2440 /* Outport: '<Root>/hall_state' */
2441 rtY->hall_state = rtb_Add_k;
2442}
2443
2444/* Model initialize function */
2445void PMSM_Controller_initialize(RT_MODEL *const rtM)
2446{
2447 DW *rtDW = rtM->dwork;
2448 PrevZCX *rtPrevZCX = rtM->prevZCSigState;
2449
2450 {
2451 int32_T i;
2452 rtPrevZCX->pi_speed_g.ResettableDelay_Reset_ZCE = POS_ZCSIG;
2453
2454 /* SystemInitialize for Atomic SubSystem: '<Root>/PMSM_Controller' */
2455 /* InitializeConditions for Delay: '<S80>/Delay' */
2456 for (i = 0; i < 20; i++) {
2457 rtDW->Delay_DSTATE_n[i] = true;
2458 }
2459
2460 /* End of InitializeConditions for Delay: '<S80>/Delay' */
2461
2462 /* SystemInitialize for IfAction SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
2463 /* SystemInitialize for Outport: '<S15>/z_counter' incorporates:
2464 * Inport: '<S15>/z_counterRawPrev'
2465 */
2466 rtDW->z_counterRawPrev = 200000U;
2467
2468 /* End of SystemInitialize for SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
2469
2470 /* SystemInitialize for Atomic SubSystem: '<S34>/Debounce_Filter' */
2471 Debounce_Filter_Init(&rtDW->Debounce_Filter_i);
2472
2473 /* End of SystemInitialize for SubSystem: '<S34>/Debounce_Filter' */
2474
2475 /* SystemInitialize for IfAction SubSystem: '<S50>/speed_mode' */
2476 /* SystemInitialize for Atomic SubSystem: '<S73>/pi_speed' */
2477 pi_speed_Init(&rtDW->pi_speed_g);
2478
2479 /* End of SystemInitialize for SubSystem: '<S73>/pi_speed' */
2480 /* End of SystemInitialize for SubSystem: '<S50>/speed_mode' */
2481 /* End of SystemInitialize for SubSystem: '<Root>/PMSM_Controller' */
2482 }
2483}
2484
2485/*
2486 * File trailer for generated code.
2487 *
2488 * [EOF]
2489 */
2490