forked from srmq/MobileSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
2873 lines (2491 loc) · 98.1 KB
/
main.cc
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
/*
* Copyright (C) 2005 ActivMedia Robotics
* Copyright (C) 2006-2010 MobileRobots Inc.
* Copyright (C) 2011-2015 Adept Technology
* Copyright (C) 2016-2017 Omron Adept Technologies
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define _BSD_SOURCE 1 // to get getcwd() from Linux's <unistd.h>
#include <stage.h>
#ifdef VERSION
#define STAGE_VERSION VERSION
#undef VERSION
#endif
#include <assert.h>
#ifndef MOBILESIM_NOGUI
#include <gtk/gtk.h>
#include <glib.h>
#endif
#include <map>
#include <set>
#include <locale.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#include <signal.h>
#include "ariaUtil.h"
#include "ArRobotParams.h"
#include "ArMap.h"
#include "ArSocket.h"
#include "MobileSim.hh"
#include "Config.hh"
#include "util.h"
#include "StageInterface.hh"
#include "EmulatePioneer.hh"
#include "RobotFactory.hh"
#include "StageRobotFactory.hh"
#include "MapLoader.hh"
#include "Socket.hh"
#include "NetworkDiscovery.hh"
using namespace MobileSim;
#include "ArSocket.h"
#include <stdio.h>
// if defined, measure time it takes to do world updates in main loop
//#define DEBUG_LOOP_TIME 1
// if defined, delete the objects on program
// exit. usually not really neccesary since the program is exiting anyway, and
// is not fully debugged (it can crash)
//#define DELETE_EMULATORS 1
// if defined, delete all robot interface objects on program exit.
// usually not really neccesary since the program is exiting anyway,
// and is not fully debugged (it can crash)
//#define DELETE_ROBOT_INTERFACES 1
// if defined, destroy stage world and call uninit at program
// exit. unually not really neccesary since the program is exiting
// anyway, and causes crashes on windows.
//#define STAGE_UNINIT 1
#define SHOW_WORLD_LOADING_DIALOG 1
/* Arbitrary limit for number of robots allowed in the initial
* configuration dialog. Theoretically, we're only limited by
* the number of TCP ports possible (1024 through 65545).
* MobileSim will probably become more and more unusable, however,
* the closer you approach this limit.
*/
#define ROBOTS_MENU_LIMIT 200
/* If undefined, temp files won't be deleted. Useful
for debugging them.
*/
#define CLEANUP_TEMP_FILES 1
#define COPYRIGHT_TEXT "Stage 2.0 is (C) Copyright 2007 Richard Vaughan, Brian Gerkey, Andrew Howard, and others.\n" \
"MobileSim is (C) Copyright 2005, ActivMedia Robotics LLC, (C) Copyright 2006-2010 MobileRobots Inc.,\n" \
"(C) Copyright 2011-2015, Adept Technology Inc., (C) Copyright 2016-2017 Omron Adept Technologies.\n" \
"This software is released under the GNU General Public License.\n" \
"This software comes with NO WARRANTY. This is free software, and you are\n" \
"welcome to redistribute it under certain conditions; see the LICENSE.txt file\n" \
"for details.\n\n"
#ifndef MOBILESIM_DEFAULT_DIR
#ifdef WIN32
#define MOBILESIM_DEFAULT_DIR "\\Program Files\\MobileRobots\\MobileSim"
#define MOBILESIM_DEFAULT_HELP_URL "file:C:/Program%20Files/MobileRobots/MobileSim/README.html";
#elif defined(__APPLE__)
#define MOBILESIM_DEFAULT_DIR "/usr/local/MobileSim" // TODO this should be inside MobileSim.app bundle directory
#define MOBILESIM_DEFAULT_HELP_URL "file:///usr/local/MobileSim/README.html" // TODO this should be inside MobileSim.app bundle directory
#else
#define MOBILESIM_DEFAULT_DIR "/usr/local/MobileSim"
#define MOBILESIM_DEFAULT_HELP_URL "file:///usr/local/MobileSim/README.html"
#endif
#endif
/* Remember the world file we created so we can delete it */
char TempWorldFile[MAX_PATH_LEN];
/* Some predefined ActivMedia robot models for use in the GUI */
const char* CommonRobotModels[] = {
"p3dx-sh-lms1xx",
"p3dx-sh-lms200",
"p3dx-sh",
"p3at-sh-lms1xx",
"p3at-sh-lms200",
"p3at-sh",
"amigo-sh",
"amigo-sh-tim3xx",
"powerbot-sh",
"peoplebot-sh",
"seekur",
"seekurjr",
"pioneer-lx",
"research-patrolbot",
"p2de",
"p2at",
"p3atiw-sh",
"p3dx-no-error",
"p3dx-big-rot-error",
"p3dx-big-x-error",
0
};
/* Type used to store what simulated robot instances to create. Map from unique robot
* name to model or .p file.
*/
typedef std::map<std::string, std::string> RobotModels;
/* Type to store what factories to create.*/
typedef std::list<std::string> RobotFactoryRequests;
/* Used to load maps */
MapLoader mapLoader;
ArMap *map = NULL;
unsigned long MobileSim::log_stats_freq = 0;
/* Create a temporary world file and load it. Return NULL on error */
stg_world_t* create_stage_world(const char* mapfile,
/*std::map<std::string, std::string>& robotInstanceRequests, std::list<std::string>& robotFactoryRequests,*/
const char* libdir,
//mobilesim_start_place_t start,
//double start_override_x, double start_override_y, double start_override_th,
double world_res = 0.0,
void (*loop_callback)() = NULL);
/* Return a name for a robot defined in a pfile that is usable in stage etc.
* This will not be the same as the robot subtype, it's based on the pfile name.
*/
const char *pfile_model_name(const char *pfile)
{
/* Skip past any directories */
const char *modelname = strrchr(pfile, '/');
if(modelname)
{
if(modelname[0] == '/') ++modelname;
}
else
{
modelname = pfile;
}
return modelname;
}
/* Figure out what directory to use for our external resources */
const char* find_libdir();
/* Delete temporary files: */
void cleanup_temp_files();
/* Utility to get data out of a GHashTable */
/* unused
typedef std::map<std::string, std::pair<std::string, std::string> > RobotTypeMap;
void add_model_to_robot_type_map_if_position(stg_model_t* model, char* model_name, void* map_p);
*/
/* Write a model definition to the given file based on parameters loaded from
* the given ARIA parameter file.
* Returns the name the model type was defined as, which will be a substring in
* pfile, or NULL on error.
*/
const char* worldfile_define_model_from_pfile(FILE* fp, const char* pfile);
#ifndef MOBILESIM_NOGUI
#ifdef GTK_CHECK_VERSION
#if GTK_CHECK_VERSION(2, 4, 0)
# define GTKFILEDIALOG GtkFileChooserDialog
# define GTKFILECHOOSER GtkFileChooser
# define GTKCOMBOWIDGET GtkComboBox
# define USE_GTKFILECHOOSER 1
# define USE_GTKCOMBOBOX 1
# define USE_GTKEXPANDER 1
#else
# define GTKFILEDIALOG GtkFileSelection
# define GTKFILECHOOSER GtkFileSelection
# define GTKCOMBOWIDGET GtkCombo
#endif
#else
#warning GTK_CHECK_VERSION not defined
# define GTKFILEDIALOG GtkFileChooserDialog
# define GTKFILECHOOSER GtkFileChooser
# define GTKCOMBOWIDGET GtkComboBox
# define USE_GTKFILECHOOSER 1
# define USE_GTKCOMBOBOX 1
# define USE_GTKEXPANDER 1
#endif
/* Show the map/options dialog displayed at startup if no map is
* given on the command line. Exit the program if the dialog is closed.
* If robotInstanceRequests is NULL, do not show any robot model or num. robots selection
* controls. If not NULL, then robot model selection and number controls are
* shown, and entries are added to the map accordingly.
* @return 0 if window is closed (cancelled), 1 if map was selected, 2 if "no map" was chosen
*/
int map_options_dialog(std::string& map, RobotModels* robotInstanceRequests, RobotFactoryRequests *robotFactoryRequests, MobileSim::Options *opts);
int map_options_dialog(std::string& map, MobileSim::Options *opts) {
return map_options_dialog(map, NULL, NULL, opts);
}
// show "busy loading world" dialog and return. hide and destroy the widget to hide the dialog.
GtkWidget *busy_loading_dialog();
#endif
/* Print help info */
void usage()
{
puts(
/* column 80--v */
"MobileSim " MOBILESIM_VERSION "\n"
"Usage: MobileSim [-m <map>] [-r <robot model> ...] [...options...]\n\n"
" --map <map> : Load map file (e.g. created with Mapper3)\n"
" -m <map> : Same as -map <map>.\n"
" --nomap : Create an \"empty\" map to start with.\n"
" Same as -map \"\". Ignored if -m is also given.\n"
" --robot <model>[:<name>] :\n"
" Create a simulated robot of the given model.\n"
" If an ARIA robot parameter file is given for <model>,\n"
" then MobileSim attempts to load that file from the\n"
" current working directory, and create an equivalent\n"
" model definition. May be repeated with different names\n"
" and models to create multiple simulated robots. Name \n"
" portion is optional.\n"
" Example: -robot p3dx -robot amigo:bot1 -robot custom.p:bot2\n"
" See PioneerRobotModels.world.inc for model definitions.\n"
" -r <model>[:name]: Same as --robot <model>[:<name>]\n"
" --robot-factory <model> :\n"
" Instead of creating one robot of the given model, accept\n"
" any number of client programs on its port, creating a new\n"
" instance of the model for each client, and destroying it\n"
" when the client disconnects.\n"
" -R <model> : Same as --robot-factory <model>\n"
" -p <port> : Emulate Pioneer connections starting with TCP port <port>.\n"
" (Default: 8101)\n"
#ifndef MOBILESIM_NOGUI
" --fullscreen-gui : Display window in fullscreen mode.\n"
" --maximize-gui : Display window maximized.\n"
" --minimize-gui : Display window minimized (iconified)\n"
#endif
#ifdef WIN32
" --noninteractive : Don't display any interactive dialog boxes that might\n"
" block program execution, and enable log rotation after\n"
" default maximum log file size of 10MB\n"
#else
" --noninteractive : Don't display any interactive dialog boxes that might\n"
" block program execution, enable automatic crash-restart,\n"
" and enable log rotation after default maximum log file\n"
" size of 10MB\n"
" --daemonize : Run as a daemon (background process) after initialization\n"
" (still creates GUI). Forces noninteractive mode. \n"
" Not available on Windows.\n"
#endif
#ifndef MOBILESIM_NOGUI
" --lite-graphics : Disable some graphics for slightly better performance\n"
" --no-graphics : Disable all graphics drawing for slightly better\n"
" performance\n"\
" --no-gui : Disable GUI entirely. Automatically enables noninteractive\n"
" mode as well.\n"
#endif
" --html : Print log messages and other output in HTML rather than\n"
" plain text\n"
" --cwd <dir> : Change directory to <dir> at startup. Client programs\n"
" can then load maps relative to this directory.\n"
" --log-file <file>: Print log messages to <file> instead of standard error.\n"
" -l <file> : Same as --log-file <file>.\n"
" --log-file-max-size <size> :\n"
" If the amount of data (bytes) written to the log file\n"
" exceeds <size>, then rotate the files (up to 5) and open\n"
" a new file. This keeps the total size of the log files\n"
" under <size>*5 bytes. If --noninteractive is given,\n"
" default is 5 MB (5*1024^2 bytes).\n"
" If --noninteractive is not given, or <size> is 0, no\n"
" limit is used and logs won't automatically rotate.\n"
" --update-interval <ms> : \n"
" Time between each simulation update step. Default is 100\n"
" ms. Less may improve simulation accuracy but impact client\n"
" responsiveness and data update; more may reduce CPU load\n"
" but impact accuracy (especially movement resolution).\n"
" --update-sim-time <ms> : \n"
" How much simulated time each simulation update takes.\n"
" Default is equal to --update-interval.\n"
" --start <x>,<y>,<th> OR --start outside OR --start random :\n"
" Use <x>, <y>, <th> (mm and degrees) as robot starting\n"
" point (even if the map has Home objects). Or, use the\n"
" keyword \"outside\" to cause robots to start 2m outside\n"
" the map bounds -- it later must be moved within the map\n"
" bounds to be used -- or, use the keyword \"random\" to\n"
" randomly choose a starting place within the map bounds.\n"
" --resolution <r> : Use resolution <r> (milimeters) for collisions and\n"
" sensors. Default is 20mm (2cm)\n"
" --ignore-command <num> :\n"
" Ignore the command whose number is given. Refer to robot\n"
" manual and MobileSim documentation for command numbers.\n"
" May be repeated. Warning, MobileSim and/or your program\n"
" may not function correctly if some critical commands are\n"
" ignored.\n"
" --verbose : Be a more verbose about logging, especially things \n"
" that might be logged frequently (e.g. ignored unsupported\n"
" commands, some internal/debugging information, etc.)\n"
" --log-timing-stats [sec]:\n"
" Log timing stats every [sec] seconds (default 30 sec).\n"
" --bind-to-address <address> :\n"
" Only listen on network interface with IP address \n"
" <address> for new client connections (default is to\n"
" listen on all addresses).\n"
#ifndef WIN32
" --no-crash-debug :\n"
" Disable GDB crash handler (just abort program on fatal\n"
" signals). (Not available on Windows)\n"
" --no-crash-restart :\n"
" Disable automatic restart normally done if in noninteractive\n"
" mode. (Not available on Windows)\n"
#endif
" --srisim-compat :\n"
" Enable compatability with SRISim by accepting \n"
" OLD_SIM_EXIT (62), OLD_SET_TRUE_X (66), OLD_SET_TRUE_Y (67),\n"
" OLD_SET_TRUE_TH (68), OLD_RESET_TO_ORIGIN (69). See the\n"
" MobileSim docs for details on new replacement commands.\n"
" --no-srisim-laser-compat :\n"
" Disable compatability with SRISim laser commands\n"
" OLD_LRF_ENABLE (35), OLD_LRF_CFG_START (36),\n"
" OLD_LRF_CFG_END (37), and OLD_LRF_CFG_INC (38).\n"
" See MobileSim docs for details on new replacement commands.\n"
" --log-packets-received :\n"
" Log all packets received from client.\n"
" --log-movement-sent :\n"
" Log position and velocity values sent to client in SIP \n"
" (including with protocol conversion factors applied)."
" --echo-stage-worldfile :\n"
" Print contents of stage world file while loading (for MobileSim debugging)\n"
" --warn-unsupported-commands : \n"
" Warn when unrecognized or unsupported commands are received and ignored.\n"
" --lines-chunksize: \n"
" When a map is loaded or reloaded, the map lines are split into chunks for\n"
" processing, to avoid long communication blackouts and subsequent disconnects.\n"
" If disconnects are happening during map loads, try setting this below " DEFAULT_MLPC_STR "\n"
" --points-chunksize: \n"
" When a map is loaded or reloaded, the map points are split into chunks for\n"
" processing, to avoid long communication blackouts and subsequent disconnects.\n"
" If disconnects are happening during map loads, try setting this below " DEFAULT_MPPC_STR "\n"
" --no-network-discovery : \n"
" Don't respond to network broadcast discovery requests.\n"
" --odom-error-mode <random_init|random_each_update|constant|none> :\n"
" Specify odometry error behavior (see documentation).\n"
" --help : Print this help text and exit\n"
" --version or -v : Print MobileSim version number and exit\n"
"\n"
"MobileSim is based on the Stage 2.0 simulator library: see <http://playerstage.sf.net>\n\n"
COPYRIGHT_TEXT
);
}
/* Get temp. dir. from environment or use default path */
const char* temp_dir()
{
const char* tempdir = getenv("TEMP");
if(!tempdir)
tempdir = getenv("TMP");
if(!tempdir)
#ifdef WIN32
tempdir = "\\TEMP";
#else
tempdir = "/tmp";
#endif
return tempdir;
}
/* Return path to home directory if Linux, or My Documents directory if Windows. */
const char* user_docs_dir()
{
const char *d;
#ifdef WIN32
d = (const char*)g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
#else
d = (const char*)g_get_home_dir();
#endif
return d;
}
/* Remember some info about the map we loaded so that other objects can use it. */
static mobilesim_start_place_t mobilesim_startplace = mobilesim_start_home;
static double map_min_x = 0;
static double map_min_y = 0;
static double map_max_x = 0;
static double map_max_y = 0;
static double map_home_x = 0;
static double map_home_y = 0;
static double map_home_th = 0;
void load_map_done(MapLoadedInfo info)
//StageMapLoader::Request *loadReq, double min_x, double min_y, double max_x, double max_y, double home_x, double home_y, double home_th)
{
// this callback is called asynchronously, theoretically could get called
// twice at once.
map_min_x = info.min_x;
map_min_y = info.min_y;
map_max_x = info.max_x;
map_max_y = info.max_y;
map_home_x = info.have_home ? info.home_x : 0;
map_home_y = info.have_home ? info.home_y : 0;
map_home_th = info.have_home ? info.home_th : 0;
}
// This is a bit hacky, relying the fact that the MobileSim launch script (in /usr/local/apps/MobileSim/init.sh)
// changes the working directory to '/home/admin' if EM is running on the same VM, and changes the working
// directory to '/home/admin/sim' if this instance of MS is running solo
bool check_host_for_EM(const char* working_dir)
{
if(working_dir && strcmp(working_dir, "/home/admin") == 0)
{
//ArLog::log(ArLog::Normal, "Matches: /home/admin");
return true;
}
else
{
//ArLog::log(ArLog::Normal, "Doesn't matche: /home/admin");
return false;
}
}
/* obsolete for now. could be useful later if we want to make it responsive to whether its running directory is 'home/admin' or 'home/admin/sim'
bool check_host_for_process(const char *ps_name)
{
bool foundProcess = false;
char search_string[256];
system("pwd");
sprintf(search_string, "ps -U root -u root -N | grep %s", ps_name);
sprintf(search_string, "ps -U root -u root -N | grep %s > searchresult.txt", ps_name);
system(search_string);
FILE *pFile;
pFile = fopen ("searchresult.txt", "r");
if (pFile != NULL)
{
char read_string[256];
read_string[0] = '\0';
fgets(read_string, 255, pFile);
if ((unsigned int)strlen(read_string) > 0)
foundProcess = true;
}
else
{
ArLog::log(ArLog::Normal, "Error opening file");
}
system("rm searchresult.txt");
return foundProcess;
}
*/
ArGlobalFunctor1<MapLoadedInfo> load_map_done_cb(&load_map_done);
int stage_load_file_cb(stg_world_t* /*world*/, char* filename, void* userdata)
{
MapLoader *loader = (MapLoader*)userdata;
//print_debug("MobileSim load file callback for filename %s!\n", filename);
stg_print_msg("MobileSim load file callback for filename %s!\n", filename);
loader->newMap(filename, NULL, &load_map_done_cb);
return 0;
}
std::set<RobotInterface*> robotInterfaces;
std::set<RobotFactory*> robotFactories;
void log_file_full_cb(FILE* /*fp*/, size_t /*sz*/, size_t max)
{
stg_print_warning("MobileSim: This log file is full (maximum size %d bytes). Rotating logs and starting a new one...", max);
mobilesim_rotate_log_files(NULL);
}
// Command line arguments
MobileSim::Options options;
void do_gtk_iteration()
{
if(!options.NonInteractive)
gtk_main_iteration_do(FALSE);
}
int main(int argc, char** argv)
{
MobileSim::Options& opt = options; // shortcut
// save command-line arguments
opt.argc = argc;
opt.argv = argv;
// Map of robot name -> model string.
RobotModels robotInstanceRequests;
// List or set of model type strings.
RobotFactoryRequests robotFactoryRequests;
//
// // Stored config
// MobileSimConfig config;
for(int i = 1; i < argc; ++i) {
if(command_argument_match(argv[i], "h", "help")) {
usage();
exit(0);
}
else if(command_argument_match(argv[i], "v", "version")) {
puts("MobileSim " MOBILESIM_VERSION "\nUse --help for more information.\n");
exit(0);
}
else if(command_argument_match(argv[i], "p", "port")) {
if(++i < argc) {
opt.port = atoi(argv[i]);
} else {
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "m", "map")) {
if(++i < argc) {
opt.map = argv[i];
} else {
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "nomap")) {
// if -m was already given, don't change it. otherwise set to empty string
opt.map = "";
opt.nomap = true;
}
else if(command_argument_match(argv[i], "r", "robot")) {
if(++i < argc) {
std::string arg(argv[i]);
size_t sep = arg.find(":");
std::string model;
std::string name;
if(sep == arg.npos) {
// No name given, invent one
model = arg.substr(0, sep);
name = model;
char buf[4];
for(int i = 2; robotInstanceRequests.find(name) != robotInstanceRequests.end(); i++) {
snprintf(buf, 4, "%03d", i);
name = model + "_" + buf;
}
} else {
model = arg.substr(0, sep);
name = arg.substr(sep+1);
}
//stg_print_msg("MobileSim: Robot named \"%s\" will be a \"%s\" model.", name.c_str(), model.c_str());
robotInstanceRequests[name] = model;
} else {
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "R", "robot-factory")) {
if(++i < argc) {
stg_print_msg("MobileSim: Will create robot factory for model \"%s\".", argv[i]);
robotFactoryRequests.push_back(argv[i]);
} else {
usage();
exit(ERR_USAGE);
}
}
#ifndef MOBILESIM_NOGUI
else if(command_argument_match(argv[i], "", "maximize") || command_argument_match(argv[i], "", "maximize-gui"))
{
opt.windowmode = opt.MAXIMIZE_WINDOW;
}
else if(command_argument_match(argv[i], "", "minimize") || command_argument_match(argv[i], "", "minimize-gui"))
{
opt.windowmode = opt.MINIMIZE_WINDOW;
}
else if(command_argument_match(argv[i], "", "fullscreen") || command_argument_match(argv[i], "", "fullscreen-gui"))
{
opt.windowmode = opt.FULLSCREEN_WINDOW;
}
else if(command_argument_match(argv[i], "", "noninteractive") || command_argument_match(argv[i], "", "non-interactive"))
{
opt.NonInteractive = true;
}
#endif
#ifndef WIN32
else if(command_argument_match(argv[i], "", "daemonize"))
{
opt.Daemon = true;
}
#endif
#ifndef MOBILESIM_NOGUI
else if(command_argument_match(argv[i], "", "no-graphics") ||
command_argument_match(argv[i], "", "nographics") ||
command_argument_match(argv[i], "", "disable-graphics"))
{
opt.graphicsmode = opt.NO_GRAPHICS;
}
else if(command_argument_match(argv[i], "", "no-gui") ||
command_argument_match(argv[i], "", "nogui") ||
command_argument_match(argv[i], "", "disable-gui"))
{
opt.graphicsmode = opt.NO_GUI;
opt.NonInteractive = true;
}
else if(command_argument_match(argv[i], "", "lite-graphics") || command_argument_match(argv[i], "", "litegraphics"))
{
opt.graphicsmode = opt.LITE_GRAPHICS;
}
#endif
else if(command_argument_match(argv[i], "", "html"))
{
opt.log_html = true;
stg_set_print_format(STG_PRINT_HTML);
}
else if(!strcasecmp(argv[i], "--cwd") || !strcasecmp(argv[i], "-cwd") || !strcasecmp(argv[i], "--cd") || !strcasecmp(argv[i], "-cd") )
{
if(++i < argc)
{
opt.change_to_directory = argv[i];
}
else
{
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "l", "log-file"))
{
if(++i < argc)
{
opt.log_file = argv[i];
}
else
{
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "log-file-max-size"))
{
if(++i < argc)
opt.log_file_max_size = atoi(argv[i]);
else
{
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "update-interval"))
{
if(++i < argc)
{
opt.interval_real = (stg_msec_t) atoi(argv[i]);
}
else
{
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "update-sim-time"))
{
if(++i < argc)
{
opt.interval_sim = (stg_msec_t) atoi(argv[i]);
}
else
{
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "resolution"))
{
if(++i < argc)
{
opt.world_res = atof(argv[i]);
}
else
{
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "start"))
{
if(++i < argc)
{
if(strcmp(argv[i], "outside") == 0)
{
mobilesim_startplace = mobilesim_start_outside;
}
else if(strcmp(argv[i], "random") == 0)
{
mobilesim_startplace = mobilesim_start_random;
}
else
{
if(sscanf(argv[i], "%ld,%ld,%d", &opt.start_pos_override_pose_x, &opt.start_pos_override_pose_y, &opt.start_pos_override_pose_th) != 3)
{
fputs("Error: --start must be used either with the form <x>,<y>,<th> (e.g. 200,4650,90) (mm and deg), or the keyword \"random\" or \"outside\".", stderr);
exit(ERR_USAGE);
}
mobilesim_startplace = mobilesim_start_fixedpos;
}
}
else
{
fputs("Error: --start must be used either with the form <x>,<y>,<th> (e.g. 200,4650,90) (mm and deg), or the keyword \"random\" or \"outside\".", stderr);
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "ignore-command"))
{
if(i >= argc || argv[i+1][0] == '-')
{
fputs("Error: --ignore-command must be followed by command number.", stderr);
exit(ERR_USAGE);
}
int c = atoi(argv[++i]);
stg_print_msg("MobileSim: Will ignore command #%d.", c);
opt.ignore_commands.insert(c);
}
else if(command_argument_match(argv[i], "", "verbose"))
{
opt.verbose = true;
}
else if(command_argument_match(argv[i], "", "log-timing-stats"))
{
int sec = 30;
if(i+1 < argc && argv[i+1][0] != '-')
sec = atoi(argv[++i]);
MobileSim::log_stats_freq = stg_log_stats_freq = sec*1000; // msec
stg_print_msg("MobileSim: Will log timing information every %d seconds.", sec);
}
else if(command_argument_match(argv[i], "", "bind-to-address"))
{
opt.listen_address = argv[++i];
}
else if(command_argument_match(argv[i], "", "no-crash-handler") ||
command_argument_match(argv[i], "", "disable-crash-handler"))
{
// old, deprecated command line argument
opt.EnableCrashDebug = false;
opt.EnableCrashRestart = false;
}
else if(command_argument_match(argv[i], "", "no-crash-debug") ||
command_argument_match(argv[i], "", "disable-crash-debug"))
{
opt.EnableCrashDebug = false;
}
else if(command_argument_match(argv[i], "", "no-crash-restart") ||
command_argument_match(argv[i], "", "disable-crash-restart"))
{
opt.EnableCrashRestart = false;
}
else if(command_argument_match(argv[i], "", "restarting-after-crash"))
{
opt.RestartedAfterCrash = true;
}
else if(command_argument_match(argv[i], "", "srisim-compat"))
{
opt.srisim_compat = true;
}
else if(command_argument_match(argv[i], "", "no-srisim-laser-compat") ||
command_argument_match(argv[i], "", "disable-srisim-laser-compat"))
{
opt.srisim_laser_compat = false;
}
else if(command_argument_match(argv[i], "", "no-menu"))
{
stg_show_menubar = 0;
}
else if(command_argument_match(argv[i], "", "no-messages"))
{
stg_show_messages_view = 0;
}
else if(command_argument_match(argv[i], "", "log-packets-received"))
{
opt.log_packets_received = true;
}
else if(command_argument_match(argv[i], "", "echo-stage-worldfile"))
{
opt.echo_stage_worldfile = true;
}
else if(command_argument_match(argv[i], "", "warn-unsupported-commands"))
{
opt.warn_unsupported_commands = true;
}
else if(command_argument_match(argv[i], "", "log-movement-sent"))
{
opt.log_sips_sent = true;
}
else if(command_argument_match(argv[i], "", "commercial"))
{
opt.commercial = true;
opt.run_network_discovery = false;
opt.odom_error_mode = MobileSim::Options::RANDOM_INIT;
}
else if(command_argument_match(argv[i], "", "no-network-discovery"))
{
opt.run_network_discovery = false;
}
else if(command_argument_match(argv[i], "", "lines-chunksize"))
{
if(++i < argc)
{
opt.mapLoadLinesPerChunk = atol(argv[i]);
}
else
{
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "points-chunksize"))
{
if(++i < argc)
{
opt.mapLoadPointsPerChunk = atol(argv[i]);
}
else
{
usage();
exit(ERR_USAGE);
}
}
else if(command_argument_match(argv[i], "", "odom-error-mode"))
{
if(++i < argc)
{
if(strcasecmp(argv[i], "random_init") == 0)
{
opt.odom_error_mode = MobileSim::Options::RANDOM_INIT;
}
else if(strcasecmp(argv[i], "random_each_update") == 0)
{
opt.odom_error_mode = MobileSim::Options::RANDOM_EACH_UPDATE;
}
else if(strcasecmp(argv[i], "constant") == 0)
{
opt.odom_error_mode = MobileSim::Options::CONSTANT;
}
else if(strcasecmp(argv[i], "none") == 0)
{
opt.odom_error_mode = MobileSim::Options::NONE;
}
else
{
printf("Error: Unrecognized odom-error-mode \"%s\". Expected one of: random_init, random_each_update, constant, none.\n\n", argv[i]);
usage();
exit(ERR_USAGE);
}
}
else
{
usage();
exit(ERR_USAGE);
}
}
else if(i == (argc-1))
{
// last argument on the end of the command line. if it's not a flag,
// assume it's a map file.
if(argv[i][0] == '-')
{
usage();
exit(ERR_USAGE);
}
opt.map = argv[i];
}
else {
stg_print_warning("MobileSim: Ignoring unrecognized command-line argument \"%s\".", argv[i]);
}
}
#ifdef MOBILESIM_NOGUI
opt.NonInteractive = true;
opt.graphicsmode = NO_GUI;
#endif
if(opt.log_file)
{
// force noncolor text if not html
if(!opt.log_html)
stg_set_print_format(STG_PRINT_PLAIN_TEXT);
stg_print_msg("MobileSim: Rotating log files (%s) if present...", opt.log_file);
mobilesim_rotate_log_files(NULL);
FILE* fp = ArUtil::fopen(opt.log_file, "w");
if(!fp)
{
stg_print_error("MobileSim: Could not open log file \"%s\" for writing.", opt.log_file);
exit(ERR_USAGE);
}
stg_set_log_file(fp);
if(opt.log_file_max_size == 0 && opt.NonInteractive)
opt.log_file_max_size = 5 * 1024 * 1024; // default of 5 MB
if(opt.log_file_max_size > 0)
stg_set_log_file_max_size(opt.log_file_max_size, &log_file_full_cb);
}
print_msg("MobileSim version " MOBILESIM_VERSION " built " MOBILESIM_BUILDDATE);
opt.log_argv();
for(RobotModels::const_iterator i = robotInstanceRequests.begin(); i != robotInstanceRequests.end(); ++i)
{
stg_print_msg("MobileSim: Robot named \"%s\" will be a \"%s\"", i->first.c_str(), i->second.c_str());
}
/* Directory where supporting files (robot model defs, icons)
can be found.
*/
const char* libdir = find_libdir();
/* Initialize Stage and GTK */
stg_about_info_appname = (char*)"MobileSim";
stg_about_info_description = (char*)"Simulator for MobileRobots/ActivMedia robots, based on the Stage robot simulator library (with modifications by MobileRobots Inc).";
stg_about_info_url = (char*)"http://robots.mobilerobots.com";
stg_about_info_copyright = (char*) COPYRIGHT_TEXT ;