Subversion Repositories shark

Rev

Rev 1343 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1342 giacomo 1
#include "tmwtypes.h"
2
#include "rtmodel.h"
3
#include "rt_sim.h"
4
#include "rt_logging.h"
5
#include "rt_nonfinite.h"
6
 
7
#include "ext_work.h"
8
#include "rt_nonfinite.h"
9
 
10
#include "kernel/kern.h"
11
 
12
/*=========*
13
 * Defines *
14
 *=========*/
15
 
16
#ifndef TRUE
17
#define FALSE (0)
18
#define TRUE  (1)
19
#endif
20
 
21
#ifndef EXIT_FAILURE
22
#define EXIT_FAILURE  1
23
#endif
24
#ifndef EXIT_SUCCESS
25
#define EXIT_SUCCESS  0
26
#endif
27
 
28
#define QUOTE1(name) #name
29
#define QUOTE(name) QUOTE1(name)    /* need to expand name    */
30
 
31
#define RUN_FOREVER -1.0
32
 
33
#define EXPAND_CONCAT(name1,name2) name1 ## name2
34
#define CONCAT(name1,name2) EXPAND_CONCAT(name1,name2)
35
#define RT_MODEL            CONCAT(MODEL,_rtModel)
36
 
37
extern RT_MODEL *MODEL(void);
38
 
39
extern void MdlInitializeSizes(void);
40
extern void MdlInitializeSampleTimes(void);
41
extern void MdlStart(void);
42
extern void MdlOutputs(int_T tid);
43
extern void MdlUpdate(int_T tid);
44
extern void MdlTerminate(void);
45
 
46
real_T rtInf;
47
real_T rtMinusInf;
48
real_T rtNaN;
49
 
50
# define rt_CreateIntegrationData(S)  \
51
      rtsiSetSolverName(rtmGetRTWSolverInfo(S),"FixedStepDiscrete");
52
# define rt_UpdateContinuousStates(S) \
53
      rtmSetT(S, rtsiGetSolverStopTime(rtmGetRTWSolverInfo(S)));
54
 
55
/*==================================*
56
 * Global data local to this module *
57
 *==================================*/
58
 
59
static struct {
60
  int_T    stopExecutionFlag;
61
  int_T    isrOverrun;
62
  int_T    overrunFlags[NUMST];
63
  const char_T *errmsg;
64
} GBLbuf;
65
 
66
/* Function: rt_InitInfAndNaN ==================================================
67
 * Abstract:
68
 *      Initialize the rtInf, rtMinusInf, and rtNaN needed by the
69
 *      generated code. NaN is initialized as non-signaling. Assumes IEEE.
70
 */
71
void rt_InitInfAndNaN(int_T realSize)
72
{
73
    short one = 1;
74
    enum {
75
        LittleEndian,
76
        BigEndian
77
    } machByteOrder = (*((char *) &one) == 1) ? LittleEndian : BigEndian;
78
 
79
    switch (realSize) {  
80
      case 4:
81
        switch (machByteOrder) {
82
          case LittleEndian: {
83
              typedef struct {
84
                  uint32_T fraction : 23;
85
                  uint32_T exponent  : 8;
86
                  uint32_T sign      : 1;
87
              } LittleEndianIEEEDouble;
88
 
89
              (*(LittleEndianIEEEDouble*)&rtInf).sign      = 0;
90
              (*(LittleEndianIEEEDouble*)&rtInf).exponent  = 0xFF;
91
              (*(LittleEndianIEEEDouble*)&rtInf).fraction  = 0;
92
              rtMinusInf = rtInf;
93
              rtNaN = rtInf;
94
              (*(LittleEndianIEEEDouble*)&rtMinusInf).sign = 1;
95
              (*(LittleEndianIEEEDouble*)&rtNaN).fraction  = 0x7FFFFF;
96
          }
97
          break;
98
          case BigEndian: {
99
              typedef struct {
100
                  uint32_T sign      : 1;
101
                  uint32_T exponent  : 8;
102
                  uint32_T fraction  : 23;
103
              } BigEndianIEEEDouble;
104
 
105
              (*(BigEndianIEEEDouble*)&rtInf).sign      = 0;
106
              (*(BigEndianIEEEDouble*)&rtInf).exponent  = 0xFF;
107
              (*(BigEndianIEEEDouble*)&rtInf).fraction  = 0;
108
              rtMinusInf = rtInf;
109
              rtNaN = rtInf;
110
              (*(BigEndianIEEEDouble*)&rtMinusInf).sign = 1;
111
              (*(BigEndianIEEEDouble*)&rtNaN).fraction  = 0x7FFFFF;
112
          }
113
          break;
114
        }
115
        break;    
116
 
117
      case 8:
118
        switch (machByteOrder) {
119
          case LittleEndian: {
120
              typedef struct {
121
                  struct {
122
                      uint32_T fraction2;
123
                  } wordH;
124
                  struct {
125
                      uint32_T fraction1 : 20;
126
                      uint32_T exponent  : 11;
127
                      uint32_T sign      : 1;
128
                  } wordL;
129
              } LittleEndianIEEEDouble;
130
 
131
              (*(LittleEndianIEEEDouble*)&rtInf).wordL.sign      = 0;
132
              (*(LittleEndianIEEEDouble*)&rtInf).wordL.exponent  = 0x7FF;
133
              (*(LittleEndianIEEEDouble*)&rtInf).wordL.fraction1 = 0;
134
              (*(LittleEndianIEEEDouble*)&rtInf).wordH.fraction2 = 0;
135
 
136
              rtMinusInf = rtInf;
137
              (*(LittleEndianIEEEDouble*)&rtMinusInf).wordL.sign = 1;
138
              (*(LittleEndianIEEEDouble*)&rtNaN).wordL.sign      = 0;
139
              (*(LittleEndianIEEEDouble*)&rtNaN).wordL.exponent  = 0x7FF;
140
              (*(LittleEndianIEEEDouble*)&rtNaN).wordL.fraction1 = 0xFFFFF;
141
              (*(LittleEndianIEEEDouble*)&rtNaN).wordH.fraction2 = 0xFFFFFFFF;
142
          }
143
          break;
144
          case BigEndian: {
145
              typedef struct {
146
                  struct {
147
                      uint32_T sign      : 1;
148
                      uint32_T exponent  : 11;
149
                      uint32_T fraction1 : 20;
150
                  } wordL;
151
                  struct {
152
                      uint32_T fraction2;
153
                  } wordH;
154
              } BigEndianIEEEDouble;
155
 
156
              (*(BigEndianIEEEDouble*)&rtInf).wordL.sign      = 0;
157
              (*(BigEndianIEEEDouble*)&rtInf).wordL.exponent  = 0x7FF;
158
              (*(BigEndianIEEEDouble*)&rtInf).wordL.fraction1 = 0;
159
              (*(BigEndianIEEEDouble*)&rtInf).wordH.fraction2 = 0;
160
 
161
              rtMinusInf = rtInf;
162
              (*(BigEndianIEEEDouble*)&rtMinusInf).wordL.sign = 1;
163
              (*(BigEndianIEEEDouble*)&rtNaN).wordL.sign      = 0;
164
              (*(BigEndianIEEEDouble*)&rtNaN).wordL.exponent  = 0x7FF;
165
              (*(BigEndianIEEEDouble*)&rtNaN).wordL.fraction1 = 0xFFFFF;
166
              (*(BigEndianIEEEDouble*)&rtNaN).wordH.fraction2 = 0xFFFFFFFF;
167
          }
168
          break;
169
        }
170
        break;
171
      default:
172
        cprintf("Unable to initialize rtInf, rtMinusInf and rtNaN\n");
173
        sys_end();
174
        break;
175
    }
176
 
177
} /* end rt_InitInfAndNaN */
178
 
179
/* Function: rtOneStep ========================================================
180
 *
181
 * Abstract:
182
 *      Perform one step of the model. This function is modeled such that
183
 *      it could be called from an interrupt service routine (ISR) with minor
184
 *      modifications.
185
 */
186
static void rt_OneStep(RT_MODEL *S)
187
{
188
    real_T tnext;
189
 
190
    /***********************************************
191
     * Check and see if base step time is too fast *
192
     ***********************************************/
193
 
194
    if (GBLbuf.isrOverrun++) {
195
        GBLbuf.stopExecutionFlag = 1;
196
        return;
197
    }
198
 
199
    /***********************************************
200
     * Check and see if error status has been set  *
201
     ***********************************************/
202
 
203
    if (rtmGetErrorStatus(S) != NULL) {
204
        GBLbuf.stopExecutionFlag = 1;
205
        return;
206
    }
207
 
208
    /* enable interrupts here */
209
 
210
    rtExtModeOneStep(rtmGetRTWExtModeInfo(S),
211
                     (boolean_T *)&rtmGetStopRequested(S));
212
 
213
    tnext = rt_SimGetNextSampleHit();
214
    rtsiSetSolverStopTime(rtmGetRTWSolverInfo(S),tnext);
215
 
216
    MdlOutputs(0);
217
 
218
    MdlUpdate(0);
219
    rt_SimUpdateDiscreteTaskSampleHits(rtmGetNumSampleTimes(S),
220
                                       rtmGetTimingData(S),
221
                                       rtmGetSampleHitPtr(S),
222
                                       rtmGetTPtr(S));
223
 
224
    if (rtmGetSampleTime(S,0) == CONTINUOUS_SAMPLE_TIME) {
225
        rt_UpdateContinuousStates(S);
226
    }
227
 
228
    GBLbuf.isrOverrun--;
229
 
230
    rtExtModeCheckEndTrigger();
231
 
232
} /* end rtOneStep */
233
 
234
int main() {
235
 
236
    RT_MODEL  *S;
237
    const char *status;
238
    real_T     finaltime = -2.0;
239
 
240
     /****************************
241
     * Initialize global memory *
242
     ****************************/
243
    (void)memset(&GBLbuf, 0, sizeof(GBLbuf));  
244
 
245
     /************************
246
     * Initialize the model *
247
     ************************/
248
    rt_InitInfAndNaN(sizeof(real_T));
249
 
250
    S = MODEL();
251
    if (rtmGetErrorStatus(S) != NULL) {
252
        cprintf("Error during model registration: %s\n",
253
                      rtmGetErrorStatus(S));
254
        sys_end();
255
    }
256
    if (finaltime >= 0.0 || finaltime == RUN_FOREVER) rtmSetTFinal(S,finaltime);
257
 
258
    MdlInitializeSizes();
259
    MdlInitializeSampleTimes();
260
 
261
    status = rt_SimInitTimingEngine(rtmGetNumSampleTimes(S),
262
                                    rtmGetStepSize(S),
263
                                    rtmGetSampleTimePtr(S),
264
                                    rtmGetOffsetTimePtr(S),
265
                                    rtmGetSampleHitPtr(S),
266
                                    rtmGetSampleTimeTaskIDPtr(S),
267
                                    rtmGetTStart(S),
268
                                    &rtmGetSimTimeStep(S),
269
                                    &rtmGetTimingData(S));
270
 
271
    if (status != NULL) {
272
        cprintf("Failed to initialize sample time engine: %s\n", status);
273
        sys_end();
274
    }
275
    rt_CreateIntegrationData(S);
276
 
277
    rtExtModeCheckInit();
278
    rtExtModeWaitForStartMsg(rtmGetRTWExtModeInfo(S),
279
                             (boolean_T *)&rtmGetStopRequested(S));
280
 
281
    cprintf("\n** starting the model **\n");
282
 
283
    MdlStart();
284
    if (rtmGetErrorStatus(S) != NULL) {
285
      GBLbuf.stopExecutionFlag = 1;
286
    }
287
 
288
     /*************************************************************************
289
     * Execute the model.  You may attach rtOneStep to an ISR, if so replace *
290
     * the call to rtOneStep (below) with a call to a background task        *
291
     * application.                                                          *
292
     *************************************************************************/
293
 
294
    if (rtmGetTFinal(S) == RUN_FOREVER) {
295
        cprintf("\n**May run forever. Model stop time set to infinity.**\n");
296
    }
297
 
298
    while (!GBLbuf.stopExecutionFlag &&
299
           (rtmGetTFinal(S) == RUN_FOREVER ||
300
            rtmGetTFinal(S)-rtmGetT(S) > rtmGetT(S)*DBL_EPSILON)) {
301
 
302
        rtExtModePauseIfNeeded(rtmGetRTWExtModeInfo(S),
303
                               (boolean_T *)&rtmGetStopRequested(S));
304
 
305
        cprintf(".");
306
 
307
        if (rtmGetStopRequested(S)) break;
308
        rt_OneStep(S);
309
    }
310
 
311
    if (!GBLbuf.stopExecutionFlag && !rtmGetStopRequested(S)) {
312
        /* Execute model last time step */
313
        rt_OneStep(S);
314
    }
315
 
316
    /********************
317
     * Cleanup and exit *
318
     ********************/
319
 
320
    rtExtModeShutdown();
321
 
322
    if (GBLbuf.errmsg) {
323
        cprintf("%s\n",GBLbuf.errmsg);
324
        sys_end();
325
    }
326
 
327
    if (GBLbuf.isrOverrun) {
328
        cprintf("%s: ISR overrun - base sampling rate is too fast\n",QUOTE(MODEL));
329
        sys_end();
330
    }
331
 
332
    if (rtmGetErrorStatus(S) != NULL) {
333
        cprintf("%s\n", rtmGetErrorStatus(S));
334
        sys_end();
335
    }
336
 
337
    MdlTerminate();
1344 giacomo 338
 
1342 giacomo 339
    return 0;
340
 
341
} /* end main */