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 std::pair<float, float> initial_formation_center;
92 std::string initial_formation_root_object_name;
93 float formation_min_space_between_neighbors;
94 float formation_max_space_between_neighbors;
95 float chessboard_distance_between_neighbors;
96 uint32_t formation_attempts_per_point;
97 uint32_t formation_max_restarts;
98 std::string formation_filename;
99 std::pair<float, float> imported_formation_min_coords;
100 std::pair<float, float> imported_formation_max_coords;
101// std::pair<float, float> formation_offset; ///< Apply an offset to the formation -- used for the chessboard-style formations.
102// float formation_rotation; ///< Apply a rotation to the formation -- used for the chessboard-style formations.
103 bool formation_cluster_at_center;
104 float light_map_nb_bin_x;
105 float light_map_nb_bin_y;
106
107 double last_frame_shown_t = -1.0;
108 double last_frame_saved_t = -1.0;
109 double last_data_saved_t = -1.0;
110
111 // Fonts
112 FC_Font* font;
113
114 // Mouse dragging for visualization
115 bool dragging_pos_by_mouse = false;
116 int last_mouse_x;
117 int last_mouse_y;
118
119 // Data logger
120 bool enable_data_logging;
121 std::unique_ptr<DataLogger> data_logger;
122 uint64_t data_logger_flush_row_count;
123
124 // Dummy robot used as current robot in the global_step callback
125 std::unique_ptr<PogobotObject> dummy_global_robot;
126 DiskGeometry dummy_global_robot_geom = DiskGeometry(26.5);
127
128
129public:
131
139 Simulation(Configuration& _config);
140
146 virtual ~Simulation();
147
153 void init_all();
154
161 void create_robots();
162
167 void create_objects();
168
175 void create_arena();
176
182 void create_walls();
183
189 void init_box2d();
190
197 void init_config();
198
206 void init_SDL();
207
213 void speed_up();
214
220 void speed_down();
221
225 void pause();
226
230 void help_message();
231
238 void handle_SDL_events();
239
245 void compute_neighbors();
246
252 void init_callbacks();
253
261 void init_data_logger();
262
270 void init_console_logger();
271
277 void draw_scale_bar();
278
284 void render_all();
285
291 void export_frames();
292
298 void export_data();
299
306 void main_loop();
307
312
318 void delete_old_data();
319
326
333
340
346 arena_polygons_t const& get_arena_geometry() { return arena_polygons; };
347
353 boundary_condition_t const& get_boundary_condition() { return boundary_condition; };
354
360
366 uint32_t get_nb_robots() const { return robots.size(); }
367
368};
369
371extern std::unique_ptr<Simulation> simulation;
372
373
374#endif // SIMULATOR_H
375
376// MODELINE "{{{1
377// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
378// 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:24
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:1194
void speed_up()
Speeds up the simulation GUI.
Definition simulator.cpp:692
void speed_down()
Slows down the simulation GUI.
Definition simulator.cpp:698
void compute_neighbors()
Computes neighboring robots.
Definition simulator.cpp:831
void init_SDL()
Initializes SDL and related subsystems.
Definition simulator.cpp:508
Simulation(Configuration &_config)
Constructs a Simulation object.
Definition simulator.cpp:76
void init_config()
Initializes the simulation configuration.
Definition simulator.cpp:453
void init_all()
Initializes the simulation components.
Definition simulator.cpp:101
void main_loop()
Runs the main simulation loop.
Definition simulator.cpp:1036
uint32_t get_nb_robots() const
Returns the number of robots in the simulation.
Definition simulator.h:366
void handle_SDL_events()
Handles SDL events.
Definition simulator.cpp:727
DataLogger * get_data_logger()
Retrieves the data logger.
Definition simulator.cpp:1189
void draw_scale_bar()
Draws a scale bar on the GUI.
Definition simulator.cpp:915
void pause()
Toggles the simulation pause state.
Definition simulator.cpp:704
void create_walls()
Creates arena walls.
Definition simulator.cpp:393
boundary_condition_t const & get_boundary_condition()
Retrieve the arena boundary conditions.
Definition simulator.h:353
void init_data_logger()
Initializes the data logger.
Definition simulator.cpp:871
void help_message()
Displays a help message with GUI keyboard shortcuts.
Definition simulator.cpp:708
void init_box2d()
Initializes the Box2D world.
Definition simulator.cpp:444
void stop_simulation_main_loop()
Stops the main simulation loop.
Definition simulator.cpp:1174
void export_frames()
Exports the current frame to a PNG file.
Definition simulator.cpp:1000
void apply_periodic_wrapping()
Wrap objects position to have period boundary conditions.
Definition simulator.cpp:674
virtual ~Simulation()
Destructor.
Definition simulator.cpp:84
void render_all()
Renders all simulation components.
Definition simulator.cpp:940
void init_callbacks()
Initializes simulation callbacks.
Definition simulator.cpp:867
void create_robots()
Creates the robot instances.
Definition simulator.cpp:614
void delete_old_data()
Deletes old data files.
Definition simulator.cpp:1178
void init_console_logger()
Initializes the console logger.
Definition simulator.cpp:903
void create_objects()
Creates objects in the simulation.
Definition simulator.cpp:108
arena_polygons_t const & get_arena_geometry()
Retrieve the arena geometry.
Definition simulator.h:346
void create_arena()
Creates the arena from a CSV file.
Definition simulator.cpp:275
LightLevelMap * get_light_map()
Retrieves the light map.
Definition simulator.cpp:1198
void export_data()
Exports simulation data from the current robot.
Definition simulator.cpp:1018
bool current_robot_enable_data_logging
Flag to enable data logging on the current robot.
Definition simulator.h:130
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).