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#include "trajectory_traces.h"
12
13#include <optional>
14#include <unordered_set>
15
23extern "C" int robot_main(void);
24
32//void set_current_robot(Robot& robot);
34
36
37
39
40
48 Configuration& config;
49
50 // SDL globals
51 SDL_Window* window = nullptr;
52 SDL_Renderer* renderer = nullptr;
53 bool enable_gui = true;
54 bool paused = false;
55 bool one_tick_requested = false;
56 bool running = true;
57 bool show_time = false;
58 bool show_scale_bar = false;
59 bool show_comm = false;
60 bool show_comm_above_all = false;
61 bool show_lateral_leds = false;
62 bool show_light_levels = false;
63 bool show_trajectory_traces = false;
64
65 double t = 0.0f;
66
67 uint16_t window_width = 800;
68 uint16_t window_height = 600;
69 uint16_t sub_step_count = 4;
70 double GUI_speed_up = 1.0;
71
72 float arena_width = 1000.0;
73 float arena_height = 1000.0;
74 float arena_surface = 1e6;
75 float max_comm_radius = 00.0f;
76 float comm_ignore_occlusions = false;
77
79 // domain box in Box2D units (scaled)
80 b2Vec2 domain_min{0.0f, 0.0f};
81 float domain_w = 0.0f;
82 float domain_h = 0.0f;
83
84 b2WorldId worldId;
85 //std::vector<Robot> robots; ///< Vector of robots in the simulation.
86 arena_polygons_t arena_polygons;
87 arena_polygons_t scaled_arena_polygons;
88
89 // Objects
90 std::map<std::string, std::vector<std::shared_ptr<Object>>> objects;
91 std::vector<std::shared_ptr<Pogowall>> wall_objects;
92 std::vector<std::shared_ptr<PhysicalObject>> phys_objects;
93 std::vector<std::shared_ptr<PogobotObject>> robots;
94 std::vector<std::shared_ptr<Object>> non_robots;
95 std::unique_ptr<LightLevelMap> light_map;
96 TrajectoryTraces trajectory_traces;
97 std::string initial_formation;
98 std::pair<float, float> initial_formation_center;
99 std::string initial_formation_root_object_name;
100 float formation_min_space_between_neighbors;
101 float formation_max_space_between_neighbors;
102 float chessboard_distance_between_neighbors;
103 uint32_t formation_attempts_per_point;
104 uint32_t formation_max_restarts;
105 std::string formation_filename;
106 std::pair<float, float> imported_formation_min_coords;
107 std::pair<float, float> imported_formation_max_coords;
108// std::pair<float, float> formation_offset; ///< Apply an offset to the formation -- used for the chessboard-style formations.
109// float formation_rotation; ///< Apply a rotation to the formation -- used for the chessboard-style formations.
110 bool formation_cluster_at_center;
111 float light_map_nb_bin_x;
112 float light_map_nb_bin_y;
113
114 double last_frame_shown_t = -1.0;
115 double last_frame_saved_t = -1.0;
116 double last_data_saved_t = -1.0;
117
118 // Fonts
119 FC_Font* font;
120
121 // Mouse dragging for visualization
122 bool dragging_pos_by_mouse = false;
123 int last_mouse_x;
124 int last_mouse_y;
125 std::weak_ptr<PogobotObject> selected_robot;
126
127 // Find the robot at the given screen position
128 std::shared_ptr<PogobotObject> find_robot_at_screen_position(int mouse_x, int mouse_y) const;
129 // Run per-robot click callback for the robot under the mouse.
130 void handle_robot_click(int mouse_x, int mouse_y);
131 // Render a visual indication that the robot is selected (e.g. a circle around it)
132 void render_selected_robot();
133
134 // Data logger
135 bool enable_data_logging;
136 std::unique_ptr<DataLogger> data_logger;
137 uint64_t data_logger_flush_row_count;
138 std::optional<std::unordered_set<std::string>> data_logger_categories;
139
140 // Dummy robot used as current robot in the global_step callback
141 std::unique_ptr<PogobotObject> dummy_global_robot;
142 DiskGeometry dummy_global_robot_geom = DiskGeometry(26.5);
143
144
145public:
147
155 Simulation(Configuration& _config);
156
162 virtual ~Simulation();
163
169 void init_all();
170
177 void create_robots();
178
183 void create_objects();
184
191 void create_arena();
192
198 void create_walls();
199
205 void init_box2d();
206
213 void init_config();
214
222 void init_SDL();
223
229 void speed_up();
230
236 void speed_down();
237
241 void pause();
242
246 void help_message();
247
254 void handle_SDL_events();
255
261 void compute_neighbors();
262
268 void init_callbacks();
269
277 void init_data_logger();
278
286 void init_console_logger();
287
293 void draw_scale_bar();
294
299
305 void render_all();
306
312 void export_frames();
313
319 void export_data();
320
327 void main_loop();
328
333
339 void delete_old_data();
340
347
354
361
367 arena_polygons_t const& get_arena_geometry() { return arena_polygons; };
368
374 boundary_condition_t const& get_boundary_condition() { return boundary_condition; };
375
381
387 uint32_t get_nb_robots() const { return robots.size(); }
388
389};
390
392extern std::unique_ptr<Simulation> simulation;
393
394
395#endif // SIMULATOR_H
396
397// MODELINE "{{{1
398// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
399// 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:26
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:1370
void speed_up()
Speeds up the simulation GUI.
Definition simulator.cpp:703
void speed_down()
Slows down the simulation GUI.
Definition simulator.cpp:709
void compute_neighbors()
Computes neighboring robots.
Definition simulator.cpp:934
void init_SDL()
Initializes SDL and related subsystems.
Definition simulator.cpp:519
Simulation(Configuration &_config)
Constructs a Simulation object.
Definition simulator.cpp:81
void init_config()
Initializes the simulation configuration.
Definition simulator.cpp:465
void init_all()
Initializes the simulation components.
Definition simulator.cpp:106
void main_loop()
Runs the main simulation loop.
Definition simulator.cpp:1189
uint32_t get_nb_robots() const
Returns the number of robots in the simulation.
Definition simulator.h:387
void handle_SDL_events()
Handles SDL events.
Definition simulator.cpp:812
DataLogger * get_data_logger()
Retrieves the data logger.
Definition simulator.cpp:1365
void draw_scale_bar()
Draws a scale bar on the GUI.
Definition simulator.cpp:1029
void pause()
Toggles the simulation pause state.
Definition simulator.cpp:715
void create_walls()
Creates arena walls.
Definition simulator.cpp:405
boundary_condition_t const & get_boundary_condition()
Retrieve the arena boundary conditions.
Definition simulator.h:374
void init_data_logger()
Initializes the data logger.
Definition simulator.cpp:974
void help_message()
Displays a help message with GUI keyboard shortcuts.
Definition simulator.cpp:719
void init_box2d()
Initializes the Box2D world.
Definition simulator.cpp:456
void stop_simulation_main_loop()
Stops the main simulation loop.
Definition simulator.cpp:1347
void toggle_trajectory_traces()
Toggles robot trajectory trace rendering.
Definition simulator.cpp:1053
void export_frames()
Exports the current frame to a PNG file.
Definition simulator.cpp:1149
void apply_periodic_wrapping()
Wrap objects position to have period boundary conditions.
Definition simulator.cpp:685
virtual ~Simulation()
Destructor.
Definition simulator.cpp:89
void render_all()
Renders all simulation components.
Definition simulator.cpp:1083
void init_callbacks()
Initializes simulation callbacks.
Definition simulator.cpp:970
void create_robots()
Creates the robot instances.
Definition simulator.cpp:625
void delete_old_data()
Deletes old data files.
Definition simulator.cpp:1351
void init_console_logger()
Initializes the console logger.
Definition simulator.cpp:1017
void create_objects()
Creates objects in the simulation.
Definition simulator.cpp:113
arena_polygons_t const & get_arena_geometry()
Retrieve the arena geometry.
Definition simulator.h:367
void create_arena()
Creates the arena from a CSV file.
Definition simulator.cpp:287
LightLevelMap * get_light_map()
Retrieves the light map.
Definition simulator.cpp:1374
void export_data()
Exports simulation data from the current robot.
Definition simulator.cpp:1167
bool current_robot_enable_data_logging
Flag to enable data logging on the current robot.
Definition simulator.h:146
Definition trajectory_traces.h:14
std::vector< std::vector< b2Vec2 > > arena_polygons_t
Definition geometry.h:13
std::unique_ptr< Simulation > simulation
Global simulation instance.
Definition simulator.cpp:79
void set_current_robot(PogobotObject &robot)
Sets the current active robot.
Definition simulator.cpp:33
boundary_condition_t
Definition simulator.h:35
@ periodic
Definition simulator.h:35
@ solid
Definition simulator.h:35
void dummy_global_robot_init()
Definition simulator.cpp:31
int robot_main(void)
Main entry point for the robot code (C linkage).