This website uses browsing/session and functional cookies to ensure you get the best experience. Learn More

Calling yarp from Matlab

From Wiki for iCub and Friends
Jump to: navigation, search

Instructions for calling YARP from Matlab are now officilly maintained on the YARP online documentation:

http://wiki.icub.org/yarpdoc/yarp_swig.html

For instructions on the installation on a Mac OSX, you can refer to Calling yarp from Matlab on Mac OSX

These instructions are considered obsolete and will be removed.

Contents

Prerequisites

Installation instructions

Install SWIG and the Java Development Kit. Ubuntu Linux:

 apt-get install swig default-jdk

Read Murielle Richard's tutorial: File:Yarp4Matlab.pdf, File:Yarp4Matlab.doc. It was written for Windows, but it is useful for other operating systems, too. Errata: the command in the second window terminal should be yarp connect /matlab/write /matlab/read

In the remainder of this article, paths such as example/swig are meant to be relative to the YARP source code directory: $YARP_ROOT/example/swig

YARP side

  • cmake example/swig
  • in CMake, set the following variables and the following paths (adjust them to your particular installation):
    • toggle CREATE_JAVA on
    • JAVA_INCLUDE_PATH: $JAVA_JDK_ROOT\include (Windows) or /usr/lib/jvm/java-6-openjdk/include (Linux)
    • JAVA_INCLUDE_PATH2: $JAVA_JDK_ROOT\include\win32 (Windows) or /usr/lib/jvm/java-6-openjdk/include/linux (Linux)
  • generate and compile the example SWIG project

You should obtain:

  • a number of *.java files in example/swig/generated_src
  • a shared library called jyarp.dll (Windows) or libjyarp.so (Linux) in example/swig

Next, compile all the Java classes in example/swig/generated_src:

  javac -source 1.3 -target 1.3 *.java

Next, copy the *.class files in two directories as follows. LoadYarp.class and YarpImageHelper.class go to (say) ~/jyarp; all the remaining files go to ~/jyarp/yarp. (This is because the latter files have to be in a directory called "yarp", like the corresponding Java package.) Add ~/jyarp to MATLAB's classpath.txt file (read below for further information about this).

Now, either copy the shared library to a location that is in your PATH, or put its path in MATLAB's librarypath.txt.

CHECK THIS: Now, either the example/swig/generated_src directory to your CLASSPATH environment variable, or define MATLAB's classpath.txt as explained below.

Optional step: to generate the Java class documentation, go to example/swig/generated_src and type:

 javadoc -d ..\doc\html *.java

Common problems and workarounds

  • If the yarp2/example/swig compilation fails due to a CMake error, check that JAVA_AWT_LIBRARY is set to /jdk1.6_xx/lib/jawt.lib or similar.
  • Sometimes, inside CMakeLists.txt you have to change the line
SET(CMAKE_SWIG_FLAGS "-outdir;generated_src;-Wall;-module;yarp")

to be:

SET(CMAKE_SWIG_FLAGS "-Wall;-module;yarp")

in order to get rid of an error about output directories. If you need to do this, then ignore the "generated_src" part from all the tips.

MATLAB side

Define the location of class files

You need to tell MATLAB the location of the YARP *.class files. Do this by adding the following line to your classpath.txt:

 YOUR_YARP_PATH/example/swig/generated_src

(Replace YOUR_YARP_PATH with the full path to YARP)

You can ask MATLAB about the location of your classpath.txt by typing

 which classpath.txt 

Usually it is C:\MATLAB6p5\toolbox\local\ (Windows), /home/gsaponaro/MATLAB_R2008b/toolbox/local/classpath.txt (Linux)

Define the location of YARP shared library

You need to tell Matlab the location of the yarp shared library (e.g. jyarp.dll in windows and libjyarp.so in linux) file. Do this by adding the following line to your librarypath.txt:

 YOUR_YARP_PATH/example/swig/Debug

(Replace YOUR_YARP_PATH here with the full path to YARP)

You can ask Matlab about the location of your librarypath.txt by typing

 which librarypath.txt 

Usually it is C:\MATLAB6p5\toolbox\local\ (Windows), /home/gsaponaro/MATLAB_R2008b/toolbox/local/classpath.txt (Linux).

Call YARP within MATLAB

Now the next time you run matlab you should be able to call yarp code.

For example try:

 LoadYarp; % this loads the yarp library and initializes the network
 b = yarp.Bottle;
 b.fromString('hello from MATLAB')

Smarter examples can be found in example/matlab

Common problems and workarounds

  • You need to close and run MATLAB again every time you change your classpath.txt file or the .java/.class.
  • If MATLAB for Linux gives errors like "libstdc++.so.6 version `CXXABI_1.3.1' not found", then you are experiencing a conflict among libraries. In particular, the library libstdc++.so.6 used by MATLAB may differ from the library used by your C++ compiler. Sometimes the problem can be fixed by replacing the MATLAB libraries with the C++ library used by the compiler. Be careful, though, as this might stop MATLAB from working. Another solution is to compile ACE, YARP and the code in $YARP_ROOT/example/swig with the same version of the compiler that was used to compile your copy of MATLAB.
  • If MATLAB for Linux gives errors like "relocation R_X86_64_32 against `__gxx_personality_v0' can not be used when making a shared object; recompile with -fPIC /home/icub/Code/yarp2/lib/libYARP_dev.a: could not read symbols: Bad value collect2: ld returned 1 exit status", try to recompile yarp2 with the variable CREATE_SHARED_LIBRARIES toggled on.
  • (Advice from Breno Carneiro Pinheiro) It may prove necessary to replace both ligcc_s.so.1 and libstdc++.so in MATLABPATH/glnx86/ to the one in system /usr/lib/
  • (Advice from Breno Carneiro Pinheiro) It may be necessary to change declarations like port=Port in /swig/matlab/yarp_read and yarp_write to port=yarp.Port
Personal tools
Namespaces

Variants
Actions
Navigation
Print/export
Toolbox