9 #include <mrpt/core/lock_helper.h> 10 #include <mrpt/math/TTwist2D.h> 11 #include <mrpt/system/filesystem.h> 12 #include <mrpt/version.h> 20 using namespace mvsim;
34 MRPT_LOG_DEBUG(
"Dtor: Waiting for GUI thread to quit...");
37 MRPT_LOG_DEBUG(
"Dtor: GUI thread shut down successful.");
41 MRPT_LOG_DEBUG(
"Dtor: GUI thread already shut down.");
76 #if MRPT_VERSION >= 0x270 80 worldVisual_->getViewport()->lightParameters().ambient = {
90 auto glVizSensors = mrpt::opengl::CSetOfObjects::Create();
91 glVizSensors->setName(
"group_sensors_viz");
96 getTimeLogger().setMinLoggingLevel(this->getMinLoggingLevel());
107 const double t0 = mrpt::Clock::nowDouble();
113 timlogger_.registerUserMeasure(
"run_simulation.dt", dt);
119 ASSERT_(simulTimestep > 0);
124 const double timetol = 1e-4;
130 if (remainingTime <= 0)
break;
133 remainingTime >= simulTimestep ? simulTimestep : remainingTime);
140 const double t1 = mrpt::Clock::toDouble(mrpt::Clock::now());
142 timlogger_.registerUserMeasure(
"run_simulation.cpu_dt", t1 - t0);
155 context.
world =
this;
164 mrpt::system::CTimeLoggerEntry tle(
timlogger_,
"timestep.0.prestep");
167 if (e.second) e.second->simul_pre_timestep(context);
172 mrpt::system::CTimeLoggerEntry tle(
173 timlogger_,
"timestep.1.dynamics_integrator");
187 mrpt::system::CTimeLoggerEntry tle(
195 if (!e.second)
continue;
197 e.second->simul_post_timestep(context);
203 if (e.second->hadCollision())
213 itV->second->resetCollisionFlag();
215 reset_collision_flags_.clear();
217 lckListObjs.unlock();
220 mrpt::system::CTimeLoggerEntry tle4(
227 std::this_thread::sleep_for(std::chrono::milliseconds(1));
232 "Timeout waiting for async sensors to be simulated in opengl " 234 timlogger_.registerUserMeasure(
"timestep.timeout_3D_sensors", 1.0);
240 timlogger_.registerUserMeasure(
"timestep", ts);
241 if (ts > dt)
timlogger_.registerUserMeasure(
"timestep.too_slow_alert", ts);
255 const std::string
s = mrpt::system::trim(s_in);
260 bool is_relative =
true;
261 if (s.size() > 2 && s[1] ==
':' && (s[2] ==
'/' || s[2] ==
'\\'))
263 if (s.size() > 0 && (s[0] ==
'/' || s[0] ==
'\\')) is_relative =
false;
267 if (!ret.empty() && ret[ret.size() - 1] !=
'/' &&
268 ret[ret.size() - 1] !=
'\\')
275 return mrpt::system::filePathSeparatorsToNative(ret);
281 if (veh.second) v(*veh.second);
293 if (b.second) v(*b.second);
299 client_.setVerbosityLevel(this->getMinLoggingLevel());
309 o.second->registerOnServer(
client_);
311 lckListObjs.unlock();
320 block->setBlockIndex(
blocks_.size());
323 blocks_.insert(BlockList::value_type(block->getName(), block));
330 block->getName(), std::dynamic_pointer_cast<
Simulable>(block)));
336 static bool firstTimeCheck =
true;
338 auto lambdaMinimumSensorPeriod = [
this]() -> std::optional<double> {
339 std::optional<double> ret;
342 if (!veh.second)
continue;
343 for (
const auto&
s : veh.second->getSensors())
345 const double T =
s->sensor_period();
347 mrpt::keep_min(*ret, T);
361 auto sensorMinPeriod = lambdaMinimumSensorPeriod();
362 if (sensorMinPeriod) mrpt::keep_min(
simulTimestep_, *sensorMinPeriod);
365 "Physics simulation timestep automatically determined as: %.02f ms",
368 firstTimeCheck =
false;
370 else if (firstTimeCheck)
372 firstTimeCheck =
false;
373 auto sensorMinPeriod = lambdaMinimumSensorPeriod();
377 "Physics simulation timestep (%.02f ms) should be >= than the " 378 "minimum sensor period (%.02f ms) to avoid missing sensor " 403 static bool first =
true;
mrpt::system::CTimeLogger timlogger_
bool sensor_has_to_create_egl_context()
std::unique_ptr< b2World > box2d_world_
bool simulator_must_close() const
mrpt::opengl::COpenGLScene worldPhysical_
B2_API const b2Vec2 b2Vec2_zero
Useful constant.
double get_simul_time() const
SimulableList simulableObjects_
std::string serverAddress_
double simul_time
Current time in the simulated world.
void runVisitorOnWorldElements(const world_element_visitor_t &v)
std::mutex simulationStepRunningMtx_
void force_set_simul_time(double newSimulatedTime)
Normally should not be called by users, for internal use only.
std::function< void(Block &)> block_visitor_t
mrpt::system::CTicTac timer_iteration_
void runVisitorOnBlocks(const block_visitor_t &v)
std::shared_ptr< Block > Ptr
std::function< void(VehicleBase &)> vehicle_visitor_t
void internal_one_timestep(double dt)
void insertBlock(const Block::Ptr &block)
static void FreeOpenGLResources()
std::string resolve_path(const std::string &uri)
auto & getListOfSimulableObjectsMtx()
double get_simul_timestep() const
Simulation fixed-time interval for numerical integration.
std::recursive_mutex copy_of_objects_dynstate_mtx_
auto & physical_objects_mtx()
void free_opengl_resources()
RemoteResourcesManager remoteResources_
void internal_initialize()
std::function< void(WorldElementBase &)> world_element_visitor_t
bool pending_running_sensors_on_3D_scene()
mrpt::system::CTimeLogger & getTimeLogger()
void internal_advertiseServices()
std::map< std::string, mrpt::math::TPose3D > copy_of_objects_dynstate_pose_
Updated in internal_one_step()
void runVisitorOnVehicles(const vehicle_visitor_t &v)
std::set< std::string > copy_of_objects_had_collision_
std::mutex reset_collision_flags_mtx_
std::recursive_mutex worldPhysicalMtx_
std::string local_to_abs_path(const std::string &in_path) const
std::recursive_mutex world_cs_
mrpt::opengl::COpenGLScene::Ptr worldVisual_
LightOptions lightOptions_
const std::string & serverHostAddress() const
std::optional< double > simul_start_wallclock_time_
World()
Default ctor: inits an empty world.
std::mutex simul_time_mtx_
std::map< std::string, mrpt::math::TTwist2D > copy_of_objects_dynstate_twist_
void run_simulation(double dt)
WorldElementList worldElements_
std::string xmlPathToActualPath(const std::string &modelURI) const
std::set< std::string > reset_collision_flags_
std::mutex simulableObjectsMtx_