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