hio_5_conveyor_belts.cpp
Go to the documentation of this file.
1 /** @file hio_5_conveyor_belts.cpp
2 
3 Tutorial, conveyor belt chain example for hiosys plugin
4 
5 @ingroup Tutorials
6 
7 @include hio_5_conveyor_belts.cpp
8 
9 */
10 
11 #include "libfaudes.h"
12 
13 // make the faudes namespace available to our program
14 using namespace faudes;
15 
17 
18 /*
19 -----------------------------------------------------------------
20 Conveyor Belts, only left to right, designed with hiosys-approach
21 -----------------------------------------------------------------
22 Controller design, physical interface and simulation model for the conveyor-belt chain "cb11-cb4-cb12-cb5-cb13-cb6-cb14-cb10" of the LRT Fischertechnik model.
23 E.g. for the first 4 conveyor belts, we obtain the following hierarchy of automata:
24 
25  ABCD_controller
26  |
27  -------------------------------------
28  | |
29  AB_controller CD_controller
30  | |
31  -------------------- ------------------
32  | | | |
33 A_controller B_controller C_controller D_controller
34  | | | |
35  -------------------------------------------------------------------------
36  phys_interface(eg.'phys_A_l_implies_cb11-x-y.vio')
37  -------------------------------------------------------------------------
38  | | | | \
39  plant_cb11 plant_cb4 plant_cb12 plant_cb5 plant_TICK
40 
41 The lowest level ("plant_") implements a timed physical model of the conveyor-belt chain.
42 The model 'plant_TICK' implements the passing of time for the controllers 'A_'
43 to 'D_'(20 time units per wait-loop). For HIL-Simulation, the "plant_" layer is replaced
44 by IO devices.
45 
46 The physical interface translates physical sensor edges to abstract inputs for
47 the controllers 'A_' to 'D_', and translates abstract controller outputs to
48 physical actuator edges.
49 
50 The folder data/5_conveyor_belts/hil_demo/ contains a script 'run.sh' and
51 appendage to run the controllers in HIL simulation with the lrtlab Fischertechnik model.
52 See data/5_conveyor_belts/hil_demo/README.txt for details. To update hil_demo
53 with the latest controllers computed by this tutorial, run
54 hio_5_conveyor_belts_update_hil_demo.sh
55 
56 Note: execution of this tutorial can take about two hours!
57 */
58 
59  std::string errstr;
60 
61 //////////////////////////////////////////
62 // read plantA:
63 //////////////////////////////////////////
64 
65  std::cout << std::endl << "******** reading files: plant A, spec A, constraints LC_A, LP_A and LE_A." << std::endl;
66  //read plantA and corresp. alphabets:
67  HioPlant plantA("data/5_conveyor_belts/A_plant.gen");
68  plantA.Write("tmp_hio_cb_A_plant.gen");
69  if(!IsHioPlantForm(plantA,errstr)) std::cout<<errstr;
70  std::cout<<std::endl<<"plant done.";
71  //read contraints:
72  // L_P of plant A: has been left minimal, designed as empty generator for simmplicity
73  // this is allowed, because, for SINGLE plants, the controller is computed
74  // such that its projection to SigmaP is a constraint for liveness of the plant.
75  Generator LP_A("data/5_conveyor_belts/A_LP.gen");
76  // L_E of plant A (no A_nack): designed as empty generator with alphabet
77  // {A_nack} for simmplicity
78  Generator LE_A("data/5_conveyor_belts/A_LE.gen");
79 
80  //read specification and corr. alphabets:
81  HioPlant specA("data/5_conveyor_belts/A_spec.gen");
82  if(!IsHioPlantForm(specA,errstr)) std::cout<<errstr;
83  specA.Write("tmp_hio_cb_A_spec.gen");
84 
85  // read constraint L_C:
86  HioConstraint LC_A("data/5_conveyor_belts/A_LC.gen");
87  if(!IsHioConstraintForm(LC_A,errstr)) std::cout<<errstr;
88  LC_A.Write("tmp_hio_cb_A_LC.gen");
89 
90  // // do controller synthesis:
91  HioController controllerA;
92  std::cout << std::endl << "******** local controller synthesis for plant A, B, C,...,H." << std::endl;
93  HioSynthMonolithic(plantA,specA,LC_A,LP_A,LE_A,controllerA);
94  LP_A.Clear();
95  FAUDES_TEST_DUMP("HioSynthMonolithic: Fischertechnik Conveyor Belts",controllerA);
96  // controllerA.Name("Controller A");
97  controllerA.Write("tmp_hio_cb_A_controller.gen");
98  // // controller without errStates
99  // Parallel(controllerA,plantA,controllerA);
100  // Project(controllerA,plantA.PEvents()+specA.PEvents(),controllerA);
101  // PrefixClosure(controllerA);
102  // StateMin(controllerA,controllerA);
103  // controllerA.StateNamesEnabled(false);
104  // controllerA.Name("controller A");
105  // controllerA.Write("tmp_hio_cb_A_controller.gen");
106 
107  plantA.Clear();
108 
109  // // remark: controller synthesis for plants B, C,... is analogous.
110  // // Hence, the controllers are obtained by copy of the controllerA-file and appr. string replacement
111  HioController controllerX;
112  controllerA.Version("A_","B_",controllerX);
113  controllerX.Write("tmp_hio_cb_B_controller.gen");
114  controllerA.Version("A_","C_",controllerX);
115  controllerX.Write("tmp_hio_cb_C_controller.gen");
116  controllerA.Version("A_","D_",controllerX);
117  controllerX.Write("tmp_hio_cb_D_controller.gen");
118  controllerA.Version("A_","E_",controllerX);
119  controllerX.Write("tmp_hio_cb_E_controller.gen");
120  controllerA.Version("A_","F_",controllerX);
121  controllerX.Write("tmp_hio_cb_F_controller.gen");
122  controllerA.Version("A_","G_",controllerX);
123  controllerX.Write("tmp_hio_cb_G_controller.gen");
124  controllerA.Version("A_","H_",controllerX);
125  controllerX.Write("tmp_hio_cb_H_controller.gen");
126 
127  controllerA.Clear();
128  controllerX.Clear();
129 
130 // //////////////////////////////////////////
131 // // plantAB: I/O shuffle and composition with environment
132 // //////////////////////////////////////////
133 
134  // plants are abstracted by specs, where all specs specB,specC... for a single conveyor belt are analogous to specA
135  HioPlant specB;
136  specA.Version("A_","B_",specB);
137  specB.Write("tmp_hio_cb_B_spec.gen");
138 
139  std::cout <<std::endl<< "******** IOshuffle of spec A and spec B." << std::endl;
140  HioPlant shuffAB;
141  HioShuffle(specA, specB, shuffAB);
142  FAUDES_TEST_DUMP("HioShuffle: Fischertechnik Conveyor Belts",shuffAB);
143  specA.Clear();
144  specB.Clear();
145 
146  std::cout<<std::endl<<"**** Statistics of shuffAB: "<<std::endl;
147  shuffAB.SWrite();
148  std::cout<<std::endl<<"**** Doing statemin...";
149  StateMin(shuffAB,shuffAB);
150  std::cout<<std::endl<<"**** Statistics after statemin: "<<std::endl;
151  shuffAB.SWrite();
152  shuffAB.StateNamesEnabled(false);
153  shuffAB.Name("IO shuffle AB");
154  shuffAB.Write("tmp_hio_cb_AB_shuff.gen_rename"); // _rename to avoid dot processing
155 
156  std::cout <<std::endl<< "******** environment: read files AB_Env1 and AB_Env2, composition to environment model." << std::endl;
157  //read environmentAB, which is derived from two components:
158  HioEnvironment envAB1("data/5_conveyor_belts/AB_Env1.gen");
159  HioEnvironment envAB2("data/5_conveyor_belts/AB_Env2.gen");
160  envAB1.Write("tmp_hio_cb_AB_env1.gen");
161  envAB2.Write("tmp_hio_cb_AB_env2.gen");
162  HioEnvironment envAB;
163  // compose preserving event attributes
164  aParallel(envAB1,envAB2,envAB);
165  envAB1.Clear();
166  envAB2.Clear();
167  StateMin(envAB,envAB);
168  envAB.StateNamesEnabled(false);
169  envAB.Name("envAB");
170  if(!IsHioEnvironmentForm(envAB,errstr)) std::cout<<errstr;
171  envAB.Write("tmp_hio_cb_AB_env.gen");
172 
173  std::cout <<std::endl<< "******** Composition shuffAB||envAB." << std::endl;
174 
175  ///////////////////////////////////////////////////
176  // controller synthesis for plantAB:
177  ///////////////////////////////////////////////////
178 
179  std::cout <<std::endl<< "******** Specification and constraints for compound AB." << std::endl;
180 
181  //read specAB
182  HioPlant specAB("data/5_conveyor_belts/AB_spec.gen");
183  if(!IsHioPlantForm(specAB,errstr)) std::cout<<errstr;
184  specAB.Write("tmp_hio_cb_AB_spec.gen");
185 
186  // read LC_AB
187  HioConstraint LC_AB("data/5_conveyor_belts/AB_LC.gen");
188  if(!IsHioConstraintForm(LC_AB,errstr)) std::cout<<errstr;
189  LC_AB.Write("tmp_hio_cb_AB_LC.gen");
190  // read LL_AB
191  Generator LL_AB("data/5_conveyor_belts/AB_LL.gen");
192 
193  // local constraints LC_B and LE_B from LC_A and LE_A
194  Generator LC_B, LE_B;
195  LC_A.Version("A_","B_",LC_B);
196  LE_A.Version("A_","B_",LE_B);
197  Generator intConstr_AB;
198  Parallel(LC_A,LC_B,intConstr_AB);
199  Parallel(intConstr_AB,LE_A,intConstr_AB);
200  Parallel(intConstr_AB,LE_B,intConstr_AB);
201  intConstr_AB.Name("intConstr_AB");
202  intConstr_AB.StateNamesEnabled(false);
203  LE_A.Clear();
204  LE_B.Clear();
205  LC_A.Clear();
206  LC_B.Clear();
207 
208  std::cout <<std::endl<< "******** Controller synthesis for compound AB." << std::endl;
209  //calculate IO controller for plantAB
210  HioController controllerAB;
211  HioSynthHierarchical(shuffAB, envAB, specAB, intConstr_AB, LC_AB, LL_AB, controllerAB);
212  controllerAB.Name("controllerAB");
213  controllerAB.SWrite();
214  controllerAB.Write("tmp_hio_cb_AB_controller.gen_rename");
215 
216  HioController controllerX2;
217 
218  controllerAB.Version("AB_","CD_",controllerX);
219  controllerX.Version("A_","C_",controllerX2);
220  controllerX2.Version("B_","D_",controllerX);
221  controllerX.Write("tmp_hio_cb_CD_controller.gen_rename");
222 
223  controllerAB.Version("AB_","EF_",controllerX);
224  controllerX.Version("A_","E_",controllerX2);
225  controllerX2.Version("B_","F_",controllerX);
226  controllerX.Write("tmp_hio_cb_EF_controller.gen_rename");
227 
228  controllerAB.Version("AB_","GH_",controllerX);
229  controllerX.Version("A_","G_",controllerX2);
230  controllerX2.Version("B_","H_",controllerX);
231  controllerX.Write("tmp_hio_cb_GH_controller.gen_rename");
232 
233 
234 
235  shuffAB.Clear();
236  envAB.Clear();
237  intConstr_AB.Clear();
238 
239 //######################################################################
240  //*****
241  //***** plantABCD: I/O shuffle and composition with environment.*****
242  //*****
243 //######################################################################
244 
245  std::cout <<std::endl<< "******** IOshuffle of spec AB and spec CD." << std::endl;
246 
247  // specCD from specAB
248  HioPlant specCD;
249  specAB.Version("AB_","CD_",specCD);
250  specCD.Write("tmp_hio_cb_CD_spec.gen");
251 
252  // HioShuffle
253  HioPlant shuffABCD;
254  HioShuffle(specAB, specCD, shuffABCD);
255  //specAB.Clear(); // needed later to create specABCD
256  specCD.Clear();
257  StateMin(shuffABCD,shuffABCD);
258  shuffABCD.Name("shuff ABCD");
259  shuffABCD.StateNamesEnabled(false);
260  shuffABCD.SWrite();
261  shuffABCD.Write("tmp_hio_cb_ABCD_shuff.gen_rename");
262 
263  std::cout <<std::endl<< "******** environmentAB: read files, composition to environment model." << std::endl;
264  //read environmentABCD:
265  HioEnvironment envABCD1("data/5_conveyor_belts/ABCD_Env1.gen");
266  envABCD1.Write("tmp_hio_cb_ABCD_env1.gen");
267  HioEnvironment envABCD2("data/5_conveyor_belts/ABCD_Env_ABCDfreetr.gen");
268  envABCD2.Write("tmp_hio_cb_ABCD_env_freetr.gen");
269  HioEnvironment envABCD;
270  aParallel(envABCD1,envABCD2,envABCD);
271  envABCD2.Read("data/5_conveyor_belts/ABCD_Env_ABCDoccfl.gen");
272  envABCD2.Write("tmp_hio_cb_ABCD_env_occfl.gen");
273  aParallel(envABCD,envABCD2,envABCD);
274  envABCD1.Clear();
275  envABCD2.Clear();
276  StateMin(envABCD,envABCD);
277  envABCD.StateNamesEnabled(false);
278  envABCD.Name("envABCD");
279  if(!IsHioEnvironmentForm(envABCD,errstr)) std::cout<<errstr;
280  envABCD.SWrite();
281  envABCD.Write("tmp_hio_cb_ABCD_env.gen");
282 
283  ///////////////////////////////////////////////////
284  // controller synthesis for plantABCD:
285  ///////////////////////////////////////////////////
286 
287  // specABCD: same as spec AB or spec CD, spec EF, spec GH
288  HioPlant specABCD;
289  specAB.Version("AB_","ABCD_",specABCD);
290  specAB.Clear();
291  specABCD.Write("tmp_hio_cb_ABCD_spec.gen");
292 
293  // internal contraints:
294  Generator LC_CD,LL_CD;
295  LC_AB.Version("AB_","CD_",LC_CD);
296  LL_AB.Version("AB_","CD_",LL_CD);
297 
298  Generator intConstr_ABCD;
299  Parallel(LC_AB,LC_CD,intConstr_ABCD);
300  Parallel(intConstr_ABCD,LL_AB,intConstr_ABCD);
301  Parallel(intConstr_ABCD,LL_CD,intConstr_ABCD);
302  intConstr_ABCD.Name("intConstr_ABCD");
303  intConstr_ABCD.StateNamesEnabled(false);
304 
305  // external constraints:
306  Generator LC_ABCD,LL_ABCD;
307  LC_AB.Version("AB_","ABCD_",LC_ABCD);
308  LL_AB.Version("AB_","ABCD_",LL_ABCD);
309 
310  //calculate IO controller for plantABCD
311 
312  std::cout <<std::endl<< "******** Controller synthesis for compound ABCD." << std::endl;
313  //calculate IO controller for plantABCD
314  HioController controllerABCD;
315  // caution: takes up to 2 hours!
316  std::cout <<std::endl<< "**** Synthesis procedure is running - this can take about two hours! ..." << std::endl;
317  //HioSynthHierarchical(shuffABCD, envABCD, specABCD, intConstr_ABCD, LC_ABCD, LL_ABCD, controllerABCD);
318  controllerABCD.Name("controllerABCD");
319  controllerABCD.SWrite();
320  controllerABCD.Write("tmp_hio_cb_ABCD_controller.gen_rename");
321  controllerABCD.Read("tmp_hio_cb_ABCD_controller.gen_rename");
322 
323  shuffABCD.Clear();
324  envABCD.Clear();
325  intConstr_ABCD.Clear();
326 
327  controllerABCD.Version("ABCD_","EFGH_",controllerX);
328  controllerX.Version("AB_","EF_",controllerX2);
329  controllerX2.Version("CD_","GH_",controllerX);
330  controllerX.Write("tmp_hio_cb_EFGH_controller.gen_rename");
331 
332  controllerABCD.Version("ABCD_","ABCDEFGH_",controllerX);
333  controllerX.Version("CD_","EFGH_",controllerX2);
334  controllerX2.Version("AB_","ABCD_",controllerX);
335  controllerX.Write("tmp_hio_cb_ABCDEFGH_controller.gen_rename");
336 
337  std::cout << std::endl << "done.";
338 
339  return 0;
340 }
341 
342 /** Run the tutorial */
343 int main() {
344  // call simple machine example
345  try {
346  conveyor_belts();
347  }
348  catch (Exception& e) {
349  std::cout << "function: " << e.Where() << std::endl;
350  std::cout << "exception description: " << e.What() << std::endl;
351  std::cout << "exception id: " << e.Id() << std::endl;
352 }
353  return 0;
354 }
#define FAUDES_TEST_DUMP(mes, dat)
Test protocol record macro ("mangle" filename for platform independance)
Definition: cfl_helper.h:483
Faudes exception class.
virtual const char * What() const
Returns error description.
virtual unsigned int Id() const
Returns error id.
virtual const char * Where() const
Returns Function.
virtual void Clear(void)
Clear generator data.
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
void SWrite(TokenWriter &rTw) const
Write statistics comment to TokenWriter.
Definition: cfl_types.cpp:256
Base class of all FAUDES generators.
void Name(const std::string &rName)
Set the generator's name.
bool StateNamesEnabled(void) const
Whether libFAUEDS functions are requested to generate state names.
virtual void Version(const std::string &rVersion, vGenerator &rResGen) const
Create another version of this generator.
virtual void Clear(void)
Clear generator data.
void StateMin(const Generator &rGen, Generator &rResGen)
State set minimization.
void aParallel(const Generator &rGen1, const Generator &rGen2, Generator &rResGen)
Parallel composition.
void Parallel(const Generator &rGen1, const Generator &rGen2, Generator &rResGen)
Parallel composition.
void HioSynthMonolithic(const HioPlant &rPlant, const HioPlant &rSpec, const HioConstraint &rSc, const HioConstraint &rSp, const HioConstraint &rSe, HioController &rController)
HioSynthMonolithic: I/O controller synthesis procedure for monolithic plant.
void HioSynthHierarchical(const HioPlant &rHioShuffle, const HioEnvironment &rEnvironment, const HioPlant &rSpec, const Generator &rIntConstr, const HioConstraint &rSc, const HioConstraint &rSl, HioController &rController)
HioSynthHierarchical: I/O controller synthesis procedure for I/O-shuffle of i plants and their intera...
void HioShuffle(const Generator &rPlantA, const Generator &rPlantB, const EventSet &rYp, const EventSet &rUp, const EventSet &rYe, const EventSet &rUe, Generator &rIOShuffAB)
HioShuffle: IO-shuffle of rPlantA and rPlantB according to definition.
int conveyor_belts()
int main()
Run the tutorial.
Includes all libFAUDES headers, incl plugings
libFAUDES resides within the namespace faudes.
bool IsHioConstraintForm(HioConstraint &rHioConstraint, StateSet &rQY, StateSet &rQU, EventSet &rErrEvSet, TransSet &rErrTrSet, StateSet &rErrStSet, std::string &rReportStr)
IsHioConstraintForm: check if rHioConstraint is in I/O-constraint form and assign state attributes.
bool IsHioEnvironmentForm(HioEnvironment &rHioEnvironment, StateSet &rQYe, StateSet &rQUe, StateSet &rQUl, StateSet &rQYlUe, EventSet &rErrEvSet, TransSet &rErrTrSet, StateSet &rErrStSet, std::string &rReportStr)
IsHioEnvironmentForm: check if rHioEnvironment is in I/O-environment form and assign state attributes...
bool IsHioPlantForm(HioPlant &rHioPlant, StateSet &rQYpYe, StateSet &rQUp, StateSet &rQUe, EventSet &rErrEvSet, TransSet &rErrTrSet, StateSet &rErrStSet, std::string &rReportStr)
IsHioPlantForm: check if rHioPlant is in I/O-plant form and assign state attributes.
Definition: hio_plant.cpp:16

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen