9 #include <mrpt/core/lock_helper.h> 17 #include "GenericAnswer.pb.h" 18 #include "SrvGetPose.pb.h" 19 #include "SrvGetPoseAnswer.pb.h" 20 #include "SrvSetPose.pb.h" 21 #include "SrvSetPoseAnswer.pb.h" 23 using namespace mvsim;
75 m_timlogger.registerUserMeasure(
"run_simulation.dt", dt);
84 const double timetol = 1e-6;
93 m_timlogger.registerUserMeasure(
"run_simulation.cpu_dt", t1 - t0);
104 context.
world =
this;
111 mrpt::system::CTimeLoggerEntry tle(
m_timlogger,
"timestep.0.prestep");
114 if (e.second) e.second->simul_pre_timestep(context);
119 mrpt::system::CTimeLoggerEntry tle(
128 mrpt::system::CTimeLoggerEntry tle(
132 if (e.second) e.second->simul_post_timestep(context);
137 if (ts > dt)
m_timlogger.registerUserMeasure(
"timestep_too_slow_alert", ts);
142 std::string localFileName;
143 if (modelURI.substr(0, 7) ==
"http://" ||
144 modelURI.substr(0, 8) ==
"https://")
146 MRPT_TODO(
"Retrieve models from online sources");
150 else if (modelURI.substr(0, 7) ==
"file://")
152 localFileName = modelURI.substr(7);
155 localFileName = modelURI;
170 bool is_relative =
true;
171 if (s.size() > 2 && s[1] ==
':' && (s[2] ==
'/' || s[2] ==
'\\'))
173 if (s.size() > 0 && (s[0] ==
'/' || s[0] ==
'\\')) is_relative =
false;
177 if (!ret.empty() && ret[ret.size() - 1] !=
'/' &&
178 ret[ret.size() - 1] !=
'\\')
195 if (veh.second) v(*veh.second);
208 MRPT_TODO(
"Allow changing server IP from xml?");
209 m_client.setVerbosityLevel(this->getMinLoggingLevel());
216 o.second->registerOnServer(
m_client);
223 std::function<mvsim_msgs::SrvSetPoseAnswer(
224 const mvsim_msgs::SrvSetPose&)>(
225 [
this](
const mvsim_msgs::SrvSetPose& req) {
228 mvsim_msgs::SrvSetPoseAnswer ans;
229 ans.set_objectisincollision(
false);
231 const auto sId = req.objectid();
233 if (
auto itV = m_simulableObjects.find(sId);
234 itV != m_simulableObjects.end())
236 if (req.has_relativeincrement() &&
237 req.relativeincrement())
242 req.pose().x(), req.pose().y(),
243 req.pose().z(), req.pose().yaw(),
244 req.pose().pitch(), req.pose().roll());
245 itV->second->setPose(p.asTPose());
247 auto* absPose = ans.mutable_objectglobalpose();
248 absPose->set_x(p.
x());
249 absPose->set_y(p.
y());
250 absPose->set_z(p.z());
251 absPose->set_yaw(p.
yaw());
252 absPose->set_pitch(p.
pitch());
253 absPose->set_roll(p.
roll());
257 itV->second->setPose(
258 {req.pose().x(), req.pose().y(), req.pose().z(),
259 req.pose().yaw(), req.pose().pitch(),
262 ans.set_success(
true);
263 ans.set_objectisincollision(
264 itV->second->hadCollision());
265 itV->second->resetCollisionFlag();
269 ans.set_success(
false);
277 std::function<mvsim_msgs::SrvGetPoseAnswer(
278 const mvsim_msgs::SrvGetPose&)>(
279 [
this](
const mvsim_msgs::SrvGetPose& req) {
282 mvsim_msgs::SrvGetPoseAnswer ans;
283 const auto sId = req.objectid();
284 ans.set_objectisincollision(
false);
286 if (
auto itV = m_simulableObjects.find(sId);
287 itV != m_simulableObjects.end())
289 ans.set_success(
true);
291 auto* po = ans.mutable_pose();
296 po->set_pitch(p.
pitch);
297 po->set_roll(p.
roll);
299 ans.set_objectisincollision(
300 itV->second->hadCollision());
301 itV->second->resetCollisionFlag();
305 ans.set_success(
false);
314 block->setBlockIndex(
m_blocks.size());
317 m_blocks.insert(BlockList::value_type(block->getName(), block));
321 block->getName(), std::dynamic_pointer_cast<
Simulable>(block)));
const b2Vec2 b2Vec2_zero(0.0f, 0.0f)
Useful constant.
SimulableList m_simulableObjects
std::recursive_mutex m_world_cs
first time the GUI window is created.
WorldElementList m_world_elements
mrpt::system::CTicTac m_timer_iteration
#define THROW_EXCEPTION(msg)
std::string xmlPathToActualPath(const std::string &modelURI) const
mrpt::system::TTimeStamp now()
double simul_time
Current time in the simulated world.
mrpt::system::CTimeLogger m_timlogger
void runVisitorOnWorldElements(const world_element_visitor_t &v)
#define MRPT_LOG_DEBUG(_STRING)
std::shared_ptr< Block > Ptr
std::function< void(VehicleBase &)> vehicle_visitor_t
void internal_one_timestep(double dt)
std::unique_ptr< b2World > m_box2d_world
MRPT_TODO("Modify ping to run on Windows + Test this")
void insertBlock(const Block::Ptr &block)
void advertiseService(const std::string &serviceName, const std::function< OUTPUT_MSG_T(const INPUT_MSG_T &)> &callback)
std::string BASE_IMPEXP filePathSeparatorsToNative(const std::string &filePath)
std::mutex m_simulationStepRunningMtx
std::string resolvePath(const std::string &in_path) const
b2Body * m_b2_ground_body
std::function< void(WorldElementBase &)> world_element_visitor_t
std::atomic_bool m_gui_thread_must_close
void runVisitorOnVehicles(const vehicle_visitor_t &v)
std::string BASE_IMPEXP trim(const std::string &str)
World()
Default ctor: inits an empty world.
void run_simulation(double dt)