Ask Your Question

ROS/Matlab Simulink communication

asked 2012-01-06 00:08:57 -0500

Erik_S gravatar image

updated 2012-01-06 00:42:20 -0500

mjcarroll gravatar image

Dear Ros users, I've successfully created a simulator in gazebo. Now i want to communicate to the controller via topics. I can't seem to succeed at creating a publisher/subscriber S-function. I've included all the necessary header files, but Matlab crashes during initialization(part of the crashdump included). Even though i only call upon ros::init.

I've seen some question about this, yet no definitive answers. Has there been any earlier (succesful or not) attempt at this?



Segment of ros code causing the crash.

 int argc = 0;
 char** argv = NULL;
 ros::NodeHandle n;


            Abort signal detected at Fri Jan  6 15:04:10 2012

  Crash Decoding  : Disabled
  Current Visual  : 0x21 (class 4, depth 24)
  Default Encoding: UTF-8
  GNU C Library   : 2.11.1 stable
  MATLAB Root     : /usr/local/MATLAB/R2011b
  MATLAB Version  : (R2011b)
  Operating System: Linux 2.6.32-37-generic-pae #81-Ubuntu SMP Fri Dec 2 22:24:22 UTC 2011 i686
  Processor ID    : x86 Family 6 Model 10 Stepping 5, GenuineIntel
  Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
  Window System   : The X.Org Foundation (10706000), display :0.0

Fault Count: 1

Abnormal termination:
Abort signal

Register State (from fault):
  EAX = 00000000  EBX = 00001025
  ECX = 00001059  EDX = 00000006
  ESP = b03db450  EBP = b03db468
  ESI = 00000400  EDI = b6be0ff4

  EIP = b78d7430  EFL = 00000202

   CS = 00000073   DS = 0000007b   SS = 0000007b
   ES = 0000007b   FS = 00000000   GS = 00000033

Stack Trace (from fault):
[  0] 0xb7761255     /usr/local/MATLAB/R2011b/bin/glnx86/ _ZN2fl4diag15stacktrace_base7captureERKNS0_14thread_contextEj+000169
[  1] 0xb7764175     /usr/local/MATLAB/R2011b/bin/glnx86/
[  2] 0xb7764569     /usr/local/MATLAB/R2011b/bin/glnx86/ _ZN2fl4diag13terminate_logEPKcRKNS0_14thread_contextEb+000181
[  3] 0xb73df93f    /usr/local/MATLAB/R2011b/bin/glnx86/ _ZN2fl4diag13terminate_logEPKcPK8ucontextb+000096
[  4] 0xb73dc996    /usr/local/MATLAB/R2011b/bin/glnx86/
[  5] 0xb73ddd35    /usr/local/MATLAB/R2011b/bin/glnx86/
[  6] 0xb73deab6    /usr/local/MATLAB/R2011b/bin/glnx86/
[  7] 0xb78d7410 __kernel_rt_sigreturn+000000
[  8] 0xb78d7430 __kernel_vsyscall+000016
[  9] 0xb6ab5651                       /lib/tls/i686/cmov/ gsignal+000081
[ 10] 0xb6ab8a82                       /lib/tls/i686/cmov/ abort+000386
edit retag flag offensive close merge delete


@Erik_S WOW ! no solution here, but I follow your project in eagerness
Arkapravo gravatar image Arkapravo  ( 2012-01-06 00:47:31 -0500 )edit
mjcarroll gravatar image mjcarroll  ( 2012-01-06 04:13:03 -0500 )edit
I've used IPC brigde in a commmon M-file. However you can't use mexglx functions in s-files, which are written in c(++) (i do not know how, haven't come across it). The architecture/lay-out of these s-files makes it very hard to use the ipc-bridge files, for you can't create you own threads in them
Erik_S gravatar image Erik_S  ( 2012-01-07 17:52:40 -0500 )edit
So i did not see the possibility to use these, but please correct me if i'm wrong. I'd love to know how to use the ipc-brigde in simulink. Therefore i was trying to use the roscpp functions myself.(Not working in simulink is not option unfortunately.)
Erik_S gravatar image Erik_S  ( 2012-01-07 17:53:18 -0500 )edit

6 Answers

Sort by ยป oldest newest most voted

answered 2012-01-09 20:01:16 -0500

Erik_S gravatar image

The problem seemingly lies in the fact that MATLAB 2011b has his own version of libboost, version 1.44.0. ROS makes use of version libboost 1.40.0, matlab compiles against its own (1.44.0) libboost libraries, therefore causing errors.

The program works fine in MATLAB 2011a which uses libboost 1.40.0.

edit flag offensive delete link more


@Erik_S Hey, how did you find out what MATLAB ships with what boost version? Thanks!
rado0x54 gravatar image rado0x54  ( 2012-01-15 20:57:18 -0500 )edit
in ubuntu cd <matlabfolder>/bin/glnx86 ls -l libboo* then it should show you all the libboost entries. 2011a is comes with 1.40 and 2011b comes with 1.44.. EDIT just noticed you already found out.
Erik_S gravatar image Erik_S  ( 2012-01-16 06:15:32 -0500 )edit
Still, thanks a lot!
rado0x54 gravatar image rado0x54  ( 2012-01-16 10:24:36 -0500 )edit

answered 2012-01-16 18:43:55 -0500

rado0x54 gravatar image

Here my dummy s-function. Note, this was modified from the c-function template. I'm new to the whole s-functions and it would probably be nicer to switch to the cpp template. You will have to comment everything that has to do with telekyb::Timer or telekyb::Time. This is not necessary for the ros stuff to work.

Btw, ROS_INFO Macro does not output to MATLAB (should work in rxconsole), but cout does.

#define S_FUNCTION_NAME  test_s_fun

#include "simstruc.h"

//#include <string>
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Float64.h>

// Telekyb Timer
#include <telekyb_base/Time.hpp>
using namespace telekyb;

 * S-function methods *

/* Function: mdlInitializeSizes ===============================================
 * Abstract:
 *    The sizes information is used by Simulink to determine the S-function
 *    block's characteristics (number of inputs, outputs, states, etc.).
static void mdlInitializeSizes(SimStruct *S)
    /* See sfuntmpl_doc.c for more details on the macros below */

    ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        /* Return if number of expected != number of actual parameters */

    ssSetNumContStates(S, 0);
    ssSetNumDiscStates(S, 0);

    if (!ssSetNumInputPorts(S, 1)) return;
    ssSetInputPortWidth(S, 0, 1);
    ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
     * Set direct feedthrough flag (1=yes, 0=no).
     * A port has direct feedthrough if the input is used in either
     * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
     * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
    ssSetInputPortDirectFeedThrough(S, 0, 1);

    if (!ssSetNumOutputPorts(S, 1)) return;
    ssSetOutputPortWidth(S, 0, 1);

    ssSetNumSampleTimes(S, 1);
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);
    ssSetNumPWork(S, 0);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);

    /* Specify the sim state compliance to be same as a built-in block */
    ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);

    ssSetOptions(S, 0);

/* Function: mdlInitializeSampleTimes =========================================
 * Abstract:
 *    This function is used to specify the sample time(s) for your
 *    S-function. You must register the same number of sample times as
 *    specified in ssSetNumSampleTimes.
static void mdlInitializeSampleTimes(SimStruct *S)
    ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);


#define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
  /* Function: mdlInitializeConditions ========================================
   * Abstract:
   *    In this function, you should initialize the continuous and discrete
   *    states for your S-function block.  The initial states are placed
   *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
   *    You can also perform any other initialization activities that your
   *    S-function may require. Note, this routine will be called at the
   *    start of simulation and if it is present in an enabled subsystem
   *    configured to reset states, it will be call when the enabled subsystem
   *    restarts execution to reset the states.
  static void mdlInitializeConditions(SimStruct *S)

void chatterCallback(const std_msgs::String::ConstPtr& msg)
    ROS_INFO("I heard: [%s]", msg->data.c_str());

double doubleData;

void doubleCallback(const std_msgs::Float64::ConstPtr& msg)
   std::cout << "Received Double value! " << msg->data << std::endl;
   doubleData = msg->data;

ros::Time start;

void timerCallback(const ros::TimerEvent& event)
    //ROS_INFO("Got ROS Timer CB!");
    std::cout << "Got ROS Timer CB! Time: " << (ros::Time::now() - start).toSec() << std::endl;

ros::Subscriber sub;
ros::AsyncSpinner* spinner;
ros::Timer timer;

ros::Subscriber doubleSub;
ros::Publisher doublePub;

Timer rtTimer;
Time sleepTime;

#define MDL_START  /* Change to #undef ...
edit flag offensive delete link more

answered 2012-01-13 07:00:55 -0500

rado0x54 gravatar image

Got it working with MATLAB 2011b. We had to recompile ros with boost Version 1_44. Thanks for the tip Erik_S!

I actually just took the 1_44 headers from the website and used the Matlab binaries for ros compilation. worked nicely.

image description

edit flag offensive delete link more


Did something simular.Are you also running an S-function subscriber? I've succeeded in the publishing part. However subscribing seems to always involve starting a callback function, which matlab does not allow. EDIT - NVM it does allow static voids. Ill keep you posted
Erik_S gravatar image Erik_S  ( 2012-01-16 06:12:18 -0500 )edit
Yeah, everything works nicely, even subscriptions or Timer Callbacks. I don't completely understand why callback definitions should not be allowed. Just start an Asyncspinner with a few threads (otherwise the CallbackQueue will never be processes). I can post a short example tomorrow.
rado0x54 gravatar image rado0x54  ( 2012-01-16 10:08:05 -0500 )edit

Hi rado0x54 Are you using a shared C++ library? I'm trying to create a ROS C++ node that uses (and links) to a shared C++ MATLAB R2012b Library compiled with mcc, but I'm having issues.Are there any more specific instructions for compiling ROS fuerte with MATLAB's boost headers and binaries?Thanks!

Enrique Fernandez gravatar image Enrique Fernandez  ( 2013-03-27 05:12:21 -0500 )edit

Hi...I am trying to interface Simulink with ROS from Ubuntu 12.04. I am following your post but I am not sure how did you compile your test_s_fun.cpp using the mex command in matlab. I always get an error which says 'ros/ros.h' is not a file or directory. How did you include ROS libraries?? I tried adding the include path under File->SetPath in matlab. But this doesn't work either....It will be very helpful if you can provide the basic steps to achieve this.

ROSLearner gravatar image ROSLearner  ( 2013-11-17 07:42:58 -0500 )edit

answered 2012-01-13 04:53:28 -0500

rado0x54 gravatar image

Hey guys,

I'm actually trying to get this running myself. I'm able to compile an mex (with ros::init call) file with MacOS, as well as Ubuntu. Unfortunatley I get a segfault with both.

It looks like a boost version problem to me, so I'll try to play around with this a little.

Here my trace:

       Segmentation violation detected at Fri Jan 13 19:31:53 2012

  Crash Decoding  : Disabled
  Current Visual  : None
  Default Encoding: US-ASCII
  MATLAB Root     : /Applications/
  MATLAB Version  : (R2011b)
  Operating System: Darwin 11.2.0 Darwin Kernel Version 11.2.0: Tue Aug  9 20:54:00 PDT 2011; root:xnu-1699.24.8~1/RELEASE_X86_64 x86_64
  Processor ID    : x86 Family 6 Model 15 Stepping 11, GenuineIntel
  Virtual Machine : Java 1.6.0_29-b11-402-11M3527 with Apple Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
  Window System   : No active display

Fault Count: 1

Abnormal termination:
Segmentation violation

Register State (from fault):
  RAX = 000000011cdaffa5  RBX = 000000010a9e1600
  RCX = 000000010280d6c5  RDX = 000000010280d6c0
  RSP = 0000000000000002  RBP = 0000000106c53268
  RSI = 00007fff8ae9302f  RDI = 00000001323eabe0

   R8 = 0000000000000000   R9 = 0000000000000018
  R10 = 0000000106c52a00  R11 = 0000000000000000
  R12 = 000000010280d6c5  R13 = 000000010280d6c0
  R14 = 00000001323eaa90  R15 = 0000000000daffa5

  RIP = 000000010a9e1390  RFL = 000000010275e826

   CS = 0000000106c53278   FS = 0000000000000002   GS = 0000000006c4f000

Stack Trace (from fault):
[  0] 0x000000010014f6b6 /Applications/ _ZN2fl4diag15stacktrace_base7captureERKNS0_14thread_contextEm+000150
[  1] 0x000000010015038c /Applications/ fl_diag_terminate+000396
[  2] 0x0000000100151c1e /Applications/ _ZN2fl4diag13terminate_logEPKcRKNS0_14thread_contextEb+000158
[  3] 0x00000001007ae853 /Applications/ mnSigInitCallbacks+000371
[  4] 0x00000001007afe14 /Applications/ _Z32mnRunPathDependentInitializationv+002116
[  5] 0x00000001007b01d8 /Applications/ _Z32mnRunPathDependentInitializationv+003080
[  6] 0x00000001007b0863 /Applications/ _Z32mnRunPathDependentInitializationv+004755
[  7] 0x00000001007b1505 /Applications/ mnFatalSignalHandler+000149
[  8] 0x00007fff8aecacfa                  /usr/lib/system/libsystem_c.dylib+00666874 _sigtramp+000026
[  9] 0x00007fff8aec8789                  /usr/lib/system/libsystem_c.dylib+00657289 free+000194
[ 10] 0x0000000132cdcb51             /opt/local/lib/libboost_regex-mt.dylib+00301905 _ZN5boost9re_detail26basic_regex_implementationIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS8_j+000365
[ 11] 0x0000000132cd1ff7             /opt/local/lib/libboost_regex-mt.dylib+00258039 _ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9do_assignEPKcS7_j+000437
[ 12] 0x000000011cda96e4 /Users/mriedel/ros/ros_comm/tools/rosconsole/lib/librosconsole.dylib+00067300 _ZN3ros7console9Formatter4initEPKc+000116
[ 13] 0x000000011cd9df58 /Users/mriedel/ros/ros_comm/tools/rosconsole/lib/librosconsole.dylib+00020312 _ZN3ros7console13do_initializeEv+000296
[ 14] 0x000000011cd9e5b5 /Users/mriedel/ros/ros_comm/tools/rosconsole/lib/librosconsole.dylib+00021941 _ZN3ros7console10initializeEv+000085
[ 15] 0x000000011cdaee1e /Users/mriedel/ros/ros_comm/tools/rosconsole/lib/librosconsole.dylib+00089630 _Z41__static_initialization_and_destruction_0ii+001950
[ 16] 0x00007fff6aec9d1a /Users/mriedel/ros/ros_comm/tools/rosconsole/lib/librosconsole.dylib+140730208292122
edit flag offensive delete link more

answered 2012-01-13 05:25:55 -0500

rado0x54 gravatar image

Ok, Matlab 2011b ships their own version of boost. The libs are here (on MacOS): mriedel$ ls bin/maci64/libboost_*
bin/maci64/libboost_date_time.dylib     bin/maci64/libboost_math_c99.dylib      bin/maci64/libboost_prg_exec_monitor.dylib  bin/maci64/libboost_serialization.dylib     bin/maci64/libboost_unit_test_framework.dylib
bin/maci64/libboost_filesystem.dylib        bin/maci64/libboost_math_c99f.dylib     bin/maci64/libboost_program_options.dylib   bin/maci64/libboost_signals.dylib       bin/maci64/libboost_wave.dylib
bin/maci64/libboost_graph.dylib         bin/maci64/libboost_math_tr1.dylib      bin/maci64/libboost_random.dylib        bin/maci64/libboost_system.dylib        bin/maci64/libboost_wserialization.dylib
bin/maci64/libboost_iostreams.dylib     bin/maci64/libboost_math_tr1f.dylib     bin/maci64/libboost_regex.dylib         bin/maci64/libboost_thread.dylib

So I'm quite sure I'll have to compile my ROS Libraries against the same version of boost. However, is there a way that I can find out the version of boost, by just looking the compiled libraries.


edit flag offensive delete link more


Just FYI. The Linux and Windows Version of MATLAB have the Boost Version appended to the library name. Mac OS has not, but it's definitely the same version :).
rado0x54 gravatar image rado0x54  ( 2012-01-16 05:53:00 -0500 )edit

answered 2016-03-27 15:50:44 -0500

Atlancer gravatar image

updated 2016-03-27 15:51:33 -0500

I was also able to make it work with MATLAB 2016b and Boost 1.56 on 64-bit machine. First, I compiled ROS from source with Boost 1.56 using the following tutorial: Then, I compiled the .cpp file with the following command in MATLAB: mex ROS_sub.cpp stdafx.cpp dllmain.cpp -IC:/opt/rosdeps/hydro/x64/include/ -IC:/opt/ros/hydro/x64/include/ -I. -LC:/opt/rosdeps/hydro/x64/lib/ -LC:/opt/ros/hydro/x64/lib/ -DROS_BUILD_SHARED_LIBS=1 -lroscpp -lroscpp_serialization -lrosconsole -lrostime

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2012-01-06 00:08:57 -0500

Seen: 6,215 times

Last updated: Mar 27 '16