pages tagged simulinkwshhttps://rtime.felk.cvut.cz/~sojka/tags/simulink/wshikiwiki2019-04-28T13:51:35ZOn generating Linux applications from Simulinkhttps://rtime.felk.cvut.cz/~sojka/blog/on-generating-linux-applications-from-simulink/2019-04-28T13:51:35Z2013-07-25T15:36:07Z
<p>
I use Matlab/Simulink R2012b under Linux to model dynamic systems. In
order to perform hardware-in-the-loop testing, I want to run my models
in real-time on Linux with rt_preempt patch. Running a Simulink model
in real-time can be achieved by converting the model to the C code,
compiling it and running the resulting application. Simulink supports
this by means of <i>Simulink Coder</i>. Producing a Linux application with
Simulink Coder is not as straightforward as one might think, though.
</p>
<div id="outline-container-orgdbe8397" class="outline-2">
<h2 id="orgdbe8397">About Simulink Coder</h2>
<div class="outline-text-2" id="text-orgdbe8397">
<p>
<a href="http://www.mathworks.com/products/simulink-coder/index.html">Simulink Coder</a> (formerly called Real-Time Workshop) can generate the
code in many styles and for several platforms and operating systems.
Surprisingly, Linux is also among <a href="http://www.mathworks.com/products/simulink-coder/description2.html">supported environments</a>.
</p>
<p>
Simulink Coder can, by default, generate code for so called <i>Generic
Real-Time Target</i> (GRT). Such code should be suitable for running on
desktop platforms. If you want to run the code on a more constrained
embedded system or you have additional requirements on the code (e.g.
compliance with safety standards), there is <a href="http://www.mathworks.com/products/embedded-coder/">Embedded Coder</a> for you.
Embedded Coder extends Simulink Coder with additional capabilities and
targets. The default Embedded Coder target is called <i>Embedded
Real-Time Target</i> (ERT).
</p>
<p>
As it turns out, neither GRT nor ERT target alone can be used to
create Linux executable even if documentation says the opposite.
</p>
</div>
</div>
<div id="outline-container-orga8d1ce7" class="outline-2">
<h2 id="orga8d1ce7">Available options for generating Linux applications</h2>
<div class="outline-text-2" id="text-orga8d1ce7">
<p>
From the documentation it seems that there are several options
available out of the box that allow to generate Linux applications
from Simulink. They are:
</p>
<ol class="org-ol">
<li><p>
The use of Embedded Real-Time target (<code>ert.tlc</code>) and selecting
<i>NativeThreadExample</i> as the target operating system in <i>Model
Configuration → Code Generation → Templates</i>.
</p>
<p>
When I tried to generate the code, I got this error:
</p>
<blockquote>
<p>
This model specifies the ‘Target operating system’ as
‘NativeThreadsExample’ from the ‘Template’ section of the ‘Code
Generation’ pane of the Configuration parameters dialog. This example
target is currently supported only when the model is configured for
concurrent execution.
</p>
</blockquote>
<p>
After some searching, I figured out how to enable <i>concurrent
execution</i>. It can be done at <i>View → Model Explorer → right click
on Configuration → Show Concurrent Execution options</i> and then
check <i>Allow tasks to execute concurrently on target</i>. Even though
I enabled this and configure the tasks, I still got the same error.
</p></li>
<li>The second option might be using the <i>IDE Link Target</i> that allows
to compile the generated code by Eclipse IDE. Even though I don’t
like Eclipse, I tried to set it up according to the documentation.
Again no success. I ended up with some strange errors suggesting
that there is a problem with Eclipse (more precisely its JVM) being
a 32 bit application and Matlab a 64 bit application. My guess is
that this feature is tested only on Windows. Since Matlab R2012b is
not distributed for 32 bit Linux, it is not possible to test this
option with 32 bit Matlab. Furthermore, in release notes of R2013a
it is announced that Eclipse support will be dropped in future
releases.</li>
</ol>
</div>
</div>
<div id="outline-container-org9086f4e" class="outline-2">
<h2 id="org9086f4e">Custom target for Linux applications</h2>
<div class="outline-text-2" id="text-org9086f4e">
</div>
<div id="outline-container-orgf2bb37d" class="outline-3">
<h3 id="orgf2bb37d">Simple target</h3>
<div class="outline-text-3" id="text-orgf2bb37d">
<p>
Since I failed with both above options I decided to write a custom
code generation target for Linux. One option would be to use <a href="http://sourceforge.net/projects/lintarget/">a target
previously developed at our department</a>, but it has several drawbacks:
</p>
<ol class="org-ol">
<li>The generated code cannot be compiled. It seems that the interface
to the generated code changed between Simulink version from 2007
(when the target was developed) and R2012b used by me.</li>
<li>I don’t like that the target is meant to be used on Windows
(hardcoded backslashes etc.) and additionally,</li>
<li>it contains features that I do not need (CANOpen block set).</li>
</ol>
<p>
Therefore I decided to write my own target that should be as simple as
possible.
</p>
<p>
After creating the target by copying the <code>ert.tlc</code> file and modifying
it slightly (basically replacing <code>ert</code> with <code>ert_linux</code>) and selecting
<i>NativeThreadsExample</i> as in the step 1 above, the code was
successfully generated. You can see this target <a href="https://rtime.felk.cvut.cz/gitweb/ert_linux.git/tree/8f90f30d7be0af2c82e51608ea08e07529e49496">here</a>. Such a target
has two problems:
</p>
<ol class="org-ol">
<li>It does not support <i>external mode</i>, i.e. a mode in which Simulink
can connect to the running application to provide on-line parameter
tuning and real-time data plotting.</li>
<li>The generated code uses POSIX timers that use signals to wake up
the threads. Signals have quite big overhead and there are rumors
that their delivery can suffer from priority inversion.</li>
</ol>
<p>
The only way to get Linux application with support for external mode
seemed to develop a target with custom <i>main.c</i> file.
</p>
</div>
</div>
<div id="outline-container-org2209f2e" class="outline-3">
<h3 id="org2209f2e">Advanced target</h3>
<div class="outline-text-3" id="text-org2209f2e">
<p>
I developed the target that provides the <i>main.c</i> file (actually
called <code>ert_main.c</code>) and supports external mode. Although the
documentation is quite extensive (several thousands pages in total),
if was really hard to figure out how to do it properly. Several times
it was necessary to study awful TLC<sup><a id="fnr.1" class="footref" href="https://rtime.felk.cvut.cz/~sojka/tags/simulink/#fn.1" role="doc-backlink">1</a></sup> source code which is, IMHO,
very badly structured. Fortunately, I could use <a href="http://sourceforge.net/projects/lintarget/">Lukáš Hamáček’s</a> target
as an example, which helped me a lot.
</p>
<p>
The result of my work is can be <a href="https://rtime.felk.cvut.cz/gitweb/ert_linux.git">downloaded from our repository</a>. The
target has the following features:
</p>
<ul class="org-ul">
<li>Support for external mode over TCP.</li>
<li>Support for both single-rate and multi-rate systems.</li>
<li>Simulation threads are scheduled as <code>SCHED_FIFO</code> (real-time
priority).</li>
<li>External mode communication runs in non-real-time thread.</li>
</ul>
<p>
I tested the target on several systems and everything seemed to work
as expected. My only concern now is about thread safety. Given that
Simulink produces the error messages about concurrent execution of
threads (see above), I’m not sure whether the generated code can be
safely run in multiple threads. Also the code implementing external
mode server contains several semaphores but only when it is compiled
for the VxWorks OS. I’ll probably check this later if we encounter any
problems that might be caused by the code not being thread safe.
</p>
</div>
</div>
</div>
<div id="outline-container-org9757121" class="outline-2">
<h2 id="org9757121">Conclusion</h2>
<div class="outline-text-2" id="text-org9757121">
<p>
It is surprising how few relevant results Google gives when one asks
for ”<a href="https://www.google.cz/search?q%3Dsimulink%2Breal-time%2Bworkshop%2Blinux">simulink real-time workshop linux</a>”. It seems that the <a href="https://rtime.felk.cvut.cz/gitweb/ert_linux.git">Embedded
Coder target for Linux</a> described in this post might be useful for
people waning to run their Simulink models on Linux. Although that
the target may not work perfectly and in all configurations, I think
it is now good enough for interested people to try. If you experience
any problems with the target, let me know.
</p>
</div>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.1" class="footnum" href="https://rtime.felk.cvut.cz/~sojka/tags/simulink/#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
Target Language Compiler – the tool used to convert Simulink
blocks to C code.
</p></div></div>
</div>
</div>