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