-
Notifications
You must be signed in to change notification settings - Fork 3
/
playerstage_instructions_STAGE320.tex
1794 lines (1477 loc) · 117 KB
/
playerstage_instructions_STAGE320.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[a4paper]{report}
\title{How to Use Player/Stage\\ 2nd Edition}
\author{Jennifer Owen}
\usepackage{xspace}
\usepackage{graphicx}
\usepackage{verbatim}
\usepackage{url}
%\usepackage{tocbibind}
\usepackage[perpage]{footmisc}
\usepackage[british]{babel}
\usepackage[ pdftex, plainpages = false, pdfpagelabels,
% pdfpagelayout = OneColumn, % display single page, advancing flips the page - Sasa Tomic
bookmarks,
bookmarksopen = true,
bookmarksnumbered = true,
breaklinks = true,
linktocpage,
pagebackref,
colorlinks = true,
linkcolor = blue,
urlcolor = blue,
citecolor = red,
anchorcolor = green,
hyperindex = true,
hyperfigures
]{hyperref}
\newcommand{\plst}{Player/Stage\xspace}
\newcommand{\pl}{Player\xspace}
\begin{document}
\maketitle
This document is intended as a guide for anyone learning \plst for the first time. It explains the process of setting up a new simulation environment and how to then make your simulation do something, using a case study along the way. Whilst it is aimed at \plst users, those just wishing to use \pl on their robot may also find sections of this document useful (particularly the parts about coding with Player).
If you have any questions about using \plst there is a guide to getting help from the \pl community here:
\begin{center}
\url{http://playerstage.sourceforge.net/wiki/Getting_help}
\end{center}
This edition of the manual uses Stage version 3.2.X as there are significant differences with the previous versions of Stage and the previous edition of this manual is now out of date.
\tableofcontents
\chapter{Introduction}\label{sec:Introduction}
\plst is a robot simulation tool, it comprises of one program, \pl, which is a \emph{Hardware Abstraction Layer}. That means that it talks to the bits of hardware on the robot (like a claw or a camera) and lets you control them with your code, meaning you don't need to worry about how the various parts of the robot work. Stage is a plugin to \pl which listens to what \pl is telling it to do and turns these instructions into a simulation of your robot. It also simulates sensor data and sends this to \pl which in turn makes the sensor data available to your code.
A simulation then, is composed of three parts:
\begin{itemize}
\item Your code. This talks to \pl.
\item \pl. This takes your code and sends instructions to a robot. From the robot it gets sensor data and sends it to your code.
\item Stage. Stage interfaces with \pl in the same way as a robot's hardware would. It receives instructions from \pl and moves a simulated robot in a simulated world, it gets sensor data from the robot in the simulation and sends this to \pl.
\end{itemize}
Together \pl and Stage are called \plst, and they make a simulation of your robots.
These instructions will be focussing on how to use \plst to make a simulation, but hopefully this will still be a useful resource for anyone just using \pl (which is the same thing but on a real robot, without any simulation software).
\section{A Note on Installing \plst}
Instructions on how to install \plst onto your computer aren't really the focus of this document. It is very difficult though. If you're lucky the install will work first time but there are a lot of dependencies which may need installing. For computers running Ubuntu there is a very good set of instructions here (including a script for downloading the many prerequisites):
\begin{center}
\url{http://www.control.aau.dk/~tb/wiki/index.php/Installing_Player_and_Stage_in_Ubuntu}
\end{center}
For MAC users you might find the following install instructions useful:
\begin{center}
\url{http://alanwinfield.blogspot.com/2009/07/installing-playerstage-on-os-x-with.html}
\end{center}
Alternatively, you could try the suggestions on the \pl ``getting help'' page:
\begin{center}
\url{http://playerstage.sourceforge.net/wiki/Getting_help}
\end{center}
\chapter{The Basics}\label{sec:Basics}
\section{Important File Types}\label{sec:Basics:FileTypes}
In \plst there are 3 kinds of file that you need to understand to get going with \plst:
\begin{itemize}
\item a .world file
\item a .cfg (configuration) file
\item a .inc (include) file
\end{itemize}
The .world file tells \plst what things are available to put in the world. In this file you describe your robot, any items which populate the world and the layout of the world. The .inc file follows the same syntax and format of a .world file but it can be \emph{included}. So if there is an object in your world that you might want to use in other worlds, such as a model of a robot, putting the robot description in a .inc file just makes it easier to copy over, it also means that if you ever want to change your robot description then you only need to do it in one place and your multiple simulations are changed too.
The .cfg file is what \pl reads to get all the information about the robot that you are going to use.This file tells \pl which drivers it needs to use in order to interact with the robot, if you're using a real robot these drivers are built in to \pl\footnote{Or you can download or write your own drivers, but I'm not going to talk about how to do this here.}, alternatively, if you want to make a simulation, the driver is always Stage (this is how \pl uses Stage in the same way it uses a robot: it thinks that it is a hardware driver and communicates with it as such). The .cfg file tells \pl how to talk to the driver, and how to interpret any data from the driver so that it can be presented to your code. Items described in the .world file should be described in the .cfg file if you want your code to be able to interact with that item (such as a robot), if you don't need your code to interact with the item then this isn't necessary. The .cfg file does all this specification using interfaces and drivers, which will be discussed in the following section.
\section{Interfaces, Drivers and Devices} \label{sec:Basics:InterfaceDriverDevices}
\begin{itemize}
\item Drivers are pieces of code that talk directly to hardware. These are built in to \pl so it is not important to know how to write these as you begin to learn \plst. The drivers are specific to a piece of hardware so, say, a laser driver will be different to a camera driver, and also different to a driver for a different brand of laser. This is the same as the way that drivers for graphics cards differ for each make and model of card. Drivers produce and read information which conforms to an ``interface''.
\item Interfaces are a set way for a driver to send and receive information from \pl. Like drivers, interfaces are also built in to \pl and there is a big list of them in the \pl manual\footnote{\url{http://playerstage.sourceforge.net/doc/Player-3.0.2/player/group__interfaces.html}}. They specify the syntax and semantics of how drivers and \pl interact.
\item A device is a driver that is bound to an interface so that \pl can talk to it directly. This means that if you are working on a real robot that you can interact with a real device (laser, gripper, camera etc) on the real robot, in a simulated robot you can interact with their simulations.
\end{itemize}
The official documentation actually describes these 3 things quite well with an example\footnote{\url{http://playerstage.sourceforge.net/doc/Player-3.0.2/player/group__tutorial__devices.html}}:
\begin{quotation}
Consider the laser interface. This interface defines a format in which a planar range-sensor can return range readings (basically a list of ranges, with some meta-data). The laser interface is just that: an interface. You can't do anything with it.
Now consider the sicklms200 driver. This driver controls a SICK LMS200, which is particular planar range sensor that is popular in mobile robot applications. The sicklms200 driver knows how to communicate with the SICK LMS200 over a serial line and retrieve range data from it. But you don't want to access the range data in some SICK-specific format. So the driver also knows how to translate the retrieved data to make it conform to the format defined by the laser interface.
The sicklms200 driver can be bound to the laser interface \ldots to create a device, which might have the following address:
localhost:6665:laser:0
The fields in this address correspond to the entries in the \mbox{player\_devaddr\_t} structure: host, robot, interface, and index. The host and robot fields (localhost and 6665) indicate where the device is located. The interface field indicates which interface the device supports, and thus how it can be used.
Because you might have more than one laser, the index field allows you to pick among the devices that support the given interface and are located on the given host:robot Other lasers on the same host:robot would be assigned different indexes.
\end{quotation}
The last paragraph there gets a bit technical, but don't worry. \pl talks to parts of the robot using ports (the default port is 6665), if you're using Stage then \pl and Stage communicate through these ports (even if they're running on the same computer).
All this line does is tell \pl which port to listen to and what kind of data to expect. In the example it's laser data which is being transmitted on port 6665 of the computer that \pl is running on (localhost).
You could just as easily connect to another computer by using its IP address instead of ``localhost''. The specifics of writing a device address in this way will be described in section \ref{sec:ConfigurationFile}.
\chapter{Building a World} \label{sec:BuildingAWorld}
First we will run a world and configuration file that comes bundled with Stage. In your bash shell navigate to the Stage/worlds folder, by default (in Linux at least) this is /usr/local/share/stage/worlds. Once in the correct folder type the following command to run the ``simple world'' that comes with \plst:
\begin{verbatim}
player simple.cfg
\end{verbatim}
Assuming \plst is installed properly you should now have a window open which looks figure \ref{fig:BuildingAWorld:SimpleWorld}.
\begin{figure}
\centering
\includegraphics[width=0.8\linewidth]{./pics/simpleworld.png}
\caption{The simple.cfg world after being run}
\label{fig:BuildingAWorld:SimpleWorld}
\end{figure}
Congratulations, you can now build Player/Stage simulations! You may note that the robot in the simple.cfg simulation will immediately start moving, don't worry about this for now, we will discuss how to achieve this in section \ref{sec:BuildingAWorld:BuildingRobot}.
% TODO how did the stage simple.cfg automatically start running?
\section{Building an Empty World} \label{sec:BuildingAWorld:EmptyWorld}
As you can see in section \ref{sec:BuildingAWorld}, when we tell \pl to build a world we only give it the .cfg file as an input. This .cfg file needs to tell us where to find our .world file, which is where all the items in the simulation are described. To explain how to build a Stage world containing nothing but walls we will use an example.\newline
To start building an empty world we need a .cfg file. First create a document called \verb|empty.cfg| and copy the following code into it:
\begin{verbatim}
driver
(
name "stage"
plugin "stageplugin"
provides ["simulation:0" ]
# load the named file into the simulator
worldfile "empty.world"
)
\end{verbatim}
The configuration file syntax is described in section \ref{sec:ConfigurationFile}, but basically what is happening here is that your configuration file is telling \pl that there is a driver called \verb|stage| in the \verb|stageplugin| library, and this will give \pl data which conforms to the \verb|simulation| interface. To build the simulation \pl needs to look in the worldfile called \verb|empty.world| which is stored in the same folder as this .cfg. If it was stored elsewhere you would have to include a filepath, for example \verb|./worlds/empty.world|. Lines that begin with the hash symbol (\#) are comments.
When you build a simulation, any simulation, in Stage the above chunk of code should always be the first thing the configuration file says. Obviously the name of the worldfile should be changed depending on what you called it though.
Now a basic configuration file has been written, it is time to tell \plst what to put into this simulation. This is done in the .world file.
\subsection{Models} \label{sec:BuildingAWorld:EmptyWorld:Models}
A worldfile is basically just a list of models that describes all the stuff in the simulation. This includes the basic environment, robots and other objects. The basic type of model is called ``model'', and you define a model using the following syntax:
\begin{verbatim}
define model_name model
(
# parameters
)
\end{verbatim}
This tells \plst that you are \verb|defining| a \verb|model| which you have called \verb|model_name|, and all the stuff in the round brackets are parameters of the model. To begin to understand \plst model parameters, let's look at the \verb|map.inc| file that comes with Stage, this contains the \verb\floorplan\ model, which is used to describe the basic environment of the simulation (i.e. walls the robots can bump into):
\begin{verbatim}
define floorplan model
(
# sombre, sensible, artistic
color "gray30"
# most maps will need a bounding box
boundary 1
gui_nose 0
gui_grid 0
gui_move 0
gui_outline 0
gripper_return 0
fiducial_return 0
laser_return 1
)
\end{verbatim}
We can see from the first line that they are defining a \verb|model| called \verb|floorplan|.
\begin{itemize}
\item \verb|color|: Tells \plst what colour to render this model, in this case it is going to be a shade of grey.
\item boundary: Whether or not there is a bounding box around the model. This is an example of a binary parameter, which means the if the number next to it is 0 then it is false, if it is 1 or over then it's true. So here we DO have a bounding box around our ``map'' model so the robot can't wander out of our map.
\item \verb|gui_nose|: this tells \plst that it should indicate which way the model is facing. Figure \ref{fig:BuildingAWorld:EmptyWorld:Models:GUINose} shows the difference between a map with a nose and one without.
\item \verb|gui_grid|: this will superimpose a grid over the model. Figure \ref{fig:BuildingAWorld:EmptyWorld:Models:GUIGrid} shows a map with a grid.
\item \verb|gui_move|: this indicates whether it should be possible to drag and drop the model. Here it is 0, so you cannot move the map model once \plst has been run. In section \ref{sec:BuildingAWorld} when the \plst example \verb|simple.cfg| was run it was possible to drag and drop the robot because its \verb|gui_move| variable was set to 1.
\item \verb|gui_outline|: indicates whether or not the model should be outlined. This makes no difference to a map, but it can be useful when making models of items within the world.
\item \verb|fiducial_return|: any parameter of the form some\_sensor\_return describes how that kind of sensor should react to the model. ``Fiducial'' is a kind of robot sensor which will be described later in section \ref{sec:BuildingAWorld:BuildingRobot:RobotSensors}. Setting \verb|fiducial_return| to 0 means that the map cannot be detected by a fiducial sensor.
\item \verb|gripper_return|: Like \verb|fiducial_return|, \verb|gripper_return| tells \plst that your model can be detected by the relevant sensor, i.e. it can be gripped by a gripper. Here \verb|gripper_return| is set to 0 so the map cannot be gripped by a gripper.
\end{itemize}
\begin{figure}
\centering
\begin{minipage}[c]{0.45\linewidth}
\centering
\includegraphics[width=\linewidth]{./pics/empty_world/gui_nonose_example.png}
\end{minipage}%
\hspace{0.05\linewidth}
\begin{minipage}[c]{0.45\linewidth}
\centering
\includegraphics[width=\linewidth]{./pics/empty_world/gui_nose_example.png}
\end{minipage}
\caption{The left picture shows an empty map without a nose. The right picture shows the same map with a nose to indicate orientation, this is the horizontal line from the centre of the map to the right, it shows that the map is actually facing to the right.}
\label{fig:BuildingAWorld:EmptyWorld:Models:GUINose}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=0.7\linewidth]{./pics/empty_world/gui_nonose_example.png}
\caption{An empty map with gui\_grid enabled. With gui\_grid disabled this would just be an empty white square.}
\label{fig:BuildingAWorld:EmptyWorld:Models:GUIGrid}
\end{figure}
To make use of the \verb|map.inc| file we put the following code into our world file:
\begin{verbatim}
include "map.inc"
\end{verbatim}
This inserts the \verb|map.inc| file into our world file where the include line is. This assumes that your worldfile and \verb|map.inc| file are in the same folder, if they are not then you'll need to include the filepath in the quotes. Once this is done we can modify our definition of the map model to be used in the simulation. For example:
\begin{verbatim}
floorplan
(
bitmap "bitmaps/helloworld.png"
size [12 5 1]
)
\end{verbatim}
What this means is that we are using the model ``floorplan'', and making some extra definitions; both ``bitmap'' and ``size'' are parameters of a \plst model. Here we are telling \plst that we defined a bunch of parameters for a type of model called ``floorplan'' (contained in map.inc) and now we're using this ``floorplan'' model definition and adding a few extra parameters.
\begin{itemize}
\item \verb|bitmap|: this is the filepath to a bitmap, which can be type bmp, jpeg, gif or png. Black areas in the bitmap tell the model what shape to be, non-black areas are not rendered, this is illustrated in figure \ref{fig:BuildingAWorld:EmptyWorld:Models:HelloWorld}. In the map.inc file we told the map that its ``color'' would be grey. This parameter does not affect how the bitmaps are read, \plst will always look for black in the bitmap, the \verb\color\ parameter just alters what colour the map is rendered in the simulation.
\item \verb|size|: This is the size \emph{in metres} of the simulation. All sizes you give in the world file are in metres, and they represent the actual size of things. If you have 3m x 4m robot testing arena that is 2m high and you want to simulate it then the \verb\size\ is [3 4 2]. The first number is the size in the $x$ dimension, the second is the $y$ dimension and the third is the $z$ dimension.
\end{itemize}
\begin{figure}
\centering
\begin{minipage}[c]{0.4\linewidth}
\centering
\includegraphics[width=\linewidth]{./pics/empty_world/writing.png}
\end{minipage}%
\hspace{0.05\linewidth}
\begin{minipage}[c]{0.5\linewidth}
\centering
\includegraphics[width=\linewidth]{./pics/empty_world/helloworld.png}
\end{minipage}
\caption{The left image is our "helloworld.png" bitmap, the right image is what \plst interprets that bitmap as. The coloured areas are walls, the robot can move everywhere else.}
\label{fig:BuildingAWorld:EmptyWorld:Models:HelloWorld}
\end{figure}
A full list of model parameters and their descriptions can be found in the official Stage manual\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__model.html}}. Most of the useful parameters have already been described here, however there are a few other types of model which are relevant to building simulations of robots, these will be described later in section \ref{sec:BuildingAWorld:BuildingRobot}.
\subsection{Describing the \plst Window} \label{sec:BuildingAWorld:EmptyWorld:PLSTWindow}
The worldfile also can be used to describe the simulation window that \plst creates. \plst will automatically make a window for the simulation if you don't put any window details in the worldfile, however, it is often useful to put this information in anyway. This prevents a large simulation from being too big for the window, or to increase or decrease the size of the simulation.
Like a model, a window is an inbuilt, high-level entity with lots of parameters. Unlike models though, there can be only one window in a simulation and only a few of its parameters are really needed. The simulation window is described with the following syntax:
\begin{verbatim}
window
(
# parameters...
)
\end{verbatim}
The two most important parameters for the window are \verb|size| and \verb|scale|.
\begin{itemize}
\item \verb|size|: This is the size the simulation window will be \emph{in pixels}. You need to define both the width and height of the window using the following syntax: \verb|size [width height]|.
\item \verb|scale|: This is how many metres of the simulated environment each pixel shows. The bigger this number is, the smaller the simulation becomes. The optimum value for the scale is $\frac{window\_size}{floorplan\_size}$ and it should be rounded downwards so the simulation is a little smaller than the window it's in, some degree of trial and error is needed to get this right.
\end{itemize}
A full list of window parameters can be found in the Stage manual under ``WorldGUI''\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__worldgui.html}}.
\subsection{Making a Basic Worldfile}\label{sec:BuildingAWorld:EmptyWorld:BasicWorldfile}
We have already discussed the basics of worldfile building: models and the window. There are just a few more parameters to describe which don't belong in either a model or a window description, these are optional though, and the defaults are pretty sensible.
\begin{itemize}
\item \verb|interval_sim|: This is how many simulated milliseconds there are between each update of the simulation window, the default is 100 milliseconds.
\item \verb|interval_real|: This is how many real milliseconds there are between each update of the simulation window. Balancing this parameter and the \verb|interval\_sim| parameter controls the speed of the simulation. Again, the default value is 100 milliseconds, both these interval parameter defaults are fairly sensible, so it's not always necessary to redefine them.
\end{itemize}
The Stage manual contains a list of the high-level worldfile parameters\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__world.html}}.\newline
Finally, we are able to write a worldfile!
\begin{verbatim}
include "map.inc"
# configure the GUI window
window
(
size [700.000 700.000]
scale 41
)
# load an environment bitmap
floorplan
(
bitmap "bitmaps/cave.png"
size [15 15 0.5]
)
\end{verbatim}
If we save the above code as empty.world (correcting any filepaths if necessary) we can run its corresponding empty.cfg file (see section \ref{sec:BuildingAWorld:EmptyWorld}) to get the simulation shown in figure \ref{fig:BuildingAWorld:EmptyWorld:BasicWorldfile:FinalEmptyWorld}.
\begin{figure}
\centering
\includegraphics[width=0.7\linewidth]{./pics/empty_world/finalEmptyWorld.png}
\caption{Our Empty World.}
\label{fig:BuildingAWorld:EmptyWorld:BasicWorldfile:FinalEmptyWorld}
\end{figure}
\section{Building a Robot} \label{sec:BuildingAWorld:BuildingRobot}
In \plst a robot is just a slightly advanced kind of model, all the parameters described in section \ref{sec:BuildingAWorld:EmptyWorld:Models} can still be applied.
\subsection{Sensors and Devices} \label{sec:BuildingAWorld:BuildingRobot:RobotSensors}
There are six built-in kinds of model that help with building a robot, they are used to define the sensors and actuators that the robot has. These are associated with a set of model parameters which define by which sensors the model can be detected (these are the \verb\_return\s mentioned earlier). Each of these built in models acts as an \emph{interface} (see section \ref{sec:Basics:InterfaceDriverDevices}) between the simulation and \pl. If your robot has one of these kinds of sensor on it, then you need to use the relevant model to describe the sensor, otherwise Stage and \pl won't be able to pass the data between each other. It is possible to write your own interfaces, but the stuff already included in \plst should be sufficient for most people's needs. A full list of interfaces that \pl supports can be found in the \pl manual\footnote{\url{http://playerstage.sourceforge.net/doc/Player-3.0.2/player/group__interfaces.html}} although only the following are supported by the current distribution of Stage (version 3.2.X). Unless otherwise stated, these models use the \pl interface that shares its name:
\subsubsection{camera}\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__model__camera.html}}
The camera model adds a camera to the robot model and allows your code to interact with the simulated camera. The camera parameters are as follows:
\begin{itemize}
\item \verb|resolution [x y]|: the resolution, in pixels, of the camera's image.
\item \verb|range [min max]|: the minimum and maximum range that the camera can detect
\item \verb|fov [x y]|: the field of view of the camera \emph{in DEGREES}.
\item \verb|pantilt [pan tilt]|: the horizontal angle the camera can move through (pan) and the vertical angle (tilt). So for instance \verb\pantilt [90 20]\ allows the camera to move $45^\circ$ left and $45^\circ$ right and $10^\circ$ up and $10^\circ$ down.
\end{itemize}
\subsubsection{blobfinder}\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__model__blobfinder.html}}
This simulates colour detection software that can be run on the image from the robot's camera. It is not necessary to include a model of the camera in your description of the robot if you want to use a blobfinder, the blobfinder will work on its own. The blobfinder can only find a model if its \verb|blob_return| parameter is true. The parameters for the blobfinder are described in the Stage manual, but the most useful ones are here:
\begin{itemize}
\item \verb|colors_count <int>|: the number of different colours the blobfinder can detect
\item \verb|colors [ ]|: the names of the colours it can detect. This is given to the blobfinder definition in the form \verb|["black" "blue" "cyan"]|. These colour names are from the built in X11 colour database rgb.txt. This is built in to Linux.\footnote{rgb.txt can normally be found at /usr/share/X11/rgb.txt assuming it's properly installed, alternatively a Google search for ``rgb.txt'' will give you the document.}
\item \verb|image [x y]|: the size of the image from the camera, in pixels.
\item \verb|range <float>|: The maximum range that the camera can detect, in metres.
\item \verb|fov <float>|: field of view of the blobfinder \emph{in RADIANS}.
\end{itemize}
\subsubsection{fiducial} \footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__model__fiducial.html}}
A fiducial is a fixed point in an image, so the fiducial finder simulates image processing software that locates fixed points in an image. The fiducialfinder is able to locate objects in the simulation whose \verb|fiducial_return| parameter is set to true. Stage also allows you to specify different types of fiducial using the \verb|fiducial_key| parameter of a model. This means that you can make the robots able to tell the difference between different fiducials by what key they transmit. The fiducial finder and the concept of \verb|fiducial_key|s is properly explained in the Stage manual. The fiducial sensors parameters are:
\begin{itemize}
\item \verb\range_min\: The minimum range at which a fiducial can be detected, in metres.
\item \verb\range_max\: The maximum range at which a fiducial can be detected, in metres.
\item \verb\range_max_id\: The maximum range at which a fiducial's key can be accurately identified. If a fiducial is closer that \verb\range_max\ but further away than \verb\range_max_id\ then it detects that there is a fiducial but can't identify it.
\item \verb\fov\: The field of view of the fiducial finder \emph{in RADIANS}.
\end{itemize}
\subsubsection{ranger}\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__model__ranger.html}}\label{sec:BuildingAWorld:BuildingRobot:RobotSensors:Ranger}
This simulates any kind of obstacle detection device (e.g. sonars or infra-red sensors). These can locate models whose \verb|ranger_return| is true. Using a ranger model you can define any number of ranger devices and apply them all to a single robot. Unlike the other types of model this doesn't use the interface with its name but instead the \verb\sonar\ interface, there is more about this in section \ref{sec:ConfigurationFile:FinishingCFG}. The parameters for the \verb\ranger\ model and their inputs are described in the Stage manual, but basically:
\begin{itemize}
\item \verb|scount <int>|: The number of ranger sensors in this ranger model
\item \verb|spose[ranger_number] [x y yaw]|: Tells the simulator where the rangers are placed around the robot. How to write the \verb|[x y yaw]| data is explained in section \ref{sec:BuildingAWorld:BuildingRobot:RobotSensors}.
\item \verb|ssize [x y]|: how big the sensors are.
\item \verb|sview [min max fov]|: defines the maximum and minimum distances that can be sensed and also the field of view \emph{in DEGREES}.
\end{itemize}
\subsubsection{laser}\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__model__laser.html}}
A laser is a special case of ranger sensor which only allows one ranger (so there's none of the \verb|scount|, \verb|spose| stuff), but it has a very large field of view.
If a model has its \verb|laser_return| parameter enabled then a laser can detect it. Details about laser parameters can be found in the Stage manual, however the most useful parameters are:
\begin{itemize}
\item \verb\samples\: The number of ranger readings the laser takes. The laser model behaves like a large number of rangers sensors all with the same $x$ and $y$ coordinates relative to the robot's centre, each of these rangers has a slightly different yaw. The rangers are spaced so that there are \verb\samples\ number of rangers distributed evenly to give the laser's field of view. So if the field of view is $180^\circ$ and there are 180 samples the rangers are $1^\circ$ apart.
\item \verb|range_max|: the maximum range of the laser.
\item \verb|fov|: the field of view of the laser \emph{in RADIANS}.
\end{itemize}
\subsubsection{gripper}\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__model__gripper.html}}\label{sec:BuildingAWorld:BuildingRobot:RobotSensors:Gripper}
The gripper model is a simulation of the gripper you get on a Pioneer robot.\footnote{The Pioneer grippers looks like a big block on the front of the robot with two big sliders that close around an object.} If you put a gripper on your robot model it means that your robot is able to pick up objects and move them around within the simulation. The online Stage manual says that grippers are deprecated in Stage 3.X.X, however this is not actually the case and grippers are very useful if you want your robot to be able to manipulate and move items. The parameters you can use to customise the gripper model are:
\begin{itemize}
\item \verb\size [x y z]\: The x and y dimensions of the gripper.
\item \verb\pose [x y z yaw]\: Where the gripper is placed on the robot, relative to the robot's geometric centre. The pose parameter is decribed properly in section \ref{sec:BuildingAWorld:BuildingRobot:RobotSensors}.
\end{itemize}
\subsubsection{position}\footnote{\url{http://playerstage.sourceforge.net/doc/Stage-3.2.1/group__model__position.html}}\label{sec:BuildingAWorld:BuildingRobot:RobotSensors:Position}
The position model simulates the robot's odometry, this is when the robot keeps track of where it is by recording how many times its wheels spin and the angle it turns. This robot model is the most important of all because it allows the robot model to be embodied in the world, meaning it can collide with anything which has its \verb|obstacle_return| parameter set to true. The position model uses the \verb|position2d| interface, which is essential for \pl because it tells \pl where the robot actually is in the world.
The most useful parameters of the position model are:
\begin{itemize}
\item \verb|drive|: Tells the odometry how the robot is driven. This is usually ``diff'' which means the robot is controlled by changing the speeds of the left and right wheels independently. Other possible values are ``car'' which means the robot uses a velocity and a steering angle, or ``omni'' which means it can control how it moves along the $x$ and $y$ axes of the simulation.
\item \verb|localization|: tells the model how it should record the odometry ``odom'' if the robot calculates it as it moves along or ``gps'' for the robot to have perfect knowledge about where it is in the simulation.
\item \verb|odom_error [x y angle]|: The amount of error that the robot will make in the odometry recordings.
% \item \verb|mass <int>|: How heavy the robot is.
\end{itemize}
\subsection{An Example Robot} \label{sec:BuildingAWorld:BuildingRobot:ExampleRobot}
To demonstrate how to build a model of a robot in \plst we will build our own example. First we will describe the physical properties of the robot, such as size and shape. Then we will add sensors onto it so that it can interact with its environment.
\subsubsection{The Robot's Body}\label{sec:BuildingAWorld:BuildingRobot:ExampleRobot:Body}
Let's say we want to model a rubbish collecting robot called ``Bigbob''. The first thing we need to do is describe its basic shape, to do this you need to know your robot's dimensions in metres. Figure \ref{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:BasicBigbob} shows the basic shape of Bigbob drawn onto some cartesian coordinates, the coordinates of the corners of the robot have been recorded. We can then build this model using the \verb|block| model parameter\footnote{In this example we're using blocks with the position model type but we could equally use it with other model types.}:
\begin{figure}
\centering
\includegraphics[width=0.7\linewidth]{./pics/robot_building/bigbob1.png}
\caption{The basic shape we want to make Bigbob, the units on the axes are in metres.}
\label{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:BasicBigbob}
\end{figure}
\begin{verbatim}
define bigbob position
block
(
points 6
point[0] [0.75 0]
point[1] [1 0.25]
point[2] [1 0.75]
point[3] [0.75 1]
point[4] [0 1]
point[5] [0 0]
z [0 1]
)
)
\end{verbatim}
In the first line of this code we state that we are defining a \verb|position| model called \verb|bigbob|. Next \verb|block| declares that this \verb\position\ model contains a block.
The following lines go on to describe the shape of the block; \verb|points 6| says that the block has 6 corners and \verb|point[number] [x y]| gives the coordinates of each corner of the polygon in turn. It is important to go around the robot doing each corner in turn going \emph{anti-clockwise} around the robot, otherwise \plst won't properly render the block. Finally, the \verb\z [height_from height_to]\ states how tall the robot should be, the first parameter being a lower coordinate in the $z$ plane, and the second parameter being the upper coordinate in the $z$ plane. In this example we are saying that the block describing Bigbob's body is on the ground (i.e. its lower $z$ coordinate is at 0) and it is 1 metre tall. If I wanted it to be from 50cm off the ground to 1m then I could use \verb\z [0.5 1]\.\newline
Now in the same way as we built the body we can add on some teeth for Bigbob to collect rubbish between. Figure \ref{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:BigbobTeeth} shows Bigbob with teeth plotted onto a cartesian grid:
\begin{figure}
\centering
\includegraphics[width=0.6\linewidth]{./pics/robot_building/bigbob2.png}
\caption{The new shape of Bigbob.}
\label{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:BigbobTeeth}
\end{figure}
\begin{verbatim}
define bigbob position
(
size [1.25 1 1]
# the shape of Bigbob
block
(
points 6
point[5] [0 0]
point[4] [0 1]
point[3] [0.75 1]
point[2] [1 0.75]
point[1] [1 0.25]
point[0] [0.75 0]
z [0 1]
)
block
(
points 4
point[3] [1 0.75]
point[2] [1.25 0.75]
point[1] [1.25 0.625]
point[0] [1 0.625]
z [0 0.5]
)
block
(
points 4
point[3] [1 0.375]
point[2] [1.25 0.375]
point[1] [1.25 0.25]
point[0] [1 0.25]
z [0 0.5]
)
)
\end{verbatim}
To declare the size of the robot you use the \verb|size [x y z]| parameter, this will cause the polygon described to be scaled to fit into a box which is \verb|x| by \verb\y\ in size and \verb\z\ metres tall. The default size is 1 x 1 x 1 metres, so because the addition of rubbish-collecting teeth made Bigbob longer, the size parameter was needed to stop \plst from making the robot smaller than it should be. In this way we could have specified the polygon coordinates to be 4 times the distance apart and then declared its size to be \verb|1.25 x 1 x 1| metres, and we would have got a robot the size we wanted. For a robot as large as Bigbob this is not really important, but it could be useful when building models of very small robots.
It should be noted that it doesn't actually matter where in the cartesian coordinate system you place the polygon, instead of starting at \verb|(0, 0)| it could just as easily have started at \verb|(-1000, 12345)|. With the \verb\block\ parameter we just describe the \emph{shape} of the robot, not its size or location in the map. \newline
You may have noticed that in figures \ref{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:BasicBigbob} and \ref{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:BigbobTeeth} Bigbob is facing to the right of the grid. When you place any item in a \plst simulation they are, by default, facing to the right hand side of the simulation. Figure \ref{fig:BuildingAWorld:EmptyWorld:Models:GUIGrid} shows that the grids use a typical Cartesian coordinate system, and so if you want to alter the direction an object in the simulation is pointing (its ``yaw'') any angles you give use the x-axis as a reference, just like vectors in a Cartesian coordinate system (see figure \ref{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:EmptyCartGrid}) and so the default yaw is $0^{\circ}$. This is also why in section \ref{sec:BuildingAWorld:EmptyWorld} the \verb|gui_nose| shows the map is facing to the right. Figure \ref{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:Yaws} shows a few examples of robots with different yaws.
\begin{figure}
\centering
\includegraphics[width=0.5\linewidth]{./pics/robot_building/cartesian_grid_wpolars.png}
\caption{A cartesian grid showing how angles are described.}
\label{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:EmptyCartGrid}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=0.6\linewidth]{./pics/robot_building/yaw_examples.png}
\caption{Starting from the top right robot and working anti-clockwise, the yaws of these robots are 0, 90, -45 and 200.}
\label{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:Yaws}
\end{figure}
By default, \plst assumes the robot's centre of rotation is at its geometric centre based on what values are given to the robot's \verb|size| parameter. Bigbob's \verb|size| is \verb|1.25 x 1 x 1| so \plst will place its centre at \verb|(0.625, 0.5, 0.5)|, which means that Bigbob's wheels would be closer to its teeth. Instead let's say that Bigbob's centre of rotation is in the middle of its main body (shown in figure \ref{fig:BuildingAWorld:BuildingRobot:ExampleRobot:Body:BasicBigbob}) which puts the centre of rotation at \verb|(0.5, 0.5, 0.5)|. To change this in robot model you use the \verb|origin [x-offset y-offset z-offset]| command:
\begin{verbatim}
define bigbob position
(
# actual size
size [1.25 1 1]
# centre of rotation offset
origin [0.125 0 0]
# the shape of Bigbob
block
...
...
...
)
\end{verbatim}
Finally we will specify the \verb|drive| of Bigbob, this is a parameter of the \verb|position| model and has been described earlier.
\begin{verbatim}
define bigbob position
(
# actual size
size [1.25 1 1]
# centre of rotation offset
origin [0.125 0 0]
# the shape of Bigbob
block
...
...
...
# positonal things
drive "diff"
)
\end{verbatim}
\subsubsection{The Robot's Sensors}\label{sec:BuildingAWorld:BuildingRobot:RobotSensors}
Now that Bigbob's body has been built let's move on to the sensors. We will put sonar and blobfinding sensors onto Bigbob so that it can detect walls and see coloured blobs it can interpret as rubbish to collect. We will also put a laser between Bigbob's teeth so that it can detect when an item passes in between them.
We will start with the sonars. The first thing to do is to define a model for the sonar array that is going to be attached to Bigbob:
\begin{verbatim}
define bigbobs_sonars ranger
(
# parameters...
)
\end{verbatim}
Here we tell \plst that we will \verb|define| a set of sonar sensors called \verb|bigbobs_sonars| and we're using the model type \verb|ranger| to tell \plst that this is a model of some ranging devices. Let's put four sonars on Bigbob, one on the front of each tooth, and one on the front left and the front right corners of its body.
When building Bigbob's body we were able to use any location on a coordinate grid that we wanted and could declare our shape polygons to be any distance apart we wanted so long as we resized the model with \verb|size|. In contrast, sensors - all sensors not just rangers - must be positioned according to the \emph{robot's} origin and actual size. To work out the distances in metres it helps to do a drawing of where the sensors will go on the robot and their distances from the robot's origin. When we worked out the shape of Bigbob's body we used its actual size, so we can use the same drawings again to work out the distances of the sensors from the origin as shown in figure \ref{fig:BuildingAWorld:BuildingRobot:RobotSensors:Sonars}.
\begin{figure}
\centering
\includegraphics[width=0.6\linewidth]{./pics/robot_building/bigbob_sonars.png}
\caption{The position of Bigbob's sonars (in red) relative to its origin. The origin is marked with a cross, some of the distances from the origin to the sensors have been marked. The remaining distances can be done by inspection.}
\label{fig:BuildingAWorld:BuildingRobot:RobotSensors:Sonars}
\end{figure}
Now we know how many sensors we want, and their coordinates relative to the origin we can begin to build our model of the sonar array. Here we will use the \verb|scount| and \verb|spose| parameters mentioned in \ref{sec:BuildingAWorld:BuildingRobot:RobotSensors:Ranger}. The values for \verb|spose| are \verb|[x y yaw]|, remember that yaw is in degrees and is measured relative to the $x$ axis.
\begin{verbatim}
define bigbobs_sonars ranger
(
# number of sonars
scount 4
# define the pose of each transducer [xpos ypos heading]
spose[0] [ 0.75 0.1875 0 ] #fr left tooth
spose[1] [ 0.75 -0.1875 0 ] #fr right tooth
spose[2] [ 0.25 0.5 30] # left corner
spose[3] [ 0.25 -0.5 -30] # right corner
)
\end{verbatim}
The process of working out where the sensors go relative to the origin of the robot is the most complicated part of describing the sensor, the rest is easy. To define the size, range and field of view of the sonars we just consult the sonar device's datasheet.
\begin{verbatim}
define bigbobs_sonars ranger
(
# number of sonars
scount 4
# define the pose of each transducer [xpos ypos heading]
spose[0] [ 0.75 0.1875 0 ] #fr left tooth
spose[1] [ 0.75 -0.1875 0 ] #fr right tooth
spose[2] [ 0.25 0.5 30] # left corner
spose[3] [ 0.25 -0.5 -30] # right corner
# define the field of view of each transducer
# [range_min range_max view_angle]
sview [0.3 2.0 10]
# define the size of each transducer [xsize ysize] in metres
ssize [0.01 0.05]
)
\end{verbatim}
Now that Bigbob's sonars are done we will attached a blobfinder:
\begin{verbatim}
define bigbobs_eyes blobfinder
(
# parameters
)
\end{verbatim}
Bigbob is a rubbish-collector so here we should tell it what colour of rubbish to look for. Let's say that the intended application of Bigbob is in an orange juice factory and he picks up any stray oranges or juice cartons that fall on the floor. Oranges are orange, and juice cartons are (let's say) dark blue so Bigbob's blobfinder will look for these two colours:
\begin{verbatim}
define bigbobs_eyes blobfinder
(
# number of colours to look for
colors_count 2
# which colours to look for
colors ["orange" "DarkBlue"]
)
\end{verbatim}
Then we define the properties of the camera, again these come from a datasheet:
\begin{verbatim}
define bigbobs_eyes blobfinder
(
# number of colours to look for
colors_count 2
# which colours to look for
colors ["orange" "DarkBlue"]
# camera parameters
image [160 120] #resolution
range 5.00
fov 1.047196667 # 60 degrees = pi/3 radians
)
\end{verbatim}
The last sensor that needs adding to Bigbob is the laser, which will be used to detect whenever a piece of rubbish has been collected, the laser's location on the robot is shown in figure \ref{fig:BuildingAWorld:BuildingRobot:RobotSensors:Laser}. Following the same principles as for our previous sensor models we can create a description of this laser:
\begin{verbatim}
define bigbobs_laser laser
(
# distance between teeth in metres
range_max 0.25
# does not need to be big
fov 20
#number of ranges measured
samples 180
pose [0.625 0.125 -0.975 270]
size [0.025 0.025 0.025]
)
\end{verbatim}
With this laser we've set its maximum range to be the distance between teeth, and the field of view is arbitrarily set to $20^{\circ}$. We have calculated the laser's \verb|pose| in exactly the same way as the sonars \verb|spose|, by measuring the distance from the laser's centre to the robot's origin (which we set with the \verb\origin\ parameter earlier). The $z$ coordinate of the pose parameter when describing parts of the robot is relative to the very top of the robot. In this case the robot is 1 metre tall so we put the laser at $-0.975$ so that it is on the ground. The laser's yaw is set to $270^{\circ}$ so that it points across Bigbob's teeth. We also set the size of the laser to be 2.5cm cube so that it doesn't obstruct the gap between Bigbob's teeth.
\begin{figure}
\centering
\includegraphics[width=0.6\linewidth]{./pics/robot_building/bigbob_laser.png}
\caption{The position of Bigbob's laser (in red) and its distance, in metres, relative to its origin (marked with a cross).}
\label{fig:BuildingAWorld:BuildingRobot:RobotSensors:Laser}
\end{figure}
Now that we have a robot body and sensor models all we need to do is put them together and place them in the world. To add the sensors to the body we need to go back to the \verb|bigbob position| model:
\begin{verbatim}
define bigbob position
(
# actual size
size [1.25 1 1]
# centre of rotation offset
origin [0.125 0 0]
# the shape of Bigbob
block
...
...
...
# positonal things
drive "diff"
# sensors attached to bigbob
bigbobs_sonars()
bigbobs_eyes()
bigbobs_laser()
)
\end{verbatim}
The extra line \verb|bigbobs_sonars()| adds the sonar model called \verb|bigbobs_sonars()| onto the \verb|bigbob| model, likewise for \verb|bigbobs_eyes()| and \verb|bigbobs_laser()|. After this final step we now have a complete model of our robot bigbob, the full code for which can be found in appendix A. At this point it's worthwhile to copy this into a .inc file, so that the model could be used again in other simulations or worlds.
To put our Bigbob model into our empty world (see section \ref{sec:BuildingAWorld:EmptyWorld:BasicWorldfile}) we need to add the robot to our worldfile empty.world:
\begin{verbatim}
include "map.inc"
include "bigbob.inc"
# size of the whole simulation
size [15 15]
# configure the GUI window
window
(
size [ 700.000 700.000 ]
scale 35
)
# load an environment bitmap
floorplan
(
bitmap "bitmaps/cave.png"
size [15 15 0.5]
)
bigbob
(
name "bob1"
pose [-5 -6 0 45]
color "green"
)
\end{verbatim}
Here we've put all the stuff that describes Bigbob into a .inc file \verb|bigbob.inc|, and when we include this, all the code from the .inc file is inserted into the .world file. The section here is where we put a version of the bigbob model into our world:
\begin{verbatim}
bigbob
(
name "bob1"
pose [-5 -6 0 45]
color "green"
)
\end{verbatim}
Bigbob is a model description, by not including any \verb|define| stuff in the top line there it means that we are making an instantiation of that model, with the name \verb|bob1|. Using an object-oriented programming analogy, \verb|bigbob| is our class, and \verb|bob1| is our object of class \verb|bigbob|. The \verb|pose [x y yaw]| parameter works in the same was as \verb|spose [x y yaw]| does. The only differences are that the coordinates use the centre of the simulation as a reference point and \verb|pose| lets us specify the initial position and heading of the entire \verb\bob1\ model, not just one sensor within that model.
Finally we specify what colour \verb|bob1| should be, by default this is red. The \verb|pose| and \verb|color| parameters could have been specified in our bigbob model but by leaving them out it allows us to vary the colour and position of the robots for each different robot of type \verb|bigbob|, so we could declare multiple robots which are the same size, shape and have the same sensors, but are rendered by \plst in different colours and are initialised at different points in the map.\newline
When we run the new empty.world with \plst we see our Bigbob robot is occupying the world, as shown in figure \ref{fig:BuildingAWorld:BuildingRobot:RobotSensors:FinalRobot}.
\begin{figure}
\centering
\includegraphics[width=0.7\linewidth]{./pics/robot_building/final_robot_build.png}
\caption{Our bob1 robot placed in the empty world.}
\label{fig:BuildingAWorld:BuildingRobot:RobotSensors:FinalRobot}
\end{figure}
\section{Building Other Stuff} \label{sec:BuildingAWorld:OtherStuff}
We established in section \ref{sec:BuildingAWorld:BuildingRobot:RobotSensors} that Bigbob works in a orange juice factory collecting oranges and juice cartons. Now we need to build models to represent the oranges and juice cartons so that Bigbob can interact with things.
We'll start by building a model of an orange:
\begin{verbatim}
define orange model
(
# parameters...
)
\end{verbatim}
The first thing to define is the shape of the orange. The \verb|block| parameter is one way of doing this, which we can use to build a blocky approximation of a circle. An alternative to this is to use \verb|bitmap| which we previously saw being used to create a map. What the bitmap command actually does is take in a picture, and turn it into a series of blocks which are connected together to make a model the same shape as the picture. This is illustrated in figure \ref{fig:BuildingAWorld:OtherStuff:Ghosts}.
\begin{figure}
\centering
\begin{minipage}[c]{0.3\linewidth}
\centering
\includegraphics{./pics/oranges_box/ghost_original.png} %[width=\linewidth]
\end{minipage}%
\hspace{0.05\linewidth}
\begin{minipage}[c]{0.6\linewidth}
\centering
\includegraphics[width=\linewidth]{./pics/oranges_box/ghost_woutline.png}
\end{minipage}
\caption{The left image is the original picture, the right image is its \plst interpretation.}
\label{fig:BuildingAWorld:OtherStuff:Ghosts}
\end{figure}
\begin{figure}
\centering
\begin{minipage}[c]{0.2\linewidth}
\centering
\includegraphics{./pics/oranges_box/circle.png}
\caption{./bitmaps/circle.png}
\label{fig:circle.png}
\end{minipage}%
\hspace{0.1\linewidth}
\begin{minipage}[c]{0.6\linewidth}
\centering
\includegraphics[width=\linewidth]{./pics/oranges_box/orange_and_bob.png}
\caption{The orange model rendered in the same \plst window as Bigbob.}
\label{fig:BuildingAWorld:OtherStuff:OrangeAndBob}
\end{minipage}
\end{figure}
\begin{verbatim}
define orange model
(
bitmap "bitmaps/circle.png"
size [0.15 0.15 0.15]
color "orange"
)
\end{verbatim}
In this bit of code we describe a model called \verb|orange| which uses a bitmap to define its shape and represents an object which is $15cm$ x $15cm$ x $15cm$ and is coloured orange. Figure \ref{fig:BuildingAWorld:OtherStuff:OrangeAndBob} shows our orange model next to Bigbob.
Building a juice carton model is similarly quite easy:
\begin{verbatim}
define carton model
(
# a carton is retangular
# so make a square shape and use size[]
block
(
points 4
point[0] [1 0]
point[1] [1 1]
point[2] [0 1]
point[3] [0 0]
z [0 1]
)
# average litre carton size is ~ 20cm x 10cm x 5cm ish
size [0.1 0.2 0.2]
color "DarkBlue"
)
\end{verbatim}
We can use the \verb|block| command since juice cartons are boxy, with boxy things it's slightly easier to describe the shape with \verb|block| than drawing a bitmap and using that. In the above code I used \verb\block\ to describe a metre cube (since that's something that can be done pretty easily without needing to draw a carton on a grid) and then resized it to the size I wanted using \verb\size\.
Now that we have described basic \verb|orange| and \verb|carton| models it's time to put some oranges and cartons into the simulation. This is done in the same way as our example robot was put into the world:
\begin{verbatim}
orange
(
name "orange1"
pose [-2 -5 0 0]
)
carton
(
name "carton1"
pose [-3 -5 0 0]
)
\end{verbatim}
We created models of oranges and cartons, and now we are declaring that there will be an instance of these models (called \verb|orange1| and \verb|carton1| respectively) at the given positions. Unlike with the robot, we declared the \verb|color| of the models in the description so we don't need to do that here. If we did have different colours for each orange or carton then it would mess up the blobfinding on Bigbob because the robot is only searching for orange and dark blue.
At this point it would be useful if we could have more than just one orange or carton in the world (Bigbob would not be very busy if there wasn't much to pick up), it turns out that this is also pretty easy:
\begin{verbatim}
orange(name "orange1" pose [-1 -5 0 0])
orange(name "orange2" pose [-2 -5 0 0])
orange(name "orange3" pose [-3 -5 0 0])
orange(name "orange4" pose [-4 -5 0 0])
carton(name "carton1" pose [-2 -4 0 0])
carton(name "carton2" pose [-2 -3 0 0])
carton(name "carton3" pose [-2 -2 0 0])
carton(name "carton4" pose [-2 -1 0 0])
\end{verbatim}
Up until now we have been describing models with each parameter on a new line, this is just a way of making it more readable for the programmer -- especially if there are a lot of parameters. If there are only a few parameters or you want to be able to comment it out easily, it can all be put onto one line. Here we declare that there will be four \verb|orange| models in the simulation with the names \verb|orange1| to \verb|orange4|, we also need to specify different poses for the models so they aren't all on top of each other. Properties that the orange models have in common (such as shape, colour or size) should all be in the model definition.
The full worldfile is included in appendix B, this includes the orange and carton models as well as the code for putting them in the simulation. Figure \ref{fig:BuildingAWorld:OtherStuff:FinalRobotAndStuff} shows the populated \plst simulation.
\begin{figure}
\centering
\includegraphics[width=0.8\linewidth]{./pics/oranges_box/final_robot_and_stuff.png}
\caption{The Bigbob robot placed in the simulation along with junk for it to pick up.}
\label{fig:BuildingAWorld:OtherStuff:FinalRobotAndStuff}
\end{figure}
\chapter{Writing a Configuration (.cfg) File} \label{sec:ConfigurationFile}
As mentioned earlier, \pl is a hardware abstraction layer which connects your code to the robot's hardware. It does this by acting as a Server/Client type program where your code and the robot's sensors are clients to a \pl server which then passes the data and instructions around to where it all needs to go. This stuff will be properly explained in section \ref{sec:Coding}, it all sounds more complicated than it is because \plst takes care of all the difficult stuff. The configuration file is needed in order to tell the \pl server which drivers to use and which interfaces the drivers will be using.
For each model in the simulation or device on the robot that you want to interact with, you will need to specify a driver. This is far easier than writing worldfile information, and follows the same general syntax. The driver specification is in the form:
\begin{verbatim}
driver
(
name "driver_name"
provides [device_address]
# other parameters...
)
\end{verbatim}
The \verb|name| and \verb|provides| parameters are mandatory information, without them \pl won't know which driver to use (given by \verb|name|) and what kind of information is coming from the driver (\verb|provides|). The \verb|name| parameter is not arbitrary, it must be the name of one of \pl's inbuilt drivers\footnote{It is also possible to build your own drivers for a hardware device but this document won't go into how to do this because it's not relevant to \plst.} that have been written for \pl to interact with a robot device. A list of supported driver names is in the \pl Manual\footnote{\url{http://playerstage.sourceforge.net/doc/Player-3.0.2/player/group__drivers.html}}, although when using Stage the only one that is needed is \verb|"stage"|.
The \verb|provides| parameter is a little more complicated than \verb|name|. It is here that you tell \pl what interface to use in order to interpret information given out by the driver (often this is sensor information from a robot), any information that a driver \verb\provides\ can be used by your code. For a Stage simulated robot the \verb|"stage"| driver can provide the interfaces to the sensors discussed in section \ref{sec:BuildingAWorld:BuildingRobot:RobotSensors}.
Each interface shares the same name as the sensor model, so for example a \verb|ranger| model would use the \verb|ranger| interface to interact with \pl and so on (the only exception to this being the \verb|position| model which uses the \verb|position2d| interface).
The \pl manual contains a list of all the different interfaces that can be used\footnote{\url{http://playerstage.sourceforge.net/doc/Player-3.0.2/player/group__interfaces.html}}, the most useful ones have already been mentioned in section \ref{sec:BuildingAWorld:BuildingRobot:RobotSensors}, although there are others too numerable to list here.
The input to the \verb|provides| parameter is a ``device address'', which specifies which TCP port an interface to a robot device can be found, section \ref{sec:ConfigurationFile:DeviceAddress} has more information about device addresses. This uses the key:host:robot:interface:index form separated by white space.
\begin{verbatim}
provides ["key:host:robot:interface:index"
"key:host:robot:interface:index"
"key:host:robot:interface:index"
...]
\end{verbatim}
After the two mandatory parameters, the next most useful driver parameter is \verb|model|. This is only used if \verb|"stage"| is the driver, it tells \pl which particular model in the worldfile is providing the interfaces for this particular driver. A different driver is needed for each model that you want to use.
Models that aren't required to do anything (such as a map, or in the example of section \ref{sec:BuildingAWorld:OtherStuff} oranges and boxes) don't need to have a driver written for them.\newline
The remaining driver parameters are \verb|requires| and \verb|plugin|. The \verb|requires| is used for drivers that need input information such as \verb|"vfh"|, it tells this driver where to find this information and which interface it uses.
The \verb|requires| parameter uses the same key:host:robot:interface:index syntax as the \verb|provides| parameter. Finally the \verb|plugin| parameter is used to tell \pl where to find all the information about the driver being used.
Earlier we made a .cfg file in order to create a simulation of an empty (or at least unmoving) world, the .cfg file read as follows:
\begin{verbatim}
driver
(
name "stage"
plugin "stageplugin"
provides ["simulation:0" ]
# load the named file into the simulator
worldfile "empty.world"
)
\end{verbatim}
This has to be done at the beginning of the configuration file because it tells \pl that there is a driver called \verb|"stage"| that we're going to use and the code for dealing with this driver can be found in the \verb|stageplugin| plugin. This needs to be specified for Stage because Stage is an add-on for \pl, for drivers that are built into \pl by default the \verb|plugin| doesn't need to be specified.
\section{Device Addresses - key:host:robot:interface:index} \label{sec:ConfigurationFile:DeviceAddress}
A device address is used to tell \pl where the driver you are making will present (or receive) information and which interface to use in order to read this information. This is a string in the form \verb|key:host:robot:interface:index| where each field is separated by a colon.
\begin{itemize}
\item \verb\key\: The \pl manual states that: \textit{``The purpose of the key field is to allow a driver that supports multiple interfaces of the same type to map those interfaces onto different devices''}\footnote{\url{http://playerstage.sourceforge.net/doc/Player-2.1.0/player/group__tutorial__config.html\#device_addresses}}.
This is a driver level thing and has a lot to do with the \verb\name\ of the driver that you are using, generally for \verb\"stage"\ the \verb\key\ doesn't need to be used. If you're using \pl without Stage then there is a useful section about device address keys in the \pl manual\footnote{\url{http://playerstage.sourceforge.net/doc/Player-3.0.2/player/group__tutorial__config.html\#device_key}}.
\item \verb\host\: This is the address of the host computer where the device is located. With a robot it could be the IP address of the robot. The default host is ``localhost'' which means the computer on which \pl is running.
\item \verb\robot\: this is the TCP port through which \pl should expect to receive data from the interface usually a single robot and all its necessary interfaces are assigned to one port. The default port used is 6665, if there were two robots in the simulation the ports could be 6665 and 6666 although there's no rule saying which number ports you can or can't use.
\item \verb\interface\: The interface to use in order to interact with the data. There is no default value for this option because it is a mandatory field.
\item \verb\index\: If a robot has multiple devices of the same type, for instance it has 2 cameras to give the robot depth perception, each device uses the same interface but gives slightly different information. The index field allows you to give a slightly different address to each device. So two cameras could be \verb|camera:0| and \verb|camera:1|.
This is very different from the \verb\key\ field because having a ``driver that supports multiple interfaces of the same type'' is NOT the same as having multiple devices that use the same interface. Again there is no default index, as this is a mandatory field in the device address, but you should use 0 as the index if there is only one of that kind of device.
\end{itemize}
If you want to use any of the default values it can just be left out of the device address. So we could use the default host and robot port and specify (for example) a laser interface just by doing \verb\"laser:0"\.
However, if you want to specify fields at the beginning of the device address but not in the middle then the separating colons should remain. For example if we had a host at \verb|"127.0.0.1"| with a \verb\laser\ interface then we would specify the address as \verb|"127.0.0.1::laser:0"|, the robot field is empty but the colons around it are still there. You may notice that the key field here was left off as before.
\section{Putting the Configuration File Together}\label{sec:ConfigurationFile:FinishingCFG}
We have examined the commands necessary to build a driver for a model in the worldfile, now it is just a case of putting them all together. To demonstrate this process we will build a configuration file for the worldfile developed in section \ref{sec:BuildingAWorld}. In this world we want our code to be able to interact with the robot, so in our configuration file we need to specify a driver for this robot.
\begin{verbatim}
driver
(
# parameters...
)
\end{verbatim}
The inbuilt driver that \plst uses for simulations is called \verb|"stage"| so the driver name is \verb|"stage"|.
\begin{verbatim}
driver
(
name "stage"
)
\end{verbatim}
The Bigbob robot uses \verb|position|, \verb|laser|, \verb|blobfinder| and \verb|ranger| sensors. These correspond to the \verb|position2d|, \verb|laser|, \verb|blobfinder| and interfaces respectively. The \verb|ranger| sensor is a special case because the ranger interface has not currently been implemented (for at least version Stage 3.2.2 or earlier). To work around this you'll need to use the \verb\sonar\ interfaces instead (there is apparently an IR interface which could be used instead of \verb\ranger\, but it doesn't seem to work on either Stage 2 or Stage 3).
We want our code to be able to read from these sensors, so we need to declare interfaces for them and tell \pl where to find each device's data, for this we use the configuration file's \verb\provides\ parameter. This requires that we construct device addresses for each sensor; to remind ourselves, this is in the key:host:robot:interface:index format. We aren't using any fancy drivers, so we don't need to specify a key. We are running our robot in a simulation on the same computer as our \pl sever, so the host name is \verb\localhost\ which is the default, so we also don't need to specify a host. The robot is a TCP port to receive robot information over, picking which port to use is pretty arbitrary but what usually happens is that the first robot uses the default port 6665 and subsequent robots use 6666, 6667, 6668 etc. There is only one robot in our simulation so we will use port 6665 for all our sensor information from this robot.
We only have one sensor of each type, so our devices don't need separate indices. What would happen if we did have several sensors of the same type (like say two cameras) is that we put the first device at index 0 and subsequent devices using the same interface have index 1, then 2, then 3 and so on.\footnote{There are lots of ranger sensors in our model but when we created the robot's sensors in section \ref{sec:BuildingAWorld:BuildingRobot:RobotSensors} we put them all into the same ranger model. So as far as the configuration file is concerned there is only one raging device using either the sonar or IR interface, because all the separate ranger devices are lumped together into this one model. We don't need to declare each ranger on an index of its own.}
Finally we use interfaces appropriate to the sensors the robot has, so in our example these are the \verb|position2d|, \verb|laser|, \verb|blobfinder| interfaces and for our \verb\ranger\ devices we will use \verb\sonar\.
Putting all this together under the \verb\provides\ parameter gives us:
\begin{verbatim}
driver
(
name "stage"
provides ["6665:position2d:0"
"6665:sonar:0"
"6665:blobfinder:0"
"6665:laser:0" ]
)
\end{verbatim}
The device addresses can be on the same line as each other or separate lines, just so long as they're separated by some form of white space.
The last thing to do on our driver is the \verb|model "model_name"| parameter which needs to be specified because we are using \plst. This tells the simulation software that anything you do with this driver will affect the model \verb\"model_name"\ in the simulation. In the simulation we built we named our robot model ``bob1'', so our final driver for the robot will be:
\begin{verbatim}
driver
(
name "stage"
provides ["6665:position2d:0"
"6665:sonar:0"
"6665:blobfinder:0"
"6665:laser:0"]
model "bob1"
)
\end{verbatim}
If our simulation had multiple Bigbob robots in, the configuration file drivers would be very similar to one another. If we created a second robot in our worldfile and called it ``bob2'' then the driver would be:
\begin{verbatim}
driver
(
name "stage"
provides ["6666:position2d:0"
"6666:sonar:0"
"6666:blobfinder:0"
"6666:laser:0"]
model "bob2"
)
\end{verbatim}
Notice that the port number and model name are the only differences because the robots have all the same sensors.
A driver of this kind can be built for any model that is in the worldfile, not just the robots. For instance a map driver can be made which uses the \verb\map\ interface and will allow you to get size, origin and occupancy data about the map. The only requirement is that if you want to do something to the model with your code then you need to build a driver for it in the configuration file. Finally when we put the bit which declares the \verb\stage\ driver (this part is compulsory for any simulation configuration file) together with our drivers for the robot we end up with our final configuration file:
\begin{verbatim}
driver
(
name "stage"
plugin "stageplugin"
provides ["simulation:0" ]
# load the named file into the simulator
worldfile "worldfile_name.world"
)
driver
(
name "stage"
provides ["6665:position2d:0"
"6665:sonar:0"
"6665:blobfinder:0"
"6665:laser:0"]
model "bob1"
)