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