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