Overview
Comment:*** empty log message ***
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/master | trunk
Files: files | file ages | folders
SHA3-256: e521a22e53ccc6b3bee0c2fbae029d2c7c91c8c812e527310027520b35f8e34a
User & Date: geraint@users.sourceforge.net on 2002-05-10 13:47:51
Other Links: branch diff | manifest | tags
Context
2002-05-10
14:07:16
Preserve .cc files. check-in: cf208f97b4 user: geraint@users.sourceforge.net tags: origin/master, trunk
13:47:51
*** empty log message *** check-in: e521a22e53 user: geraint@users.sourceforge.net tags: origin/master, trunk
13:24:58
Added initial support for building Simulink S-functions.
Rates do not update properly yet.
Inertial switches do not work yet.
Implicit integration not supported yet.

build with: mtt -i euler MotorGenerator sfun mexglx. check-in: c120c65076 user: geraint@users.sourceforge.net tags: origin/master, trunk

Changes

Added mttroot/mtt/lib/rep/sfun_rep/Makefile version [0703d3d39e].



























>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
#! /usr/bin/make -f

$(SYS)_sfun.mexglx: $(SYS)_sfun.cc $(SYS)_def.h $(SYS)_state.mexglx $(SYS)_ode.mexglx $(SYS)_odeo.mexglx
	cp -a $(SYS)_ode.mexglx ..
	cp -a $(SYS)_odeo.mexglx ..
	cp -a $(SYS)_state.mexglx ..
	mex $(SYS)_sfun.cc

$(SYS)_sfun.cc:: ${MTT_REP}/sfun_rep/sfun.cc.tmpl
	cat $^ | sed 's/<mtt_model_name>/$(SYS)/g' > $@

%::
	mtt -q $(OPTS) `echo $* | sed 's/\(.*\)_\(.*\)\.\(.*\)/\1 \2 \3/'`

Added mttroot/mtt/lib/rep/sfun_rep/sfun.cc.tmpl version [7920284121].















































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// -*-c++-*-
// <mtt_model_name>_sfun.cc:
// Matlab S-function simulation of <mtt_model_name>

#define S_FUNCTION_NAME <mtt_model_name>_sfun
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"
#include <iostream>
#include <string>
#include "<mtt_model_name>_def.h"

mxArray *
get_parameters_from_simulink (SimStruct *S)
{
  static const mxArray *constpar = mxCreateDoubleMatrix (MTTNPAR, 1, mxREAL);
  static       mxArray *     par = mxCreateDoubleMatrix (MTTNPAR, 1, mxREAL);
  double *constptr;
  double *ptr;
  std::cerr << "Parameters:\t";
  for (int i = 0; i < MTTNPAR; i++) {
    constpar = ssGetSFcnParam (S, i);
    constptr = mxGetPr (constpar);
    ptr      = mxGetPr (     par);
    *ptr     = *constptr;
    std::cerr << *ptr << ' ';
  }
  std::cerr << std::endl;
  return par;
}

mxArray *
get_states_from_simulink (SimStruct *S)
{
  static mxArray *x = mxCreateDoubleMatrix (MTTNX, 1, mxREAL);
  static double *states;
  static double *ptr = mxGetPr (x);
  std::cerr << "States:\t\t";
  states = ssGetContStates (S);
  for (int i = 0; i < MTTNX; i++) {
    ptr [i] = states [i];
    std::cerr << ptr [i] << ' ';
  }
  std::cerr << std::endl;
  return x;
}

mxArray *
get_inputs_from_simulink (SimStruct *S)
{
  static mxArray *u = mxCreateDoubleMatrix (MTTNU, 1, mxREAL);
  static double *ptr = mxGetPr (u);
  std::cerr << "Inputs:\t\t";
  for (int i = 0; i < MTTNU; i++) {
    ptr [i] = *ssGetInputPortRealSignalPtrs (S, i)[0];
    std::cerr << ptr [i] << ' ';
  }
  std::cerr << std::endl;
  return u;
}

mxArray *
get_time_from_simulink (SimStruct *S)
{
  static mxArray *t = mxCreateDoubleMatrix (1, 1, mxREAL);
  double *ptr = mxGetPr (t);
  ptr [0] = ssGetT (S);
  std::cerr << "Time:\t\t" << ptr [0] << std::endl;
  return t;
}

// S-function methods

static void mdlInitializeSizes(SimStruct *S)
{
    ssSetNumSFcnParams(S, MTTNPAR); 
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        return;
    }

    ssSetNumContStates(S, MTTNX);
    ssSetNumDiscStates(S, 0);

    if (!ssSetNumInputPorts(S, 1)) return;
    ssSetInputPortWidth(S, 0, 1);
    for (int i = 0; i < MTTNU; i++) {
      ssSetInputPortDirectFeedThrough(S, i, 1);
    }

    if (!ssSetNumOutputPorts(S, MTTNY)) return;
    for (int i = 0; i < MTTNY; i++) {
      ssSetOutputPortWidth(S, i, 1);
    }

    ssSetNumSampleTimes(S, 1);
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);
    ssSetNumPWork(S, 0);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);

    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
}

static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);
}

#define MDL_INITIALIZE_CONDITIONS
static void mdlInitializeConditions(SimStruct *S)
{
  static mxArray *states = mxCreateDoubleMatrix (MTTNX, 1, mxREAL);
  static double *ptr;

  mxArray *plhs[1];
  mxArray *prhs[1];

  plhs[0] = states;
  
  prhs[0] = get_parameters_from_simulink (S);
  
  mexCallMATLAB (1, plhs, 1, prhs, "<mtt_model_name>_state");  
  ptr = mxGetPr (plhs[0]);

  std::cerr << "Initial states;\t";
  for (int i = 0; i < MTTNX; i++) {
    ssGetContStates (S)[i] = ptr [i];
    std::cerr << ptr [i] << ' ';
  }
  std::cerr << std::endl;
}

static void mdlOutputs(SimStruct *S, int_T tid)
{
  static mxArray *outputs = mxCreateDoubleMatrix (MTTNY, 1, mxREAL);
  static double *ptr;

  mxArray *plhs[1];
  mxArray *prhs[4];

  plhs[0] = outputs;

  prhs[0] = get_states_from_simulink (S);
  prhs[1] = get_inputs_from_simulink (S);
  prhs[2] = get_time_from_simulink (S);
  prhs[3] = get_parameters_from_simulink (S);
 
  UNUSED_ARG(tid); // not used in single tasking mode

  mexCallMATLAB (1, plhs, 4, prhs, "<mtt_model_name>_odeo");
  ptr = mxGetPr (plhs [0]);

  std::cerr << "Outputs:\t";
  for (int i = 0; i < MTTNY; i++) {
      ssGetOutputPortRealSignal (S,i)[0] = ptr [i];
      std::cerr << ptr [i] << ' ';
  }
  std::cerr << std::endl;
}

#define MDL_DERIVATIVES
static void mdlDerivatives(SimStruct *S)
{
  static mxArray *rates = mxCreateDoubleMatrix (MTTNX, 1, mxREAL);
  static double *ptr;

  mxArray *plhs[1];
  mxArray *prhs[4];

  plhs[0] = rates;

  prhs[0] = get_states_from_simulink (S);
  prhs[1] = get_inputs_from_simulink (S);
  prhs[2] = get_time_from_simulink (S);
  prhs[3] = get_parameters_from_simulink (S);

  mexCallMATLAB (1, plhs, 4, prhs, "<mtt_model_name>_ode");
  ptr = mxGetPr (plhs[0]);

  std::cerr << "Rates:\t\t";
  for (int i = 0; i < MTTNX; i++) {
    ssGetdX (S)[i] = ptr [i];
    std::cerr << ptr[i] << ' ';
  }
  std::cerr << std::endl;
}

static void mdlTerminate(SimStruct *S)
{
    UNUSED_ARG(S);
}

#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 ]