-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
518 lines (434 loc) · 14.7 KB
/
main.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* Copyright 2013-2018 The MathWorks, Inc.
*
* File: rt_cppclass_main.cpp
*
* Abstract:
*
* A real-time main that runs generated Simulink Coder code under most
* operating systems. Based on the definition of MULTITASKING, a single-task
* or multitask step function is employed.
*
* This file is a useful starting point for creating a custom main when
* targeting a custom floating point processor or integer micro-controller.
*
* Alternatively for ERT targets, you can generate a sample ert_main.cpp file
* with the generated code by selecting the "Generate an example main program"
* option. In this case, ert_main.cpp is precisely customized to the
* model requirements.
*
* Required Defines:
*
* MODEL - Model name
* NUMST - Number of sample times
*
*/
/*==================*
* Required defines *
*==================*/
#ifndef MODEL
# error Must specify a model name. Define MODEL=name.
#else
/* create generic macros that work with any model */
# define EXPAND_CONCAT(name1,name2) name1 ## name2
# define CONCAT(name1,name2) EXPAND_CONCAT(name1,name2)
# define MODEL_INSTANCE CONCAT(MODEL,_Obj)
#endif
#ifndef NUMST
# error Must specify the number of sample times. Define NUMST=number.
#endif
#if CLASSIC_INTERFACE == 1
# error "Classic call interface is not supported by rt_cppclass_main.cpp."
#endif
#if ONESTEPFCN==0
#error Separate output and update functions are not supported by rt_cppclass_main.cpp. \
You must update rt_cppclass_main.cpp to suit your application needs, or select \
the 'Single output/update function' option.
#endif
#if TERMFCN==0
#error The terminate function is required by rt_cppclass_main.cpp. \
You must update rt_cppclass_main.cpp to suit your application needs, or select \
the 'Terminate function required' option.
#endif
#define QUOTE1(name) #name
#define QUOTE(name) QUOTE1(name) /* need to expand name */
#ifndef SAVEFILE
# define MATFILE2(file) #file ".mat"
# define MATFILE1(file) MATFILE2(file)
# define MATFILE MATFILE1(MODEL)
#else
# define MATFILE QUOTE(SAVEFILE)
#endif
/*==========*
* Includes *
*==========*/
#include "my_mpcACCsystem.h"
#include <iostream>
#include <dios.h>
#include <sys/divm.h>
#include <stdbool.h>
#include <map>
#include "rtwtypes.h"
#if !defined(INTEGER_CODE) || INTEGER_CODE == 0
# include <stdio.h> /* optional for printf */
#else
extern "C" {
extern int printf(const char *, ...);
extern int fflush(void *);
}
#endif
#include <string.h> /* optional for strcmp */
#include "rtmodel.h" /* includes model.h */
#include "rt_logging.h"
#ifdef UseMMIDataLogging
#include "rt_logging_mmi.h"
#endif
#include "ext_work.h"
#ifdef MODEL_STEP_FCN_CONTROL_USED
#error rt_cppclass_main.cpp does not support model step function prototype control.
#endif
/*========================*
* Setup for multitasking *
*========================*/
/*
* Let MT be synonym for MULTITASKING (to shorten command line for DOS)
*/
#if defined(MT)
# if MT == 0
# undef MT
# else
# define MULTITASKING 1
# endif
#endif
#if defined(TID01EQ) && TID01EQ == 1
#define FIRST_TID 1
#else
#define FIRST_TID 0
#endif
/*====================*
* External functions *
*====================*/
#if !defined(MULTITASKING)
/* single-rate step function */
#define MODEL_STEP(obj) obj.MODEL_STEPNAME()
#else
/* multirate step function */
#define MODEL_STEP MODEL_STEPNAME
#endif
/*==================================*
* Global data local to this module *
*==================================*/
#ifndef MULTITASKING
static boolean_T OverrunFlags[1]; /* ISR overrun flags */
static boolean_T eventFlags[1]; /* necessary for overlapping preemption */
#else
static boolean_T OverrunFlags[NUMST];
static boolean_T eventFlags[NUMST];
#endif
/* Create model instance */
static MODEL_CLASSNAME MODEL_INSTANCE;
/*===================*
* Visible functions *
*===================*/
#if !defined(MULTITASKING) /* single task */
/* Function: rtOneStep ========================================================
*
* Abstract:
* Perform one step of the model. This function is modeled such that
* it could be called from an interrupt service routine (ISR) with minor
* modifications.
*/
static void rt_OneStep(MODEL_CLASSNAME & mdl)
{
/* Disable interrupts here */
/***********************************************
* Check and see if base step time is too fast *
***********************************************/
if (OverrunFlags[0]++) {
rtmSetErrorStatus(mdl.getRTM(), "Overrun");
}
/*************************************************
* Check and see if an error status has been set *
* by an overrun or by the generated code. *
*************************************************/
if (rtmGetErrorStatus(mdl.getRTM()) != NULL) {
return;
}
/* Save FPU context here (if necessary) */
/* Re-enable interrupts here */
/* Set model inputs here */
/**************
* Step model *
**************/
MODEL_STEP(mdl);
/* Get model outputs here */
/**************************
* Decrement overrun flag *
**************************/
OverrunFlags[0]--;
rtExtModeCheckEndTrigger();
/* Disable interrupts here */
/* Restore FPU context here (if necessary) */
/* Re-enable interrupts here */
} /* end rtOneStep */
#else /* multitask */
/* Function: rtOneStep ========================================================
*
* Abstract:
* Perform one step of the model. This function is modeled such that
* it could be called from an interrupt service routine (ISR) with minor
* modifications.
*
* Note that error checking is only necessary when this routine is
* attached to an interrupt.
*
* Also, you may wish to unroll any or all of for and while loops to
* improve the real-time performance of this function.
*/
static void rt_OneStep(MODEL_CLASSNAME & mdl)
{
int_T i;
/* Disable interrupts here */
/***********************************************
* Check and see if base step time is too fast *
***********************************************/
if (OverrunFlags[0]++) {
rtmSetErrorStatus(mdl.getRTM(), "Overrun");
}
/*************************************************
* Check and see if an error status has been set *
* by an overrun or by the generated code. *
*************************************************/
if (rtmGetErrorStatus(mdl.getRTM()) != NULL) {
return;
}
/* Save FPU context here (if necessary) */
/* Re-enable interrupts here */
/*************************************************
* Update EventFlags and check subrate overrun *
*************************************************/
for (i = FIRST_TID+1; i < NUMST; i++) {
if (rtmStepTask(mdl.getRTM(),i) && eventFlags[i]++) {
OverrunFlags[0]--;
OverrunFlags[i]++;
/* Sampling too fast */
rtmSetErrorStatus(mdl.getRTM(), "Overrun");
return;
}
if (++rtmTaskCounter(mdl.getRTM(),i) == rtmCounterLimit(mdl.getRTM(),i))
rtmTaskCounter(mdl.getRTM(), i) = 0;
}
/* Set model inputs associated with base rate here */
/*******************************************
* Step the model for the base sample time *
*******************************************/
MODEL_STEP(mdl,0);
/* Get model outputs associated with base rate here */
/************************************************************************
* Model step complete for base sample time, now it is okay to *
* re-interrupt this ISR. *
************************************************************************/
OverrunFlags[0]--;
/*********************************************************
* Step the model for any other sample times (subrates) *
*********************************************************/
for (i = FIRST_TID+1; i < NUMST; i++) {
/*************************************************************
* If task "i" is running, don't run any lower priority task *
*************************************************************/
if (OverrunFlags[i]) return;
if (eventFlags[i]) {
OverrunFlags[i]++;
/* Set model inputs associated with subrate here */
/******************************************
* Step the model for sample time "i" *
******************************************/
MODEL_STEP(mdl,i);
/* Get model outputs associated with subrate here */
/**********************************************
* Indicate task complete for sample time "i" *
**********************************************/
OverrunFlags[i]--;
eventFlags[i]--;
}
}
rtExtModeCheckEndTrigger();
/* Disable interrupts here */
/* Restore FPU context here (if necessary) */
/* Enable interrupts here */
} /* end rtOneStep */
#endif /* MULTITASKING */
/* Function: rt_InitModel ====================================================
*
* Abstract:
* Initialized the model and the overrun flags
*
*/
static void rt_InitModel(MODEL_CLASSNAME & mdl)
{
#if defined(MULTITASKING)
int i;
for(i=0; i < NUMST; i++) {
OverrunFlags[i] = 0;
eventFlags[i] = 0;
}
#else
OverrunFlags[0] = 0;
eventFlags[0] = 0;
#endif
/************************
* Initialize the model *
************************/
mdl.initialize();
}
/* Function: rt_TermModel ====================================================
*
* Abstract:
* Terminates the model and prints the error status
*
*/
static int_T rt_TermModel(MODEL_CLASSNAME & mdl)
{
mdl.terminate();
{
const char_T *errStatus = (const char_T *) (rtmGetErrorStatus(mdl.getRTM()));
if (errStatus != NULL && strcmp(errStatus, "Simulation finished")) {
(void)printf("%s\n", errStatus);
#if defined(MULTITASKING)
int_T i;
for (i = 0; i < NUMST; i++) {
if (OverrunFlags[i]) {
(void)printf("ISR overrun - sampling rate too"
"fast for sample time index %d.\n", i);
}
}
#else
if (OverrunFlags[0]) {
(void)printf("ISR overrun - base sampling rate too fast.\n");
}
#endif
return(1);
}
}
return(0);
}
/* Function: next ====================================================
*
* Abstract:
* Sync the büchi automata (property) with the system
* Property: <>([]safe)
* Type: Liveness (execute with --liveness)
*/
int next( int state, std::map<APs, bool> AP ) {
__dios_trace_f("AP[safe]: %x", AP[safe]);
switch ( state ) {
case -1:
return 0;
/*
T0_init:
if
:: (true) -> goto T0_init
:: (safe) -> goto accept_S1
fi;
*/
case 0:
if (!AP[safe]) {return 0;}
if (AP[safe]) {return __vm_choose( 2 ) ? 0 : 1;}
/*
accept_S1:
if
:: (safe) -> goto accept_S1
:: (!(safe)) -> goto T0_S2
fi;
*/
case 1:
__vm_ctl_flag( 0, _VM_CF_Accepting);
if (AP[safe]) {return 1;}
if (!AP[safe]) {return 2;}
/*
T0_S2:
if
:: (true) -> goto T0_S2
fi;
*/
case 2:
return 2;
default:
return state;
}
}
/* Function: main =============================================================
*
* Abstract:
* Execute model on a generic target such as a workstation.
*/
int_T main(int_T argc, const char *argv[])
{
int_T ret;
/* External mode */
rtParseArgsForExtMode(argc, argv);
/*******************************************
* warn if the model will run indefinitely *
*******************************************/
#if MAT_FILE==0 && EXT_MODE==0
printf("warning: the simulation will run with no stop time; "
"to change this behavior select the 'MAT-file logging' option\n");
fflush(NULL);
#endif
/************************
* Initialize the model *
************************/
rt_InitModel(MODEL_INSTANCE);
/* External mode */
rtSetTFinalForExtMode(&rtmGetTFinal(MODEL_INSTANCE.getRTM()));
rtExtModeCheckInit(NUMST);
rtExtModeWaitForStartPkt(rtmGetRTWExtModeInfo(MODEL_INSTANCE.getRTM()),
NUMST,
(boolean_T *)&rtmGetStopRequested(MODEL_INSTANCE.getRTM()));
(void)printf("\n** starting the model **\n");
/***********************************************************************
* Execute (step) the model. You may also attach rtOneStep to an ISR, *
* in which case you replace the call to rtOneStep with a call to a *
* background task. Note that the generated code sets error status *
* to "Simulation finished" when MatFileLogging is specified in TLC. *
***********************************************************************/
/***********************************************************************
* Instructions to embed property
* while ( true )
* {
* __dios_reschedule();
* system_step();
* buchi_step();
* }
* */
/*states for model checking with Divine*/
int state = -1, oldstate=0;
//while (rtmGetErrorStatus(MODEL_INSTANCE.getRTM()) == NULL &&
//!rtmGetStopRequested(MODEL_INSTANCE.getRTM())) {
while(true){
__dios_reschedule();
oldstate = state;
rtExtModePauseIfNeeded(rtmGetRTWExtModeInfo(MODEL_INSTANCE.getRTM()),
NUMST,
(boolean_T *)&rtmGetStopRequested(MODEL_INSTANCE.getRTM()));
if (rtmGetStopRequested(MODEL_INSTANCE.getRTM())) break;
/* external mode */
rtExtModeOneStep(rtmGetRTWExtModeInfo(MODEL_INSTANCE.getRTM()),
NUMST,
(boolean_T *)&rtmGetStopRequested(MODEL_INSTANCE.getRTM()));
rt_OneStep(MODEL_INSTANCE);
state = next( state, MODEL_INSTANCE.AP);
__dios_trace_f( "state: %d -> %d", oldstate, state );
}
/*******************************
* Cleanup and exit (optional) *
*******************************/
#ifdef UseMMIDataLogging
rt_CleanUpForStateLogWithMMI(rtmGetRTWLogInfo(MODEL_INSTANCE.getRTM()));
#endif
rt_StopDataLogging(MATFILE,rtmGetRTWLogInfo(MODEL_INSTANCE.getRTM()));
ret = rt_TermModel(MODEL_INSTANCE);
rtExtModeShutdown(NUMST);
return ret;
}
/* [EOF] main.cpp */