1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include "RaspberrySmithPredictor.h" |
4 | #include "RaspberrySmithPredictor_private.h" |
5 | #include "rtwtypes.h" |
6 | #include "limits.h" |
7 | #include "rt_nonfinite.h" |
8 | #include "linuxinitialize.h" |
9 | #define UNUSED(x) x = x |
10 | |
11 | /* Function prototype declaration*/ |
12 | void exitFcn(int sig); |
13 | void *terminateTask(void *arg); |
14 | void *baseRateTask(void *arg); |
15 | void *subrateTask(void *arg); |
16 | volatile boolean_T runModel = true; |
17 | sem_t stopSem; |
18 | sem_t baserateTaskSem; |
19 | pthread_t schedulerThread; |
20 | pthread_t baseRateThread; |
21 | unsigned long threadJoinStatus[8]; |
22 | int terminatingmodel = 0; |
23 | void *baseRateTask(void *arg) |
24 | { |
25 | runModel = (rtmGetErrorStatus(RaspberrySmithPredictor_M) == (NULL)) && |
26 | !rtmGetStopRequested(RaspberrySmithPredictor_M); |
27 | while (runModel) { |
28 | sem_wait(&baserateTaskSem); |
29 | |
30 | /* External mode */ |
31 | { |
32 | boolean_T rtmStopReq = false; |
33 | rtExtModePauseIfNeeded(RaspberrySmithPredictor_M->extModeInfo, 2, |
34 | &rtmStopReq); |
35 | if (rtmStopReq) { |
36 | rtmSetStopRequested(RaspberrySmithPredictor_M, true); |
37 | } |
38 | |
39 | if (rtmGetStopRequested(RaspberrySmithPredictor_M) == true) { |
40 | rtmSetErrorStatus(RaspberrySmithPredictor_M, "Simulation finished"); |
41 | break; |
42 | } |
43 | } |
44 | |
45 | /* External mode */ |
46 | { |
47 | boolean_T rtmStopReq = false; |
48 | rtExtModeOneStep(RaspberrySmithPredictor_M->extModeInfo, 2, &rtmStopReq); |
49 | if (rtmStopReq) { |
50 | rtmSetStopRequested(RaspberrySmithPredictor_M, true); |
51 | } |
52 | } |
53 | |
54 | RaspberrySmithPredictor_step(); |
55 | |
56 | /* Get model outputs here */ |
57 | rtExtModeCheckEndTrigger(); |
58 | runModel = (rtmGetErrorStatus(RaspberrySmithPredictor_M) == (NULL)) && |
59 | !rtmGetStopRequested(RaspberrySmithPredictor_M); |
60 | } |
61 | |
62 | runModel = 0; |
63 | terminateTask(arg); |
64 | pthread_exit((void *)0); |
65 | return NULL; |
66 | } |
67 | |
68 | void exitFcn(int sig) |
69 | { |
70 | UNUSED(sig); |
71 | rtmSetErrorStatus(RaspberrySmithPredictor_M, "stopping the model"); |
72 | } |
73 | |
74 | void *terminateTask(void *arg) |
75 | { |
76 | UNUSED(arg); |
77 | terminatingmodel = 1; |
78 | printf("**terminating the model**\n"); |
79 | fflush(stdout); |
80 | |
81 | { |
82 | runModel = 0; |
83 | } |
84 | |
85 | rtExtModeShutdown(2); |
86 | |
87 | /* Disable rt_OneStep() here */ |
88 | |
89 | /* Terminate model */ |
90 | RaspberrySmithPredictor_terminate(); |
91 | sem_post(&stopSem); |
92 | return NULL; |
93 | } |
94 | |
95 | int main(int argc, char **argv) |
96 | { |
97 | UNUSED(argc); |
98 | UNUSED(argv); |
99 | printf("**starting the model**\n"); |
100 | fflush(stdout); |
101 | rtmSetErrorStatus(RaspberrySmithPredictor_M, 0); |
102 | rtExtModeParseArgs(argc, (const char_T **)argv, NULL); |
103 | |
104 | /* Initialize model */ |
105 | RaspberrySmithPredictor_initialize(); |
106 | |
107 | /* External mode */ |
108 | rtSetTFinalForExtMode(&rtmGetTFinal(RaspberrySmithPredictor_M)); |
109 | rtExtModeCheckInit(2); |
110 | |
111 | { |
112 | boolean_T rtmStopReq = false; |
113 | rtExtModeWaitForStartPkt(RaspberrySmithPredictor_M->extModeInfo, 2, |
114 | &rtmStopReq); |
115 | if (rtmStopReq) { |
116 | rtmSetStopRequested(RaspberrySmithPredictor_M, true); |
117 | } |
118 | } |
119 | |
120 | rtERTExtModeStartMsg(); |
121 | |
122 | /* Call RTOS Initialization function */ |
123 | myRTOSInit(0.02, 0); |
124 | |
125 | /* Wait for stop semaphore */ |
126 | sem_wait(&stopSem); |
127 | return 0; |
128 | } |
129 | |