Army ant simulation
main.cpp
Go to the documentation of this file.
1 
4 #include "Box2D/Box2D.h"
5 #include "SFML/Graphics.hpp"
6 #include "SFML/Window.hpp"
7 #include <iostream>
8 
9 #include "Config.h"
10 #include "Demo.h"
11 
13 void parse_argument(char* argv[], int i, config::sConfig& cfg);
14 void load_config(std::string filename, config::sConfig& cfg);
15 void help();
16 
17 int main(int argc, char* argv[])
18 {
19  //TODO: create a demo class which take the nb of robot, the scene ... in arguments to do all the steps
20 
22  b2Vec2 Gravity(0.f, 0.f);
23  b2World* world = new b2World(Gravity);
24 
26  bool config_file = false;
27  std::string config_file_path;
28 
29  config::sConfig configParam;
30  default_parameters(configParam);
31 
32  for (int i = 1; i < argc; i++) { /* We will iterate over argv[] to get the parameters stored inside.
33  * Note that we're starting on 1 because we don't need to know the
34  * path of the program, which is stored in argv[0] */
35 // if (i + 1 != argc){ // Check that we haven't finished parsing already
36  if (std::string(argv[i]) == "-h" || std::string(argv[i]) == "--help") {
37  printf("tetetete \n");
38  help();
39  return 0;
40  }
41  else if (i + 1 != argc){
42  // The first thing to check is whether a configuration file has been specified
43  if (std::string(argv[i]) == "-cp" || std::string(argv[i]) == "--configuration_path") {
44  // We know the next argument *should* be the filename:
45  config_file_path = std::string(argv[i + 1]);
46  config_file = true;
47  load_config(config_file_path, configParam);
48  break;
49  }
50  else{
51  parse_argument(argv, i, configParam);
52  }
53  }
54 
55  if(configParam.terrain.v_angle > 0){
56  configParam.terrain.v_width = tan(configParam.terrain.v_angle)*2*configParam.terrain.v_height;
57  }
58 
59 // }
60  }
61 
62  Demo myDemo(world, configParam);
63  myDemo.init();
64  myDemo.demoLoop();
65  myDemo.writeResultFile();
66 
67  return 0;
68 }
69 
73 
74  cfg.terrain.runaway = 8; //7
75  cfg.terrain.v_width = 10; //10.2
76  cfg.terrain.v_height = 3.5; //8
77  cfg.terrain.v_angle = 50/RAD_TO_DEG;
78 
79  cfg.simulation.gravity = 0.0;
80  cfg.simulation.robot_distance = 3.5;
81  cfg.simulation.robot_phase = 0;
82  cfg.simulation.robot_delay = 3.25;
83  cfg.simulation.robot_initial_posX = 5.20;
84  cfg.simulation.robot_initial_posY = 1.0;
85  cfg.simulation.nb_robots = 25; //250
86  cfg.simulation.bridge_duration = 100;
87  cfg.simulation.dissolution_duration = 200;
88  cfg.simulation.visualization = true;
89 
90  cfg.controller.angle_limit = PI/2;// PI/2
91  cfg.controller.bridge_delay = 5; //5
92  cfg.controller.walk_delay = 0.5; //Should not be changed
93  cfg.controller.time_before_pushing = 1;
94  cfg.controller.max_robot_window = 50;
95  cfg.controller.stability_condition = 60;
96 
97  cfg.robot.body_length = 1.02;
98  cfg.robot.speed = 2*PI; //Should not be changed
99 
100  cfg.window.WINDOW_X_PX = 1920;
101  cfg.window.WINDOW_Y_PX = 1080;
102 
103  cfg.logfile_name = "exp";
104  cfg.logfile_path = "experiments/";
105 
106 }
107 
108 void parse_argument(char* argv[], int i, config::sConfig& cfg){
109 
110  // Terrain parameters
111  if (std::string(argv[i]) == "-r" || std::string(argv[i]) == "--terrain_runaway") {
112  cfg.terrain.runaway = atof(argv[i + 1]);
113  }
114  else if (std::string(argv[i]) == "-vw" || std::string(argv[i]) == "--v_width") {
115  cfg.terrain.v_width = atof(argv[i + 1]);
116  }
117  else if (std::string(argv[i]) == "-vh" || std::string(argv[i]) == "--v_height") {
118  cfg.terrain.v_height = atof(argv[i + 1]);
119  }
120  else if (std::string(argv[i]) == "-va" || std::string(argv[i]) == "--v_half_angle") {
121  cfg.terrain.v_angle = atof(argv[i + 1])/RAD_TO_DEG;
122  }
123 
124  // Simulation parameters
125  else if (std::string(argv[i]) == "-g" || std::string(argv[i]) == "--gravity") {
126  cfg.simulation.gravity = atof(argv[i + 1]);
127  }
128  else if (std::string(argv[i]) == "-rd" || std::string(argv[i]) == "--robot_distance") {
129  cfg.simulation.robot_distance = atof(argv[i + 1]);
130  }
131  else if (std::string(argv[i]) == "-rp" || std::string(argv[i]) == "--robot_phase") {
132  cfg.simulation.robot_phase = atof(argv[i + 1])/RAD_TO_DEG;
133  }
134  else if (std::string(argv[i]) == "-rt" || std::string(argv[i]) == "--robot_delay") {
135  cfg.simulation.robot_delay = atof(argv[i + 1]);
136  }
137  else if (std::string(argv[i]) == "-rx" || std::string(argv[i]) == "--robot_init_x") {
138  cfg.simulation.robot_initial_posX = atof(argv[i + 1]);
139  }
140  else if (std::string(argv[i]) == "-ry" || std::string(argv[i]) == "--robot_init_y") {
141  cfg.simulation.robot_initial_posY = atof(argv[i + 1]);
142  }
143  else if (std::string(argv[i]) == "-nb_r" || std::string(argv[i]) == "--number_robots") {
144  cfg.simulation.nb_robots = atoi(argv[i + 1]);
145  }
146  else if (std::string(argv[i]) == "-t" || std::string(argv[i]) == "--bridge_duration") {
147  cfg.simulation.bridge_duration = atof(argv[i + 1]);
148  }
149  else if (std::string(argv[i]) == "-td" || std::string(argv[i]) == "--dissolution_duration") {
150  cfg.simulation.dissolution_duration = atof(argv[i + 1]);
151  }
152  else if (std::string(argv[i]) == "-vz" || std::string(argv[i]) == "--enable_visualization") {
153  if (std::string(argv[i+1]) == "true"){
154  cfg.simulation.visualization = true;
155  }
156  else if (std::string(argv[i+1]) == "false"){
157  cfg.simulation.visualization = false;
158  }
159  else{
160  std::cout<< "the argument should be either 'true' or 'false'"<<std::endl;
161  }
162  }
163 
164  // Controller parameters
165  else if (std::string(argv[i]) == "-a" || std::string(argv[i]) == "--limit_angle") {
166  cfg.controller.angle_limit = atof(argv[i + 1]);
167  }
168  else if (std::string(argv[i]) == "-bt" || std::string(argv[i]) == "--bridge_delay") {
169  cfg.controller.bridge_delay = atof(argv[i + 1]);
170  }
171  else if (std::string(argv[i]) == "-wt" || std::string(argv[i]) == "--walk_delay") {
172  cfg.controller.walk_delay = atof(argv[i + 1]);
173  }
174  else if (std::string(argv[i]) == "-pt" || std::string(argv[i]) == "--pushing_delay") {
175  cfg.controller.time_before_pushing = atof(argv[i + 1]);
176  }
177  else if (std::string(argv[i]) == "-mr" || std::string(argv[i]) == "--max_robots") {
178  cfg.controller.max_robot_window = atoi(argv[i + 1]);
179  }
180  else if (std::string(argv[i]) == "-st" || std::string(argv[i]) == "--stability_condition") {
181  cfg.controller.stability_condition = atof(argv[i + 1]);
182  }
183 
184  // Robots parameters
185  else if (std::string(argv[i]) == "-bl" || std::string(argv[i]) == "--body_length") {
186  cfg.robot.body_length = atof(argv[i + 1]);
187  std::cout << cfg.robot.body_length << std::endl;
188  }
189  else if (std::string(argv[i]) == "-v" || std::string(argv[i]) == "--robot_speed") {
190  cfg.robot.speed = atof(argv[i + 1]);
191  }
192 
193  // Window parameters
194  else if (std::string(argv[i]) == "-wx" || std::string(argv[i]) == "--window_x") {
195  cfg.window.WINDOW_X_PX = atoi(argv[i + 1]);
196  std::cout << cfg.window.WINDOW_X_PX << std::endl;
197  }
198  else if (std::string(argv[i]) == "-wy" || std::string(argv[i]) == "--window_y") {
199  cfg.window.WINDOW_Y_PX = atoi(argv[i + 1]);
200  }
201 
202  // File parameters
203  else if (std::string(argv[i]) == "-fp" || std::string(argv[i]) == "--file_path") {
204  cfg.logfile_path = std::string(argv[i + 1]);
205  }
206  else if (std::string(argv[i]) == "-fn" || std::string(argv[i]) == "--file_name") {
207  cfg.logfile_name = std::string(argv[i + 1]);
208  }
209 
210  std::cout << argv[i] << " ";
211 }
212 
213 
216 void help(){
217  std::cout << "Launch a simulation for the ant bridge formation" << std::endl;
218  std::cout << "Usage: Simulation_v2 [parameters]" << std::endl;
219  std::cout << "Parameters:" << std::endl;
220  std::cout << " -cp PATH Give the path of the configuration file (--configuration_path) \n" << std::endl;
221 
222  std::cout << " Terrain parameters:" << std::endl;
223  std::cout << " -r DIST Sets the length of the v-terrain runaway relatively to the robot body length (--terrain_runaway, default = 7) " << std::endl;
224  std::cout << " -vw WIDTH Sets the width of the v shape relatively to the robot body length (--v_width, default = 10) " << std::endl;
225  std::cout << " -vh HEIGHT Sets the height of the v shape relatively to the robot body length (--v_height, default = 8) \n" << std::endl;
226  std::cout << " -va DEG Sets the half-angle of the v shape in deg, when angle is set > 0 the width is not taken into account (--v_height, default = 0) \n" << std::endl;
227 
228  std::cout << " Simulation parameters:" << std::endl;
229  std::cout << " -g VALUE Sets the gravity (--gravity, default = 0.0) " << std::endl;
230  std::cout << " -rd BL Sets the distance between the creation of two successive robots in body length (--robot_distance, default = 1.1) " << std::endl;
231  std::cout << " -rp RAD Sets the phase shift between two successive robots in rad (--robot_phase, default = 0) " << std::endl;
232  std::cout << " -rt SEC Sets the delay between the creation of two successive robots in s (--robot_delay, default = 3.0) " << std::endl;
233  std::cout << " -rx BL Sets the initial x distance of the robot from the V start (--robot_init_x, default = 10) " << std::endl;
234  std::cout << " -ry POS Sets the initial y position of the robot (--robot_init_y, default = 10) " << std::endl;
235  std::cout << " -nb_r VALUE Sets the number of robots for the whole simulation (--number_robots, default = 10) " << std::endl;
236  std::cout << " -t SEC Sets the duration of the bridge part of the simulation in s (--simulation_duration, default = 300) " << std::endl;
237  std::cout << " -td SEC Sets the duration of the dissolution part of the simulation in s (--dissolution_duration, default = 300) " << std::endl;
238  std::cout << " -vz BOOL Has to be false to disable the visualization (--enable_visualization, default = true) \n" << std::endl;
239 
240  std::cout << " Controller parameters:" << std::endl;
241  std::cout << " -a RAD Sets the minimum angle before the robot is allowed to grab (--limit_angle, default = PI/2) " << std::endl;
242  std::cout << " -bt SEC Sets the pause delay in the bridge state in s (--bridge_delay, default = 5.0) " << std::endl;
243  std::cout << " -wt SEC Sets the pause delay in the walking state in s (--walk_delay, default = 0.5) " << std::endl;
244  std::cout << " -pt SEC Sets the maximum duration of the movement before a robot is considered as pushing in s: create a grip (--pushing_delay, default = 5.0) " << std::endl;
245  std::cout << " -mr VALUE Sets the maximum number of robots in the window (--max_robots, default = 10) " << std::endl;
246  std::cout << " -st SEC Sets the time after which a bridge is considered stable in s (--stability_condition, default = 10) \n" << std::endl;
247 
248  std::cout << " Robot parameters:" << std::endl;
249  std::cout << " -bl MET Sets the body length of the robot in m (--body_length, default = 0.82) " << std::endl;
250  std::cout << " -v SPEED Sets the rotational speed of the robot in rad/s (--robot_speed, default = 2*PI) " << std::endl;
251 
252  std::cout << " Window parameters:" << std::endl;
253  std::cout << " -wx PX Sets the width of the window in pixels (--window_x, default = 1920) " << std::endl;
254  std::cout << " -wy PX Sets the height of the window in pixels (--window_y, default = 1080) " << std::endl;
255 
256  std::cout << " Result parameters:" << std::endl;
257  std::cout << " -fp PATH Sets the path to write the result files (--file_path, default = log/) " << std::endl;
258  std::cout << " -fn NAME Sets the name of the result file (--file_name, default = exp_) " << std::endl;
259 }
260 
261 void load_config(std::string filename, config::sConfig& cfg){
262 
263  config::Configuration configFile;
264  configFile.Load(filename);
265 
266 // if (m_config.Get("robot_delay", m_delay) &&
267 // m_config.Get("nb_robots", m_maxRobots) )
268 // {
269 // std::cout << "Parameter in configuration file have been loaded." << std::endl;
270 // }
271 
272  //-------- simulation parameters
273  if (configFile.Get("gravity", cfg.simulation.gravity) &&
274  configFile.Get("robot_delay", cfg.simulation.robot_delay) &&
275  configFile.Get("robot_initial_posX", cfg.simulation.robot_initial_posX) &&
276  configFile.Get("robot_initial_posY", cfg.simulation.robot_initial_posY) &&
277  configFile.Get("nb_robots", cfg.simulation.nb_robots) &&
278  configFile.Get("bridge_duration", cfg.simulation.bridge_duration) &&
279  configFile.Get("dissolution_duration", cfg.simulation.dissolution_duration) &&
280  configFile.Get("visualization", cfg.simulation.visualization))
281  {
282  std::cout << "Simulation parameters in configuration file have been loaded." << std::endl;
283  }
284  else
285  {
286  std::cout << "Missing simulation parameter in configuration file." << std::endl;
287  }
288 
289  //-------- terrain parameters
290  if (configFile.Get("runaway", cfg.terrain.runaway) &&
291  configFile.Get("v_width", cfg.terrain.v_width) &&
292  configFile.Get("v_half_angle", cfg.terrain.v_angle) &&
293  configFile.Get("v_height", cfg.terrain.v_height) )
294  {
295  cfg.terrain.v_angle = cfg.terrain.v_angle/RAD_TO_DEG;
296  std::cout << "Terrain's parameters in configuration file have been loaded." << std::endl;
297  }
298  else
299  {
300  std::cout << "Missing terrain parameter in configuration file." << std::endl;
301  }
302 
303  //-------- controller parameters
304  if (configFile.Get("angle_limit", cfg.controller.angle_limit) &&
305  configFile.Get("bridge_delay", cfg.controller.bridge_delay) &&
306  configFile.Get("time_before_pushing", cfg.controller.time_before_pushing) &&
307  configFile.Get("max_robot_window", cfg.controller.max_robot_window) &&
308  configFile.Get("stability_condition", cfg.controller.stability_condition) &&
309  configFile.Get("walk_delay", cfg.controller.walk_delay) )
310  {
311  std::cout << "Terrain's parameters in configuration file have been loaded." << std::endl;
312  }
313  else
314  {
315  std::cout << "Missing terrain parameter in configuration file." << std::endl;
316  }
317 
318  //-------- robot parameters
319  if (configFile.Get("body_length", cfg.robot.body_length) &&
320  configFile.Get("speed", cfg.robot.speed))
321  {
322  std::cout << cfg.robot.body_length << std::endl;
323  std::cout << cfg.robot.speed << std::endl;
324  std::cout << "Robot's parameters in configuration file have been loaded." << std::endl;
325  }
326  else
327  {
328  std::cout << "Missing robot parameter in configuration file." << std::endl;
329  }
330 
331  //-------- window parameters
332  if (configFile.Get("WINDOW_X_PX", cfg.window.WINDOW_X_PX) &&
333  configFile.Get("WINDOW_Y_PX", cfg.window.WINDOW_Y_PX))
334  {
335  std::cout << "Window's parameters in configuration file have been loaded." << std::endl;
336  }
337  else
338  {
339  std::cout << "Missing window parameter in configuration file." << std::endl;
340  }
341 
342  //-------- result parameters
343  if (configFile.Get("logfile_name", cfg.logfile_name) &&
344  configFile.Get("logfile_path", cfg.logfile_path) )
345  {
346  std::cout << "Result's parameters in configuration file have been loaded." << std::endl;
347  }
348  else
349  {
350  std::cout << "Missing result parameter in configuration file." << std::endl;
351  }
352 }
void help()
Definition: main.cpp:216
void init()
Definition: Demo.cpp:86
Implementation of the Demo class and the MyContactListener_v2 class.
Definition: Demo.h:60
Implementation of the Configuration class used to parse the configuration file and the sConfig struct...
int main(int argc, char *argv[])
Definition: main.cpp:17
void default_parameters(config::sConfig &cfg)
Definition: main.cpp:72
double runaway
Definition: Config.h:32
void demoLoop()
Definition: Demo.cpp:95
void writeResultFile()
Definition: Demo.cpp:446