Difference between revisions of "Calling yarp from Matlab on Mac OSX"

From Wiki for iCub and Friends
Jump to: navigation, search
(Creation of the page)
 
(Updated yarp bindings for MAC OSX with up-to-date instructions)
Line 18: Line 18:
 
In the following we note $YARP_DIR the directory where your yarp code is located. If you installed it with homebrew, YARP_DIR=/Library/Caches/Homebrew/yarp--svn-HEAD/ or YARP_DIR=/Library/Caches/Homebrew/yarp-VersionNo/
 
In the following we note $YARP_DIR the directory where your yarp code is located. If you installed it with homebrew, YARP_DIR=/Library/Caches/Homebrew/yarp--svn-HEAD/ or YARP_DIR=/Library/Caches/Homebrew/yarp-VersionNo/
  
   cd  $YARP_DIR/example/swig/
+
   cd  $YARP_ROOT/bindings
   ccmake .
+
  mkdir build
 +
  cd build
 +
   ccmake ../
  
In cmake, hit 'c' to configure and toggle CREATE_JAVA on.
+
In cmake, hit 'c' to configure and toggle CREATE_JAVA on. Hit 'c' to configure again, 'g' to generate.
You should obtain:
+
Compile with 'make' in order to generate files. You should obtain:
 
*    a number of *.java files in  $YARP_DIR/example/swig/generated_src
 
*    a number of *.java files in  $YARP_DIR/example/swig/generated_src
 
*    a shared library called libjyarp.jnilib in  $YARP_DIR/example/swig
 
*    a shared library called libjyarp.jnilib in  $YARP_DIR/example/swig
Line 42: Line 44:
 
   rm LoadYarp.class
 
   rm LoadYarp.class
 
   rm YarpImageHelper.class
 
   rm YarpImageHelper.class
 
  
 
= Matlab side =
 
= Matlab side =

Revision as of 20:33, 21 August 2015

This page is a step-to-step how to install yarp for matlab on a Mac OSX. It roughly follows instructions of Calling yarp from Matlab and File:Yarp4Matlab.pdf.

Prerequisites


Install Yarp

Please follow instructions from Homebrew or MacOSX: installation to install Yarp.

Install Swig

You can install swig from its source code SWIG (Simplified Wrapper and Interface Generator) or with homebrew:

 brew install swig

Yarp side

In the following we note $YARP_DIR the directory where your yarp code is located. If you installed it with homebrew, YARP_DIR=/Library/Caches/Homebrew/yarp--svn-HEAD/ or YARP_DIR=/Library/Caches/Homebrew/yarp-VersionNo/

 cd  $YARP_ROOT/bindings
 mkdir build
 cd build
 ccmake ../

In cmake, hit 'c' to configure and toggle CREATE_JAVA on. Hit 'c' to configure again, 'g' to generate. Compile with 'make' in order to generate files. You should obtain:

  • a number of *.java files in $YARP_DIR/example/swig/generated_src
  • a shared library called libjyarp.jnilib in $YARP_DIR/example/swig
 cd src
 cp *.java ../generated_src/
 cd ../generated_src/∞∞

Compile all the Java classes in example/swig/generated_src:

 javac -source 1.3 -target 1.3 *.java

Copy the classes to different folders. Let's say you want to store it in folder $JYARP_DIR

 cp LoadYarp.class $JYARP_DIR
 cp YarpImageHelper.class $JYARP_DIR
 mkdir $JYARP_DIR/yarp
 cp *.class $JYARP_DIR/yarp/
 cd $JYARP_DIR/yarp/
 rm LoadYarp.class
 rm YarpImageHelper.class

Matlab side

Edit Matlab's classpath.txt and librarypath.txt (which you can find the path with the matlab command

 which classpath.txt

It usually is in folder /Applications/MATLAB_*.app/toolbox/local/

Add to both files the paths:

 $JYARP_DIR
 $YARP_DIR/example/swig/generated_src
 $YARP_DIR/example/swig


Test call yarp within matlab

First test

In matlab command window, run:

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


This should not output any error (otherwise refer to the paragraph "Possible issues". And the last command should output

 hello from MATLAB


Second test

First find yarp_read.m and yarp_write.m in folder $YARP_DIR/example/matlab. In the current version of yarp, you might want to edit them as in File:Yarp read.m and File:Yarp write.m , ie: you need to :

  • add "import yarp.Port"
  • add "import yarp.Bottle"
  • change "disp(b);" to "disp(b.toString_c());"


Then, open a terminal and launch

 yarp server

Open a first matlab command window (A) and go to $YARP_DIR/example/matlab and launch

 yarp_write

Open a second matlab command window (B) and go to $YARP_DIR/example/matlab and launch

 yarp_read

Open a terminal and launch

 yarp connect /matlab/write /matlab/read

Now you can go to matlab window A and type any text. It should echo in matlab window B. To quit, type 'quit' in matlab window A

Possible issues

Some issues have been reported:

LoadYarp

Errors might happen when you call from Matlab

 LoadYarp 

If a message error

 LoadYarp; ??? "Undefined function or variable 'LoadYarp'"

appears, it means that you did not edit the classpath.txt and librarypath.txt files to point to the location of LoadYarp.class


If a message error

 ?? Java exception occurred:
 java.lang.NoClassDefFoundError: yarp/Network
 at LoadYarp.<init>(LoadYarp.java:21) 

appears, it means that it did not find file Network.class. Maybe it is because of the classpath.txt and librarypath.txt again. Network.class needs to be in a directory named "yarp", which in turn needs to be in a directory which also contains LoadYarp.class and YarpImageHelper.class