Calling yarp from Matlab

From Wiki for iCub and Friends
Revision as of 22:17, 19 May 2010 by Gsaponaro (talk | contribs) (YARP side)
Jump to: navigation, search

Here we collect some information on using YARP from MATLAB.


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<code>
  • a shared library called jyarp.dll (Windows) or (Linux) in <code>example/swig<code>

Next, compile all the Java classes in <code>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).

PROBABLY UNNECESSARY, JUST CONFIGURE MATLAB librarypath.txt AS EXPLAINED LATER: Copy the shared library to a location that is in your PATH.

NOT SURE IF NECESSARY: You might find it useful to to add the example/swig/generated_src directory to your CLASSPATH environment variable.

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

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

Common problems and workarouns

  • 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.


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:


(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 in linux) file. Do this by adding the following line to your librarypath.txt:


(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.fromString('hello from MATLAB')

Smarter examples can be found in example/matlab

Common problems and workarouns

  • 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 " version `CXXABI_1.3.1' not found", then you are experiencing a conflict among libraries. In particular, the library 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.