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