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