RSBm » History » Version 28

S. Wrede, 02/17/2013 05:11 PM

1 1 S. Wrede
h1. RSB.m
2 1 S. Wrede
3 28 S. Wrede
{{>toc}}
4 28 S. Wrede
5 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.
6 3 S. Wrede
7 26 S. Wrede
For this, a very preliminary support library for Matlab is available here: https://code.cor-lab.org/git/rsb.git.matlab
8 3 S. Wrede
9 3 S. Wrede
It currently includes a simple example demonstrating the live plotting of rst.JointAngles data.
10 3 S. Wrede
11 22 S. Wrede
h2. Compililation and Installation
12 22 S. Wrede
13 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):
14 22 S. Wrede
<pre>
15 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
16 22 S. Wrede
</pre>
17 22 S. Wrede
18 27 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@.
19 22 S. Wrede
20 22 S. Wrede
h2. Usage in Matalb
21 22 S. Wrede
22 3 S. Wrede
After installation of this library, the current steps in using RSB in Matlab are as follows:
23 1 S. Wrede
24 22 S. Wrede
h3. Loading the Java RSB libraries into Matlab
25 3 S. Wrede
26 3 S. Wrede
<pre>
27 8 S. Wrede
javaaddpath /usr/share/java/RSBJava-0.5.0.jar
28 9 S. Wrede
javaaddpath /usr/share/java/protobuf.jar
29 1 S. Wrede
javaaddpath /usr/share/java/rstsandbox.jar
30 15 Anonymous
javaaddpath /usr/share/java/rst.jar
31 13 S. Wrede
</pre>
32 14 S. Wrede
33 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.
34 15 Anonymous
<pre>
35 13 S. Wrede
javaaddpath /public/amarsi_workshop/lib/rsb.m-0.5.0.jar
36 13 S. Wrede
</pre>
37 13 S. Wrede
38 3 S. Wrede
<pre>
39 3 S. Wrede
rsb.matlab.ConverterRegistration.register()
40 3 S. Wrede
</pre>
41 1 S. Wrede
42 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.
43 6 S. Wrede
44 22 S. Wrede
h3. Get queue object for domain-specific data
45 6 S. Wrede
46 6 S. Wrede
<pre>
47 6 S. Wrede
%   Scope must map to a rst.kinematics.JointAngles type
48 6 S. Wrede
angles = rsb.matlab.JointAnglesQueue;
49 6 S. Wrede
</pre>
50 1 S. Wrede
51 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.
52 6 S. Wrede
53 22 S. Wrede
h3. Connect queue to RSB listener
54 6 S. Wrede
55 10 S. Wrede
<pre>
56 6 S. Wrede
sub = rsb.Factory.getInstance().createListener('/oncilla');
57 6 S. Wrede
sub.activate()
58 6 S. Wrede
sub.addHandler(angles, true)
59 1 S. Wrede
</pre>
60 6 S. Wrede
61 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
62 6 S. Wrede
63 22 S. Wrede
h3. Doing something useful, e.g., data visualization 
64 6 S. Wrede
65 6 S. Wrede
Having the queue, we can now operate directly on the returned floats. For instance, display them in a live plot:
66 6 S. Wrede
67 6 S. Wrede
<pre>
68 6 S. Wrede
a = zeros(1,200)
69 6 S. Wrede
while(1)
70 6 S. Wrede
a(1:end - 1) = a(2:end);
71 21 S. Wrede
% blocks until new data is available
72 6 S. Wrede
a(end) = angles.take(1000);
73 6 S. Wrede
angles.getQueue.clear;
74 6 S. Wrede
plot(a);
75 6 S. Wrede
getframe;
76 1 S. Wrede
end
77 6 S. Wrede
</pre>
78 6 S. Wrede
79 6 S. Wrede
To prevent memory leaks, the above code clears the queue after each (blocking) call to @take@.
80 6 S. Wrede
81 22 S. Wrede
h3. Unload the RSB Java libraries
82 6 S. Wrede
83 6 S. Wrede
<pre>
84 11 S. Wrede
javarmpath ${home}/workspace/RSB.m/dist/lib/rsb.m-0.5.0.jar
85 11 S. Wrede
javarmpath /usr/share/java/rst.jar
86 11 S. Wrede
javarmpath /usr/share/java/rstsandbox.jar
87 11 S. Wrede
javarmpath /usr/share/java/RSBJava-0.5.0.jar
88 11 S. Wrede
javarmpath /usr/share/java/protobuf.jar
89 1 S. Wrede
</pre>
90 7 S. Wrede
91 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
92 6 S. Wrede
93 6 S. Wrede
h2. Older notes
94 1 S. Wrede
95 12 S. Wrede
Sending data from Matlab to RSB:
96 1 S. Wrede
97 1 S. Wrede
<pre>
98 1 S. Wrede
>> javaaddpath /vol/cit/share/java/RSBJava-0.4.0.jar
99 1 S. Wrede
>> javaaddpath /opt/local/share/java/protobuf.jar
100 1 S. Wrede
>> f = rsb.Factory.getInstance()
101 1 S. Wrede
 
102 1 S. Wrede
f =
103 1 S. Wrede
 
104 1 S. Wrede
rsb.Factory@5c4e0c39
105 1 S. Wrede
 
106 1 S. Wrede
>> s = Scope('/example/informer')
107 1 S. Wrede
 
108 1 S. Wrede
s =
109 1 S. Wrede
 
110 1 S. Wrede
/example/informer/
111 1 S. Wrede
112 2 S. Wrede
>> i = f.createInformer(s)
113 2 S. Wrede
No rsb.conf found in configuration directory '/Users/swrede/.config'
114 2 S. Wrede
No rsb.conf found in working directory '/Users/swrede/Documents/MATLAB'
115 2 S. Wrede
 
116 2 S. Wrede
i =
117 2 S. Wrede
 
118 2 S. Wrede
rsb.Informer@75222b8e
119 2 S. Wrede
120 2 S. Wrede
>> i.activate
121 2 S. Wrede
18.07.2011 18:21:22 rsb.Informer$InformerStateInactive activate
122 2 S. Wrede
INFO: Informer activated: [Scope:/example/informer/,Type:String]
123 2 S. Wrede
>> i.send('RSB')
124 2 S. Wrede
 
125 2 S. Wrede
ans =
126 2 S. Wrede
 
127 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 = {}]]
128 1 S. Wrede
</pre>
129 16 Anonymous
130 16 Anonymous
h2. Helpers
131 16 Anonymous
132 16 Anonymous
*Catching Java exceptions*
133 16 Anonymous
134 16 Anonymous
<pre>
135 16 Anonymous
>> try
136 16 Anonymous
lhhipangles.take(12)
137 16 Anonymous
catch,
138 16 Anonymous
s = lasterror();
139 16 Anonymous
end
140 16 Anonymous
>> s
141 16 Anonymous
</pre>
142 17 S. Wrede
143 17 S. Wrede
*Working with the returned Java arrays*
144 17 S. Wrede
145 18 Anonymous
Naive conversion to matrix type:
146 18 Anonymous
<pre>
147 18 Anonymous
function [ matrix ] = parseArray( array )
148 18 Anonymous
%PARSEARRAY Parse Java array into Matlab matrix
149 18 Anonymous
%   Not nice. We need to find a better solution.
150 18 Anonymous
matrix = zeros(8,1);
151 18 Anonymous
for i = 1:8
152 18 Anonymous
    matrix(i) = array(i);
153 18 Anonymous
end
154 18 Anonymous
end
155 18 Anonymous
</pre>
156 18 Anonymous
157 19 S. Wrede
*Helpful Mathworks Documentation* 
158 19 S. Wrede
159 20 S. Wrede
* "Cells and matrices":http://www.mathworks.de/help/techdoc/ref/cell.html
160 19 S. Wrede
* "Working with Java arrays":http://www.mathworks.de/help/techdoc/matlab_external/f15351.html