Project

General

Profile

Genom3tutorialdemo-genom3 » History » Version 12

Aurélie Clodic, 2014-07-01 10:26

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
period: 100ms;
148
priority: 200;
149
stack: 20k;
150
codel start: tstart(in ::ids) yield main;
151
codel main: tmain(in d, out s)
152
yield main, stop;
153
codel stop: tstop() yield ether;
154
};
155
</pre>
156 1 Aurélie Clodic
157
Tasks can define the following properties:
158
* @period@ The granularity of the codel scheduler. Periodic task will sequence the codels they manage at that frequency.
159
* @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.
160
* @priority@ Can be used to prioritize different tasks whithin the same component.
161
* @scheduling real-time@ This indicates that the task requires real-time scheduling. This may not be supported by all templates.
162
* @stack@ Defines the required stack size for this task. The stack size should be big enough to run all codels that the task manages.
163
164
165 7 Aurélie Clodic
In our example, we choose to get the task_period from the idl file i.e. @demo::task_period@ parameter:
166 1 Aurélie Clodic
<pre>
167
/* ---- Execution task declaration ---- */
168
169
  task motion {
170
    period	demo::task_period ms;
171
    priority	100;
172
    stack	4000;
173
    codel <start>	InitDemoSDI(out ::ids, port out Mobile) yield ether;
174 2 Aurélie Clodic
  };
175
</pre>
176 6 Aurélie Clodic
177 12 Aurélie Clodic
h4. services declarations
178 6 Aurélie Clodic
179 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.
180
181
Service description follows this scheme:
182
<pre>
183
service name(inout s) {
184
doc "Service description";
185
task taskname;
186
validate svalidate();
187
throws ERROR_1, ERROR_2, ...;
188
interrupts name;
189
codel <start> sstart() yield step1;
190
codel <step1> sstep1() yield step1, step2;
191
codel <step2> sstep2() yield ether;
192
codel <stop> sstop() yield ether;
193
};
194
</pre>
195
where:
196
* @name@ corresponds to the name of the service
197
* @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.
198
* @task@ corresponds to the task that handle the service
199
* @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
200
* @throws@ corresponds to the list of failure report(s)
201
* @interrupts@ corresponds to the service(s) that would be automatically interrupted when the this service is called
202 1 Aurélie Clodic
203 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.
204 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.
205 12 Aurélie Clodic
206
!activitydiagram.jpg!
207 2 Aurélie Clodic
208 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.
209 3 Aurélie Clodic
210 1 Aurélie Clodic
3 types of services are available through GenoM3: attribute, function and activity. Each follow the same service pattern.
211 2 Aurélie Clodic
212 1 Aurélie Clodic
213
h5. @attribute@
214 3 Aurélie Clodic
215 7 Aurélie Clodic
@attribute@ service type should be used...
216 3 Aurélie Clodic
217 1 Aurélie Clodic
<pre>
218
 attribute SetSpeed(in speedRef = demo::SLOW	:"Mobile speed")
219
  {
220
    doc		"To change speed";
221
    validate	controlSpeed (local in speedRef);
222
    throw	INVALID_SPEED;
223
  };
224
225
  attribute GetSpeed(out speedRef =	:"Mobile speed")
226
  {
227
    doc		"To get current speed value";
228
  };
229
</pre>
230
231
h5. @function@
232 7 Aurélie Clodic
233 1 Aurélie Clodic
<pre>
234
 function Stop()
235
  {
236
    doc		"Stops motion and interrupts all motion requests";
237
    interrupts	MoveDistance, GotoPosition;
238
  };
239
</pre>
240
241
h5. @activity@
242 7 Aurélie Clodic
243 1 Aurélie Clodic
<pre>
244
activity MoveDistance(in double distRef = 0	:"Distance in m")
245
  {
246
    doc		"Move of the given distance";
247
    validate	controlDistance(in distRef, in state.position);
248
249
    codel <start>	mdStartEngine(in distRef, in state.position,
250
                              out posRef) yield exec, ether;
251
    codel <exec>	mdGotoPosition(in speedRef, in posRef, inout state,
252
                               port out Mobile) yield exec, end;
253
    codel <end, stop>	mdStopEngine() yield ether;
254
    interrupts	MoveDistance, GotoPosition;
255
    task	motion;
256
    throw	TOO_FAR_AWAY;
257
  };
258
</pre>
259 8 Aurélie Clodic
260
261
h3. Module generation
262
263
You can copy demo.gen and demoStruct.idl in your demo-genom3 repository.
264
265
<pre>
266
genom3 skeleton demo.gen
267
</pre>
268
269
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.
270 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.
271
* 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. 
272 1 Aurélie Clodic
273 9 Aurélie Clodic
h3. Codels writing
274
275
In the codels directory, you would find these two files: @demo_codels.c@ and @demo_motion_codels.c@.
276
277
h4. @demo_codels.c@