Project

General

Profile

Genom3tutorialdemo-genom3 » History » Version 18

Aurélie Clodic, 2014-07-04 10:48
correct ROS_PACKAGE_PATH

1 1 Aurélie Clodic
h1. Genom3tutorialdemo-genom3
2
3
{{toc}}
4
5
6 6 Aurélie Clodic
This section will illustrate genom3 concepts through a concrete example. 
7
8
The demo compoment will control a virtual mobile that can move in a 1D world. Some of the services the module offers are:
9 1 Aurélie Clodic
* read and set the current speed at any moment
10
* move the mobile to a given position
11
* move the mobile of a given distance (from its current position)
12
* monitor when the mobile passes a given position
13
* stop the motion 
14
15 6 Aurélie Clodic
Moreover, the demo component export a port with the current state of the mobile (position and speed).
16 1 Aurélie Clodic
17
To implement this, we first create a directory named demo-genom3. 
18
19
<pre>
20
mkdir demo-genom3
21
cd demo-genom3
22
</pre>
23
24 7 Aurélie Clodic
25
26
27 6 Aurélie Clodic
h3. The component description file: .gen file
28 1 Aurélie Clodic
29 6 Aurélie Clodic
We will describe the .gen description file. It is made up of several parts, each of them being identified with a keyword
30 1 Aurélie Clodic
* @component@
31
* @ids@
32
* @port@
33
* @exception@
34
* @task@
35
* @attribute@
36
* @function@
37
* @activity@
38
39 5 Aurélie Clodic
This .gen reflects the fact that a component need to define an interface made of a set of services and data ports. A component must provide a service invocation facility as well as unidirectional (input and output) data ports. It must be able to send and receive events. It has to define an internal data structure, named the @ids@, that is used by codels to store global and permanent objects.
40
Each codel has access to the @ids@ objects and use them to communicate with other codels within the component.
41
Finally, a component has to define one or more @execution tasks@ that represent the execution context (e.g. thread) in which services are run. Those tasks may be periodic or aperiodic.
42 1 Aurélie Clodic
43 11 Aurélie Clodic
!blockdiagram.jpg!
44 10 Aurélie Clodic
45 8 Aurélie Clodic
The easiest way is to see how all of this is defined is to start from an existing file, so you can download ".gen" file and ".idl" file. We will now explain step by step each part of these files.
46 5 Aurélie Clodic
47 7 Aurélie Clodic
h4. Data structure definition 
48 5 Aurélie Clodic
49 7 Aurélie Clodic
A component description always start with the definition of the data types used in the interface. Types are typically defined in separate files and @#included@ in the description, so that the definitions can be shared amongst other components. The syntax used is the subset of the IDL language (part of the OMG CORBA [5] specication) related to data type definitions. Using IDL guarantees the programming language independance and offers a standardized approach. Although IDL is part of CORBA, it should be clear that components are not tied anyhow to CORBA.
50
51
<pre>
52
#include "demoStruct.idl"
53
</pre>
54
55
The #include "demoStruct.idl" statement works as a header file in C and includes idl data structure. This file contains all the necessary declarations for the definition of the internal database. These structures are then used in the .gen file. In this example, the file "demoStruct.idl" contains the definition of the position and the speed. This file is preferably located in the same directory as .gen file, since it contributes to the definition of the component interface. 
56
57
<pre>
58
#ifndef IDL_DEMO_STRUCT
59
#define IDL_DEMO_STRUCT
60
61
module demo {
62
63
  const unsigned long task_period = 400;
64
  const double millisecond = 0.001;
65
66
  struct state {
67
    double position;  /* current position (m) */
68
    double speed;     /* current speed (m/s) */
69
  };
70
71
  enum speed {
72
    SLOW,
73
    FAST
74
  };
75
};
76
#endif 
77
</pre>
78
79
80 1 Aurélie Clodic
h4. @component@
81 5 Aurélie Clodic
82 1 Aurélie Clodic
The component declaration describes the instance of the GenoM3 component. It is defined by a unique name (an identifier) that also defines an IDL scope for any embedded types. See the "component declaration documentation":http://homepages.laas.fr/mallet/share/doc/genom3/genom3.html/Component-declaration.html#Component-declaration for details.
83
84
<pre>
85
component demo {
86
  version	"1.1";
87
  email		"openrobots@laas.fr";
88
  lang		"c";
89
  require	"genom3 >= 2.99.20";
90
</pre>
91
92
93
* version : The component version number, as a string
94
* lang : The programming language of the codels interface
95 5 Aurélie Clodic
* email : A string containing the e-mail address of the author of the component.
96 1 Aurélie Clodic
* requires : A list of dependencies of the component. It indicates an external dependency on a software package that is required. It assumes that the package is using the pkg-config utility. Each string should contain a package name in pkg-config format. 
97
98
NB: component declaration is not over, the final } is left for the end @.gen@ file.
99
100
h4. @ids@
101 5 Aurélie Clodic
102
@ids@ stands for internal data structure, it is used by codels to store global and permanent objects.
103
Each codel has access to the @ids@ objects and use them to communicate with other codels within the component.
104
105 1 Aurélie Clodic
For our example, we considerto store the state, the speed reference and the position of the mobile: 
106
<pre>
107
ids {
108
  demo::state state;          /* Current state */
109
  demo::speed speedRef;       /* Speed reference */
110
  double      posRef;
111
};
112
</pre>
113
114 7 Aurélie Clodic
@demo::state@ and @demo::speed@ are types defined in the demoStruct.idl file (see above).
115 5 Aurélie Clodic
116 1 Aurélie Clodic
h4. @port@
117
118 5 Aurélie Clodic
A component define input and output data ports, used to implement data flow connections in parallel to services.
119
Ports implement the data flow between components as a publish/subscribe model. Ports have a name and a type and can be either out (for publishing data) or in (for subscribing to a sibling out port). Data ports Data ports are defined via the in or out keyword, followed by an IDL type and the name of the port e.g.: 
120 4 Aurélie Clodic
<pre>
121
port in data<type> name;
122
port out data<type> name;
123
</pre>
124
125 5 Aurélie Clodic
In our example, we choose to export the state of the mobile to let this information accessible for example to another port from another component:
126 1 Aurélie Clodic
<pre>
127
/* ---- Port declaration ---- */
128
  port out demo::state Mobile;
129
</pre>
130
131
h4. @exception@
132 6 Aurélie Clodic
133 1 Aurélie Clodic
It is possible to...
134
<pre>
135
/* ---- exception declaration ---- */
136
  exception TOO_FAR_AWAY {double overshoot;};
137
  exception INVALID_SPEED;
138
</pre>
139
140
h4. @execution task@
141
142 6 Aurélie Clodic
Tasks define an execution context suitable for running activities. A task may define a state machine and associated codels. 
143
The state machine starts in the start state when the task is created during component initialization. 
144
Task description follows this scheme:
145
<pre>
146
task name {
147 14 Aurélie Clodic
period 100ms;
148
priority 200;
149
stack 20k;
150
codel start tstart(in ::ids) yield main;
151
codel main tmain(in d, out s) yield main, stop;
152
codel stop tstop() yield ether;
153 6 Aurélie Clodic
};
154
</pre>
155 1 Aurélie Clodic
156
Tasks can define the following properties:
157
* @period@ The granularity of the codel scheduler. Periodic task will sequence the codels they manage at that frequency.
158
* @delay@ The delay from the beginning of each period after which codels are run. This can be used to delay two tasks running at the same period in the same component.
159
* @priority@ Can be used to prioritize different tasks whithin the same component.
160
* @scheduling real-time@ This indicates that the task requires real-time scheduling. This may not be supported by all templates.
161
* @stack@ Defines the required stack size for this task. The stack size should be big enough to run all codels that the task manages.
162
163
164 7 Aurélie Clodic
In our example, we choose to get the task_period from the idl file i.e. @demo::task_period@ parameter:
165 1 Aurélie Clodic
<pre>
166
/* ---- Execution task declaration ---- */
167
168
  task motion {
169
    period	demo::task_period ms;
170
    priority	100;
171
    stack	4000;
172
    codel <start>	InitDemoSDI(out ::ids, port out Mobile) yield ether;
173 2 Aurélie Clodic
  };
174
</pre>
175 6 Aurélie Clodic
176 12 Aurélie Clodic
h4. services declarations
177 6 Aurélie Clodic
178 2 Aurélie Clodic
A service is an interface for running codels. It can be invoked via a request on the component service port. A service has optional input and output data and a list of failure reports. Input data is stored in the IDS (and output read from there), so that codels can access it. A service might be incompatible with other services of the same component or can be started multiple time, provided it is compatible with itself. It always interrupts other incompatible services when starting. A service invocation triggers an activity that manages codel execution. An activity is described by a Petri net in which places correspond to codels execution and transitions are events generated either externally or implicitely by the return value of codels.
179
180
Service description follows this scheme:
181
<pre>
182
service name(inout s) {
183
doc "Service description";
184
task taskname;
185
validate svalidate();
186
throws ERROR_1, ERROR_2, ...;
187
interrupts name;
188
codel <start> sstart() yield step1;
189
codel <step1> sstep1() yield step1, step2;
190
codel <step2> sstep2() yield ether;
191
codel <stop> sstop() yield ether;
192
};
193
</pre>
194
where:
195
* @name@ corresponds to the name of the service
196
* @s@ corresponds to the parameter of the service. Parameter could be in/inout/out depending of its type. It is possible to have several parameters.
197
* @task@ corresponds to the task that handle the service
198
* @validate@ corresponds to a function that would be called once at the beginning of the service execution and that aims to check service parameter(s) consistency
199
* @throws@ corresponds to the list of failure report(s)
200
* @interrupts@ corresponds to the service(s) that would be automatically interrupted when the this service is called
201 1 Aurélie Clodic
202 2 Aurélie Clodic
When an activity starts, the start event is generated and the corresponding codel executed. Similarly, the activity is interrupted whenever the stop event is generated.
203 1 Aurélie Clodic
Asynchronous events trigger the execution of the corresponding codel (if any). A special sleep transition is defined so that an activity can be put in a sleeping state, waiting for external events or a stop to trigger a new transition. The activity stops when all active places in the Petri net have returned the special ether event. If the execution task of a service is periodic, transitions are executed at each period. They are otherwise executed as soon as all the codels corresponding to active places have returned. The codel execution order is undefined.
204 12 Aurélie Clodic
205
!activitydiagram.jpg!
206 2 Aurélie Clodic
207 7 Aurélie Clodic
It should be noticed that no direct remote procedure call (RPC) for service invocation between components is allowed. RPC should be performed by external applications that take care of setting up the architecture of components. While this differs from traditional approaches, this guarantees that components can be controlled and will not interfere with the system. This also grants an increased reusability since no component explicitely depends on a particular set of services implemented by other components.
208 3 Aurélie Clodic
209 1 Aurélie Clodic
3 types of services are available through GenoM3: attribute, function and activity. Each follow the same service pattern.
210 2 Aurélie Clodic
211 1 Aurélie Clodic
212
h5. @attribute@
213 3 Aurélie Clodic
214 7 Aurélie Clodic
@attribute@ service type should be used...
215 3 Aurélie Clodic
216 1 Aurélie Clodic
<pre>
217
 attribute SetSpeed(in speedRef = demo::SLOW	:"Mobile speed")
218
  {
219
    doc		"To change speed";
220
    validate	controlSpeed (local in speedRef);
221
    throw	INVALID_SPEED;
222
  };
223
224
  attribute GetSpeed(out speedRef =	:"Mobile speed")
225
  {
226
    doc		"To get current speed value";
227
  };
228
</pre>
229
230
h5. @function@
231 7 Aurélie Clodic
232 1 Aurélie Clodic
<pre>
233
 function Stop()
234
  {
235
    doc		"Stops motion and interrupts all motion requests";
236
    interrupts	MoveDistance, GotoPosition;
237
  };
238
</pre>
239
240
h5. @activity@
241 7 Aurélie Clodic
242 1 Aurélie Clodic
<pre>
243
activity MoveDistance(in double distRef = 0	:"Distance in m")
244
  {
245
    doc		"Move of the given distance";
246
    validate	controlDistance(in distRef, in state.position);
247
248
    codel <start>	mdStartEngine(in distRef, in state.position,
249
                              out posRef) yield exec, ether;
250
    codel <exec>	mdGotoPosition(in speedRef, in posRef, inout state,
251
                               port out Mobile) yield exec, end;
252
    codel <end, stop>	mdStopEngine() yield ether;
253
    interrupts	MoveDistance, GotoPosition;
254
    task	motion;
255
    throw	TOO_FAR_AWAY;
256
  };
257
</pre>
258 8 Aurélie Clodic
259 13 Aurélie Clodic
h4. Data flow
260
261
!dataflow.jpg!
262 8 Aurélie Clodic
263
h3. Module generation
264
265
You can copy demo.gen and demoStruct.idl in your demo-genom3 repository.
266
267
<pre>
268
genom3 skeleton demo.gen
269
</pre>
270
271
From now on, the component is ready to be compiled and run. You can have a look at the result of the command. GenoM3 created two new directories (@codels/@ and @autoconf/@) and several new files.
272 9 Aurélie Clodic
* Algorithms (or a part of them) are grouped in the directory @codels/@. The files in that directory give you template to start from, and also let GenoM3 produce a module even if you still do not have written a single line of code.
273
* The @Makefile.am@ and @configure.ac@ files are also under your control. These are the main files which are used for the compilation of the module. 
274 1 Aurélie Clodic
275 9 Aurélie Clodic
h3. Codels writing
276
277
In the codels directory, you would find these two files: @demo_codels.c@ and @demo_motion_codels.c@.
278
279
h4. @demo_codels.c@
280 15 Aurélie Clodic
281
http://git.laas.fr/git/demo-genom/tree/codels/demo_codels.c?h=genom3
282
283
In this file, we will found codels executed by the control task such as the validation codels , e.g. controlSpeed (Validation codel of attribute SetSpeed), controlPosition (Validation codel of activity GotoPosition) and controlDistance (Validation codel of activity MoveDistance).. 
284
285
286
h4. @demo_motion_codels.c@
287
288
http://git.laas.fr/git/demo-genom/tree/codels/demo_motion_codels.c?h=genom3
289
290
In this file, we will found functions attached to the motion task, such as :
291
* <start> codel of the task
292
* <start> and <exec> codel of the GotoPosition activity 
293
* etc
294
295
h3. Module compilation
296
297
We are now ready to compile the module. Note that you could have done it with the empty codels just after generating the skeleton. The result would have been a valid GenoM3 module, runnable, but not doing anyhing of course.
298
299
 # bootstrap the autotools build system
300
<pre>
301
% autoreconf -vi
302
% mkdir build && cd build
303
</pre>
304
 # configure the codels with a set of templates, e.g. for pocolibs :
305
<pre>
306
% ../configure --prefix=$INSTALL_DIR --with-templates=pocolibs/server,pocolibs/client/c
307
</pre>
308
or for ros :
309
<pre>
310
% ../configure --prefix=$INSTALL_DIR --with-templates=ros/server,ros/client/c
311
</pre>
312
 # build your module
313
<pre>
314
% make
315
</pre>
316
317
Iterate until there are no more errors. Then, you can install your module:
318
<pre>
319
% make install
320
</pre>
321
322
323
h3. Module execution
324
325
Given the template choosen the way you will run your module may be different. 
326
327
328
h4. Pocolibs
329
330 16 Aurélie Clodic
h5. Setup (bash version)
331 15 Aurélie Clodic
332 16 Aurélie Clodic
<pre>
333
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$INSTALL_PATH/lib/pkgconfig
334
</pre>
335 15 Aurélie Clodic
336 16 Aurélie Clodic
h5. Launch
337 17 Aurélie Clodic
338
First you need to launch pocolibs:
339 1 Aurélie Clodic
<pre>
340 17 Aurélie Clodic
% h2 init
341 1 Aurélie Clodic
</pre>
342 17 Aurélie Clodic
your module:
343
<pre>
344
% demo -b
345
</pre>
346
and genomix:
347
<pre>
348
% genomixd &
349
</pre>
350
351
Finalement, thanks to eltclsh you will be able to access to your module:
352
<pre>
353
% eltclsh
354
eltclsh > package require genomix
355
eltclsh > genomix::connect 
356
genomix1
357
eltclsh > genomix1  load demo
358
eltclsh > ::demo::GetSpeed 
359
speedRef ::demo::SLOW
360
</pre>
361
362
Note that the use of the tab completion will help you to have access to your module available services. 
363
Moreover, if you do not know which parameter your module needs, you can call the service and then use tab to have access to the parameters.
364
365
Use "-ack" if you want to launch a request in background (useful if you do not want to loose access to the shell):
366
<pre>
367
eltclsh > ::demo::GetSpeed -ack
368
</pre>
369
 
370 15 Aurélie Clodic
371 1 Aurélie Clodic
h4. ROS
372 16 Aurélie Clodic
373
h5. Setup (bash version)
374
<pre>
375
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$INSTALL_PATH/lib/pkgconfig
376 18 Aurélie Clodic
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$INSTALL_PATH/src/ros-nodes:$INSTALL_PATH/share
377 16 Aurélie Clodic
export PYTHONPATH=/opt/ros/groovy/lib/python2.7/dist-packages:$INSTALL_PATH/lib/python2.7/site-packages
378
</pre>
379
380
h5. Launch
381
First you need to launch ros master:
382
<pre>
383
% roscore 
384
</pre>
385
Once it is initialized you can run your module:
386
<pre>
387
% demo-ros -b
388
</pre>
389
390
Then you will be able to use all ros facilities to access to your module, e.g.:
391
<pre>
392
% rosnode info demo
393
</pre>
394
will give you access to all availables services and topics.
395
396
To access to a topic, e.g. /demo/Mobile:
397
<pre>
398
% rostopic echo /demo/Mobile
399
</pre>
400
401
To call a service, e.g. /demo/SetSpeed:
402
<pre>
403
% rosservice call /demo/SetSpeed '{speedRef: {value: 1}}'
404
</pre>