]> rtime.felk.cvut.cz Git - ert_linux.git/blob - ert_linux/ert_linux_multitasking_main.tlc
Simplify external mode support by using provided functions
[ert_linux.git] / ert_linux / ert_linux_multitasking_main.tlc
1 %%\r
2 %% linux_ert_target_multitasking_main.tlc\r
3 %%\r
4 %% description:\r
5 %%  This TLC script generates ert_main for multi-rate / multi-tasking\r
6 %%  case.\r
7 %%\r
8 %% date: 3nd Feb 2009\r
9 %%\r
10 %% author: Lukas Hamacek, Michal Sojka\r
11 %% e-mail: hamacl1@fel.cvut.cz\r
12 %% Department of Control Engineering\r
13 %% Faculty of Electrical Engineering\r
14 %% Czech Technical University in Prague\r
15 %%\r
16 %% modifications:\r
17 %%  2009/2/3 - Lukas Hamacek\r
18 %%      creation of the file\r
19 %%\r
20 \r
21 %% GENERATEDECLARATIONS\r
22 %%  This function generates main function declarations.\r
23 %%\r
24 \r
25 %assign ::tid01Eq = LibGetTID01EQ()\r
26 \r
27 %function generateDeclarations() Output\r
28 \r
29   /* Multirate - Multitasking case main file */\r
30   #define _POSIX_C_SOURCE 199309L        /* For clock_gettime() */\r
31   #include <stdio.h>                     /* This ert_main.c example uses printf/fflush */\r
32   #include <pthread.h>                   /* Thread library header file */\r
33   #include <sched.h>                     /* OS scheduler header file */\r
34   #include <semaphore.h>                 /* Semaphores library header file */\r
35   #include <time.h>\r
36   #include <stdlib.h>\r
37   #include "%<modelName>.h"              /* Model's header file */\r
38   #include "rtwtypes.h"                  /* MathWorks types */\r
39   %if extMode == 1\r
40     #include "ext_work.h"                  /* External mode header file */\r
41   %endif\r
42 \r
43   /*==================*\r
44    * Required defines *\r
45    *==================*/\r
46 \r
47   #ifndef MODEL\r
48   # error Must specify a model name.  Define MODEL=name.\r
49   #else\r
50   /* create generic macros that work with any model */\r
51   # define EXPAND_CONCAT(name1,name2) name1 ## name2\r
52   # define CONCAT(name1,name2) EXPAND_CONCAT(name1,name2)\r
53   # define MODEL_INITIALIZE CONCAT(MODEL,_initialize)\r
54   # define MODEL_STEP       CONCAT(MODEL,_step)\r
55   # define MODEL_TERMINATE  CONCAT(MODEL,_terminate)\r
56   # define RT_MDL           CONCAT(MODEL,_M)\r
57   #endif\r
58 \r
59   /* Error checking */\r
60   #define STRINGIZE(num) #num\r
61   #define POS(line) __FILE__ ":" STRINGIZE(line)\r
62   #define CHECK0(expr) do { int __err = (expr); if (__err) { fprintf(stderr, "Error: %s returned '%s' at " POS(__LINE__) "\n", #expr, strerror(__err)); exit(1); } } while (0);\r
63   #define CHECKE(expr) do { if ((expr) == -1) { perror(#expr " at " POS(__LINE__)); exit(1); } } while (0);\r
64 \r
65   /**\r
66    * Maximal priority used by base rate thread.\r
67    */\r
68   #define MAX_PRIO (sched_get_priority_min(SCHED_FIFO) + %<numSampleTimes>)\r
69 \r
70   /**\r
71    * Thread handle of the base rate thread.\r
72    * Fundamental sample time = %<fundamentalStepSize>s\r
73    */\r
74   pthread_t base_rate_thread;\r
75 \r
76   /**\r
77    * Thread handles of and semaphores for sub rate threads. The array\r
78    * is indexed by TID, i.e. the first one or two elements are unused.\r
79   %foreach i = numSampleTimes\r
80       %if i == 0 || i == 1 && tid01Eq\r
81       %continue\r
82     %endif\r
83     %assign s = sampleTime%<i>\r
84     %assign o = offset%<i>\r
85    * TID%<i>: sample time = %<s>s, offset = %<o>s\r
86   %endforeach\r
87    */\r
88   struct sub_rate {\r
89   pthread_t thread;\r
90   sem_t     sem;\r
91   } sub_rate[%<numSampleTimes>];\r
92 \r
93   /**\r
94    * Flag if the simulation has been terminated.\r
95    */\r
96   int simulationFinished = 0;\r
97 \r
98 %endfunction\r
99 \r
100 %function generateRtOneStep() Output\r
101   %foreach j = numSampleTimes - 1\r
102     %assign i = j + 1\r
103     %if i == 1 && tid01Eq\r
104       %continue\r
105     %endif\r
106     void *sub_rate%<i>(void *arg)\r
107     {\r
108       while(!simulationFinished) {\r
109         sem_wait(&sub_rate[%<i>].sem);    /* sem_val = 1 */\r
110         printf("%s\n", __func__);\r
111         %<modelName>_step%<i>();\r
112         %if extMode == 1\r
113           rtExtModeUpload(%<i>, %<RTMGetTaskTimeForTID(i)>);\r
114         %endif\r
115         sem_wait(&sub_rate[%<i>].sem);    /* sem_val = 0 */\r
116       }\r
117     }\r
118   %endforeach\r
119 %endfunction\r
120 \r
121 \r
122 %% GENERATEMAIN\r
123 %%  This function generates code of the main function function.\r
124 %%\r
125 %function generateMain() Output\r
126   /**\r
127    * This is the thread function of the main loop.\r
128    * Fundamental sample time = %<fundamentalStepSize>s\r
129    */\r
130   void * base_rate()\r
131   {\r
132     %assign  sec = FEVAL("uint32", fundamentalStepSize)\r
133     %assign nsec = FEVAL("uint32", (fundamentalStepSize - FEVAL("floor", fundamentalStepSize))*1000000000)\r
134     struct timespec now, next;\r
135     struct timespec period = { %<sec>, %<nsec> }; /* %<fundamentalStepSize> seconds */\r
136     boolean_T eventFlags[%<numSampleTimes>];             /* Model has %<numSampleTimes> rates */\r
137     int_T taskCounter[%<numSampleTimes>] = %<FcnInitializeTaskCounter()>;\r
138     int_T OverrunFlags[%<numSampleTimes>];\r
139     int step_sem_value;\r
140     int_T i;\r
141 \r
142     %if extMode == 1\r
143       %<SLibGenERTExtModeInit()>\r
144     %endif\r
145 \r
146     clock_gettime(CLOCK_MONOTONIC, &next);\r
147 \r
148     /* Main loop, running until all the threads are terminated */\r
149     while(rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {\r
150       printf("%s\n", __func__);\r
151       /* Check subrate overrun, set rates that need to run this time step*/\r
152       %<LibCallSetEventForThisBaseStep("eventFlags")>\\r
153 \r
154       %foreach i = numSampleTimes\r
155         %if i == 0 || i == 1 && tid01Eq\r
156           %continue\r
157         %endif\r
158         %assign s = sampleTime%<i>\r
159         %assign o = offset%<i>\r
160         /* Sampling rate %<i>, sample time = %<s>, offset = %<o> */\r
161         if (eventFlags[%<i>]) {\r
162           eventFlags[%<i>] = FALSE;\r
163           sem_getvalue(&sub_rate[%<i>].sem, &step_sem_value);\r
164           if (step_sem_value) {\r
165             rtmSetErrorStatus(%<modelName>_M, "Overrun");\r
166             printf("Sub rate %<i> overrun, sample time=%<s>s, offset=%<o>s is too fast\n");\r
167             break;\r
168           }\r
169           sem_post(&sub_rate[%<i>].sem);\r
170           sem_post(&sub_rate[%<i>].sem);\r
171         }\r
172       %endforeach\r
173 \r
174       %if extMode == 1\r
175         /* external mode */\r
176         rtExtModeUpload(0, %<RTMGetTaskTimeForTID(0)>);\r
177         %if tid01Eq\r
178           rtExtModeUpload(1, %<RTMGetTaskTimeForTID(1)>);\r
179         %endif\r
180 \r
181         rtExtModeCheckEndTrigger();\r
182         rtExtModePauseIfNeeded(rtmGetRTWExtModeInfo(RT_MDL),\r
183                                NUMST,\r
184                                (boolean_T *)&rtmGetStopRequested(RT_MDL));\r
185         rtExtModeOneStep(rtmGetRTWExtModeInfo(RT_MDL),\r
186                          NUMST,\r
187                          (boolean_T *)&rtmGetStopRequested(RT_MDL));\r
188       %endif\r
189 \r
190       next.tv_sec += period.tv_sec;\r
191       next.tv_nsec += period.tv_nsec;\r
192       if (next.tv_nsec >= 1000000000) {\r
193         next.tv_sec++;\r
194         next.tv_nsec -= 1000000000;\r
195       }\r
196       clock_gettime(CLOCK_MONOTONIC, &now);\r
197       if (now.tv_sec > next.tv_sec ||\r
198           (now.tv_sec == next.tv_sec && now.tv_nsec > next.tv_nsec)) {\r
199             uint64_T nsec = (now.tv_sec - next.tv_sec) * 1000000000 + now.tv_nsec - next.tv_nsec;\r
200             fprintf(stderr, "Base rate (%<s>s) overrun by %d us\n", (int)(nsec/1000));\r
201       }\r
202       clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next, NULL);\r
203 \r
204     }\r
205 \r
206     simulationFinished = 1;\r
207     /* Final step */\r
208     for (i = %<1 + tid01Eq>; i < %<numSampleTimes>; i++) {\r
209       sem_post(&sub_rate[i].sem);\r
210       sem_post(&sub_rate[i].sem);\r
211     }\r
212   }\r
213 \r
214   /**\r
215    * This is the main function of the model.\r
216    * Multirate - Multitasking case main file\r
217    */\r
218   int_T main(int_T argc, const char_T *argv[])\r
219   {\r
220     const char_T *errStatus;\r
221     int_T i;\r
222     pthread_attr_t attr;\r
223     struct sched_param sched_param;\r
224 \r
225     %if extMode == 1\r
226       /* External mode */\r
227       rtERTExtModeParseArgs(argc, argv);\r
228     %else\r
229       (void)(argc);\r
230       (void *)(argv);\r
231     %endif\r
232 \r
233     /* Initialize model */\r
234     %<modelName>_initialize();\r
235     simulationFinished = 0;\r
236 \r
237     CHECK0(pthread_attr_init(&attr));\r
238     CHECK0(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));\r
239     CHECK0(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));\r
240 \r
241     %foreach i = numSampleTimes\r
242       %if i == 0 || i == 1 && tid01Eq\r
243         %continue\r
244       %endif\r
245       %assign s = sampleTime%<i>\r
246       %assign o = offset%<i>\r
247 \r
248       /* Initializing the step semaphore of the loop %<i> */\r
249       CHECKE(sem_init(&sub_rate[%<i>].sem, 0, 0));\r
250 \r
251       /* Starting loop %<i> thread for sample time = %<s>s, offset = %<o>s. */\r
252       sched_param.sched_priority = MAX_PRIO - %<i>;\r
253       CHECK0(pthread_attr_setschedparam(&attr, &sched_param));\r
254       CHECK0(pthread_create(&sub_rate[%<i>].thread, &attr, sub_rate%<i>, (void*)%<i>));\r
255 \r
256     %endforeach\r
257 \r
258     /* Starting the main loop */\r
259     sched_param.sched_priority = MAX_PRIO;\r
260     CHECK0(pthread_attr_setschedparam(&attr, &sched_param));\r
261     CHECK0(pthread_create(&base_rate_thread, &attr, base_rate, NULL));\r
262 \r
263     /* Wait for threads to finish */\r
264     CHECK0(pthread_attr_destroy(&attr));\r
265     pthread_join(base_rate_thread, NULL);\r
266     %foreach i = numSampleTimes\r
267       %if i == 0 || i == 1 && tid01Eq\r
268         %continue\r
269       %endif\r
270       pthread_join(sub_rate[%<i>].thread, NULL);\r
271     %endforeach\r
272 \r
273     /* Terminate model */\r
274     %<modelName>_terminate();\r
275 \r
276     errStatus = rtmGetErrorStatus(%<modelName>_M);\r
277     if(errStatus != NULL && strcmp(errStatus, "Simulation finished")) {\r
278       %%printf("%s\n", rtmGetErrorStatus(%<modelName>_M));\r
279       if(!strcmp(errStatus, "Overrun")) {\r
280         printf("ISR overrun - sampling rate too fast\n");\r
281       }\r
282       return(1);\r
283     }\r
284     return 0;\r
285   }\r
286 \r
287 /* Local Variables: */\r
288 /* compile-command: "make -f %<modelName>.mk" */\r
289 /* End: */\r
290 \r
291 %endfunction\r