1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
5 <title>[[!meta title="Simulink Blocks for CAN bus Support under Linux."]]</title>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <link><style type="text/css">
8 #main_content {max-width: 70em}
13 <div id="main_content" style="max-width: 60em;">
16 The toolbox provides basic blocks for sending and receiving
17 CAN messages for <a href="http://lintarget.sourceforge.net/">ERT Linux</a>
18 Simulink target when mainlined
19 <a href="http://elinux.org/CAN_Bus#SocketCAN_Supported_Controllers">socket based CAN bus</a>
20 support is used. The <b>cantransmit</b> and <b>canreceive</b> blocks
21 support directly encapsulation and conversion to the Simulink signals for basic
22 data types: <tt>uint8</tt>, <tt>uint16</tt> and <tt>uint32</tt>.
23 The standard Simulink defined <tt>CAN MESSAGE TYPE</tt> can be used
24 when more complex structure of CAN message sending and receiption
25 is required. The signal of <tt>CAN MESSAGE TYPE</tt> can be connected
26 to <b>CAN Pack</b> and <b>CAN Unpack</b> blocks which encode or retrieve multiple
27 signals to/from CAN message. The CAN Pack and CAN Unpack blocks
28 are provided by <tt>canmsglib</tt> which is part of standard Simulink
33 The code generation requires to include <b>CAN Setup</b> into diagram
34 to work correctly. This block is used to setup baudrate for each CAN
35 channel in the embedded environment but this option is ignored
36 in the actual setup block implementation on Linux. The baudrate
37 can be selected by system administrator from commandline
40 ip link set can0 type can bitrate 1000000
43 or on Debian based systems <tt>/etc/network/interfaces</tt>
44 configuration file can be used to setup CAN interface baudrate
51 The later setup works automatic even with CAN-USB converter hotplug.
54 <h2 id="included_files">Project Included Files</h2>
56 The projects includes blocks library <b>socketcan_lib.slx</b>, simple
57 example of provided blocks use <b>cantransmit_ert.slx</b> and more
58 complex model <b>canopen_2j_arm_ert.slx</b> which demonstrates
59 use of the block to send PDO messages to two stepper motors
60 controlled robotic arm. This simple CAN library does not
61 provide CANopen support yet so initial motors SDO parameters
62 setup is solved by <b>canopen_2j_arm_setup</b> shell
63 script which invokes <tt>cansend</tt> command.
64 The variant of the model (<b>canopen_2j_arm_rpi.slx</b>) with
65 build parameters preset for crosscompilation to Raspberry Pi
67 The CAN-USB converter from 8devices has been used in this case.
70 <h2 id="simulink_setup">Simulink Target Setup</h2>
72 Download/clone <a href="http://lintarget.sourceforge.net/">ERT Linux</a> target
73 and Linux CAN bus toolbox repositories. The example expects the toolbox
74 are placed into user local <tt>~/matlab/toolbox</tt> directory.
75 Setup Matlab path for thee projects. One option is to use <tt>~/matlab/startup.m</tt>
76 file for this purpose:
79 function startup(AddRem)
81 disp 'Executing local setup stript ~/matlab/startup.m'
85 [matlab_startup_dir, matlab_startup_filename, matlab_startup_dir_ext] = fileparts(mfilename('fullpath'));
87 addpath(fullfile(matlab_startup_dir, 'toolbox/ert_linux/ert_linux'));
88 addpath(fullfile(matlab_startup_dir, 'toolbox/socketcan-simulink/blocks'));
91 Use example model as base for the project or open <b>Simulation</b> → <b>Model Configuration Parameters</b>
92 dialog and select <b>System target file</b> to <tt>ert_linux.tlc</tt> in <b>Code Genration</b> section.
93 Check <b>Make command</b>. It should be something like:
96 make_rtw OPT_OPTS="-O2 -ggdb -Wall -D_BSD_SOURCE"
99 Use proper <tt>CC</tt> variable definition in case of cross compilation to some other target.
100 See next example for Raspberry Pi:
103 make_rtw OPT_OPTS="-O2 -ggdb -Wall -D_BSD_SOURCE" CC=arm-rpi-linux-gnueabihf-gcc
106 <h2 id="links">Links</h2>
108 <dt>Linux CAN bus toolbox repository</dt>
109 <dd><a href="http://rtime.felk.cvut.cz/gitweb/socketcan-simulink.git" target="_blank">http://rtime.felk.cvut.cz/gitweb/socketcan-simulink.git</a>
110 <br><tt>git clone git://rtime.felk.cvut.cz/socketcan-simulink.git</tt>
112 <dt>Usable Simulink Embedded Coder Target for Linux</dt>
113 <dd>Michal Sojka, Pavel Pisa<br>
114 <a href="https://www.osadl.org/RTLWS-2014.rtlws-2014.0.html">16th Real-Time Linux Workshop</a>,
115 Düsseldorf, Germany, October 2014.
116 The <a href="http://rtime.felk.cvut.cz/publications/public/ert_linux.pdf">paper (PDF)</a>
117 and <a href="http://rtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf">slides (PDF)</a>
118 are available from our
119 <a href="http://rtime.felk.cvut.cz/publications/">publications archive</a>.