1/*
2 * PMSM_Motor_TL3_sf.c
3 *
4 * Code generation for model "PMSM_Motor_TL3_sf".
5 *
6 * Model version : 1.825
7 * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020
8 * C source code generated on : Fri Apr 14 12:51:02 2023
9 *
10 * Target selection: rtwsfcn.tlc
11 * Note: GRT includes extra infrastructure and instrumentation for prototyping
12 * Embedded hardware selection: ARM Compatible->ARM Cortex-M
13 * Emulation hardware selection:
14 * Differs from embedded hardware (MATLAB Host)
15 * Code generation objectives:
16 * 1. Execution efficiency
17 * 2. RAM efficiency
18 * Validation result: Not run
19 */
20
21#include <math.h>
22#include "PMSM_Motor_TL3_sf.h"
23#include "PMSM_Motor_TL3_sf_private.h"
24#include "simstruc.h"
25#include "fixedpoint.h"
26#if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE)
27
28extern void *PMSM_Motor_TL3_malloc(SimStruct *S);
29
30#endif
31
32#ifndef __RTW_UTFREE__
33#if defined (MATLAB_MEX_FILE)
34
35extern void * utMalloc(size_t);
36extern void utFree(void *);
37
38#endif
39#endif /* #ifndef __RTW_UTFREE__ */
40
41/* Forward declaration for local functions */
42static real_T PMSM_Motor_TL3_rt_atan2d_snf(real_T u0, real_T u1);
43
44#if defined(MATLAB_MEX_FILE)
45#include "rt_nonfinite.c"
46#endif
47
48static const char_T *RT_MEMORY_ALLOCATION_ERROR =
49 "memory allocation error in generated S-Function";
50static real_T PMSM_Motor_TL3_rt_atan2d_snf(real_T u0, real_T u1)
51{
52 real_T y;
53 int32_T u0_0;
54 int32_T u1_0;
55 if (rtIsNaN(u0) || rtIsNaN(u1)) {
56 y = (rtNaN);
57 } else if (rtIsInf(u0) && rtIsInf(u1)) {
58 if (u0 > 0.0) {
59 u0_0 = 1;
60 } else {
61 u0_0 = -1;
62 }
63
64 if (u1 > 0.0) {
65 u1_0 = 1;
66 } else {
67 u1_0 = -1;
68 }
69
70 y = atan2(u0_0, u1_0);
71 } else if (u1 == 0.0) {
72 if (u0 > 0.0) {
73 y = RT_PI / 2.0;
74 } else if (u0 < 0.0) {
75 y = -(RT_PI / 2.0);
76 } else {
77 y = 0.0;
78 }
79 } else {
80 y = atan2(u0, u1);
81 }
82
83 return y;
84}
85
86/* System initialize for root system: '<Root>' */
87#define MDL_INITIALIZE_CONDITIONS
88
89static void mdlInitializeConditions(SimStruct *S)
90{
91 if (ssIsFirstInitCond(S)) {
92 B_PMSM_Motor_TL3_T *_rtB;
93 _rtB = ((B_PMSM_Motor_TL3_T *) ssGetLocalBlockIO(S));
94
95 /* InitializeConditions for DiscreteIntegrator: '<S11>/Discrete-Time Integrator1' */
96 ((real_T *)ssGetDWork(S, 0))[0] = 1.5707963267948966;
97
98 /* InitializeConditions for DiscreteIntegrator: '<S18>/Discrete-Time Integrator' */
99 ((real_T *)ssGetDWork(S, 1))[0] = 0.0;
100
101 /* InitializeConditions for DiscreteIntegrator: '<S17>/Discrete-Time Integrator' */
102 ((real_T *)ssGetDWork(S, 2))[0] = 0.0;
103
104 /* InitializeConditions for S-Function (sfun_spssw_discc): '<S20>/State-Space' incorporates:
105 * Constant: '<S19>/Constant'
106 */
107 {
108 int32_T i, j;
109 real_T *Ds = (real_T*)((void**) ssGetDWork(S, 4))[3];
110
111 /* Copy and transpose D */
112 for (i=0; i<9; i++) {
113 for (j=0; j<9; j++)
114 Ds[i*9 + j] = (PMSM_Motor_TL3_ConstP.StateSpace_DS_param[i + j*9]);
115 }
116
117 {
118 /* Switches work vectors */
119 int_T *switch_status = (int_T*) ((void**) ssGetDWork(S, 4))[9];
120 int_T *gState = (int_T*)((void**) ssGetDWork(S, 4))[12];
121 real_T *yswitch = (real_T*)((void**) ssGetDWork(S, 4))[19];
122 int_T *switchTypes = (int_T*)((void**) ssGetDWork(S, 4))[20];
123 int_T *idxOutSw = (int_T*)((void**) ssGetDWork(S, 4))[21];
124 int_T *switch_status_init = (int_T*) ((void**) ssGetDWork(S, 4))[10];
125
126 /* Initialize work vectors */
127 switch_status[0] = 0;
128 switch_status_init[0] = 0;
129 gState[0] = (int_T) 0.0;
130 yswitch[0] = 1/0.001;
131 switchTypes[0] = (int_T)7.0;
132 idxOutSw[0] = ((int_T)0.0) - 1;
133 switch_status[1] = 0;
134 switch_status_init[1] = 0;
135 gState[1] = (int_T) 0.0;
136 yswitch[1] = 1/0.001;
137 switchTypes[1] = (int_T)7.0;
138 idxOutSw[1] = ((int_T)0.0) - 1;
139 switch_status[2] = 0;
140 switch_status_init[2] = 0;
141 gState[2] = (int_T) 0.0;
142 yswitch[2] = 1/0.001;
143 switchTypes[2] = (int_T)7.0;
144 idxOutSw[2] = ((int_T)0.0) - 1;
145 switch_status[3] = 0;
146 switch_status_init[3] = 0;
147 gState[3] = (int_T) 0.0;
148 yswitch[3] = 1/0.001;
149 switchTypes[3] = (int_T)7.0;
150 idxOutSw[3] = ((int_T)0.0) - 1;
151 switch_status[4] = 0;
152 switch_status_init[4] = 0;
153 gState[4] = (int_T) 0.0;
154 yswitch[4] = 1/0.001;
155 switchTypes[4] = (int_T)7.0;
156 idxOutSw[4] = ((int_T)0.0) - 1;
157 switch_status[5] = 0;
158 switch_status_init[5] = 0;
159 gState[5] = (int_T) 0.0;
160 yswitch[5] = 1/0.001;
161 switchTypes[5] = (int_T)7.0;
162 idxOutSw[5] = ((int_T)0.0) - 1;
163 }
164 }
165
166 /* InitializeConditions for UnitDelay: '<S4>/Unit Delay' */
167 ((real_T *)ssGetDWork(S, 3))[0] = 0.0;
168 } else {
169 B_PMSM_Motor_TL3_T *_rtB;
170 _rtB = ((B_PMSM_Motor_TL3_T *) ssGetLocalBlockIO(S));
171
172 /* InitializeConditions for DiscreteIntegrator: '<S11>/Discrete-Time Integrator1' */
173 ((real_T *)ssGetDWork(S, 0))[0] = 1.5707963267948966;
174
175 /* InitializeConditions for DiscreteIntegrator: '<S18>/Discrete-Time Integrator' */
176 ((real_T *)ssGetDWork(S, 1))[0] = 0.0;
177
178 /* InitializeConditions for DiscreteIntegrator: '<S17>/Discrete-Time Integrator' */
179 ((real_T *)ssGetDWork(S, 2))[0] = 0.0;
180
181 /* InitializeConditions for S-Function (sfun_spssw_discc): '<S20>/State-Space' incorporates:
182 * Constant: '<S19>/Constant'
183 */
184 {
185 int32_T i, j;
186 real_T *Ds = (real_T*)((void**) ssGetDWork(S, 4))[3];
187
188 /* Copy and transpose D */
189 for (i=0; i<9; i++) {
190 for (j=0; j<9; j++)
191 Ds[i*9 + j] = (PMSM_Motor_TL3_ConstP.StateSpace_DS_param[i + j*9]);
192 }
193
194 {
195 /* Switches work vectors */
196 int_T *switch_status = (int_T*) ((void**) ssGetDWork(S, 4))[9];
197 int_T *gState = (int_T*)((void**) ssGetDWork(S, 4))[12];
198 real_T *yswitch = (real_T*)((void**) ssGetDWork(S, 4))[19];
199 int_T *switchTypes = (int_T*)((void**) ssGetDWork(S, 4))[20];
200 int_T *idxOutSw = (int_T*)((void**) ssGetDWork(S, 4))[21];
201 int_T *switch_status_init = (int_T*) ((void**) ssGetDWork(S, 4))[10];
202
203 /* Initialize work vectors */
204 switch_status[0] = 0;
205 switch_status_init[0] = 0;
206 gState[0] = (int_T) 0.0;
207 yswitch[0] = 1/0.001;
208 switchTypes[0] = (int_T)7.0;
209 idxOutSw[0] = ((int_T)0.0) - 1;
210 switch_status[1] = 0;
211 switch_status_init[1] = 0;
212 gState[1] = (int_T) 0.0;
213 yswitch[1] = 1/0.001;
214 switchTypes[1] = (int_T)7.0;
215 idxOutSw[1] = ((int_T)0.0) - 1;
216 switch_status[2] = 0;
217 switch_status_init[2] = 0;
218 gState[2] = (int_T) 0.0;
219 yswitch[2] = 1/0.001;
220 switchTypes[2] = (int_T)7.0;
221 idxOutSw[2] = ((int_T)0.0) - 1;
222 switch_status[3] = 0;
223 switch_status_init[3] = 0;
224 gState[3] = (int_T) 0.0;
225 yswitch[3] = 1/0.001;
226 switchTypes[3] = (int_T)7.0;
227 idxOutSw[3] = ((int_T)0.0) - 1;
228 switch_status[4] = 0;
229 switch_status_init[4] = 0;
230 gState[4] = (int_T) 0.0;
231 yswitch[4] = 1/0.001;
232 switchTypes[4] = (int_T)7.0;
233 idxOutSw[4] = ((int_T)0.0) - 1;
234 switch_status[5] = 0;
235 switch_status_init[5] = 0;
236 gState[5] = (int_T) 0.0;
237 yswitch[5] = 1/0.001;
238 switchTypes[5] = (int_T)7.0;
239 idxOutSw[5] = ((int_T)0.0) - 1;
240 }
241 }
242
243 /* InitializeConditions for UnitDelay: '<S4>/Unit Delay' */
244 ((real_T *)ssGetDWork(S, 3))[0] = 0.0;
245 }
246}
247
248/* Start for root system: '<Root>' */
249#define MDL_START
250
251static void mdlStart(SimStruct *S)
252{
253 /* instance underlying S-Function data */
254#if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE)
255#if defined(MATLAB_MEX_FILE)
256
257 /* non-finites */
258 rt_InitInfAndNaN(sizeof(real_T));
259
260#endif
261
262 PMSM_Motor_TL3_malloc(S);
263 if (ssGetErrorStatus(S) != (NULL) ) {
264 return;
265 }
266
267#endif
268
269 {
270 B_PMSM_Motor_TL3_T *_rtB;
271 _rtB = ((B_PMSM_Motor_TL3_T *) ssGetLocalBlockIO(S));
272
273 /* Start for S-Function (sfun_spssw_discc): '<S20>/State-Space' incorporates:
274 * Constant: '<S19>/Constant'
275 */
276
277 /* S-Function block: <S20>/State-Space */
278 {
279 ((void**) ssGetDWork(S, 4))[3] = (real_T*)calloc(9 * 9, sizeof(real_T));
280 ((void**) ssGetDWork(S, 4))[4] = (real_T*)calloc(9, sizeof(real_T));
281 ((void**) ssGetDWork(S, 4))[7] = (real_T*)calloc(9, sizeof(real_T));
282 ((void**) ssGetDWork(S, 4))[9] = (int_T*)calloc(6, sizeof(int_T));
283 ((void**) ssGetDWork(S, 4))[11] = (int_T*)calloc(6, sizeof(int_T));
284 ((void**) ssGetDWork(S, 4))[12] = (int_T*)calloc(6, sizeof(int_T));
285 ((void**) ssGetDWork(S, 4))[19] = (real_T*)calloc(6, sizeof(real_T));
286 ((void**) ssGetDWork(S, 4))[20] = (int_T*)calloc(6, sizeof(int_T));
287 ((void**) ssGetDWork(S, 4))[21] = (int_T*)calloc(6, sizeof(int_T));
288 ((void**) ssGetDWork(S, 4))[10] = (int_T*)calloc(6, sizeof(int_T));
289 ((void**) ssGetDWork(S, 4))[13] = (real_T*)calloc(6, sizeof(real_T));
290 }
291 }
292}
293
294/* Outputs for root system: '<Root>' */
295static void mdlOutputs(SimStruct *S, int_T tid)
296{
297 B_PMSM_Motor_TL3_T *_rtB;
298 real_T rtb_DataTypeConversion1;
299 real_T rtb_DataTypeConversion2;
300 real_T rtb_ElementaryMath_o1_tmp;
301 real_T rtb_ElementaryMath_o2_tmp;
302 _rtB = ((B_PMSM_Motor_TL3_T *) ssGetLocalBlockIO(S));
303
304 /* Trigonometry: '<S13>/Elementary Math' incorporates:
305 * DiscreteIntegrator: '<S11>/Discrete-Time Integrator1'
306 * Trigonometry: '<S16>/Trigonometric Function'
307 */
308 rtb_ElementaryMath_o1_tmp = sin(((real_T *)ssGetDWork(S, 0))[0]);
309 rtb_ElementaryMath_o2_tmp = cos(((real_T *)ssGetDWork(S, 0))[0]);
310
311 /* Fcn: '<S15>/Fcn' incorporates:
312 * DiscreteIntegrator: '<S17>/Discrete-Time Integrator'
313 * DiscreteIntegrator: '<S18>/Discrete-Time Integrator'
314 * Trigonometry: '<S13>/Elementary Math'
315 */
316 _rtB->Fcn = ((real_T *)ssGetDWork(S, 1))[0] * rtb_ElementaryMath_o2_tmp +
317 ((real_T *)ssGetDWork(S, 2))[0] * rtb_ElementaryMath_o1_tmp;
318
319 /* Fcn: '<S15>/Fcn1' incorporates:
320 * DiscreteIntegrator: '<S17>/Discrete-Time Integrator'
321 * DiscreteIntegrator: '<S18>/Discrete-Time Integrator'
322 * Trigonometry: '<S13>/Elementary Math'
323 */
324 _rtB->Fcn1 = ((-((real_T *)ssGetDWork(S, 1))[0] - 1.7320508075688772 *
325 ((real_T *)ssGetDWork(S, 2))[0]) * rtb_ElementaryMath_o2_tmp +
326 (1.7320508075688772 * ((real_T *)ssGetDWork(S, 1))[0] - ((real_T
327 *)ssGetDWork(S, 2))[0]) * rtb_ElementaryMath_o1_tmp) * 0.5;
328
329 /* S-Function (sfun_spssw_discc): '<S20>/State-Space' incorporates:
330 * Constant: '<S19>/Constant'
331 */
332
333 /* S-Function block: <S20>/State-Space */
334 {
335 real_T accum;
336
337 /* Circuit has switches */
338 int_T *switch_status = (int_T*) ((void**) ssGetDWork(S, 4))[9];
339 int_T *switch_status_init = (int_T*) ((void**) ssGetDWork(S, 4))[10];
340 int_T *SwitchChange = (int_T*) ((void**) ssGetDWork(S, 4))[11];
341 int_T *gState = (int_T*) ((void**) ssGetDWork(S, 4))[12];
342 real_T *yswitch = (real_T*)((void**) ssGetDWork(S, 4))[19];
343 int_T *switchTypes = (int_T*) ((void**) ssGetDWork(S, 4))[20];
344 int_T *idxOutSw = (int_T*) ((void**) ssGetDWork(S, 4))[21];
345 real_T *DxCol = (real_T*)((void**) ssGetDWork(S, 4))[4];
346 real_T *tmp2 = (real_T*)((void**) ssGetDWork(S, 4))[7];
347 real_T *uswlast = (real_T*)((void**) ssGetDWork(S, 4))[13];
348 int_T newState;
349 int_T swChanged = 0;
350 int loopsToDo = 20;
351 real_T temp;
352
353 /* keep an initial copy of switch_status*/
354 memcpy(switch_status_init, switch_status, 6 * sizeof(int_T));
355 memcpy(uswlast, &_rtB->StateSpace_o1[0], 6*sizeof(real_T));
356 do {
357 if (loopsToDo == 1) { /* Need to reset some variables: */
358 swChanged = 0;
359
360 /* return to the original switch status*/
361 {
362 int_T i1;
363 for (i1=0; i1 < 6; i1++) {
364 swChanged = ((SwitchChange[i1] = switch_status_init[i1] -
365 switch_status[i1]) != 0) ? 1 : swChanged;
366 switch_status[i1] = switch_status_init[i1];
367 }
368 }
369 } else {
370 /*
371 * Compute outputs:
372 * ---------------
373 */
374 real_T *Ds = (real_T*)((void**) ssGetDWork(S, 4))[3];
375
376 {
377 int_T i1;
378 real_T *y0 = &_rtB->StateSpace_o1[0];
379 for (i1=0; i1 < 9; i1++) {
380 accum = 0.0;
381
382 {
383 int_T i2;
384 const real_T *u0 = PMSM_Motor_TL3_ConstP.Constant_Value;
385 for (i2=0; i2 < 6; i2++) {
386 accum += *(Ds++) * u0[i2];
387 }
388
389 accum += *(Ds++) * _rtB->Fcn;
390 accum += *(Ds++) * _rtB->Fcn1;
391 accum += *(Ds++) * (*((const real_T**)ssGetInputPortSignalPtrs(S,
392 2)))[0];
393 }
394
395 y0[i1] = accum;
396 }
397 }
398
399 swChanged = 0;
400
401 {
402 int_T i1;
403 real_T *y0 = &_rtB->StateSpace_o1[0];
404 for (i1=0; i1 < 6; i1++) {
405 newState = ((y0[i1] > 0.0) && (gState[i1] > 0)) || (y0[i1] < 0.0) ?
406 1 : (((y0[i1] > 0.0) && gState[i1] == 0) ? 0 : switch_status[i1]);
407 swChanged = ((SwitchChange[i1] = newState - switch_status[i1]) != 0)
408 ? 1 : swChanged;
409 switch_status[i1] = newState;/* Keep new state */
410 }
411 }
412 }
413
414 /*
415 * Compute new As, Bs, Cs and Ds matrixes:
416 * --------------------------------------
417 */
418 if (swChanged) {
419 real_T *Ds = (real_T*)((void**) ssGetDWork(S, 4))[3];
420 real_T a1;
421
422 {
423 int_T i1;
424 for (i1=0; i1 < 6; i1++) {
425 if (SwitchChange[i1] != 0) {
426 a1 = 1000.0*SwitchChange[i1];
427 temp = 1/(1-Ds[i1*10]*a1);
428
429 {
430 int_T i2;
431 for (i2=0; i2 < 9; i2++) {
432 DxCol[i2]= Ds[i2 * 9 + i1]*temp*a1;
433 }
434 }
435
436 DxCol[i1] = temp;
437
438 /* Copy row nSw of Ds into tmp2 and zero it out in Ds */
439 memcpy(tmp2, &Ds[i1 * 9], 9 * sizeof(real_T));
440 memset(&Ds[i1 * 9], '\0', 9 * sizeof(real_T));
441
442 /* Cs = Cs + DxCol * tmp1, Ds = Ds + DxCol * tmp2 *******************/
443 {
444 int_T i2;
445 for (i2=0; i2 < 9; i2++) {
446 a1 = DxCol[i2];
447
448 {
449 int_T i3;
450 for (i3=0; i3 < 9; i3++) {
451 Ds[i2 * 9 + i3] += a1 * tmp2[i3];
452 }
453 }
454 }
455 }
456 }
457 }
458 }
459 } /* if (swChanged) */
460 } while (swChanged > 0 && --loopsToDo > 0);
461
462 if (loopsToDo == 0) {
463 real_T *Ds = (real_T*)((void**) ssGetDWork(S, 4))[3];
464
465 {
466 int_T i1;
467 real_T *y0 = &_rtB->StateSpace_o1[0];
468 for (i1=0; i1 < 9; i1++) {
469 accum = 0.0;
470
471 {
472 int_T i2;
473 const real_T *u0 = PMSM_Motor_TL3_ConstP.Constant_Value;
474 for (i2=0; i2 < 6; i2++) {
475 accum += *(Ds++) * u0[i2];
476 }
477
478 accum += *(Ds++) * _rtB->Fcn;
479 accum += *(Ds++) * _rtB->Fcn1;
480 accum += *(Ds++) * (*((const real_T**)ssGetInputPortSignalPtrs(S, 2)))
481 [0];
482 }
483
484 y0[i1] = accum;
485 }
486 }
487 }
488
489 /* Output new switches states */
490 {
491 int_T i1;
492 real_T *y1 = &_rtB->StateSpace_o2[0];
493 for (i1=0; i1 < 6; i1++) {
494 y1[i1] = (real_T)switch_status[i1];
495 }
496 }
497 }
498
499 /* Fcn: '<S13>/Fcn3' incorporates:
500 * Fcn: '<S13>/Fcn2'
501 * Trigonometry: '<S13>/Elementary Math'
502 */
503 rtb_DataTypeConversion2 = 2.0 * _rtB->StateSpace_o1[6] + _rtB->StateSpace_o1[7];
504 rtb_DataTypeConversion1 = (rtb_DataTypeConversion2 * rtb_ElementaryMath_o1_tmp
505 + -1.7320508075688772 * _rtB->StateSpace_o1[7] * rtb_ElementaryMath_o2_tmp) *
506 0.33333333333333331;
507
508 /* Fcn: '<S13>/Fcn2' incorporates:
509 * Trigonometry: '<S13>/Elementary Math'
510 */
511 rtb_DataTypeConversion2 = (rtb_DataTypeConversion2 * rtb_ElementaryMath_o2_tmp
512 + 1.7320508075688772 * _rtB->StateSpace_o1[7] * rtb_ElementaryMath_o1_tmp) *
513 0.33333333333333331;
514
515 /* Outport: '<Root>/Stator voltage Vs_q (V)' */
516 ((real_T *)ssGetOutputPortSignal(S, 5))[0] = rtb_DataTypeConversion2;
517
518 /* Gain: '<S11>/Gain' */
519 _rtB->Gain = 4.0 * *((const real_T **)ssGetInputPortSignalPtrs(S, 1))[0];
520
521 /* Sum: '<S18>/Sum1' incorporates:
522 * DiscreteIntegrator: '<S17>/Discrete-Time Integrator'
523 * DiscreteIntegrator: '<S18>/Discrete-Time Integrator'
524 * Gain: '<S18>/1//Lq'
525 * Gain: '<S18>/R//Lq'
526 * Gain: '<S18>/lam//Lq'
527 * Product: '<S18>/Product1'
528 */
529 _rtB->Sum1 = ((5617.9775280898875 * rtb_DataTypeConversion2 -
530 61.797752808988761 * ((real_T *)ssGetDWork(S, 1))[0]) -
531 ((real_T *)ssGetDWork(S, 2))[0] * _rtB->Gain) -
532 67.415730337078656 * _rtB->Gain;
533
534 /* Sum: '<S17>/Sum' incorporates:
535 * DiscreteIntegrator: '<S17>/Discrete-Time Integrator'
536 * DiscreteIntegrator: '<S18>/Discrete-Time Integrator'
537 * Gain: '<S17>/1//Ld'
538 * Gain: '<S17>/R//Ld'
539 * Product: '<S17>/Product'
540 */
541 _rtB->Sum = (5617.9775280898875 * rtb_DataTypeConversion1 - 61.797752808988761
542 * ((real_T *)ssGetDWork(S, 2))[0]) + _rtB->Gain * ((real_T *)
543 ssGetDWork(S, 1))[0];
544
545 /* Outport: '<Root>/Stator voltage Vs_d (V)' */
546 ((real_T *)ssGetOutputPortSignal(S, 6))[0] = rtb_DataTypeConversion1;
547
548 /* Sum: '<S4>/Add1' incorporates:
549 * Constant: '<S4>/Filter_Constant'
550 * Constant: '<S4>/One'
551 * Product: '<S4>/Product'
552 * Product: '<S4>/Product1'
553 * UnitDelay: '<S4>/Unit Delay'
554 */
555 _rtB->Add1 = _rtB->StateSpace_o1[8] * 0.001 + 0.999 * ((real_T *)ssGetDWork(S,
556 3))[0];
557
558 /* Outport: '<Root>/Stator current is_b (A)' */
559 ((real_T *)ssGetOutputPortSignal(S, 1))[0] = _rtB->Fcn1;
560
561 /* Outport: '<Root>/Stator current is_c (A)' incorporates:
562 * Sum: '<S15>/Sum'
563 */
564 ((real_T *)ssGetOutputPortSignal(S, 2))[0] = (0.0 - _rtB->Fcn1) - _rtB->Fcn;
565
566 /* Outport: '<Root>/Stator current is_a (A)' */
567 ((real_T *)ssGetOutputPortSignal(S, 0))[0] = _rtB->Fcn;
568
569 /* Outport: '<Root>/Stator current is_d (A)' incorporates:
570 * DiscreteIntegrator: '<S17>/Discrete-Time Integrator'
571 */
572 ((real_T *)ssGetOutputPortSignal(S, 4))[0] = ((real_T *)ssGetDWork(S, 2))[0];
573
574 /* Outport: '<Root>/Electromagnetic torque Te (N*m)' incorporates:
575 * DiscreteIntegrator: '<S17>/Discrete-Time Integrator'
576 * DiscreteIntegrator: '<S18>/Discrete-Time Integrator'
577 * Fcn: '<S9>/Te '
578 */
579 ((real_T *)ssGetOutputPortSignal(S, 12))[0] = (0.0 * ((real_T *)ssGetDWork(S,
580 1))[0] * ((real_T *)ssGetDWork(S, 2))[0] + 0.012 * ((real_T *)ssGetDWork(S,
581 1))[0]) * 6.0;
582
583 /* Outport: '<Root>/Stator current is_q (A)' incorporates:
584 * DiscreteIntegrator: '<S18>/Discrete-Time Integrator'
585 */
586 ((real_T *)ssGetOutputPortSignal(S, 3))[0] = ((real_T *)ssGetDWork(S, 1))[0];
587
588 /* Outport: '<Root>/Rotor angle thetam (rad)' incorporates:
589 * DiscreteIntegrator: '<S11>/Discrete-Time Integrator1'
590 * Fcn: '<S11>/Fcn'
591 */
592 ((real_T *)ssGetOutputPortSignal(S, 11))[0] = (((real_T *)ssGetDWork(S, 0))[0]
593 - 1.5707963267948966) / 4.0;
594
595 /* Gain: '<S16>/rad2deg' incorporates:
596 * Trigonometry: '<S16>/Trigonometric Function2'
597 */
598 rtb_DataTypeConversion2 = 57.295779513082323 * PMSM_Motor_TL3_rt_atan2d_snf
599 (rtb_ElementaryMath_o1_tmp, rtb_ElementaryMath_o2_tmp);
600
601 /* Outport: '<Root>/Hall effect signal h_a' incorporates:
602 * Constant: '<S12>/Constant'
603 * Constant: '<S12>/Constant1'
604 * DataTypeConversion: '<S12>/Data Type Conversion'
605 * Logic: '<S12>/Logical Operator'
606 * RelationalOperator: '<S12>/Relational Operator1'
607 * RelationalOperator: '<S12>/Relational Operator2'
608 */
609 ((real_T *)ssGetOutputPortSignal(S, 7))[0] = ((rtb_DataTypeConversion2 >=
610 -60.0) && (rtb_DataTypeConversion2 <= 120.0));
611
612 /* Outport: '<Root>/Hall effect signal h_b' incorporates:
613 * Constant: '<S12>/Constant2'
614 * Constant: '<S12>/Constant3'
615 * DataTypeConversion: '<S12>/Data Type Conversion1'
616 * Logic: '<S12>/Logical Operator1'
617 * RelationalOperator: '<S12>/Relational Operator3'
618 * RelationalOperator: '<S12>/Relational Operator4'
619 */
620 ((real_T *)ssGetOutputPortSignal(S, 8))[0] = ((rtb_DataTypeConversion2 >= 60.0)
621 || (rtb_DataTypeConversion2 <= -120.0));
622
623 /* Outport: '<Root>/Hall effect signal h_c' incorporates:
624 * Constant: '<S12>/Constant4'
625 * Constant: '<S12>/Constant5'
626 * DataTypeConversion: '<S12>/Data Type Conversion2'
627 * Logic: '<S12>/Logical Operator2'
628 * RelationalOperator: '<S12>/Relational Operator5'
629 * RelationalOperator: '<S12>/Relational Operator6'
630 */
631 ((real_T *)ssGetOutputPortSignal(S, 9))[0] = ((rtb_DataTypeConversion2 >=
632 -180.0) && (rtb_DataTypeConversion2 <= 0.0));
633
634 /* Outport: '<Root>/Rotor speed wm (rad//s)' */
635 ((real_T *)ssGetOutputPortSignal(S, 10))[0] = *((const real_T **)
636 ssGetInputPortSignalPtrs(S, 1))[0];
637 UNUSED_PARAMETER(tid);
638}
639
640/* Update for root system: '<Root>' */
641#define MDL_UPDATE
642
643static void mdlUpdate(SimStruct *S, int_T tid)
644{
645 B_PMSM_Motor_TL3_T *_rtB;
646 _rtB = ((B_PMSM_Motor_TL3_T *) ssGetLocalBlockIO(S));
647
648 /* Update for DiscreteIntegrator: '<S11>/Discrete-Time Integrator1' */
649 ((real_T *)ssGetDWork(S, 0))[0] = 5.0E-7 * _rtB->Gain + ((real_T *)ssGetDWork
650 (S, 0))[0];
651
652 /* Update for DiscreteIntegrator: '<S18>/Discrete-Time Integrator' */
653 ((real_T *)ssGetDWork(S, 1))[0] = 5.0E-7 * _rtB->Sum1 + ((real_T *)ssGetDWork
654 (S, 1))[0];
655
656 /* Update for DiscreteIntegrator: '<S17>/Discrete-Time Integrator' */
657 ((real_T *)ssGetDWork(S, 2))[0] = 5.0E-7 * _rtB->Sum + ((real_T *)ssGetDWork(S,
658 2))[0];
659
660 /* Update for S-Function (sfun_spssw_discc): '<S20>/State-Space' incorporates:
661 * Constant: '<S19>/Constant'
662 */
663 {
664 int_T *gState = (int_T*)((void**) ssGetDWork(S, 4))[12];
665
666 /* Store switch gates values for next step */
667 *(gState++) = (int_T) (*(const real_T **)ssGetInputPortSignalPtrs(S, 0))[0];
668 *(gState++) = (int_T) (*(const real_T **)ssGetInputPortSignalPtrs(S, 0))[1];
669 *(gState++) = (int_T) (*(const real_T **)ssGetInputPortSignalPtrs(S, 0))[2];
670 *(gState++) = (int_T) (*(const real_T **)ssGetInputPortSignalPtrs(S, 0))[3];
671 *(gState++) = (int_T) (*(const real_T **)ssGetInputPortSignalPtrs(S, 0))[4];
672 *(gState++) = (int_T) (*(const real_T **)ssGetInputPortSignalPtrs(S, 0))[5];
673 }
674
675 /* Update for UnitDelay: '<S4>/Unit Delay' */
676 ((real_T *)ssGetDWork(S, 3))[0] = _rtB->Add1;
677 UNUSED_PARAMETER(tid);
678}
679
680/* Termination for root system: '<Root>' */
681static void mdlTerminate(SimStruct *S)
682{
683 B_PMSM_Motor_TL3_T *_rtB;
684 _rtB = ((B_PMSM_Motor_TL3_T *) ssGetLocalBlockIO(S));
685
686 /* Terminate for S-Function (sfun_spssw_discc): '<S20>/State-Space' incorporates:
687 * Constant: '<S19>/Constant'
688 */
689
690 /* S-Function block: <S20>/State-Space */
691 {
692 /* Free memory */
693 free(((void**) ssGetDWork(S, 4))[3]);
694 free(((void**) ssGetDWork(S, 4))[4]);
695 free(((void**) ssGetDWork(S, 4))[7]);
696
697 /*
698 * Circuit has switches*/
699 free(((void**) ssGetDWork(S, 4))[12]);
700 free(((void**) ssGetDWork(S, 4))[9]);
701 free(((void**) ssGetDWork(S, 4))[11]);
702 free(((void**) ssGetDWork(S, 4))[10]);
703 }
704
705#if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE)
706
707 if (ssGetUserData(S) != (NULL) ) {
708 rt_FREE(ssGetLocalBlockIO(S));
709 }
710
711 rt_FREE(ssGetUserData(S));
712
713#endif
714
715}
716
717#if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE)
718#include "PMSM_Motor_TL3_mid.h"
719#endif
720
721/* Function to initialize sizes. */
722static void mdlInitializeSizes(SimStruct *S)
723{
724 ssSetNumSampleTimes(S, 1); /* Number of sample times */
725 ssSetNumContStates(S, 0); /* Number of continuous states */
726 ssSetNumNonsampledZCs(S, 0); /* Number of nonsampled ZCs */
727
728 /* Number of output ports */
729 if (!ssSetNumOutputPorts(S, 13))
730 return;
731
732 /* outport number: 0 */
733 if (!ssSetOutputPortVectorDimension(S, 0, 1))
734 return;
735 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
736 ssSetOutputPortDataType(S, 0, SS_DOUBLE);
737 }
738
739 ssSetOutputPortSampleTime(S, 0, 5.0E-7);
740 ssSetOutputPortOffsetTime(S, 0, 0.0);
741 ssSetOutputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
742
743 /* outport number: 1 */
744 if (!ssSetOutputPortVectorDimension(S, 1, 1))
745 return;
746 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
747 ssSetOutputPortDataType(S, 1, SS_DOUBLE);
748 }
749
750 ssSetOutputPortSampleTime(S, 1, 5.0E-7);
751 ssSetOutputPortOffsetTime(S, 1, 0.0);
752 ssSetOutputPortOptimOpts(S, 1, SS_REUSABLE_AND_LOCAL);
753
754 /* outport number: 2 */
755 if (!ssSetOutputPortVectorDimension(S, 2, 1))
756 return;
757 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
758 ssSetOutputPortDataType(S, 2, SS_DOUBLE);
759 }
760
761 ssSetOutputPortSampleTime(S, 2, 5.0E-7);
762 ssSetOutputPortOffsetTime(S, 2, 0.0);
763 ssSetOutputPortOptimOpts(S, 2, SS_REUSABLE_AND_LOCAL);
764
765 /* outport number: 3 */
766 if (!ssSetOutputPortVectorDimension(S, 3, 1))
767 return;
768 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
769 ssSetOutputPortDataType(S, 3, SS_DOUBLE);
770 }
771
772 ssSetOutputPortSampleTime(S, 3, 5.0E-7);
773 ssSetOutputPortOffsetTime(S, 3, 0.0);
774 ssSetOutputPortOptimOpts(S, 3, SS_REUSABLE_AND_LOCAL);
775
776 /* outport number: 4 */
777 if (!ssSetOutputPortVectorDimension(S, 4, 1))
778 return;
779 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
780 ssSetOutputPortDataType(S, 4, SS_DOUBLE);
781 }
782
783 ssSetOutputPortSampleTime(S, 4, 5.0E-7);
784 ssSetOutputPortOffsetTime(S, 4, 0.0);
785 ssSetOutputPortOptimOpts(S, 4, SS_REUSABLE_AND_LOCAL);
786
787 /* outport number: 5 */
788 if (!ssSetOutputPortVectorDimension(S, 5, 1))
789 return;
790 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
791 ssSetOutputPortDataType(S, 5, SS_DOUBLE);
792 }
793
794 ssSetOutputPortSampleTime(S, 5, 5.0E-7);
795 ssSetOutputPortOffsetTime(S, 5, 0.0);
796 ssSetOutputPortOptimOpts(S, 5, SS_REUSABLE_AND_LOCAL);
797
798 /* outport number: 6 */
799 if (!ssSetOutputPortVectorDimension(S, 6, 1))
800 return;
801 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
802 ssSetOutputPortDataType(S, 6, SS_DOUBLE);
803 }
804
805 ssSetOutputPortSampleTime(S, 6, 5.0E-7);
806 ssSetOutputPortOffsetTime(S, 6, 0.0);
807 ssSetOutputPortOptimOpts(S, 6, SS_REUSABLE_AND_LOCAL);
808
809 /* outport number: 7 */
810 if (!ssSetOutputPortVectorDimension(S, 7, 1))
811 return;
812 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
813 ssSetOutputPortDataType(S, 7, SS_DOUBLE);
814 }
815
816 ssSetOutputPortSampleTime(S, 7, 5.0E-7);
817 ssSetOutputPortOffsetTime(S, 7, 0.0);
818 ssSetOutputPortOptimOpts(S, 7, SS_REUSABLE_AND_LOCAL);
819
820 /* outport number: 8 */
821 if (!ssSetOutputPortVectorDimension(S, 8, 1))
822 return;
823 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
824 ssSetOutputPortDataType(S, 8, SS_DOUBLE);
825 }
826
827 ssSetOutputPortSampleTime(S, 8, 5.0E-7);
828 ssSetOutputPortOffsetTime(S, 8, 0.0);
829 ssSetOutputPortOptimOpts(S, 8, SS_REUSABLE_AND_LOCAL);
830
831 /* outport number: 9 */
832 if (!ssSetOutputPortVectorDimension(S, 9, 1))
833 return;
834 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
835 ssSetOutputPortDataType(S, 9, SS_DOUBLE);
836 }
837
838 ssSetOutputPortSampleTime(S, 9, 5.0E-7);
839 ssSetOutputPortOffsetTime(S, 9, 0.0);
840 ssSetOutputPortOptimOpts(S, 9, SS_REUSABLE_AND_LOCAL);
841
842 /* outport number: 10 */
843 if (!ssSetOutputPortVectorDimension(S, 10, 1))
844 return;
845 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
846 ssSetOutputPortDataType(S, 10, SS_DOUBLE);
847 }
848
849 ssSetOutputPortSampleTime(S, 10, 5.0E-7);
850 ssSetOutputPortOffsetTime(S, 10, 0.0);
851 ssSetOutputPortOptimOpts(S, 10, SS_REUSABLE_AND_LOCAL);
852
853 /* outport number: 11 */
854 if (!ssSetOutputPortVectorDimension(S, 11, 1))
855 return;
856 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
857 ssSetOutputPortDataType(S, 11, SS_DOUBLE);
858 }
859
860 ssSetOutputPortSampleTime(S, 11, 5.0E-7);
861 ssSetOutputPortOffsetTime(S, 11, 0.0);
862 ssSetOutputPortOptimOpts(S, 11, SS_REUSABLE_AND_LOCAL);
863
864 /* outport number: 12 */
865 if (!ssSetOutputPortVectorDimension(S, 12, 1))
866 return;
867 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
868 ssSetOutputPortDataType(S, 12, SS_DOUBLE);
869 }
870
871 ssSetOutputPortSampleTime(S, 12, 5.0E-7);
872 ssSetOutputPortOffsetTime(S, 12, 0.0);
873 ssSetOutputPortOptimOpts(S, 12, SS_REUSABLE_AND_LOCAL);
874
875 /* Number of input ports */
876 if (!ssSetNumInputPorts(S, 3))
877 return;
878
879 /* inport number: 0 */
880 {
881 if (!ssSetInputPortVectorDimension(S, 0, 6))
882 return;
883 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
884 ssSetInputPortDataType(S, 0, SS_DOUBLE);
885 }
886
887 ssSetInputPortSampleTime(S, 0, 5.0E-7);
888 ssSetInputPortOffsetTime(S, 0, 0.0);
889 ssSetInputPortOverWritable(S, 0, 0);
890 ssSetInputPortOptimOpts(S, 0, SS_NOT_REUSABLE_AND_GLOBAL);
891 }
892
893 /* inport number: 1 */
894 {
895 if (!ssSetInputPortVectorDimension(S, 1, 1))
896 return;
897 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
898 ssSetInputPortDataType(S, 1, SS_DOUBLE);
899 }
900
901 ssSetInputPortDirectFeedThrough(S, 1, 1);
902 ssSetInputPortSampleTime(S, 1, 5.0E-7);
903 ssSetInputPortOffsetTime(S, 1, 0.0);
904 ssSetInputPortOverWritable(S, 1, 0);
905 ssSetInputPortOptimOpts(S, 1, SS_NOT_REUSABLE_AND_GLOBAL);
906 }
907
908 /* inport number: 2 */
909 {
910 if (!ssSetInputPortVectorDimension(S, 2, 1))
911 return;
912 if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
913 ssSetInputPortDataType(S, 2, SS_DOUBLE);
914 }
915
916 ssSetInputPortDirectFeedThrough(S, 2, 1);
917 ssSetInputPortSampleTime(S, 2, 5.0E-7);
918 ssSetInputPortOffsetTime(S, 2, 0.0);
919 ssSetInputPortOverWritable(S, 2, 0);
920 ssSetInputPortOptimOpts(S, 2, SS_NOT_REUSABLE_AND_GLOBAL);
921 }
922
923 ssSetRTWGeneratedSFcn(S, 1); /* Generated S-function */
924
925 /* DWork */
926 if (!ssSetNumDWork(S, 6)) {
927 return;
928 }
929
930 /* '<S11>/Discrete-Time Integrator1': DSTATE */
931 ssSetDWorkName(S, 0, "DWORK0");
932 ssSetDWorkWidth(S, 0, 1);
933 ssSetDWorkUsedAsDState(S, 0, 1);
934
935 /* '<S18>/Discrete-Time Integrator': DSTATE */
936 ssSetDWorkName(S, 1, "DWORK1");
937 ssSetDWorkWidth(S, 1, 1);
938 ssSetDWorkUsedAsDState(S, 1, 1);
939
940 /* '<S17>/Discrete-Time Integrator': DSTATE */
941 ssSetDWorkName(S, 2, "DWORK2");
942 ssSetDWorkWidth(S, 2, 1);
943 ssSetDWorkUsedAsDState(S, 2, 1);
944
945 /* '<S4>/Unit Delay': DSTATE */
946 ssSetDWorkName(S, 3, "DWORK3");
947 ssSetDWorkWidth(S, 3, 1);
948 ssSetDWorkUsedAsDState(S, 3, 1);
949
950 /* '<S20>/State-Space': PWORK */
951 ssSetDWorkName(S, 4, "DWORK4");
952 ssSetDWorkWidth(S, 4, 24);
953 ssSetDWorkDataType(S, 4, SS_POINTER);
954
955 /* '<S20>/State-Space': IWORK */
956 ssSetDWorkName(S, 5, "DWORK5");
957 ssSetDWorkWidth(S, 5, 11);
958 ssSetDWorkDataType(S, 5, SS_INTEGER);
959
960 /* Tunable Parameters */
961 ssSetNumSFcnParams(S, 0);
962
963 /* Number of expected parameters */
964#if defined(MATLAB_MEX_FILE)
965
966 if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {
967
968#if defined(MDL_CHECK_PARAMETERS)
969
970 mdlCheckParameters(S);
971
972#endif /* MDL_CHECK_PARAMETERS */
973
974 if (ssGetErrorStatus(S) != (NULL) ) {
975 return;
976 }
977 } else {
978 return; /* Parameter mismatch will be reported by Simulink */
979 }
980
981#endif /* MATLAB_MEX_FILE */
982
983 /* Options */
984 ssSetOptions(S, (SS_OPTION_RUNTIME_EXCEPTION_FREE_CODE |
985 SS_OPTION_PORT_SAMPLE_TIMES_ASSIGNED ));
986
987#if SS_SFCN_FOR_SIM
988
989 {
990 ssSupportsMultipleExecInstances(S, true);
991 ssHasStateInsideForEachSS(S, false);
992 }
993
994#endif
995
996}
997
998/* Function to initialize sample times. */
999static void mdlInitializeSampleTimes(SimStruct *S)
1000{
1001 /* task periods */
1002 ssSetSampleTime(S, 0, 5.0E-7);
1003
1004 /* task offsets */
1005 ssSetOffsetTime(S, 0, 0.0);
1006}
1007
1008#if defined(MATLAB_MEX_FILE)
1009#include "fixedpoint.c"
1010#include "simulink.c"
1011#else
1012#undef S_FUNCTION_NAME
1013#define S_FUNCTION_NAME PMSM_Motor_TL3_sf
1014#include "cg_sfun.h"
1015#endif /* defined(MATLAB_MEX_FILE) */
1016