Calling yarp from Matlab

From Wiki for iCub and Friends
Revision as of 14:32, 9 June 2009 by Mtaiana (talk | contribs) (Notes: Added one solution to the compilation problem under linux.)
Jump to: navigation, search

Here we collect some information on using yarp from Matlab.

Murielle Richard has produced a tutorial:

Prerequisites

Paths such as "/example/swig" in the rest of this page refer to the YARP source code.

YARP side

Install swig and the Java development environment.

Cmake /example/swig. If Cmake asks about JAVA_INCLUDE_PATH and JAVA_INCLUDE_PATH2 put your $JAVA_JDK_ROOT\include and $JAVA_JDK_ROOT\include\win32 (or similar on Linux). Compile and install the resulting project.

  • (Note from jonas: If the java source is not generated: Check 'Show Advanced Values' in CMake; and switch on CREATE_JAVA. If compilation fails because of a CMake error you might have to specify JAVA_AWT_LIBRARY (something like /jdk1.6_xx/lib/jawt.lib).
 Note from Paul: sometimes, you have to change:
  SET(CMAKE_SWIG_FLAGS "-outdir;generated_src;-Wall;-module;yarp")
 to be:
  SET(CMAKE_SWIG_FLAGS "-Wall;-module;yarp")
 in CMakeLists.txt to get rid of an error about output directories.
 If you need to do this, remove "generated_src" from all the tips below.

You'll get a bunch of .java files in /generated_src and shared library (e.g. jyarp.dll in windows and libjyarp.so in linux). Copy the shared library to a location that is in your path (not sure about this, but I guess it is required otherwise I don't see how Matlab can later find it).

  • Note from paul: in the instructions later where a path gets added to "classpath.txt" you can also add it to "librarypath.txt" in order to pick up this shared library.

Copy /example/swig/src/*.java to /example/swig/generated_src

Compile all java classes in /example/swig/generated_src:

  javac -source 1.3 -target 1.3 *.java

You might find it useful to to add this folder (e.g. example/swig/generated_src) to your CLASSPATH environment variable.

To generate the java class documentation go to example/swig/generated_src and type:

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

Matlab side

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

  yarp_root/example/swig/generated_src

(Replace yarp_root here with the full path to YARP)

Maybe it is possible to change this to point to your YARP_ROOT env variable, if you have defined one. I did not have the time to look at it.

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

  which classpath.txt 

(usually C:\MATLAB6p5\toolbox\local\).


2. 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:

  yarp_root/example/swig/Debug

(Replace yarp_root here with the full path to YARP)

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

  which librarypath.txt 

(usually C:\MATLAB6p5\toolbox\local\).


3. 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=Bottle;
  b.fromString('hello from Matlab')

Smarter examples can be found in:

  /example/matlab 

Important: you need to close and run Matlab again every time you change either your classpath.txt file or the .java/.class.

Notes

Under linux: if you get errors like "libstdc++.so.6 version `CXXABI_1.3.1' not found" you are probably experiencing a conflict problem 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_DIR/example/swig with the same version of the compiler that was used to compile your copy of Matlab.

Under linux: if you get 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 yarp with the flag CREATE_SHARED_LIBRARIES.