Differences From Artifact [12aacfe503]:

To Artifact [8f8ea00a46]:


18
19
20
21
22
23
24
25
26


27
28
29
30
31
32
33



34
35
36
37
38
39
40
41
42
43
44
45


46
47

48
49

50
51
52


53
54

55
56




57
58
59
60
61
62
63
18
19
20
21
22
23
24


25
26
27
28
29
30



31
32
33
34
35
36
37
38
39
40
41
42
43


44
45
46

47
48

49



50
51
52
53
54


55
56
57
58
59
60
61
62
63
64
65







-
-
+
+




-
-
-
+
+
+










-
-
+
+

-
+

-
+
-
-
-
+
+


+
-
-
+
+
+
+








static double *mttu;		/* pointer to inputs */
static double *mttpar;		/* pointer to parameters */
static double *mttx;		/* pointer to states */
static double *mtty;		/* pointer to outputs */
static double  mttt;		/* time */

static double *controller_inputs;
static double *controller_outputs;
static double *MTT_outputs;
static double *MTT_inputs;

static unsigned int i;		/* loop counter */

/* Start EDIT */
/* Edit this block to define the number of controller inputs, outputs and parameters */
const int NumberOfControllerInputs	= 1; /* inputs TO controller */
const int NumberOfControllerOutputs	= 1; /* outputs FROM controller */
/* Edit this block to define the number of simulink inputs, outputs and parameters */
#define NumberOfSimulinkInputs	MTTNY /* MTT outputs */
#define NumberOfSimulinkOutputs	MTTNU /* MTT inputs */
/* End EDIT */

static void
<mtt_model_name>_process_inputs (SimStruct *S)
{
  /* insert <mtt_model_name>_struc.c */

  /* Start EDIT */
  /* Edit this block to process the model inputs and outputs */
  
  /* Remove the following line */
  ssSetErrorStatus (S, "<mtt_model_name>_sfun_interface.c has not been customised!\n");
  /* Error messages can be set using the following line */
  /*  ssSetErrorStatus (S, "<mtt_model_name>_some error message!\n"); */

  /* simple example follows */
  /* Default is to expose all MTT inputs, outputs and states */

  /* Get total of all outputs and input to controller */
  /* Get all the MTT model outputs and pass them to simulink */
  controller_inputs[0] = 0.0;
  for (i = 0; i < MTTNY; i++) {
    controller_inputs[0] += mtty[i];
  for (i = 0; i < NumberOfSimulinkInputs; i++) {
    MTT_outputs[i] = mtty[i];
  }

  /* Get all inputs from Simulink and pass them to the MTT model */
  /* overwrite first model input with output from controller */
  mttu[0] = controller_outputs[0];
  /* Any inputs not over-written here will be read from <mtt_model_name>_input.c  */
  for (i = 0; i < NumberOfSimulinkOutputs; i++) {
    mttu[i] = MTT_inputs[i];
  }

  /* End EDIT */
}


/******************************************************************************
 *                DO NOT EDIT ANYTHING BELOW THIS LINE                        *
96
97
98
99
100
101
102
103
104
105
106




107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149



150
151
152
153
154
155
156
98
99
100
101
102
103
104




105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148



149
150
151
152
153
154
155
156
157
158







-
-
-
-
+
+
+
+


















-
-
+
+




















-
-
-
+
+
+







      array_name = "mttpar";
    } else if (array == mttu) {
      array_name = "mttu";
    } else if (array == mttx) {
      array_name = "mttx";
    } else if (array == mtty) {
      array_name = "mtty";
    } else if (array == controller_inputs) {
      array_name = "controller_inputs";
    } else if (array == controller_outputs) {
      array_name = "controller_outputs";
    } else if (array == MTT_outputs) {
      array_name = "MTT_outputs";
    } else if (array == MTT_inputs) {
      array_name = "MTT_inputs";
    } else {
      array_name = "unknown_array";
    }
    sprintf(warning, "(time %f) Non-finite array element: %s[%d]\n", mttt, array_name, index);
    ssWarning(S, warning);
  }
}

static void
initialise_arrays (void)
{
  PRINT_ENTER;

  mttpar	= array_of_double (MTTNPAR);
  mttu		= array_of_double (MTTNU + MTTNYZ);
  mttx		= array_of_double (MTTNX);
  mtty		= array_of_double (MTTNY);

  controller_inputs	= array_of_double (NumberOfControllerInputs);
  controller_outputs	= array_of_double (NumberOfControllerOutputs);
  MTT_outputs	= array_of_double (NumberOfSimulinkInputs);
  MTT_inputs	= array_of_double (NumberOfSimulinkOutputs);

  PRINT_LEAVE;
}

static void
update_inputs_from_simulink (SimStruct *S)
{
  PRINT_ENTER;
  for (i = 0; i < MTTNU; i++) {
    mttu[i] = *ssGetInputPortRealSignalPtrs (S, 0)[i];
    check_finite(S, mttu, i);
  }
  for (i = 0; i < MTTNX; i++) {
    mttx[i] = *ssGetInputPortRealSignalPtrs (S, 1)[i];
    check_finite(S, mttx, i);
  }
  for (i = 0; i < MTTNY; i++) {
    mtty[i] = *ssGetInputPortRealSignalPtrs (S, 2)[i];
    check_finite(S, mtty, i);
  }
  for (i = 0; i < NumberOfControllerOutputs; i++) {
    controller_outputs[i] = *ssGetInputPortRealSignalPtrs (S, 3)[i];
    check_finite(S, controller_outputs, i);
  for (i = 0; i < NumberOfSimulinkOutputs; i++) {
    MTT_inputs[i] = *ssGetInputPortRealSignalPtrs (S, 3)[i];
    check_finite(S, MTT_inputs, i);
  }
  PRINT_LEAVE;
}

static void
update_simtime_from_simulink (SimStruct *S)
{
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189

190
191
192
193
194
195
196
176
177
178
179
180
181
182

183
184
185
186
187
188
189
190

191
192
193
194
195
196
197
198







-
+







-
+







  ssSetNumContStates(S, 0);
  ssSetNumDiscStates(S, 0);
  
  if (!ssSetNumInputPorts(S, 4)) return;
  ssSetInputPortWidth(S, 0, MTTNU);
  ssSetInputPortWidth(S, 1, MTTNX);
  ssSetInputPortWidth(S, 2, MTTNY);
  ssSetInputPortWidth(S, 3, NumberOfControllerOutputs);	/* inputs from controller */
  ssSetInputPortWidth(S, 3, NumberOfSimulinkOutputs);	/* inputs from simulink */
  ssSetInputPortDirectFeedThrough(S, 0, 1);
  ssSetInputPortDirectFeedThrough(S, 1, 1);
  ssSetInputPortDirectFeedThrough(S, 2, 1);
  ssSetInputPortDirectFeedThrough(S, 3, 1);
    
  if (!ssSetNumOutputPorts(S, 2)) return;
  ssSetOutputPortWidth(S, 0, MTTNU); /* altered inputs */
  ssSetOutputPortWidth(S, 1, NumberOfControllerInputs); /* outputs to controller */
  ssSetOutputPortWidth(S, 1, NumberOfSimulinkInputs); /* outputs to simulink */
    
  ssSetNumSampleTimes(S, 1);
  ssSetNumRWork(S, 0);
  ssSetNumIWork(S, 0);
  ssSetNumPWork(S, 0);
  ssSetNumModes(S, 0);
  ssSetNumNonsampledZCs(S, 0);
230
231
232
233
234
235
236
237
238
239



240
241
242
243
244
245
246
232
233
234
235
236
237
238



239
240
241
242
243
244
245
246
247
248







-
-
-
+
+
+







  UNUSED_ARG(tid); /* not used in single tasking mode */

  for (i = 0; i < MTTNU; i++) {
    check_finite(S, mttu, i);
    ssGetOutputPortRealSignal (S, 0)[i] = mttu[i];
  }
  
  for (i = 0; i < NumberOfControllerInputs; i++) {
    check_finite(S, controller_inputs, i);
    ssGetOutputPortRealSignal (S, 1)[i] = controller_inputs[i];
  for (i = 0; i < NumberOfSimulinkInputs; i++) {
    check_finite(S, MTT_outputs, i);
    ssGetOutputPortRealSignal (S, 1)[i] = MTT_outputs[i];
  }

  PRINT_LEAVE;
}

#define MDL_DERIVATIVES
static void mdlDerivatives(SimStruct *S)
256
257
258
259
260
261
262
263
264


265
266
267
268
269
270
271
272
273
258
259
260
261
262
263
264


265
266
267
268
269
270
271
272
273
274
275







-
-
+
+









  UNUSED_ARG(S);

  free (mttpar);
  free (mttu);
  free (mttx);
  free (mtty);

  free (controller_inputs);
  free (controller_outputs);
  free (MTT_outputs);
  free (MTT_inputs);

  PRINT_LEAVE;
}

#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
#include "simulink.c"      /* MEX-file interface mechanism */
#else
#include "cg_sfun.h"       /* Code generation registration function */
#endif

MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]