brazos-trapezoidal
mrvsfunc.c
1/* File : vsfunc.c
2 * Abstract:
3 *
4 * Example C-file S-function for defining a continuous system.
5 *
6 * Variable step S-function example.
7 * This example S-function illustrates how to create a variable step
8 * block in Simulink. This block implements a variable step delay
9 * in which the first input is delayed by an amount of time determined
10 * by the second input:
11 *
12 * dt = u(2)
13 * y(t+dt) = u(t)
14 *
15 * Copyright 1990-2013 The MathWorks, Inc.
16 */
17
18#define S_FUNCTION_NAME mrvsfunc
19#define S_FUNCTION_LEVEL 2
20
21// Include the required files from the embedded simulation#include "simstruc.h"
22
23#include "simstruc.h"
24#include "mc_types.h"
25#include "main.h"
26#include "calibrations.h"
27#include "TrapSless.h"
28
29#define U(element) (*uPtrs[element]) /* Pointer to Input Port0 */
30
31
32#ifdef PI_SIMULINK_ADJST
33#define NUMBER_OF_INPUTS ((7*NUMBER_OF_MOTORS) + 8)
34#define NUMBER_OF_OUTPUTS 26
35#else
36#define NUMBER_OF_INPUTS 10*NUMBER_OF_MOTORS
37#define NUMBER_OF_OUTPUTS 36
38#endif
39
40//inputs
41
42#define VARIABLE_SAMPLE_TIME (1e-3)
43#define SAMPLE_TIME_GENERAL_SLOT (1000e-6)
44#define SAMPLE_TIME_PWM_SLOT (62.5e-6)
45#define SAMPLE_TIME_SFUNC (62.5e-8)
46
47//inputs
48u_mc_t PHASE_U_ATOD_REGISTER;
49u_mc_t PHASE_V_ATOD_REGISTER;
50u_mc_t PHASE_W_ATOD_REGISTER;
51u_mc_t PHASE_VU_ATOD_REGISTER;
52u_mc_t PHASE_VV_ATOD_REGISTER;
53u_mc_t PHASE_VW_ATOD_REGISTER;
54u_mc_t VDC_ATOD_REGISTER;
55
56#ifdef PI_SIMULINK_ADJST
57u_mc_t SIMULINK_D_KP;
58u_mc_t SIMULINK_D_KI;
59u_mc_t SIMULINK_D_KP_FMT;
60u_mc_t SIMULINK_D_KI_FMT;
61u_mc_t SIMULINK_Q_KP;
62u_mc_t SIMULINK_Q_KI;
63u_mc_t SIMULINK_Q_KP_FMT;
64u_mc_t SIMULINK_Q_KI_FMT;
65u_mc_t SIMULINK_D_KP2;
66u_mc_t SIMULINK_D_KI2;
67u_mc_t SIMULINK_D_KP_FMT2;
68u_mc_t SIMULINK_D_KI_FMT2;
69u_mc_t SIMULINK_Q_KP2;
70u_mc_t SIMULINK_Q_KI2;
71u_mc_t SIMULINK_Q_KP_FMT2;
72u_mc_t SIMULINK_Q_KI_FMT2;
73#endif
74
75//outputs
76#ifdef IQ_MATH
77volatile mc_t PHASE_U_DUTY_REGISTER_H;
78volatile mc_t PHASE_V_DUTY_REGISTER_H;
79volatile mc_t PHASE_W_DUTY_REGISTER_H;
80volatile mc_t PHASE_U_DUTY_REGISTER_L;
81volatile mc_t PHASE_V_DUTY_REGISTER_L;
82volatile mc_t PHASE_W_DUTY_REGISTER_L;
83
84volatile uint32_t counter = 0;
85
86#else
87volatile float PHASE_U_DUTY_REGISTER_H;
88volatile float PHASE_V_DUTY_REGISTER_H;
89volatile float PHASE_W_DUTY_REGISTER_H;
90volatile float PHASE_U_DUTY_REGISTER_L;
91volatile float PHASE_V_DUTY_REGISTER_L;
92volatile float PHASE_W_DUTY_REGISTER_L;
93#endif
94volatile double PWM_PERIOD_REGISTER = 1;
95
96
97u_mc_t PHASE_U_ATOD_REGISTER2;
98u_mc_t PHASE_V_ATOD_REGISTER2;
99u_mc_t PHASE_W_ATOD_REGISTER2;
100u_mc_t PHASE_VU_ATOD_REGISTER2;
101u_mc_t PHASE_VV_ATOD_REGISTER2;
102u_mc_t PHASE_VW_ATOD_REGISTER2;
103u_mc_t VDC_ATOD_REGISTER2;
104//outputs
105volatile float PHASE_U_DUTY_REGISTER_H2;
106volatile float PHASE_V_DUTY_REGISTER_H2;
107volatile float PHASE_W_DUTY_REGISTER_H2;
108volatile float PHASE_U_DUTY_REGISTER_L2;
109volatile float PHASE_V_DUTY_REGISTER_L2;
110volatile float PHASE_W_DUTY_REGISTER_L2;
111volatile double PWM_PERIOD_REGISTER2 = 1;
112
113
114//#define MDL_CHECK_PARAMETERS
115 /* Function: mdlCheckParameters =============================================
116 * Abstract:
117 * Validate our parameters to verify:
118 * o The numerator must be of a lower order than the denominator.
119 * o The sample time must be a real positive nonzero value.
120 * o Cannot be used in code generation
121 */
122 static void mdlCheckParameters(SimStruct *S)
123 {
124// int_T i;
125//
126// for (i = 0; i < NPARAMS; i++) {
127// real_T *pr;
128// size_t el;
129// size_t nEls;
130// if (mxIsEmpty( ssGetSFcnParam(S,i)) ||
131// mxIsSparse( ssGetSFcnParam(S,i)) ||
132// mxIsComplex( ssGetSFcnParam(S,i)) ||
133// mxIsLogical( ssGetSFcnParam(S,i)) ||
134// !mxIsNumeric( ssGetSFcnParam(S,i)) ||
135// !mxIsDouble( ssGetSFcnParam(S,i)) ) {
136// ssSetErrorStatus(S,"Parameters must be real finite vectors");
137// return;
138// }
139// pr = mxGetPr(ssGetSFcnParam(S,i));
140// nEls = mxGetNumberOfElements(ssGetSFcnParam(S,i));
141// for (el = 0; el < nEls; el++) {
142// if (!mxIsFinite(pr[el])) {
143// ssSetErrorStatus(S,"Parameters must be real finite vectors");
144// return;
145// }
146// }
147// }
148//
149// if (mxGetNumberOfElements(NUM(S)) > mxGetNumberOfElements(DEN(S)) &&
150// mxGetNumberOfElements(DEN(S)) > 0 && *mxGetPr(DEN(S)) != 0.0) {
151// ssSetErrorStatus(S,"The denominator must be of higher order than "
152// "the numerator, nonempty and with first "
153// "element nonzero");
154// return;
155// }
156//
157// if (mxGetNumberOfElements(TS(S)) != 1 || mxGetPr(TS(S))[0] <= 0.0) {
158// ssSetErrorStatus(S,"Invalid sample time specified");
159// return;
160// }
161
162 }
163//#define MDL_PROCESS_PARAMETERS
164// static void mdlProcessParameters(SimStruct *S)
165// {
167// }
168
169//#define MDL_START
170//#if defined(MDL_START)
171//static void mdlStart(SimStruct *S)
172//{
173// mdlProcessParameters(S);
174//}
175//#endif /* MDL_START */
176
177/* Function: mdlInitializeSizes ===============================================
178 * Abstract:
179 * The sizes information is used by Simulink to determine the S-function
180 * block's characteristics (number of inputs, outputs, states, etc.).
181 */
182static void mdlInitializeSizes(SimStruct *S)
183{
184// ssSetNumSFcnParams(S, NUMBER_OF_PARAMETERS); /* Number of expected parameters */
185// if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
186// return; /* Parameter mismatch will be reported by Simulink */
187// }
188
189 ssSetNumContStates(S, 0);
190 ssSetNumDiscStates(S, 0);
191
192 if (!ssSetNumInputPorts(S, 1)) return;
193 ssSetInputPortWidth(S, 0, NUMBER_OF_INPUTS);
194 ssSetInputPortDirectFeedThrough(S, 0, 1);
195
196 if (!ssSetNumOutputPorts(S, 1)) return;
197 ssSetOutputPortWidth(S, 0, NUMBER_OF_OUTPUTS);
198 //Second output is the serial com output
199// ssSetOutputPortWidth(S, 1, NUMBER_OF_OUTPUTS);
200// ssSetOutputPortDataType(S, 1, SS_UINT8);
201// ssSetOutputPortSampleTime(S, 1, SAMPLE_TIME_250US);
202// if(!ssSetOutputPortVectorDimension(S, 9, 3)) return;
203// if (!ssSetOutputPortDimensionInfo(S, 9, DYNAMIC_DIMENSION)) return;
204
205 ssSetNumSampleTimes(S, 1);
206 ssSetNumRWork(S, 0);
207 ssSetNumIWork(S, 0);
208 ssSetNumPWork(S, 0);
209 ssSetNumModes(S, 0);
210 ssSetNumNonsampledZCs(S, 0);
211 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
212
213 if (ssGetSimMode(S) == SS_SIMMODE_RTWGEN && !ssIsVariableStepSolver(S)) {
214 ssSetErrorStatus(S, "S-function cannot be used with Simulink Coder "
215 "and Fixed-Step Solvers because it contains variable"
216 " sample time");
217 }
218
219 ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
220 main_init();
221
222 //In case we need parameters
223 PHASE_U_DUTY_REGISTER_H = _IQ(0.99f);
224 PHASE_V_DUTY_REGISTER_H = _IQ(0.98f);
225 PHASE_W_DUTY_REGISTER_H = _IQ(0.98f);
226
227 PHASE_U_DUTY_REGISTER_H2 = _IQ(1.0f);
228 PHASE_V_DUTY_REGISTER_H2 = _IQ(1.0f);
229 PHASE_W_DUTY_REGISTER_H2 = _IQ(1.0f);
230
231}
232
233
234
235/* Function: mdlInitializeSampleTimes =========================================
236 * Abstract:
237 * Variable-Step S-function
238 */
239static void mdlInitializeSampleTimes(SimStruct *S)
240{
241 ssSetSampleTime(S, 0, SAMPLE_TIME_SFUNC); /*40MHz*/
242 ssSetOffsetTime(S, 0, 0.0);
243// ssSetSampleTime(S,1,PWM_PERIOD_REGISTER2);
244// ssSetOffsetTime(S, 1, 0.0);
251// ssSetModelReferenceSampleTimeDefaultInheritance(S);
252
253// ssSetSampleTime(S, 0, VARIABLE_SAMPLE_TIME);
254// ssSetOffsetTime(S, 0, 0.0);
255// ssSetSampleTime(S, 1, SAMPLE_TIME_PWM_SLOT);
256// ssSetOffsetTime(S, 1, 0.0);
257// ssSetSampleTime(S, 2, SAMPLE_TIME_GENERAL_SLOT);
258// ssSetOffsetTime(S, 2, 0.0);
259
260 //ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
261 //ssSetModelReferenceSampleTimeDefaultInheritance(S);
262}
263
264
265#define MDL_GET_TIME_OF_NEXT_VAR_HIT
266static void mdlGetTimeOfNextVarHit(SimStruct *S)
267{
268 InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
269
270 /* Make sure input will increase time */
271 if (PWM_PERIOD_REGISTER <= 0.0) {
272 /* If not, abort simulation */
273 ssSetErrorStatus(S,"Variable step control input must be "
274 "greater than zero");
275 return;
276 }
277 ssSetTNext(S, ssGetT(S)+SAMPLE_TIME_PWM_SLOT);
278}
279
280
281
283// * Abstract:
284// * y = x
285// */
286//
287//static void mdlOutputs(SimStruct *S, int_T tid)
288//{
289// static unsigned long sample_time_for_1ms_cnt = 0;
290// real_T *y;
291// float temp;
292// InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
293// if(ssIsSampleHit(S,0,tid))
294// {
295// y = ssGetOutputPortRealSignal(S,0);
296//
309// VDC_ATOD_REGISTER = GET_PARAM_FXP(U(4), MTR_VOLT_BASE);
311//#ifdef PI_SIMULINK_ADJST
312// SIMULINK_D_KP = (u_mc_t)_FLOAT_TO_IQY(U(14),(u_mc_t)U(16),BASE_D_KP);
313// SIMULINK_D_KI = (u_mc_t)_FLOAT_TO_IQY(U(15)*TS,(u_mc_t)U(17),BASE_D_KI);
314// SIMULINK_D_KP_FMT = (u_mc_t)U(16);
315// SIMULINK_D_KI_FMT = (u_mc_t)U(17);
316// SIMULINK_Q_KP = (u_mc_t)_FLOAT_TO_IQY(U(18),(u_mc_t)U(20),BASE_Q_KP);
317// SIMULINK_Q_KI = (u_mc_t)_FLOAT_TO_IQY(U(19)*TS,(u_mc_t)U(21),BASE_Q_KI);
318// SIMULINK_Q_KP_FMT = (u_mc_t)U(20);
319// SIMULINK_Q_KI_FMT = (u_mc_t)U(21);
320// dqCtrl_Initialize(&ControlData[0],0);
321//#endif
322//
323//
324// TestBldcOn = (unsigned char)U(5);
325//
326// mtr_carrier_interrupt();
327// if(sample_time_for_1ms_cnt) sample_time_for_1ms_cnt--;
328// else {
329// mtr_1ms_interrupt();
330// sample_time_for_1ms_cnt = SAMPLE_TIME_GENERAL_SLOT/SAMPLE_TIME_PWM_SLOT;
331// }
332// y[0] = st_g.s4_vu_ad;
333// y[1] = st_g.s4_vv_ad;
334// y[2] = st_g.s4_vw_ad;
335// y[3] = st_g.s4_vdc_ad;
336// y[4] = st_g.s4_ref_speed_rad;
337// y[5] = st_g.s4_speed_rad;
338// y[6] = st_g.s4_v_ref;
339// y[7] = st_g.s4_ol_start_refv;
340// y[8] = st_g.s4_ol_mode1_rate_rad;
341// y[9] = st_g.s4_ol_mode2_rate_rad;
342// y[10] = st_g.s4_ol_mode2_rate_refv;
343// y[11] = st_g.s4_ol_mode3_max_refv;
344// y[12] = st_g.s4_ol_mode3_rate_refv;
345// y[13] = st_g.u1_flag_speed_ref;
346// y[14] = st_g.u1_flag_voltage_ref_state;
347// y[15] = st_g.u1_v_pattern;
348// y[16] = st_g.u2_pwm_duty;
349// y[17] = st_g.u1_mode_system;
350// y[18] = st_g.u2_run_mode;
351// y[19] = st_g.u2_error_status;
352// y[20] = TestBldcOn;
353// y[21] = st_g.s4_ref_speed_rad_ctrl;
354// y[22] = MTR_PORT_UP;
355// y[23] = MTR_PORT_UN;
356// y[24] = MTR_PORT_VP;
357// y[25] = MTR_PORT_VN;
358// y[26] = MTR_PORT_WP;
359// y[27] = MTR_PORT_WN;
360// y[28] = st_g.u2_cnt_carrier;
361// y[29] = st_g.u2_angle_shift_cnt;
362// }
363//
364//
370// /* Return the current state as the output */
371//}
372
373
374/* Function: mdlOutputs =======================================================
375 * Abstract:
376 * y = x
377 */
378//extern MTR_ST_LESS_120 st_g;
379//static float32 f4_iu_ad;
380//static float32 f4_iv_ad;
381//static float32 f4_iw_ad;
382volatile uint32_t pwm_cnter;
383volatile uint8_t pwm_cnter_dir = 1;
384//volatile uint8 up_out;
385//volatile uint8 un_out;
386//volatile uint8 vp_out;
387//volatile uint8 vn_out;
388//volatile uint8 wp_out;
389//volatile uint8 wn_out;
390volatile float32_t pwm_value = 0.0;
391volatile float32_t loaded_duty_u = 0.0;
392volatile float32_t loaded_duty_v = 0.0;
393volatile float32_t loaded_duty_w = 0.0;
394volatile float32_t loaded_up;
395volatile float32_t loaded_un;
396volatile float32_t loaded_vp;
397volatile float32_t loaded_wp;
398volatile float32_t loaded_vn;
399volatile float32_t loaded_wn;
400volatile unsigned long Counter1ms;
401volatile unsigned long Counter16k;
402volatile unsigned long Timeout1ms;
403volatile unsigned long Timeout16k;
404unsigned char TestBldcOn;
405unsigned char TestBldcOnOnce = 1;
406volatile unsigned char TestBldcOff;
407static int32_t TestBldcSpd = 1000;
408static void mdlOutputs(SimStruct *S, int_T tid)
409{
410 real_T *y;
411 InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
412 if(ssIsSampleHit(S,0,tid)) //TASK0 (variable step)
413 {
414 y = ssGetOutputPortRealSignal(S,0);
415
416 /* pwm counter */
417 /* This is the timer counter for speed calc - only at simulation */
418 if(TrapSless__GetVars() != NULL)
419 {
420 TrapSless__GetVars()->TimeCntBemf++;
421 }
422
423 /* assign to output */
424
425
426 if(TrapSless__GetVars() != NULL) {
427 TrapSless__GetVars()->bus_volt = V_TO_IQ((float32_t)U(6));
428 }
429
430 Iu_Ad_Forced_Value[0] = (float32_t)U(3);
431 Iv_Ad_Forced_Value[0] = (float32_t)U(4);
432 Iw_Ad_Forced_Value[0] = (float32_t)U(5);
433 Vu_Ad_Forced_Value[0] = (float32_t)U(0);
434 Vv_Ad_Forced_Value[0] = (float32_t)U(1);
435 Vw_Ad_Forced_Value[0] = (float32_t)U(2);
436
437 /* Set Gains Speed Kp Ki*/
438 //st_g.s4_kp_speed = GET_PARAM_FXP((float32_t)U(8), MTR_KP_BASE);
439 //st_g.s4_ki_speed = GET_PARAM_FXP((float32_t)U(9), MTR_KI_BASE);
440
441 //TestBldcOn = (unsigned char)U(7);
442 if(!TestBldcOn){
443 TestBldcOn = (unsigned char)U(7);
444 }
445 if(U(7) == 0) {
446 TestBldcOn = 0;
447 }
448
449 if(Timeout1ms) Timeout1ms--;
450 if(Timeout16k) Timeout16k--;
451
452
453 if(!pwm_cnter_dir && (pwm_cnter < ((SAMPLE_TIME_PWM_SLOT/SAMPLE_TIME_SFUNC)/2-1))) {
454 pwm_cnter += 1;
455 }
456 else if(!pwm_cnter_dir && (pwm_cnter >= ((SAMPLE_TIME_PWM_SLOT/SAMPLE_TIME_SFUNC)/2-1))) {
457 pwm_cnter_dir = 1;
458 pwm_cnter -= 1;
459 }
460
461 if(pwm_cnter_dir && (pwm_cnter > 0)) {
462 pwm_cnter -= 1;
463 }
464 else if(pwm_cnter_dir && (pwm_cnter <= 0)) {
465 pwm_cnter_dir = 0;
466 pwm_cnter += 1;
467 }
468
469 if(!Timeout1ms) {
470 Timeout1ms = 1600; //1600K/1K
471 if(TestBldcOff){
472 TestBldcOff = 0;
473 }
474 if(TestBldcOnOnce){
475 TestBldcOnOnce = 0;
476 TrapSless__SetSpd(TestBldcSpd, 100);
477 }
478
480 }
481
482 if(!Timeout16k) {
483 Timeout16k = 100; //1600K/16K
485 }
486
487 if(TrapSless__GetVars() != NULL) {
488// y[0] = (pwm_cnter >= PHASE_U_DUTY_REGISTER_H ? 0 : 1);
489 y[0] = PHASE_U_DUTY_REGISTER_H;
490 y[1] = (pwm_cnter >= PHASE_V_DUTY_REGISTER_H ? 0 : 1);
491 y[2] = (pwm_cnter >= PHASE_W_DUTY_REGISTER_H ? 0 : 1);
492 y[3] = (pwm_cnter < PHASE_U_DUTY_REGISTER_L ? 1 : 0);
493 y[4] = (pwm_cnter < PHASE_V_DUTY_REGISTER_L ? 1 : 0);
494 y[5] = (pwm_cnter < PHASE_W_DUTY_REGISTER_L ? 1 : 0);
495 y[6] = TrapSless__GetVars()->bus_curr;
496 y[7] = TrapSless__GetVars()->blankTimeDownCnter;
497 y[8] = 0;
498 y[9] = TrapSless__GetVars()->TimeCntBemfAvg;
499 y[10] = TrapSless__GetVars()->actualCmtNum; //
500 y[11] = TrapSless__GetVars()->too_early;
501 y[12] = TrapSless__GetVars()->zcross;
502 y[13] = TrapSless__GetVars()->too_late;
503 y[14] = TrapSless__GetVars()->TimeCntBemf;
504 y[15] = TrapSless__GetVars()->actual_bemf_pattern;
505 y[16] = TrapSless__GetVars()->actualCmtPattern; //
506 y[17] = TrapSless__GetVars()->TimeoutForCmt;
507 y[18] = TrapSless__GetVars()->va;
508 y[19] = TrapSless__GetVars()->vb;
509 y[20] = TrapSless__GetVars()->vc;
510 y[21] = TrapSless__GetVars()->spdRef;
511 y[22] = TrapSless__GetVars()->SpdTarget;
512 y[23] = TrapSless__GetVars()->State;
513 y[24] = TrapSless__GetVars()->vRef;
514 y[25] = TrapSless__GetVars()->openLoopPatternTimeCnt;
515 y[26] = TrapSless__GetVars()->openLoopTimeThld;
516 y[27] = TrapSless__GetVars()->dutyOut;
517 y[28] = TrapSless__GetVars()->bus_volt;
518// y[29] = TrapSless__GetVars()->u_state;
519// y[30] = TrapSless__GetVars()->v_state;
520// y[31] = TrapSless__GetVars()->w_state;
521 y[32] = TrapSless__GetVars()->speedRad;
522// y[33] = TrapSless__GetVars()->propSum;
523 y[34] = TrapSless__GetVars()->piErr;
524 y[35] = TrapSless__GetVars()->piOut;
525
526 }
527
528 }
529
530
531
532
533
534
535 /* Return the current state as the output */
536}
537
538/* Function: mdlTerminate =====================================================
539 * Abstract:
540 * No termination needed, but we are required to have this routine.
541 */
542static void mdlTerminate(SimStruct *S)
543{
544}
545
546#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
547#include "simulink.c" /* MEX-file interface mechanism */
548#else
549#include "cg_sfun.h" /* Code generation registration function */
550#endif
void TrapSless__SlowHandler()
Handler for Trapezoidal control - 1KHz call rate.
Definition: TrapSless.c:671
void TrapSless__SetSpd(int32_t spd_rpm, int32_t ramp_rpm)
BLDC motor rotation command.
Definition: TrapSless.c:874
void TrapSless__Handler()
Handler for Trapezoidal control - 16KHz call rate.
Definition: TrapSless.c:413
Automatic Generated Calibration File.
Header file for main.h.c.
Contains generic typedefs that are able to change in case of different processors.
uint32_t openLoopPatternTimeCnt
Definition: TrapSless.h:197
uint8_t actual_bemf_pattern
Definition: TrapSless.h:173
uint32_t blankTimeDownCnter
Definition: TrapSless.h:202
uint32_t openLoopTimeThld
Definition: TrapSless.h:194