Rev 1343 | Go to most recent revision | Details | 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 | GBLbuf.errmsg = rt_UpdateTXYLogVars(rtmGetRTWLogInfo(S), |
||
219 | rtmGetTPtr(S)); |
||
220 | if (GBLbuf.errmsg != NULL) { |
||
221 | GBLbuf.stopExecutionFlag = 1; |
||
222 | return; |
||
223 | } |
||
224 | |||
225 | MdlUpdate(0); |
||
226 | rt_SimUpdateDiscreteTaskSampleHits(rtmGetNumSampleTimes(S), |
||
227 | rtmGetTimingData(S), |
||
228 | rtmGetSampleHitPtr(S), |
||
229 | rtmGetTPtr(S)); |
||
230 | |||
231 | if (rtmGetSampleTime(S,0) == CONTINUOUS_SAMPLE_TIME) { |
||
232 | rt_UpdateContinuousStates(S); |
||
233 | } |
||
234 | |||
235 | GBLbuf.isrOverrun--; |
||
236 | |||
237 | rtExtModeCheckEndTrigger(); |
||
238 | |||
239 | } /* end rtOneStep */ |
||
240 | |||
241 | int main() { |
||
242 | |||
243 | RT_MODEL *S; |
||
244 | const char *status; |
||
245 | real_T finaltime = -2.0; |
||
246 | |||
247 | /**************************** |
||
248 | * Initialize global memory * |
||
249 | ****************************/ |
||
250 | (void)memset(&GBLbuf, 0, sizeof(GBLbuf)); |
||
251 | |||
252 | /************************ |
||
253 | * Initialize the model * |
||
254 | ************************/ |
||
255 | rt_InitInfAndNaN(sizeof(real_T)); |
||
256 | |||
257 | S = MODEL(); |
||
258 | if (rtmGetErrorStatus(S) != NULL) { |
||
259 | cprintf("Error during model registration: %s\n", |
||
260 | rtmGetErrorStatus(S)); |
||
261 | sys_end(); |
||
262 | } |
||
263 | if (finaltime >= 0.0 || finaltime == RUN_FOREVER) rtmSetTFinal(S,finaltime); |
||
264 | |||
265 | MdlInitializeSizes(); |
||
266 | MdlInitializeSampleTimes(); |
||
267 | |||
268 | status = rt_SimInitTimingEngine(rtmGetNumSampleTimes(S), |
||
269 | rtmGetStepSize(S), |
||
270 | rtmGetSampleTimePtr(S), |
||
271 | rtmGetOffsetTimePtr(S), |
||
272 | rtmGetSampleHitPtr(S), |
||
273 | rtmGetSampleTimeTaskIDPtr(S), |
||
274 | rtmGetTStart(S), |
||
275 | &rtmGetSimTimeStep(S), |
||
276 | &rtmGetTimingData(S)); |
||
277 | |||
278 | if (status != NULL) { |
||
279 | cprintf("Failed to initialize sample time engine: %s\n", status); |
||
280 | sys_end(); |
||
281 | } |
||
282 | rt_CreateIntegrationData(S); |
||
283 | |||
284 | rtExtModeCheckInit(); |
||
285 | rtExtModeWaitForStartMsg(rtmGetRTWExtModeInfo(S), |
||
286 | (boolean_T *)&rtmGetStopRequested(S)); |
||
287 | |||
288 | cprintf("\n** starting the model **\n"); |
||
289 | |||
290 | MdlStart(); |
||
291 | if (rtmGetErrorStatus(S) != NULL) { |
||
292 | GBLbuf.stopExecutionFlag = 1; |
||
293 | } |
||
294 | |||
295 | /************************************************************************* |
||
296 | * Execute the model. You may attach rtOneStep to an ISR, if so replace * |
||
297 | * the call to rtOneStep (below) with a call to a background task * |
||
298 | * application. * |
||
299 | *************************************************************************/ |
||
300 | |||
301 | if (rtmGetTFinal(S) == RUN_FOREVER) { |
||
302 | cprintf("\n**May run forever. Model stop time set to infinity.**\n"); |
||
303 | } |
||
304 | |||
305 | while (!GBLbuf.stopExecutionFlag && |
||
306 | (rtmGetTFinal(S) == RUN_FOREVER || |
||
307 | rtmGetTFinal(S)-rtmGetT(S) > rtmGetT(S)*DBL_EPSILON)) { |
||
308 | |||
309 | rtExtModePauseIfNeeded(rtmGetRTWExtModeInfo(S), |
||
310 | (boolean_T *)&rtmGetStopRequested(S)); |
||
311 | |||
312 | cprintf("."); |
||
313 | |||
314 | if (rtmGetStopRequested(S)) break; |
||
315 | rt_OneStep(S); |
||
316 | } |
||
317 | |||
318 | if (!GBLbuf.stopExecutionFlag && !rtmGetStopRequested(S)) { |
||
319 | /* Execute model last time step */ |
||
320 | rt_OneStep(S); |
||
321 | } |
||
322 | |||
323 | /******************** |
||
324 | * Cleanup and exit * |
||
325 | ********************/ |
||
326 | |||
327 | rtExtModeShutdown(); |
||
328 | |||
329 | if (GBLbuf.errmsg) { |
||
330 | cprintf("%s\n",GBLbuf.errmsg); |
||
331 | sys_end(); |
||
332 | } |
||
333 | |||
334 | if (GBLbuf.isrOverrun) { |
||
335 | cprintf("%s: ISR overrun - base sampling rate is too fast\n",QUOTE(MODEL)); |
||
336 | sys_end(); |
||
337 | } |
||
338 | |||
339 | if (rtmGetErrorStatus(S) != NULL) { |
||
340 | cprintf("%s\n", rtmGetErrorStatus(S)); |
||
341 | sys_end(); |
||
342 | } |
||
343 | |||
344 | MdlTerminate(); |
||
345 | return 0; |
||
346 | |||
347 | } /* end main */ |