Project

General

Profile

Genom3tutorialdemo-genom3 » History » Version 9

Aurélie Clodic, 2014-04-10 16:44

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 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.
44 5 Aurélie Clodic
45 7 Aurélie Clodic
h4. Data structure definition 
46 5 Aurélie Clodic
47 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.
48
49
<pre>
50
#include "demoStruct.idl"
51
</pre>
52
53
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. 
54
55
<pre>
56
#ifndef IDL_DEMO_STRUCT
57
#define IDL_DEMO_STRUCT
58
59
module demo {
60
61
  const unsigned long task_period = 400;
62
  const double millisecond = 0.001;
63
64
  struct state {
65
    double position;  /* current position (m) */
66
    double speed;     /* current speed (m/s) */
67
  };
68
69
  enum speed {
70
    SLOW,
71
    FAST
72
  };
73
};
74
#endif 
75
</pre>
76
77
78 1 Aurélie Clodic
h4. @component@
79 5 Aurélie Clodic
80 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.
81
82
<pre>
83
component demo {
84
  version	"1.1";
85
  email		"openrobots@laas.fr";
86
  lang		"c";
87
  require	"genom3 >= 2.99.20";
88
</pre>
89
90
91
* version : The component version number, as a string
92
* lang : The programming language of the codels interface
93 5 Aurélie Clodic
* email : A string containing the e-mail address of the author of the component.
94 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. 
95
96
NB: component declaration is not over, the final } is left for the end @.gen@ file.
97
98
h4. @ids@
99 5 Aurélie Clodic
100
@ids@ stands for internal data structure, it is used by codels to store global and permanent objects.
101
Each codel has access to the @ids@ objects and use them to communicate with other codels within the component.
102
103 1 Aurélie Clodic
For our example, we considerto store the state, the speed reference and the position of the mobile: 
104
<pre>
105
ids {
106
  demo::state state;          /* Current state */
107
  demo::speed speedRef;       /* Speed reference */
108
  double      posRef;
109
};
110
</pre>
111
112 7 Aurélie Clodic
@demo::state@ and @demo::speed@ are types defined in the demoStruct.idl file (see above).
113 5 Aurélie Clodic
114 1 Aurélie Clodic
h4. @port@
115
116 5 Aurélie Clodic
A component define input and output data ports, used to implement data flow connections in parallel to services.
117
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.: 
118 4 Aurélie Clodic
<pre>
119
port in data<type> name;
120
port out data<type> name;
121
</pre>
122
123 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:
124 1 Aurélie Clodic
<pre>
125
/* ---- Port declaration ---- */
126
  port out demo::state Mobile;
127
</pre>
128
129
h4. @exception@
130 6 Aurélie Clodic
131 1 Aurélie Clodic
It is possible to...
132
<pre>
133
/* ---- exception declaration ---- */
134
  exception TOO_FAR_AWAY {double overshoot;};
135
  exception INVALID_SPEED;
136
</pre>
137
138
h4. @execution task@
139
140 6 Aurélie Clodic
Tasks define an execution context suitable for running activities. A task may define a state machine and associated codels. 
141
The state machine starts in the start state when the task is created during component initialization. 
142
Task description follows this scheme:
143
<pre>
144
task name {
145
period: 100ms;
146
priority: 200;
147
stack: 20k;
148
codel start: tstart(in ::ids) yield main;
149
codel main: tmain(in d, out s)
150
yield main, stop;
151
codel stop: tstop() yield ether;
152
};
153
</pre>
154 1 Aurélie Clodic
155
Tasks can define the following properties:
156
* @period@ The granularity of the codel scheduler. Periodic task will sequence the codels they manage at that frequency.
157
* @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.
158
* @priority@ Can be used to prioritize different tasks whithin the same component.
159
* @scheduling real-time@ This indicates that the task requires real-time scheduling. This may not be supported by all templates.
160
* @stack@ Defines the required stack size for this task. The stack size should be big enough to run all codels that the task manages.
161
162
163 7 Aurélie Clodic
In our example, we choose to get the task_period from the idl file i.e. @demo::task_period@ parameter:
164 1 Aurélie Clodic
<pre>
165
/* ---- Execution task declaration ---- */
166
167
  task motion {
168
    period	demo::task_period ms;
169
    priority	100;
170
    stack	4000;
171
    codel <start>	InitDemoSDI(out ::ids, port out Mobile) yield ether;
172 2 Aurélie Clodic
  };
173
</pre>
174 6 Aurélie Clodic
175 2 Aurélie Clodic
h4. services declations
176 6 Aurélie Clodic
177 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.
178
179
Service description follows this scheme:
180
<pre>
181
service name(inout s) {
182
doc "Service description";
183
task taskname;
184
validate svalidate();
185
throws ERROR_1, ERROR_2, ...;
186
interrupts name;
187
codel <start> sstart() yield step1;
188
codel <step1> sstep1() yield step1, step2;
189
codel <step2> sstep2() yield ether;
190
codel <stop> sstop() yield ether;
191
};
192
</pre>
193
where:
194
* @name@ corresponds to the name of the service
195
* @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.
196
* @task@ corresponds to the task that handle the service
197
* @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
198
* @throws@ corresponds to the list of failure report(s)
199
* @interrupts@ corresponds to the service(s) that would be automatically interrupted when the this service is called
200 1 Aurélie Clodic
201 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.
202
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.
203
204 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.
205 3 Aurélie Clodic
206 1 Aurélie Clodic
3 types of services are available through GenoM3: attribute, function and activity. Each follow the same service pattern.
207 2 Aurélie Clodic
208 1 Aurélie Clodic
209
h5. @attribute@
210 3 Aurélie Clodic
211 7 Aurélie Clodic
@attribute@ service type should be used...
212 3 Aurélie Clodic
213 1 Aurélie Clodic
<pre>
214
 attribute SetSpeed(in speedRef = demo::SLOW	:"Mobile speed")
215
  {
216
    doc		"To change speed";
217
    validate	controlSpeed (local in speedRef);
218
    throw	INVALID_SPEED;
219
  };
220
221
  attribute GetSpeed(out speedRef =	:"Mobile speed")
222
  {
223
    doc		"To get current speed value";
224
  };
225
</pre>
226
227
h5. @function@
228 7 Aurélie Clodic
229 1 Aurélie Clodic
<pre>
230
 function Stop()
231
  {
232
    doc		"Stops motion and interrupts all motion requests";
233
    interrupts	MoveDistance, GotoPosition;
234
  };
235
</pre>
236
237
h5. @activity@
238 7 Aurélie Clodic
239 1 Aurélie Clodic
<pre>
240
activity MoveDistance(in double distRef = 0	:"Distance in m")
241
  {
242
    doc		"Move of the given distance";
243
    validate	controlDistance(in distRef, in state.position);
244
245
    codel <start>	mdStartEngine(in distRef, in state.position,
246
                              out posRef) yield exec, ether;
247
    codel <exec>	mdGotoPosition(in speedRef, in posRef, inout state,
248
                               port out Mobile) yield exec, end;
249
    codel <end, stop>	mdStopEngine() yield ether;
250
    interrupts	MoveDistance, GotoPosition;
251
    task	motion;
252
    throw	TOO_FAR_AWAY;
253
  };
254
</pre>
255 8 Aurélie Clodic
256
257
h3. Module generation
258
259
You can copy demo.gen and demoStruct.idl in your demo-genom3 repository.
260
261
<pre>
262
genom3 skeleton demo.gen
263
</pre>
264
265
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.
266 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.
267
* 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. 
268 1 Aurélie Clodic
269 9 Aurélie Clodic
h3. Codels writing
270
271
In the codels directory, you would find these two files: @demo_codels.c@ and @demo_motion_codels.c@.
272
273
h4. @demo_codels.c@