RSBm » History » Version 26

S. Wrede, 02/06/2013 08:25 PM

1 1 S. Wrede
h1. RSB.m
2 1 S. Wrede
3 3 S. Wrede
We are currently exploring the best way to expose RSB in Matlab. The most straightforward way to do this is to use the Java implementation directly from within Matlab. However, as the Matlab Java interpreter is not able to handle Java's Generic types, we need to wrap around some parts of the RSBJava API which use generics.
4 3 S. Wrede
5 26 S. Wrede
For this, a very preliminary support library for Matlab is available here: https://code.cor-lab.org/git/rsb.git.matlab
6 3 S. Wrede
7 3 S. Wrede
It currently includes a simple example demonstrating the live plotting of rst.JointAngles data.
8 3 S. Wrede
9 22 S. Wrede
h2. Compililation and Installation
10 22 S. Wrede
11 25 S. Wrede
After cloning the repository, you need to curently build the library from source. Given the dependencies (Apache Ant + Java libraries for ProtoBuf, RSB and RST) are available, the command to build the library with ant is as follows (please adjust the paths to your environment):
12 22 S. Wrede
<pre>
13 23 S. Wrede
$ ant -Dpbuf.lib=/usr/local/share/java/protobuf-java-2.4.1.jar -Drsb.lib=../rsb-java/dist/lib/rsb-0.8.0.jar -Drst.lib=../../rst/proto/build/java/rst-0.9.0.jar -Drstsandbox.lib=../../rst/proto/build/java/rstsandbox-0.9.0.jar
14 22 S. Wrede
</pre>
15 22 S. Wrede
16 22 S. Wrede
You may easily install the resulting Jar-file (rsb-m-<version>.jar) to the folder of your choice, e.g., @/usr/local/share/java@.
17 22 S. Wrede
18 22 S. Wrede
h2. Usage in Matalb
19 22 S. Wrede
20 3 S. Wrede
After installation of this library, the current steps in using RSB in Matlab are as follows:
21 1 S. Wrede
22 22 S. Wrede
h3. Loading the Java RSB libraries into Matlab
23 3 S. Wrede
24 3 S. Wrede
<pre>
25 8 S. Wrede
javaaddpath /usr/share/java/RSBJava-0.5.0.jar
26 9 S. Wrede
javaaddpath /usr/share/java/protobuf.jar
27 1 S. Wrede
javaaddpath /usr/share/java/rstsandbox.jar
28 15 Anonymous
javaaddpath /usr/share/java/rst.jar
29 13 S. Wrede
</pre>
30 14 S. Wrede
31 13 S. Wrede
For the Matlab bindings, please replace @${home}@ with the full path to your workspace in case you have a checked out version of RSB.m. Otherwise just change the path to an installed version of RSB.m.
32 15 Anonymous
<pre>
33 13 S. Wrede
javaaddpath /public/amarsi_workshop/lib/rsb.m-0.5.0.jar
34 13 S. Wrede
</pre>
35 13 S. Wrede
36 3 S. Wrede
<pre>
37 3 S. Wrede
rsb.matlab.ConverterRegistration.register()
38 3 S. Wrede
</pre>
39 1 S. Wrede
40 1 S. Wrede
The last call initializes the ProtocolBufferConverter instances with a few selected RST types. This mechanism will definitely be changed as it currently introduces an unwanted dependency (at this stage) to RST.
41 6 S. Wrede
42 22 S. Wrede
h3. Get queue object for domain-specific data
43 6 S. Wrede
44 6 S. Wrede
<pre>
45 6 S. Wrede
%   Scope must map to a rst.kinematics.JointAngles type
46 6 S. Wrede
angles = rsb.matlab.JointAnglesQueue;
47 6 S. Wrede
</pre>
48 1 S. Wrede
49 6 S. Wrede
JointAnglesQueue is a class of the RSB.m library that maps the RSB domain type rst.JointAngles to a matlab vector with floats. Furthermore, it allows access to the underlying BlockingQueue that provides further methods for queue management. See the RSBJava API documentation for more information.
50 6 S. Wrede
51 22 S. Wrede
h3. Connect queue to RSB listener
52 6 S. Wrede
53 10 S. Wrede
<pre>
54 6 S. Wrede
sub = rsb.Factory.getInstance().createListener('/oncilla');
55 6 S. Wrede
sub.activate()
56 6 S. Wrede
sub.addHandler(angles, true)
57 1 S. Wrede
</pre>
58 6 S. Wrede
59 21 S. Wrede
Here the usual RSBJava API is used. After calling addHandler, the angles queue receives data. You can easily check this by calling: @angles.getQueue().isEmpty()@ Further information about the queue class attached to this callback "can be found here":http://download.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html
60 6 S. Wrede
61 22 S. Wrede
h3. Doing something useful, e.g., data visualization 
62 6 S. Wrede
63 6 S. Wrede
Having the queue, we can now operate directly on the returned floats. For instance, display them in a live plot:
64 6 S. Wrede
65 6 S. Wrede
<pre>
66 6 S. Wrede
a = zeros(1,200)
67 6 S. Wrede
while(1)
68 6 S. Wrede
a(1:end - 1) = a(2:end);
69 21 S. Wrede
% blocks until new data is available
70 6 S. Wrede
a(end) = angles.take(1000);
71 6 S. Wrede
angles.getQueue.clear;
72 6 S. Wrede
plot(a);
73 6 S. Wrede
getframe;
74 1 S. Wrede
end
75 6 S. Wrede
</pre>
76 6 S. Wrede
77 6 S. Wrede
To prevent memory leaks, the above code clears the queue after each (blocking) call to @take@.
78 6 S. Wrede
79 22 S. Wrede
h3. Unload the RSB Java libraries
80 6 S. Wrede
81 6 S. Wrede
<pre>
82 11 S. Wrede
javarmpath ${home}/workspace/RSB.m/dist/lib/rsb.m-0.5.0.jar
83 11 S. Wrede
javarmpath /usr/share/java/rst.jar
84 11 S. Wrede
javarmpath /usr/share/java/rstsandbox.jar
85 11 S. Wrede
javarmpath /usr/share/java/RSBJava-0.5.0.jar
86 11 S. Wrede
javarmpath /usr/share/java/protobuf.jar
87 1 S. Wrede
</pre>
88 7 S. Wrede
89 7 S. Wrede
This example code is available as Matlab functions in the RSB.m project here: https://code.cor-lab.org/projects/rsb/repository/show/trunk/matlab/matlab
90 6 S. Wrede
91 6 S. Wrede
h2. Older notes
92 1 S. Wrede
93 12 S. Wrede
Sending data from Matlab to RSB:
94 1 S. Wrede
95 1 S. Wrede
<pre>
96 1 S. Wrede
>> javaaddpath /vol/cit/share/java/RSBJava-0.4.0.jar
97 1 S. Wrede
>> javaaddpath /opt/local/share/java/protobuf.jar
98 1 S. Wrede
>> f = rsb.Factory.getInstance()
99 1 S. Wrede
 
100 1 S. Wrede
f =
101 1 S. Wrede
 
102 1 S. Wrede
rsb.Factory@5c4e0c39
103 1 S. Wrede
 
104 1 S. Wrede
>> s = Scope('/example/informer')
105 1 S. Wrede
 
106 1 S. Wrede
s =
107 1 S. Wrede
 
108 1 S. Wrede
/example/informer/
109 1 S. Wrede
110 2 S. Wrede
>> i = f.createInformer(s)
111 2 S. Wrede
No rsb.conf found in configuration directory '/Users/swrede/.config'
112 2 S. Wrede
No rsb.conf found in working directory '/Users/swrede/Documents/MATLAB'
113 2 S. Wrede
 
114 2 S. Wrede
i =
115 2 S. Wrede
 
116 2 S. Wrede
rsb.Informer@75222b8e
117 2 S. Wrede
118 2 S. Wrede
>> i.activate
119 2 S. Wrede
18.07.2011 18:21:22 rsb.Informer$InformerStateInactive activate
120 2 S. Wrede
INFO: Informer activated: [Scope:/example/informer/,Type:String]
121 2 S. Wrede
>> i.send('RSB')
122 2 S. Wrede
 
123 2 S. Wrede
ans =
124 2 S. Wrede
 
125 2 S. Wrede
Event[id=4145dcfd-aff7-45de-b00f-0c6c315f0ce7, scope=/example/informer/, type =String, metaData=MetaData[senderId = 4d619c1a-b516-4474-9eed-eb216546ff01, createTime = 1311006099963000, sendTime = 1311006099966000, receiveTime = 0, userTimes = {}, userInfos = {}]]
126 1 S. Wrede
</pre>
127 16 Anonymous
128 16 Anonymous
h2. Helpers
129 16 Anonymous
130 16 Anonymous
*Catching Java exceptions*
131 16 Anonymous
132 16 Anonymous
<pre>
133 16 Anonymous
>> try
134 16 Anonymous
lhhipangles.take(12)
135 16 Anonymous
catch,
136 16 Anonymous
s = lasterror();
137 16 Anonymous
end
138 16 Anonymous
>> s
139 16 Anonymous
</pre>
140 17 S. Wrede
141 17 S. Wrede
*Working with the returned Java arrays*
142 17 S. Wrede
143 18 Anonymous
Naive conversion to matrix type:
144 18 Anonymous
<pre>
145 18 Anonymous
function [ matrix ] = parseArray( array )
146 18 Anonymous
%PARSEARRAY Parse Java array into Matlab matrix
147 18 Anonymous
%   Not nice. We need to find a better solution.
148 18 Anonymous
matrix = zeros(8,1);
149 18 Anonymous
for i = 1:8
150 18 Anonymous
    matrix(i) = array(i);
151 18 Anonymous
end
152 18 Anonymous
end
153 18 Anonymous
</pre>
154 18 Anonymous
155 19 S. Wrede
*Helpful Mathworks Documentation* 
156 19 S. Wrede
157 20 S. Wrede
* "Cells and matrices":http://www.mathworks.de/help/techdoc/ref/cell.html
158 19 S. Wrede
* "Working with Java arrays":http://www.mathworks.de/help/techdoc/matlab_external/f15351.html