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