Difference between revisions of "Calling yarp from Matlab"

From Wiki for iCub and Friends
Jump to: navigation, search
(YARP side: split the class files in 2 directories)
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Here we collect some information on using YARP from MATLAB.
+
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.
  
 
= Prerequisites =
 
= Prerequisites =
Line 28: Line 34:
  
 
You should obtain:
 
You should obtain:
* a number of *.java files in <code>example/swig/generated_src<code>
+
* a number of *.java files in <code>example/swig/generated_src</code>
* a shared library called jyarp.dll (Windows) or libjyarp.so (Linux) in <code>example/swig<code>
+
* a shared library called jyarp.dll (Windows) or libjyarp.so (Linux) in <code>example/swig</code>
 
 
NOT SURE IF NECESSARY:
 
Copy <code>example/swig/src/*.java</code> to <code>/example/swig/generated_src</code>
 
  
 
Next, compile all the Java classes in <code>example/swig/generated_src</code>:
 
Next, compile all the Java classes in <code>example/swig/generated_src</code>:
Line 39: Line 42:
 
Next, copy the *.class files in two directories as follows. LoadYarp.class and YarpImageHelper.class go to (say) <code>~/jyarp</code>; all the remaining files go to <code>~/jyarp/yarp</code>. (This is because the latter files have to be in a directory called "yarp", like the corresponding Java package.) Add <code>~/jyarp</code> to MATLAB's classpath.txt file (read below for further information about this).
 
Next, copy the *.class files in two directories as follows. LoadYarp.class and YarpImageHelper.class go to (say) <code>~/jyarp</code>; all the remaining files go to <code>~/jyarp/yarp</code>. (This is because the latter files have to be in a directory called "yarp", like the corresponding Java package.) Add <code>~/jyarp</code> to MATLAB's classpath.txt file (read below for further information about this).
  
PROBABLY UNNECESSARY, JUST CONFIGURE MATLAB librarypath.txt AS EXPLAINED LATER:
+
Now, either copy the shared library to a location that is in your PATH, or put its path in MATLAB's librarypath.txt.
Copy the shared library to a location that is in your PATH.
 
  
NOT SURE IF NECESSARY:
+
CHECK THIS:
You might find it useful to to add the <code>example/swig/generated_src</code> directory to your CLASSPATH environment variable.
+
Now, either the <code>example/swig/generated_src</code> 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 <code>example/swig/generated_src</code> and type:
 
Optional step: to generate the Java class documentation, go to <code>example/swig/generated_src</code> and type:
 
   javadoc -d ..\doc\html *.java
 
   javadoc -d ..\doc\html *.java
  
== Common problems and workarouns ==
+
== Common problems and workarounds ==
  
 
* If the yarp2/example/swig compilation fails due to a CMake error, check that <code>JAVA_AWT_LIBRARY</code> is set to <code>/jdk1.6_xx/lib/jawt.lib</code> or similar.
 
* If the yarp2/example/swig compilation fails due to a CMake error, check that <code>JAVA_AWT_LIBRARY</code> is set to <code>/jdk1.6_xx/lib/jawt.lib</code> or similar.
Line 86: Line 88:
 
   which librarypath.txt  
 
   which librarypath.txt  
  
(usually C:\MATLAB6p5\toolbox\local\).
+
Usually it is <code>C:\MATLAB6p5\toolbox\local\</code> (Windows), <code>/home/gsaponaro/MATLAB_R2008b/toolbox/local/classpath.txt</code> (Linux).
  
 
== Call YARP within MATLAB ==
 
== Call YARP within MATLAB ==
Line 94: Line 96:
 
For example try:
 
For example try:
 
   LoadYarp; % this loads the yarp library and initializes the network
 
   LoadYarp; % this loads the yarp library and initializes the network
   b=Bottle;
+
   b = yarp.Bottle;
 
   b.fromString('hello from MATLAB')
 
   b.fromString('hello from MATLAB')
  
 
Smarter examples can be found in <code>example/matlab</code>
 
Smarter examples can be found in <code>example/matlab</code>
  
== Common problems and workarouns ==
+
== Common problems and workarounds ==
  
 
* You need to close and run MATLAB again every time you change your classpath.txt file or the .java/.class.
 
* You need to close and run MATLAB again every time you change your classpath.txt file or the .java/.class.
Line 106: Line 108:
  
 
* 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 <code>CREATE_SHARED_LIBRARIES</code> toggled on.
 
* 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 <code>CREATE_SHARED_LIBRARIES</code> 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
 +
 +
* (Advice from Breno Carneiro Pinheiro) There's more help at http://wiki.epfl.ch/lasa/yarp-matlab

Latest revision as of 12:24, 9 July 2013

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.

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