Subversion Repositories shark

Rev

Rev 1418 | 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"
1418 giacomo 11
#include "modules/cabs.h"
1419 giacomo 12
#include "percorso.h"
1342 giacomo 13
 
1418 giacomo 14
#include "rtw.h"
15
 
1342 giacomo 16
/*=========*
17
 * Defines *
18
 *=========*/
19
 
20
#ifndef TRUE
21
#define FALSE (0)
22
#define TRUE  (1)
23
#endif
24
 
25
#ifndef EXIT_FAILURE
26
#define EXIT_FAILURE  1
27
#endif
28
#ifndef EXIT_SUCCESS
29
#define EXIT_SUCCESS  0
30
#endif
31
 
32
#define QUOTE1(name) #name
33
#define QUOTE(name) QUOTE1(name)    /* need to expand name    */
34
 
35
#define RUN_FOREVER -1.0
36
 
37
#define EXPAND_CONCAT(name1,name2) name1 ## name2
38
#define CONCAT(name1,name2) EXPAND_CONCAT(name1,name2)
39
#define RT_MODEL            CONCAT(MODEL,_rtModel)
40
 
41
extern RT_MODEL *MODEL(void);
42
 
43
extern void MdlInitializeSizes(void);
44
extern void MdlInitializeSampleTimes(void);
45
extern void MdlStart(void);
46
extern void MdlOutputs(int_T tid);
47
extern void MdlUpdate(int_T tid);
48
extern void MdlTerminate(void);
49
 
50
real_T rtInf;
51
real_T rtMinusInf;
52
real_T rtNaN;
53
 
1418 giacomo 54
CAB    input_cid[NINPUTCAB];
55
CAB    output_cid[NOUTPUTCAB];
56
 
1415 giacomo 57
#ifdef EXT_MODE
58
#  define rtExtModeSingleTaskUpload(S)    \
59
   {                                      \
60
        int stIdx;                        \
61
        rtExtModeUploadCheckTrigger();    \
62
        for (stIdx=0; stIdx<NUMST; stIdx++) {                   \
63
            if (rtmIsSampleHit(S, stIdx, 0 /*unused*/)) {       \
64
                rtExtModeUpload(stIdx,rtmGetTaskTime(S,stIdx)); \
65
            }                                                   \
66
        }                                                       \
67
   }
68
#else
69
#  define rtExtModeSingleTaskUpload(S) /* Do nothing */
70
#endif
71
 
1402 giacomo 72
#if NCSTATES > 0
73
  extern void rt_ODECreateIntegrationData(RTWSolverInfo *si);
74
  extern void rt_ODEUpdateContinuousStates(RTWSolverInfo *si);
75
 
76
# define rt_CreateIntegrationData(S) \
77
    rt_ODECreateIntegrationData(rtmGetRTWSolverInfo(S));
78
# define rt_UpdateContinuousStates(S) \
79
    rt_ODEUpdateContinuousStates(rtmGetRTWSolverInfo(S));
80
# else
1342 giacomo 81
# define rt_CreateIntegrationData(S)  \
82
      rtsiSetSolverName(rtmGetRTWSolverInfo(S),"FixedStepDiscrete");
83
# define rt_UpdateContinuousStates(S) \
84
      rtmSetT(S, rtsiGetSolverStopTime(rtmGetRTWSolverInfo(S)));
1402 giacomo 85
#endif
1342 giacomo 86
 
87
/*==================================*
88
 * Global data local to this module *
89
 *==================================*/
1368 giacomo 90
 
91
RT_MODEL  *S;
92
const char *status;
93
real_T     finaltime = -2.0;
94
volatile int simulation_run;                                                                                                                            
1342 giacomo 95
static struct {
96
  int_T    stopExecutionFlag;
97
  int_T    isrOverrun;
98
  int_T    overrunFlags[NUMST];
99
  const char_T *errmsg;
100
} GBLbuf;
101
 
102
/* Function: rt_InitInfAndNaN ==================================================
103
 * Abstract:
104
 *      Initialize the rtInf, rtMinusInf, and rtNaN needed by the
105
 *      generated code. NaN is initialized as non-signaling. Assumes IEEE.
106
 */
107
void rt_InitInfAndNaN(int_T realSize)
108
{
109
    short one = 1;
110
    enum {
111
        LittleEndian,
112
        BigEndian
113
    } machByteOrder = (*((char *) &one) == 1) ? LittleEndian : BigEndian;
114
 
115
    switch (realSize) {  
116
      case 4:
117
        switch (machByteOrder) {
118
          case LittleEndian: {
119
              typedef struct {
120
                  uint32_T fraction : 23;
121
                  uint32_T exponent  : 8;
122
                  uint32_T sign      : 1;
123
              } LittleEndianIEEEDouble;
124
 
125
              (*(LittleEndianIEEEDouble*)&rtInf).sign      = 0;
126
              (*(LittleEndianIEEEDouble*)&rtInf).exponent  = 0xFF;
127
              (*(LittleEndianIEEEDouble*)&rtInf).fraction  = 0;
128
              rtMinusInf = rtInf;
129
              rtNaN = rtInf;
130
              (*(LittleEndianIEEEDouble*)&rtMinusInf).sign = 1;
131
              (*(LittleEndianIEEEDouble*)&rtNaN).fraction  = 0x7FFFFF;
132
          }
133
          break;
134
          case BigEndian: {
135
              typedef struct {
136
                  uint32_T sign      : 1;
137
                  uint32_T exponent  : 8;
138
                  uint32_T fraction  : 23;
139
              } BigEndianIEEEDouble;
140
 
141
              (*(BigEndianIEEEDouble*)&rtInf).sign      = 0;
142
              (*(BigEndianIEEEDouble*)&rtInf).exponent  = 0xFF;
143
              (*(BigEndianIEEEDouble*)&rtInf).fraction  = 0;
144
              rtMinusInf = rtInf;
145
              rtNaN = rtInf;
146
              (*(BigEndianIEEEDouble*)&rtMinusInf).sign = 1;
147
              (*(BigEndianIEEEDouble*)&rtNaN).fraction  = 0x7FFFFF;
148
          }
149
          break;
150
        }
151
        break;    
152
 
153
      case 8:
154
        switch (machByteOrder) {
155
          case LittleEndian: {
156
              typedef struct {
157
                  struct {
158
                      uint32_T fraction2;
159
                  } wordH;
160
                  struct {
161
                      uint32_T fraction1 : 20;
162
                      uint32_T exponent  : 11;
163
                      uint32_T sign      : 1;
164
                  } wordL;
165
              } LittleEndianIEEEDouble;
166
 
167
              (*(LittleEndianIEEEDouble*)&rtInf).wordL.sign      = 0;
168
              (*(LittleEndianIEEEDouble*)&rtInf).wordL.exponent  = 0x7FF;
169
              (*(LittleEndianIEEEDouble*)&rtInf).wordL.fraction1 = 0;
170
              (*(LittleEndianIEEEDouble*)&rtInf).wordH.fraction2 = 0;
171
 
172
              rtMinusInf = rtInf;
173
              (*(LittleEndianIEEEDouble*)&rtMinusInf).wordL.sign = 1;
174
              (*(LittleEndianIEEEDouble*)&rtNaN).wordL.sign      = 0;
175
              (*(LittleEndianIEEEDouble*)&rtNaN).wordL.exponent  = 0x7FF;
176
              (*(LittleEndianIEEEDouble*)&rtNaN).wordL.fraction1 = 0xFFFFF;
177
              (*(LittleEndianIEEEDouble*)&rtNaN).wordH.fraction2 = 0xFFFFFFFF;
178
          }
179
          break;
180
          case BigEndian: {
181
              typedef struct {
182
                  struct {
183
                      uint32_T sign      : 1;
184
                      uint32_T exponent  : 11;
185
                      uint32_T fraction1 : 20;
186
                  } wordL;
187
                  struct {
188
                      uint32_T fraction2;
189
                  } wordH;
190
              } BigEndianIEEEDouble;
191
 
192
              (*(BigEndianIEEEDouble*)&rtInf).wordL.sign      = 0;
193
              (*(BigEndianIEEEDouble*)&rtInf).wordL.exponent  = 0x7FF;
194
              (*(BigEndianIEEEDouble*)&rtInf).wordL.fraction1 = 0;
195
              (*(BigEndianIEEEDouble*)&rtInf).wordH.fraction2 = 0;
196
 
197
              rtMinusInf = rtInf;
198
              (*(BigEndianIEEEDouble*)&rtMinusInf).wordL.sign = 1;
199
              (*(BigEndianIEEEDouble*)&rtNaN).wordL.sign      = 0;
200
              (*(BigEndianIEEEDouble*)&rtNaN).wordL.exponent  = 0x7FF;
201
              (*(BigEndianIEEEDouble*)&rtNaN).wordL.fraction1 = 0xFFFFF;
202
              (*(BigEndianIEEEDouble*)&rtNaN).wordH.fraction2 = 0xFFFFFFFF;
203
          }
204
          break;
205
        }
206
        break;
207
      default:
1369 giacomo 208
        cprintf("Error: Unable to initialize rtInf, rtMinusInf and rtNaN\n");
1342 giacomo 209
        sys_end();
210
        break;
211
    }
212
 
213
} /* end rt_InitInfAndNaN */
214
 
215
/* Function: rtOneStep ========================================================
216
 *
217
 * Abstract:
218
 *      Perform one step of the model. This function is modeled such that
219
 *      it could be called from an interrupt service routine (ISR) with minor
220
 *      modifications.
221
 */
222
static void rt_OneStep(RT_MODEL *S)
223
{
224
    real_T tnext;
1415 giacomo 225
 
1342 giacomo 226
    /***********************************************
227
     * Check and see if base step time is too fast *
228
     ***********************************************/
1415 giacomo 229
 
1342 giacomo 230
    if (GBLbuf.isrOverrun++) {
231
        GBLbuf.stopExecutionFlag = 1;
232
        return;
233
    }
1415 giacomo 234
 
1342 giacomo 235
    /***********************************************
236
     * Check and see if error status has been set  *
237
     ***********************************************/
1415 giacomo 238
 
1342 giacomo 239
    if (rtmGetErrorStatus(S) != NULL) {
240
        GBLbuf.stopExecutionFlag = 1;
241
        return;
242
    }
1415 giacomo 243
 
1342 giacomo 244
    /* enable interrupts here */
1415 giacomo 245
 
1342 giacomo 246
    rtExtModeOneStep(rtmGetRTWExtModeInfo(S),
1415 giacomo 247
                        (boolean_T *)&rtmGetStopRequested(S));
248
 
1342 giacomo 249
    tnext = rt_SimGetNextSampleHit();
250
    rtsiSetSolverStopTime(rtmGetRTWSolverInfo(S),tnext);
1415 giacomo 251
 
1342 giacomo 252
    MdlOutputs(0);
1415 giacomo 253
 
254
    rtExtModeSingleTaskUpload(S);
255
 
256
    if (GBLbuf.errmsg != NULL) {
257
        GBLbuf.stopExecutionFlag = 1;
258
        return;
259
    }
260
 
1342 giacomo 261
    MdlUpdate(0);
262
    rt_SimUpdateDiscreteTaskSampleHits(rtmGetNumSampleTimes(S),
263
                                       rtmGetTimingData(S),
264
                                       rtmGetSampleHitPtr(S),
265
                                       rtmGetTPtr(S));
1415 giacomo 266
 
1342 giacomo 267
    if (rtmGetSampleTime(S,0) == CONTINUOUS_SAMPLE_TIME) {
268
        rt_UpdateContinuousStates(S);
269
    }
1415 giacomo 270
 
1342 giacomo 271
    GBLbuf.isrOverrun--;
1415 giacomo 272
 
1342 giacomo 273
    rtExtModeCheckEndTrigger();
274
 
275
} /* end rtOneStep */
276
 
1368 giacomo 277
void Init_RealTime_Workshop() {
1342 giacomo 278
 
1368 giacomo 279
    /****************************
1342 giacomo 280
     * Initialize global memory *
281
     ****************************/
1368 giacomo 282
 
1342 giacomo 283
    (void)memset(&GBLbuf, 0, sizeof(GBLbuf));  
284
 
1368 giacomo 285
    /************************
1342 giacomo 286
     * Initialize the model *
287
     ************************/
1368 giacomo 288
 
1342 giacomo 289
    rt_InitInfAndNaN(sizeof(real_T));
290
 
291
    S = MODEL();
292
    if (rtmGetErrorStatus(S) != NULL) {
1369 giacomo 293
        cprintf("Error: Model registration: %s\n",
1342 giacomo 294
                      rtmGetErrorStatus(S));
295
        sys_end();
296
    }
1368 giacomo 297
 
1342 giacomo 298
    if (finaltime >= 0.0 || finaltime == RUN_FOREVER) rtmSetTFinal(S,finaltime);
299
 
300
    MdlInitializeSizes();
301
    MdlInitializeSampleTimes();
302
 
303
    status = rt_SimInitTimingEngine(rtmGetNumSampleTimes(S),
304
                                    rtmGetStepSize(S),
305
                                    rtmGetSampleTimePtr(S),
306
                                    rtmGetOffsetTimePtr(S),
307
                                    rtmGetSampleHitPtr(S),
308
                                    rtmGetSampleTimeTaskIDPtr(S),
309
                                    rtmGetTStart(S),
310
                                    &rtmGetSimTimeStep(S),
311
                                    &rtmGetTimingData(S));
312
 
313
    if (status != NULL) {
1369 giacomo 314
        cprintf("Error: Failed to initialize sample time engine: %s\n", status);
1342 giacomo 315
        sys_end();
316
    }
1368 giacomo 317
 
1342 giacomo 318
    rt_CreateIntegrationData(S);
319
 
320
    rtExtModeCheckInit();
321
    rtExtModeWaitForStartMsg(rtmGetRTWExtModeInfo(S),
322
                             (boolean_T *)&rtmGetStopRequested(S));
323
 
1368 giacomo 324
    cprintf("\n** Starting the model **\n");
1342 giacomo 325
 
326
    MdlStart();
327
    if (rtmGetErrorStatus(S) != NULL) {
328
      GBLbuf.stopExecutionFlag = 1;
329
    }
330
 
331
     /*************************************************************************
332
     * Execute the model.  You may attach rtOneStep to an ISR, if so replace *
333
     * the call to rtOneStep (below) with a call to a background task        *
334
     * application.                                                          *
335
     *************************************************************************/
336
 
337
    if (rtmGetTFinal(S) == RUN_FOREVER) {
338
        cprintf("\n**May run forever. Model stop time set to infinity.**\n");
339
    }
340
 
1368 giacomo 341
}
1342 giacomo 342
 
1368 giacomo 343
void Close_RealTime_Workshop() {
1342 giacomo 344
 
345
    /********************
346
     * Cleanup and exit *
347
     ********************/
348
 
349
    rtExtModeShutdown();
350
 
351
    if (GBLbuf.errmsg) {
1369 giacomo 352
        cprintf("Error: %s\n",GBLbuf.errmsg);
1342 giacomo 353
        sys_end();
354
    }
355
 
356
    if (GBLbuf.isrOverrun) {
1369 giacomo 357
        cprintf("Error: %s: ISR overrun - base sampling rate is too fast\n",QUOTE(MODEL));
1342 giacomo 358
        sys_end();
359
    }
360
 
361
    if (rtmGetErrorStatus(S) != NULL) {
1369 giacomo 362
        cprintf("Error: %s\n", rtmGetErrorStatus(S));
1342 giacomo 363
        sys_end();
364
    }
365
 
1368 giacomo 366
    MdlTerminate();
1344 giacomo 367
 
1368 giacomo 368
}
369
 
370
TASK RTW_body(void *arg) {
371
 
1419 giacomo 372
    real_T *input, *p;
373
 
374
    input = (real_T *)rtmGetU(S);
1368 giacomo 375
    simulation_run = 1;
376
 
377
    while (!GBLbuf.stopExecutionFlag &&
378
           (rtmGetTFinal(S) == RUN_FOREVER ||
379
            rtmGetTFinal(S)-rtmGetT(S) > rtmGetT(S)*DBL_EPSILON)) {
380
 
381
        rtExtModePauseIfNeeded(rtmGetRTWExtModeInfo(S),
382
                               (boolean_T *)&rtmGetStopRequested(S));
383
 
384
        if (rtmGetStopRequested(S)) break;
1419 giacomo 385
 
386
        /* Get PAR0 */
387
        if (input_cid[0] != -1) {
388
          p = (real_T *)cab_getmes(input_cid[0]);
389
          input[0] = *p;
390
          cab_unget(input_cid[0], p);
391
        }
392
 
393
        /* Get PAR1 */
394
        if (input_cid[1] != -1) {
395
          p = (real_T *)cab_getmes(input_cid[1]);
396
          input[1] = *p;
397
          cab_unget(input_cid[1], p);
398
        }
399
 
1368 giacomo 400
        rt_OneStep(S);
401
 
402
        task_endcycle();
403
 
404
    }
405
 
406
    if (!GBLbuf.stopExecutionFlag && !rtmGetStopRequested(S)) {
407
        /* Execute model last time step */
408
        rt_OneStep(S);
409
    }
410
 
411
    simulation_run = 0;
412
 
413
    return NULL;
414
 
415
}
416
 
1419 giacomo 417
TASK OUTPUT_body(void *arg) {
1403 giacomo 418
 
1419 giacomo 419
  real_T *output, *p;
420
  real_T th;
1403 giacomo 421
 
1419 giacomo 422
  POINT pos;
423
  PATH_ELEM *e;
424
  real_T distance = 0;
425
  real_T angle = 0;
426
  real_T curve = 0;
1403 giacomo 427
 
1419 giacomo 428
  output_cid[0] = cab_create("DISTANCE", sizeof(real_T), 2);
429
  output_cid[1] = cab_create("ANGLE", sizeof(real_T), 2);
430
  output_cid[2] = cab_create("CURVE", sizeof(real_T), 2);
1403 giacomo 431
 
1419 giacomo 432
  output = (real_T *)rtmGetY(S);
1403 giacomo 433
 
1419 giacomo 434
  while(1) {
1403 giacomo 435
 
1419 giacomo 436
    pos.x = output[0];
437
    pos.y = output[1];
438
    th = output[2];
439
 
440
    e = find_closest_elem(&pos, 1.0);
441
    if (e != 0) {
442
      distance = get_distance_from_elem(&pos, e);
443
      angle = th - get_angle_from_elem(&pos,e);
444
      curve = is_curve(e);
445
    }
446
 
447
    /* Send Distance */
448
    p = (real_T *)cab_reserve(output_cid[0]);
449
    *p = distance;
450
    cab_putmes(output_cid[0], p);
1418 giacomo 451
 
1419 giacomo 452
    /* Send Angle */
453
    p = (real_T *)cab_reserve(output_cid[1]);
454
    *p = angle;
455
    cab_putmes(output_cid[1], p);
1418 giacomo 456
 
1419 giacomo 457
    /* Send Curve */
458
    p = (real_T *)cab_reserve(output_cid[2]);
459
    *p = curve;
460
    cab_putmes(output_cid[2], p);
1403 giacomo 461
 
1419 giacomo 462
    task_endcycle();
463
  }
464
  return NULL;
1403 giacomo 465
}
466
 
1419 giacomo 467
void Init_Rtw() {
1418 giacomo 468
 
1419 giacomo 469
    HARD_TASK_MODEL     RTW_task,OUTPUT_task;
470
    PID                 RTW_pid,OUTPUT_pid;
1416 giacomo 471
 
1419 giacomo 472
    int i;
1416 giacomo 473
 
474
    int RTW_period;
475
    int RTW_wcet;
1419 giacomo 476
 
1416 giacomo 477
    int OUTPUT_period;
478
    int OUTPUT_wcet;
479
 
1368 giacomo 480
    Init_RealTime_Workshop();
1419 giacomo 481
    Init_All_Path();
1368 giacomo 482
 
1418 giacomo 483
    for (i=0;i<NINPUTCAB;i++) input_cid[i] = -1;
484
    for (i=0;i<NOUTPUTCAB;i++) output_cid[i] = -1;
1419 giacomo 485
 
1416 giacomo 486
    RTW_period =(int)(rtmGetStepSize(S) * 1000000.0);
487
    RTW_wcet = (int)(rtmGetStepSize(S) * 1000000.0 * 0.45);
1419 giacomo 488
 
1418 giacomo 489
    OUTPUT_period = (int)(10000.0);
490
    OUTPUT_wcet = (int)(10000.0 * 0.10);
1419 giacomo 491
 
1416 giacomo 492
    cprintf("Task Setup\n");
493
    cprintf("RTW    (P %8d W %8d)\n",RTW_period,RTW_wcet);
494
    cprintf("OUTPUT (P %8d W %8d)\n",OUTPUT_period,OUTPUT_wcet);
1419 giacomo 495
 
1368 giacomo 496
    hard_task_default_model(RTW_task);
1416 giacomo 497
    hard_task_def_mit(RTW_task,RTW_period);
498
    hard_task_def_wcet(RTW_task,RTW_wcet);
1368 giacomo 499
    hard_task_def_usemath(RTW_task);
500
    hard_task_def_ctrl_jet(RTW_task);
1419 giacomo 501
 
1368 giacomo 502
    RTW_pid = task_create("RTW",RTW_body,&RTW_task,NULL);
503
    if (RTW_pid == NIL) {
1419 giacomo 504
        cprintf("Error: Cannot create RealTime Workshop [RTW] Task\n");
1403 giacomo 505
        sys_end();
506
    }
507
 
1416 giacomo 508
    hard_task_default_model(OUTPUT_task);
509
    hard_task_def_mit(OUTPUT_task,OUTPUT_period);
510
    hard_task_def_wcet(OUTPUT_task,OUTPUT_wcet);
511
    hard_task_def_usemath(OUTPUT_task);
512
    hard_task_def_ctrl_jet(OUTPUT_task);
513
 
1419 giacomo 514
    OUTPUT_pid = task_create("OUTPUT",OUTPUT_body,&OUTPUT_task,NULL);
1416 giacomo 515
    if (OUTPUT_pid == NIL) {
1419 giacomo 516
        cprintf("Error: Cannot create RealTime Workshop [OUTPUT] Task\n");
1416 giacomo 517
        sys_end();
518
    }
1419 giacomo 519
 
1368 giacomo 520
    task_activate(RTW_pid);
1419 giacomo 521
    task_activate(OUTPUT_pid);
1368 giacomo 522
 
1419 giacomo 523
}
524
 
525
int main() {
526
 
527
    Init_Rtw();
528
 
1418 giacomo 529
    activate_sensors();
1419 giacomo 530
    activate_control();
1418 giacomo 531
 
1368 giacomo 532
    while(simulation_run);
533
 
534
    Close_RealTime_Workshop();
535
 
536
    sys_end();
537
 
1342 giacomo 538
    return 0;
539
 
1368 giacomo 540
}