4 #include "Box2D/Box2D.h" 5 #include "SFML/Graphics.hpp" 6 #include "SFML/Window.hpp" 17 int main(
int argc,
char* argv[])
22 b2Vec2 Gravity(0.f, 0.f);
23 b2World* world =
new b2World(Gravity);
26 bool config_file =
false;
27 std::string config_file_path;
32 for (
int i = 1; i < argc; i++) {
36 if (std::string(argv[i]) ==
"-h" || std::string(argv[i]) ==
"--help") {
37 printf(
"tetetete \n");
41 else if (i + 1 != argc){
43 if (std::string(argv[i]) ==
"-cp" || std::string(argv[i]) ==
"--configuration_path") {
45 config_file_path = std::string(argv[i + 1]);
47 load_config(config_file_path, configParam);
51 parse_argument(argv, i, configParam);
55 if(configParam.terrain.v_angle > 0){
56 configParam.terrain.v_width = tan(configParam.terrain.v_angle)*2*configParam.terrain.v_height;
62 Demo myDemo(world, configParam);
75 cfg.terrain.v_width = 10;
76 cfg.terrain.v_height = 3.5;
77 cfg.terrain.v_angle = 50/RAD_TO_DEG;
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;
86 cfg.simulation.bridge_duration = 100;
87 cfg.simulation.dissolution_duration = 200;
88 cfg.simulation.visualization =
true;
90 cfg.controller.angle_limit = PI/2;
91 cfg.controller.bridge_delay = 5;
92 cfg.controller.walk_delay = 0.5;
93 cfg.controller.time_before_pushing = 1;
94 cfg.controller.max_robot_window = 50;
95 cfg.controller.stability_condition = 60;
97 cfg.robot.body_length = 1.02;
98 cfg.robot.speed = 2*PI;
100 cfg.window.WINDOW_X_PX = 1920;
101 cfg.window.WINDOW_Y_PX = 1080;
103 cfg.logfile_name =
"exp";
104 cfg.logfile_path =
"experiments/";
111 if (std::string(argv[i]) ==
"-r" || std::string(argv[i]) ==
"--terrain_runaway") {
112 cfg.terrain.
runaway = atof(argv[i + 1]);
114 else if (std::string(argv[i]) ==
"-vw" || std::string(argv[i]) ==
"--v_width") {
115 cfg.terrain.v_width = atof(argv[i + 1]);
117 else if (std::string(argv[i]) ==
"-vh" || std::string(argv[i]) ==
"--v_height") {
118 cfg.terrain.v_height = atof(argv[i + 1]);
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;
125 else if (std::string(argv[i]) ==
"-g" || std::string(argv[i]) ==
"--gravity") {
126 cfg.simulation.gravity = atof(argv[i + 1]);
128 else if (std::string(argv[i]) ==
"-rd" || std::string(argv[i]) ==
"--robot_distance") {
129 cfg.simulation.robot_distance = atof(argv[i + 1]);
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;
134 else if (std::string(argv[i]) ==
"-rt" || std::string(argv[i]) ==
"--robot_delay") {
135 cfg.simulation.robot_delay = atof(argv[i + 1]);
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]);
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]);
143 else if (std::string(argv[i]) ==
"-nb_r" || std::string(argv[i]) ==
"--number_robots") {
144 cfg.simulation.nb_robots = atoi(argv[i + 1]);
146 else if (std::string(argv[i]) ==
"-t" || std::string(argv[i]) ==
"--bridge_duration") {
147 cfg.simulation.bridge_duration = atof(argv[i + 1]);
149 else if (std::string(argv[i]) ==
"-td" || std::string(argv[i]) ==
"--dissolution_duration") {
150 cfg.simulation.dissolution_duration = atof(argv[i + 1]);
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;
156 else if (std::string(argv[i+1]) ==
"false"){
157 cfg.simulation.visualization =
false;
160 std::cout<<
"the argument should be either 'true' or 'false'"<<std::endl;
165 else if (std::string(argv[i]) ==
"-a" || std::string(argv[i]) ==
"--limit_angle") {
166 cfg.controller.angle_limit = atof(argv[i + 1]);
168 else if (std::string(argv[i]) ==
"-bt" || std::string(argv[i]) ==
"--bridge_delay") {
169 cfg.controller.bridge_delay = atof(argv[i + 1]);
171 else if (std::string(argv[i]) ==
"-wt" || std::string(argv[i]) ==
"--walk_delay") {
172 cfg.controller.walk_delay = atof(argv[i + 1]);
174 else if (std::string(argv[i]) ==
"-pt" || std::string(argv[i]) ==
"--pushing_delay") {
175 cfg.controller.time_before_pushing = atof(argv[i + 1]);
177 else if (std::string(argv[i]) ==
"-mr" || std::string(argv[i]) ==
"--max_robots") {
178 cfg.controller.max_robot_window = atoi(argv[i + 1]);
180 else if (std::string(argv[i]) ==
"-st" || std::string(argv[i]) ==
"--stability_condition") {
181 cfg.controller.stability_condition = atof(argv[i + 1]);
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;
189 else if (std::string(argv[i]) ==
"-v" || std::string(argv[i]) ==
"--robot_speed") {
190 cfg.robot.speed = atof(argv[i + 1]);
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;
198 else if (std::string(argv[i]) ==
"-wy" || std::string(argv[i]) ==
"--window_y") {
199 cfg.window.WINDOW_Y_PX = atoi(argv[i + 1]);
203 else if (std::string(argv[i]) ==
"-fp" || std::string(argv[i]) ==
"--file_path") {
204 cfg.logfile_path = std::string(argv[i + 1]);
206 else if (std::string(argv[i]) ==
"-fn" || std::string(argv[i]) ==
"--file_name") {
207 cfg.logfile_name = std::string(argv[i + 1]);
210 std::cout << argv[i] <<
" ";
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;
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;
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;
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;
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;
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;
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;
264 configFile.Load(filename);
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))
282 std::cout <<
"Simulation parameters in configuration file have been loaded." << std::endl;
286 std::cout <<
"Missing simulation parameter in configuration file." << std::endl;
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) )
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;
300 std::cout <<
"Missing terrain parameter in configuration file." << std::endl;
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) )
311 std::cout <<
"Terrain's parameters in configuration file have been loaded." << std::endl;
315 std::cout <<
"Missing terrain parameter in configuration file." << std::endl;
319 if (configFile.Get(
"body_length", cfg.robot.body_length) &&
320 configFile.Get(
"speed", cfg.robot.speed))
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;
328 std::cout <<
"Missing robot parameter in configuration file." << std::endl;
332 if (configFile.Get(
"WINDOW_X_PX", cfg.window.WINDOW_X_PX) &&
333 configFile.Get(
"WINDOW_Y_PX", cfg.window.WINDOW_Y_PX))
335 std::cout <<
"Window's parameters in configuration file have been loaded." << std::endl;
339 std::cout <<
"Missing window parameter in configuration file." << std::endl;
343 if (configFile.Get(
"logfile_name", cfg.logfile_name) &&
344 configFile.Get(
"logfile_path", cfg.logfile_path) )
346 std::cout <<
"Result's parameters in configuration file have been loaded." << std::endl;
350 std::cout <<
"Missing result parameter in configuration file." << std::endl;
Implementation of the Demo class and the MyContactListener_v2 class.
Implementation of the Configuration class used to parse the configuration file and the sConfig struct...
int main(int argc, char *argv[])
void default_parameters(config::sConfig &cfg)