Pogosim
Loading...
Searching...
No Matches
simulator.h
Go to the documentation of this file.
1#ifndef SIMULATOR_H
2#define SIMULATOR_H
3
4#include "pogosim.h"
5#include "robot.h"
6#include "objects.h"
7#include "lights.h"
8#include "configuration.h"
9#include "data_logger.h"
10#include "SDL_FontCache.h"
11
19extern "C" int robot_main(void);
20
28//void set_current_robot(Robot& robot);
30
32
33
35
36
44 Configuration& config;
45
46 // SDL globals
47 SDL_Window* window = nullptr;
48 SDL_Renderer* renderer = nullptr;
49 bool enable_gui = true;
50 bool paused = false;
51 bool running = true;
52 bool show_time = false;
53 bool show_scale_bar = false;
54 bool show_comm = false;
55 bool show_comm_above_all = false;
56 bool show_lateral_leds = false;
57 bool show_light_levels = false;
58
59 double t = 0.0f;
60
61 uint16_t window_width = 800;
62 uint16_t window_height = 600;
63 uint16_t sub_step_count = 4;
64 double GUI_speed_up = 1.0;
65
66 float arena_width = 1000.0;
67 float arena_height = 1000.0;
68 float arena_surface = 1e6;
69 float max_comm_radius = 00.0f;
70 float comm_ignore_occlusions = false;
71
73 // domain box in Box2D units (scaled)
74 b2Vec2 domain_min{0.0f, 0.0f};
75 float domain_w = 0.0f;
76 float domain_h = 0.0f;
77
78 b2WorldId worldId;
79 //std::vector<Robot> robots; ///< Vector of robots in the simulation.
80 arena_polygons_t arena_polygons;
81 arena_polygons_t scaled_arena_polygons;
82
83 // Objects
84 std::map<std::string, std::vector<std::shared_ptr<Object>>> objects;
85 std::vector<std::shared_ptr<Pogowall>> wall_objects;
86 std::vector<std::shared_ptr<PhysicalObject>> phys_objects;
87 std::vector<std::shared_ptr<PogobotObject>> robots;
88 std::vector<std::shared_ptr<Object>> non_robots;
89 std::unique_ptr<LightLevelMap> light_map;
90 std::string initial_formation;
91 float formation_min_space_between_neighbors;
92 float formation_max_space_between_neighbors;
93 float chessboard_distance_between_neighbors;
94 uint32_t formation_attempts_per_point;
95 uint32_t formation_max_restarts;
96 std::string formation_filename;
97 std::pair<float, float> imported_formation_min_coords;
98 std::pair<float, float> imported_formation_max_coords;
99// std::pair<float, float> formation_offset; ///< Apply an offset to the formation -- used for the chessboard-style formations.
100// float formation_rotation; ///< Apply a rotation to the formation -- used for the chessboard-style formations.
101 bool formation_cluster_at_center;
102 float light_map_nb_bin_x;
103 float light_map_nb_bin_y;
104
105 double last_frame_shown_t = -1.0;
106 double last_frame_saved_t = -1.0;
107 double last_data_saved_t = -1.0;
108
109 // Fonts
110 FC_Font* font;
111
112 // Mouse dragging for visualization
113 bool dragging_pos_by_mouse = false;
114 int last_mouse_x;
115 int last_mouse_y;
116
117 // Data logger
118 bool enable_data_logging;
119 std::unique_ptr<DataLogger> data_logger;
120
121 // Dummy robot used as current robot in the global_step callback
122 std::unique_ptr<PogobotObject> dummy_global_robot;
123 DiskGeometry dummy_global_robot_geom = DiskGeometry(26.5);
124
125
126public:
128
136 Simulation(Configuration& _config);
137
143 virtual ~Simulation();
144
150 void init_all();
151
158 void create_robots();
159
164 void create_objects();
165
172 void create_arena();
173
179 void create_walls();
180
186 void init_box2d();
187
194 void init_config();
195
203 void init_SDL();
204
210 void speed_up();
211
217 void speed_down();
218
222 void pause();
223
227 void help_message();
228
235 void handle_SDL_events();
236
242 void compute_neighbors();
243
249 void init_callbacks();
250
258 void init_data_logger();
259
267 void init_console_logger();
268
274 void draw_scale_bar();
275
281 void render_all();
282
288 void export_frames();
289
295 void export_data();
296
303 void main_loop();
304
309
315 void delete_old_data();
316
323
330
337
343 arena_polygons_t const& get_arena_geometry() { return arena_polygons; };
344
350 boundary_condition_t const& get_boundary_condition() { return boundary_condition; };
351
357
363 uint32_t get_nb_robots() const { return robots.size(); }
364
365};
366
368extern std::unique_ptr<Simulation> simulation;
369
370
371#endif // SIMULATOR_H
372
373// MODELINE "{{{1
374// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
375// vim:foldmethod=marker
Class for managing hierarchical configuration parameters.
Definition configuration.h:64
DataLogger class for writing data to a Feather file using Apache Arrow.
Definition data_logger.h:19
Disk-shaped geometry.
Definition objects_geometry.h:138
A discretized 2D grid representing light intensities over a simulation area.
Definition lights.h:20
Class representing a simulated Pogobot.
Definition robot.h:142
Configuration & get_config()
Retrieves the current configuration.
Definition simulator.cpp:1183
void speed_up()
Speeds up the simulation GUI.
Definition simulator.cpp:661
void speed_down()
Slows down the simulation GUI.
Definition simulator.cpp:667
void compute_neighbors()
Computes neighboring robots.
Definition simulator.cpp:800
void init_SDL()
Initializes SDL and related subsystems.
Definition simulator.cpp:478
Simulation(Configuration &_config)
Constructs a Simulation object.
Definition simulator.cpp:76
void init_config()
Initializes the simulation configuration.
Definition simulator.cpp:427
void init_all()
Initializes the simulation components.
Definition simulator.cpp:96
void main_loop()
Runs the main simulation loop.
Definition simulator.cpp:1025
uint32_t get_nb_robots() const
Returns the number of robots in the simulation.
Definition simulator.h:363
void handle_SDL_events()
Handles SDL events.
Definition simulator.cpp:696
DataLogger * get_data_logger()
Retrieves the data logger.
Definition simulator.cpp:1178
void draw_scale_bar()
Draws a scale bar on the GUI.
Definition simulator.cpp:888
void pause()
Toggles the simulation pause state.
Definition simulator.cpp:673
void create_walls()
Creates arena walls.
Definition simulator.cpp:367
boundary_condition_t const & get_boundary_condition()
Retrieve the arena boundary conditions.
Definition simulator.h:350
void init_data_logger()
Initializes the data logger.
Definition simulator.cpp:840
void help_message()
Displays a help message with GUI keyboard shortcuts.
Definition simulator.cpp:677
void init_box2d()
Initializes the Box2D world.
Definition simulator.cpp:418
void stop_simulation_main_loop()
Stops the main simulation loop.
Definition simulator.cpp:1163
void export_frames()
Exports the current frame to a PNG file.
Definition simulator.cpp:973
void apply_periodic_wrapping()
Wrap objects position to have period boundary conditions.
Definition simulator.cpp:643
virtual ~Simulation()
Destructor.
Definition simulator.cpp:84
void render_all()
Renders all simulation components.
Definition simulator.cpp:913
void init_callbacks()
Initializes simulation callbacks.
Definition simulator.cpp:836
void create_robots()
Creates the robot instances.
Definition simulator.cpp:584
void delete_old_data()
Deletes old data files.
Definition simulator.cpp:1167
void init_console_logger()
Initializes the console logger.
Definition simulator.cpp:876
void create_objects()
Creates objects in the simulation.
Definition simulator.cpp:103
arena_polygons_t const & get_arena_geometry()
Retrieve the arena geometry.
Definition simulator.h:343
void create_arena()
Creates the arena from a CSV file.
Definition simulator.cpp:249
LightLevelMap * get_light_map()
Retrieves the light map.
Definition simulator.cpp:1187
void export_data()
Exports simulation data from the current robot.
Definition simulator.cpp:991
bool current_robot_enable_data_logging
Flag to enable data logging on the current robot.
Definition simulator.h:127
std::vector< std::vector< b2Vec2 > > arena_polygons_t
Definition geometry.h:13
std::unique_ptr< Simulation > simulation
Global simulation instance.
Definition simulator.cpp:74
void set_current_robot(PogobotObject &robot)
Sets the current active robot.
Definition simulator.cpp:32
boundary_condition_t
Definition simulator.h:31
@ periodic
Definition simulator.h:31
@ solid
Definition simulator.h:31
void dummy_global_robot_init()
Definition simulator.cpp:30
int robot_main(void)
Main entry point for the robot code (C linkage).