1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include "FoilControlRaspberrySentinel.h" |
4 | #include "FoilControlRaspberrySentinel_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 | pthread_mutex_t rateTaskFcnRunningMutex[1]; |
24 | void testForRateOverrun(int rateID); |
25 | void *baseRateTask(void *arg) |
26 | { |
27 | runModel = (rtmGetErrorStatus(FoilControlRaspberrySentinel_M) == (NULL)) && |
28 | !rtmGetStopRequested(FoilControlRaspberrySentinel_M); |
29 | while (runModel) { |
30 | sem_wait(&baserateTaskSem); |
31 | pthread_mutex_lock(&rateTaskFcnRunningMutex[0]); |
32 | |
33 | /* External mode */ |
34 | { |
35 | boolean_T rtmStopReq = false; |
36 | rtExtModePauseIfNeeded(FoilControlRaspberrySentinel_M->extModeInfo, 1, |
37 | &rtmStopReq); |
38 | if (rtmStopReq) { |
39 | rtmSetStopRequested(FoilControlRaspberrySentinel_M, true); |
40 | } |
41 | |
42 | if (rtmGetStopRequested(FoilControlRaspberrySentinel_M) == true) { |
43 | rtmSetErrorStatus(FoilControlRaspberrySentinel_M, "Simulation finished"); |
44 | break; |
45 | } |
46 | } |
47 | |
48 | /* External mode */ |
49 | { |
50 | boolean_T rtmStopReq = false; |
51 | rtExtModeOneStep(FoilControlRaspberrySentinel_M->extModeInfo, 1, |
52 | &rtmStopReq); |
53 | if (rtmStopReq) { |
54 | rtmSetStopRequested(FoilControlRaspberrySentinel_M, true); |
55 | } |
56 | } |
57 | |
58 | FoilControlRaspberrySentinel_step(); |
59 | |
60 | /* Get model outputs here */ |
61 | rtExtModeCheckEndTrigger(); |
62 | pthread_mutex_unlock(&rateTaskFcnRunningMutex[0]); |
63 | runModel = (rtmGetErrorStatus(FoilControlRaspberrySentinel_M) == (NULL)) && |
64 | !rtmGetStopRequested(FoilControlRaspberrySentinel_M); |
65 | } |
66 | |
67 | runModel = 0; |
68 | terminateTask(arg); |
69 | pthread_exit((void *)0); |
70 | return NULL; |
71 | } |
72 | |
73 | void exitFcn(int sig) |
74 | { |
75 | UNUSED(sig); |
76 | rtmSetErrorStatus(FoilControlRaspberrySentinel_M, "stopping the model"); |
77 | } |
78 | |
79 | void *terminateTask(void *arg) |
80 | { |
81 | UNUSED(arg); |
82 | terminatingmodel = 1; |
83 | printf("**terminating the model**\n"); |
84 | fflush(stdout); |
85 | |
86 | { |
87 | runModel = 0; |
88 | } |
89 | |
90 | rtExtModeShutdown(1); |
91 | |
92 | /* Disable rt_OneStep() here */ |
93 | |
94 | /* Terminate model */ |
95 | FoilControlRaspberrySentinel_terminate(); |
96 | sem_post(&stopSem); |
97 | return NULL; |
98 | } |
99 | |
100 | void testForRateOverrun(int rateID) |
101 | { |
102 | if (pthread_mutex_trylock(&rateTaskFcnRunningMutex[rateID]) == 0) { |
103 | pthread_mutex_unlock(&rateTaskFcnRunningMutex[rateID]); |
104 | } else { |
105 | reportOverrun(rateID); |
106 | } |
107 | } |
108 | |
109 | int main(int argc, char **argv) |
110 | { |
111 | UNUSED(argc); |
112 | UNUSED(argv); |
113 | printf("**starting the model**\n"); |
114 | fflush(stdout); |
115 | rtmSetErrorStatus(FoilControlRaspberrySentinel_M, 0); |
116 | rtExtModeParseArgs(argc, (const char_T **)argv, NULL); |
117 | |
118 | /* Initialize model */ |
119 | FoilControlRaspberrySentinel_initialize(); |
120 | |
121 | /* External mode */ |
122 | rtSetTFinalForExtMode(&rtmGetTFinal(FoilControlRaspberrySentinel_M)); |
123 | rtExtModeCheckInit(1); |
124 | |
125 | { |
126 | boolean_T rtmStopReq = false; |
127 | rtExtModeWaitForStartPkt(FoilControlRaspberrySentinel_M->extModeInfo, 1, |
128 | &rtmStopReq); |
129 | if (rtmStopReq) { |
130 | rtmSetStopRequested(FoilControlRaspberrySentinel_M, true); |
131 | } |
132 | } |
133 | |
134 | rtERTExtModeStartMsg(); |
135 | |
136 | /* Call RTOS Initialization function */ |
137 | myRTOSInit(0.02, 0); |
138 | |
139 | /* Wait for stop semaphore */ |
140 | sem_wait(&stopSem); |
141 | |
142 | { |
143 | int idxMutex; |
144 | for (idxMutex=0; idxMutex<1; idxMutex++) |
145 | pthread_mutex_destroy(&rateTaskFcnRunningMutex[idxMutex]); |
146 | } |
147 | |
148 | return 0; |
149 | } |
150 | |