mvsim_node_core.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2024 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/core/WorkerThreadsPool.h>
13 #include <mrpt/obs/CObservation.h>
14 #include <mrpt/obs/CObservation3DRangeScan.h>
15 #include <mrpt/obs/CObservationGPS.h>
16 #include <mrpt/obs/CObservationIMU.h>
17 #include <mrpt/obs/CObservationPointCloud.h>
18 #include <mrpt/system/CTicTac.h>
19 #include <mrpt/system/CTimeLogger.h>
20 #include <mvsim/World.h>
22 
23 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
24 #include <mvsim/Comms/Server.h>
25 #endif
26 
27 #include <atomic>
28 #include <thread>
29 
30 #if PACKAGE_ROS_VERSION == 1
31 #include <dynamic_reconfigure/server.h>
32 #include <geometry_msgs/Polygon.h>
33 #include <geometry_msgs/PoseArray.h>
34 #include <geometry_msgs/PoseWithCovarianceStamped.h>
35 #include <geometry_msgs/Twist.h>
36 #include <mvsim/mvsimNodeConfig.h>
37 #include <nav_msgs/MapMetaData.h>
38 #include <nav_msgs/OccupancyGrid.h>
39 #include <nav_msgs/Odometry.h>
40 #include <ros/ros.h>
41 #include <ros/time.h>
42 #include <rosgraph_msgs/Clock.h>
43 #include <sensor_msgs/CameraInfo.h>
44 #include <std_msgs/Bool.h>
45 #include <tf2_msgs/TFMessage.h>
46 #include <visualization_msgs/MarkerArray.h>
47 
48 // usings:
49 using ros_Time = ros::Time;
51 
52 using Msg_Polygon = geometry_msgs::Polygon;
53 using Msg_PoseArray = geometry_msgs::PoseArray;
54 using Msg_PoseWithCovarianceStamped = geometry_msgs::PoseWithCovarianceStamped;
55 using Msg_Twist = geometry_msgs::Twist;
56 using Msg_Twist_CSPtr = geometry_msgs::Twist::ConstPtr;
57 using Msg_OccupancyGrid = nav_msgs::OccupancyGrid;
58 using Msg_Odometry = nav_msgs::Odometry;
59 using Msg_MapMetaData = nav_msgs::MapMetaData;
60 using Msg_Bool = std_msgs::Bool;
61 using Msg_TFMessage = tf2_msgs::TFMessage;
62 using Msg_MarkerArray = visualization_msgs::MarkerArray;
63 using Msg_CameraInfo = sensor_msgs::CameraInfo;
64 #else
65 #include <geometry_msgs/msg/polygon.hpp>
66 #include <geometry_msgs/msg/pose_array.hpp>
67 #include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
68 #include <geometry_msgs/msg/twist.hpp>
69 #include <nav_msgs/msg/map_meta_data.hpp>
70 #include <nav_msgs/msg/occupancy_grid.hpp>
71 #include <nav_msgs/msg/odometry.hpp>
72 #include <rclcpp/clock.hpp>
73 #include <rclcpp/rclcpp.hpp>
74 #include <rclcpp/time_source.hpp>
75 #include <sensor_msgs/msg/camera_info.hpp>
76 #include <std_msgs/msg/bool.hpp>
77 #include <tf2_msgs/msg/tf_message.hpp>
78 #include <visualization_msgs/msg/marker_array.hpp>
79 
81 
82 // usings:
83 using ros_Time = rclcpp::Time;
84 using ros_Duration = rclcpp::Duration;
85 
86 using Msg_Polygon = geometry_msgs::msg::Polygon;
87 using Msg_PoseArray = geometry_msgs::msg::PoseArray;
88 using Msg_PoseWithCovarianceStamped = geometry_msgs::msg::PoseWithCovarianceStamped;
89 using Msg_Twist = geometry_msgs::msg::Twist;
90 using Msg_Twist_CSPtr = geometry_msgs::msg::Twist::ConstSharedPtr;
91 using Msg_OccupancyGrid = nav_msgs::msg::OccupancyGrid;
92 using Msg_Odometry = nav_msgs::msg::Odometry;
93 using Msg_MapMetaData = nav_msgs::msg::MapMetaData;
94 using Msg_Bool = std_msgs::msg::Bool;
95 using Msg_TFMessage = tf2_msgs::msg::TFMessage;
96 using Msg_MarkerArray = visualization_msgs::msg::MarkerArray;
97 using Msg_CameraInfo = sensor_msgs::msg::CameraInfo;
98 #endif
99 
100 namespace mvsim_node
101 {
102 #if PACKAGE_ROS_VERSION == 1
103 template <typename T, typename... Args>
105 {
106  return boost::make_shared<T>(std::forward<Args>(args)...);
107 }
108 
109 template <typename T>
111 #else
112 template <typename T, typename... Args>
113 std::shared_ptr<T> make_shared(Args&&... args)
114 {
115  return std::make_shared<T>(std::forward<Args>(args)...);
116 }
117 
118 template <typename T>
119 using shared_ptr = std::shared_ptr<T>;
120 #endif
121 } // namespace mvsim_node
122 
126 {
127  public:
129 #if PACKAGE_ROS_VERSION == 1
131 #else
132  MVSimNode(rclcpp::Node::SharedPtr& n);
133 #endif
134 
136  ~MVSimNode();
137 
138  void terminateSimulation();
139 
140  void launch_mvsim_server();
141 
142  void loadWorldModel(const std::string& world_xml_file);
143 
144  void spin();
145 
146 #if PACKAGE_ROS_VERSION == 1
147 
148  void configCallback(mvsim::mvsimNodeConfig& config, uint32_t level);
149 #endif
150 
151  void onNewObservation(const mvsim::Simulable& veh, const mrpt::obs::CObservation::Ptr& obs);
152 
155  mvsim_node::shared_ptr<mvsim::World> mvsim_world_ = mvsim_node::make_shared<mvsim::World>();
156 
157  mrpt::WorkerThreadsPool ros_publisher_workers_{
158  4 /*threads*/, mrpt::WorkerThreadsPool::POLICY_FIFO};
159 
161  double realtime_factor_ = 1.0;
163  bool headless_ = false;
164 
168 
170 
172 
173  protected:
174 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
176 #endif
177 
178 #if PACKAGE_ROS_VERSION == 1
180  ros::NodeHandle localn_{"~"};
181 #else
182  rclcpp::Node::SharedPtr n_;
183 #endif
184 
185  // === ROS Publishers ====
186 #if PACKAGE_ROS_VERSION == 1
187  // mvsim_node::shared_ptr<ros::Publisher> pub_clock_;
188 #else
189  rclcpp::TimeSource ts_{n_};
190  rclcpp::Clock::SharedPtr clock_;
191 #endif
192 
193  struct WorldPubs
194  {
195 #if PACKAGE_ROS_VERSION == 1
200 #else
201  rclcpp::Publisher<Msg_OccupancyGrid>::SharedPtr pub_map_ros;
203  rclcpp::Publisher<Msg_MapMetaData>::SharedPtr pub_map_metadata;
204 #endif
205  };
206 
208 
210  {
211 #if PACKAGE_ROS_VERSION == 1
213  sub_cmd_vel;
214 
218 
223 
225  std::map<std::string, mvsim_node::shared_ptr<ros::Publisher>> pub_sensors;
226 
230 
233 
235 #else
236  rclcpp::Subscription<Msg_Twist>::SharedPtr sub_cmd_vel;
238 
240  rclcpp::Publisher<Msg_Odometry>::SharedPtr pub_odom;
242  rclcpp::Publisher<Msg_Odometry>::SharedPtr pub_ground_truth;
243 
245  rclcpp::Publisher<Msg_PoseWithCovarianceStamped>::SharedPtr pub_amcl_pose;
246  rclcpp::Publisher<Msg_PoseArray>::SharedPtr pub_particlecloud;
247 
249  std::map<std::string, mvsim_node::shared_ptr<PublisherWrapperBase>> pub_sensors;
250 
252  rclcpp::Publisher<Msg_MarkerArray>::SharedPtr pub_chassis_markers;
254  rclcpp::Publisher<Msg_Polygon>::SharedPtr pub_chassis_shape;
256  rclcpp::Publisher<Msg_Bool>::SharedPtr pub_collision;
257 
259  rclcpp::Publisher<Msg_TFMessage>::SharedPtr pub_tf;
260  rclcpp::Publisher<Msg_TFMessage>::SharedPtr pub_tf_static;
261 
263 #endif
264  };
265 
268  std::vector<TPubSubPerVehicle> pubsub_vehicles_;
270 
273  void initPubSubs(TPubSubPerVehicle& out_pubsubs, mvsim::VehicleBase* veh);
274 
275  // === End ROS Publishers ====
276 
277  // === ROS Hooks ====
279  // === End ROS Hooks====
280 
281 #if PACKAGE_ROS_VERSION == 1
282  // rosgraph_msgs::Clock clockMsg_;
283 #endif
284  // ros_Time sim_time_; //!< Current simulation time
287 
290 
292  {
293  TThreadParams() = default;
294 
295  MVSimNode* obj = nullptr;
296  std::atomic_bool closing{false};
297  };
299  mrpt::system::CTicTac realtime_tictac_;
300 
301  double t_old_ = -1;
302 
304  bool world_init_ok_ = false;
305 
309 
310  mrpt::system::CTicTac tim_publish_tf_;
311 
314 
318  mrpt::system::CTicTac tim_teleop_refresh_;
319 
320  std::map<mvsim::VehicleBase*, double> lastCmdVelTimestamp_;
321 
323  size_t teleop_idx_veh_ = 0;
325  std::string msg2gui_;
326 
327  std::thread thGUI_;
328  static void thread_update_GUI(TThreadParams& thread_params);
329 
333 
335  void spinNotifyROS();
336 
339  std::string vehVarName(const std::string& sVarName, const mvsim::VehicleBase& veh) const;
340 
341  ros_Time myNow() const;
342  double myNowSec() const;
343 
344  mrpt::system::CTimeLogger profiler_{true /*enabled*/, "mvsim_node"};
345 
348 
349  void internalOn(const mvsim::VehicleBase& veh, const mrpt::obs::CObservation2DRangeScan& obs);
350  void internalOn(const mvsim::VehicleBase& veh, const mrpt::obs::CObservationImage& obs);
351  void internalOn(const mvsim::VehicleBase& veh, const mrpt::obs::CObservation3DRangeScan& obs);
352  void internalOn(const mvsim::VehicleBase& veh, const mrpt::obs::CObservationPointCloud& obs);
353  void internalOn(const mvsim::VehicleBase& veh, const mrpt::obs::CObservationIMU& obs);
354  void internalOn(const mvsim::VehicleBase& veh, const mrpt::obs::CObservationGPS& obs);
355 
356 }; // end class
MVSimNode::TThreadParams::obj
MVSimNode * obj
Definition: mvsim_node_core.h:295
MVSimNode::onNewObservation
void onNewObservation(const mvsim::Simulable &veh, const mrpt::obs::CObservation::Ptr &obs)
Definition: mvsim_node.cpp:955
MVSimNode::do_fake_localization_
bool do_fake_localization_
Definition: mvsim_node_core.h:167
MVSimNode::spin
void spin()
Process pending msgs, run real-time simulation, etc.
Definition: mvsim_node.cpp:317
mvsim_node::make_shared
std::shared_ptr< T > make_shared(Args &&... args)
Definition: mvsim_node_core.h:113
MVSimNode::~MVSimNode
~MVSimNode()
Definition: mvsim_node.cpp:281
MVSimNode::tim_teleop_refresh_
mrpt::system::CTicTac tim_teleop_refresh_
Definition: mvsim_node_core.h:318
MVSimNode::base_last_cmd_
ros_Time base_last_cmd_
received a vel_cmd (for watchdog)
Definition: mvsim_node_core.h:285
MVSimNode::spinNotifyROS
void spinNotifyROS()
Definition: mvsim_node.cpp:787
MVSimNode::gui_key_events_
mvsim::World::TGUIKeyEvent gui_key_events_
Definition: mvsim_node_core.h:324
MVSimNode::ts_
rclcpp::TimeSource ts_
Definition: mvsim_node_core.h:189
MVSimNode::TThreadParams
Definition: mvsim_node_core.h:291
boost::shared_ptr
MVSimNode::vehVarName
std::string vehVarName(const std::string &sVarName, const mvsim::VehicleBase &veh) const
Definition: mvsim_node.cpp:1013
MVSimNode::publisher_history_len_
int publisher_history_len_
Definition: mvsim_node_core.h:171
MVSimNode::WorldPubs::pub_map_metadata
rclcpp::Publisher< Msg_MapMetaData >::SharedPtr pub_map_metadata
Definition: mvsim_node_core.h:203
MVSimNode::force_publish_vehicle_namespace_
bool force_publish_vehicle_namespace_
If true, vehicle namespaces will be used even if there is only one vehicle:
Definition: mvsim_node_core.h:313
MVSimNode::TPubSubPerVehicle::sub_cmd_vel
rclcpp::Subscription< Msg_Twist >::SharedPtr sub_cmd_vel
Subscribers vehicle's "cmd_vel" topic.
Definition: mvsim_node_core.h:237
MVSimNode::thread_params_
TThreadParams thread_params_
Definition: mvsim_node_core.h:298
ros.h
MVSimNode::lastCmdVelTimestamp_
std::map< mvsim::VehicleBase *, double > lastCmdVelTimestamp_
Definition: mvsim_node_core.h:320
MVSimNode::TPubSubPerVehicle::pub_sensors
std::map< std::string, mvsim_node::shared_ptr< PublisherWrapperBase > > pub_sensors
Map <sensor_label> => publisher.
Definition: mvsim_node_core.h:249
time.h
MVSimNode::TPubSubPerVehicle::pub_particlecloud
rclcpp::Publisher< Msg_PoseArray >::SharedPtr pub_particlecloud
Definition: mvsim_node_core.h:246
World.h
MVSimNode::TPubSubPerVehicle
Definition: mvsim_node_core.h:209
Server.h
MVSimNode::myNow
ros_Time myNow() const
Definition: mvsim_node.cpp:544
Msg_MapMetaData
nav_msgs::msg::MapMetaData Msg_MapMetaData
Definition: mvsim_node_core.h:93
MVSimNode::MVSimNode
MVSimNode(rclcpp::Node::SharedPtr &n)
Definition: mvsim_node.cpp:130
ros_Time
rclcpp::Time ros_Time
Definition: mvsim_node_core.h:83
MVSimNode::publishWorldElements
void publishWorldElements(mvsim::WorldElementBase &obj)
Definition: mvsim_node.cpp:478
mvsim_node::shared_ptr
std::shared_ptr< T > shared_ptr
Definition: mvsim_node_core.h:119
Msg_MarkerArray
visualization_msgs::msg::MarkerArray Msg_MarkerArray
Definition: mvsim_node_core.h:96
MVSimNode
Definition: mvsim_node_core.h:125
MVSimNode::tim_publish_tf_
mrpt::system::CTicTac tim_publish_tf_
Definition: mvsim_node_core.h:310
MVSimNode::TThreadParams::TThreadParams
TThreadParams()=default
MVSimNode::clock_
rclcpp::Clock::SharedPtr clock_
Definition: mvsim_node_core.h:190
MVSimNode::realtime_factor_
double realtime_factor_
(Defaul=1.0) >1: speed-up, <1: slow-down
Definition: mvsim_node_core.h:161
mvsim_node
Definition: mvsim_node_core.h:100
MVSimNode::thread_update_GUI
static void thread_update_GUI(TThreadParams &thread_params)
Definition: mvsim_node.cpp:430
MVSimNode::ros_publisher_workers_
mrpt::WorkerThreadsPool ros_publisher_workers_
Definition: mvsim_node_core.h:157
MVSimNode::headless_
bool headless_
Definition: mvsim_node_core.h:163
tf2::Transform::getIdentity
static const Transform & getIdentity()
Msg_Twist
geometry_msgs::msg::Twist Msg_Twist
Definition: mvsim_node_core.h:89
MVSimNode::terminateSimulation
void terminateSimulation()
Definition: mvsim_node.cpp:283
Msg_TFMessage
tf2_msgs::msg::TFMessage Msg_TFMessage
Definition: mvsim_node_core.h:95
Msg_Polygon
geometry_msgs::msg::Polygon Msg_Polygon
Definition: mvsim_node_core.h:86
MVSimNode::TPubSubPerVehicle::pub_odom
rclcpp::Publisher< Msg_Odometry >::SharedPtr pub_odom
Publisher of "odom" topic.
Definition: mvsim_node_core.h:240
MVSimNode::thGUI_
std::thread thGUI_
Definition: mvsim_node_core.h:327
MVSimNode::TThreadParams::closing
std::atomic_bool closing
Definition: mvsim_node_core.h:296
tf2::Transform
Msg_Odometry
nav_msgs::msg::Odometry Msg_Odometry
Definition: mvsim_node_core.h:92
MVSimNode::TPubSubPerVehicle::pub_collision
rclcpp::Publisher< Msg_Bool >::SharedPtr pub_collision
"<VEH>/collision"
Definition: mvsim_node_core.h:256
Msg_CameraInfo
sensor_msgs::msg::CameraInfo Msg_CameraInfo
Definition: mvsim_node_core.h:97
MVSimNode::myNowSec
double myNowSec() const
Definition: mvsim_node.cpp:553
MVSimNode::TPubSubPerVehicle::pub_ground_truth
rclcpp::Publisher< Msg_Odometry >::SharedPtr pub_ground_truth
"base_pose_ground_truth" topic
Definition: mvsim_node_core.h:242
MVSimNode::WorldPubs
Definition: mvsim_node_core.h:193
MVSimNode::realtime_tictac_
mrpt::system::CTicTac realtime_tictac_
Definition: mvsim_node_core.h:299
MVSimNode::TPubSubPerVehicle::pub_amcl_pose
rclcpp::Publisher< Msg_PoseWithCovarianceStamped >::SharedPtr pub_amcl_pose
"fake_localization" pubs:
Definition: mvsim_node_core.h:245
MVSimNode::WorldPubs::pub_map_ros
rclcpp::Publisher< Msg_OccupancyGrid >::SharedPtr pub_map_ros
used for simul_map publication
Definition: mvsim_node_core.h:202
Msg_PoseArray
geometry_msgs::msg::PoseArray Msg_PoseArray
Definition: mvsim_node_core.h:87
publisher_wrapper.h
MVSimNode::notifyROSWorldIsUpdated
void notifyROSWorldIsUpdated()
Definition: mvsim_node.cpp:502
MVSimNode::period_ms_publish_tf_
double period_ms_publish_tf_
Definition: mvsim_node_core.h:308
MVSimNode::publish_tf_odom2baselink_
bool publish_tf_odom2baselink_
Definition: mvsim_node_core.h:169
MVSimNode::onROSMsgCmdVel
void onROSMsgCmdVel(Msg_Twist_CSPtr cmd, mvsim::VehicleBase *veh)
Definition: mvsim_node.cpp:768
MVSimNode::worldPubs_
WorldPubs worldPubs_
Definition: mvsim_node_core.h:207
Msg_Bool
std_msgs::msg::Bool Msg_Bool
Definition: mvsim_node_core.h:94
mvsim::VehicleBase
Definition: VehicleBase.h:44
Msg_Twist_CSPtr
geometry_msgs::msg::Twist::ConstSharedPtr Msg_Twist_CSPtr
Definition: mvsim_node_core.h:90
MVSimNode::base_watchdog_timeout_
ros_Duration base_watchdog_timeout_
Definition: mvsim_node_core.h:286
ros_Duration
rclcpp::Duration ros_Duration
Definition: mvsim_node_core.h:84
MVSimNode::publishVehicles
void publishVehicles(mvsim::VehicleBase &veh)
Definition: mvsim_node.cpp:471
MVSimNode::teleop_idx_veh_
size_t teleop_idx_veh_
for teleoperation from the GUI (selects the focused" vehicle)
Definition: mvsim_node_core.h:323
MVSimNode::tfIdentity_
const tf2::Transform tfIdentity_
Unit transform (const, once)
Definition: mvsim_node_core.h:289
ros::Time
mvsim::Simulable
Definition: Simulable.h:39
MVSimNode::TPubSubPerVehicle::pub_tf_static
rclcpp::Publisher< Msg_TFMessage >::SharedPtr pub_tf_static
Definition: mvsim_node_core.h:260
MVSimNode::initPubSubs
void initPubSubs(TPubSubPerVehicle &out_pubsubs, mvsim::VehicleBase *veh)
Definition: mvsim_node.cpp:564
mvsim::World::TGUIKeyEvent
Definition: World.h:168
Msg_OccupancyGrid
nav_msgs::msg::OccupancyGrid Msg_OccupancyGrid
Definition: mvsim_node_core.h:91
MVSimNode::world_init_ok_
bool world_init_ok_
will be true after a success call to loadWorldModel()
Definition: mvsim_node_core.h:304
MVSimNode::launch_mvsim_server
void launch_mvsim_server()
Definition: mvsim_node.cpp:246
MVSimNode::internalOn
void internalOn(const mvsim::VehicleBase &veh, const mrpt::obs::CObservation2DRangeScan &obs)
Definition: mvsim_node.cpp:1025
Msg_PoseWithCovarianceStamped
geometry_msgs::msg::PoseWithCovarianceStamped Msg_PoseWithCovarianceStamped
Definition: mvsim_node_core.h:88
MVSimNode::period_ms_teleop_refresh_
double period_ms_teleop_refresh_
Definition: mvsim_node_core.h:317
args
MVSimNode::pubsub_vehicles_mtx_
std::mutex pubsub_vehicles_mtx_
Definition: mvsim_node_core.h:269
MVSimNode::TPubSubPerVehicle::pub_chassis_markers
rclcpp::Publisher< Msg_MarkerArray >::SharedPtr pub_chassis_markers
"<VEH>/chassis_markers"
Definition: mvsim_node_core.h:252
MVSimNode::msg2gui_
std::string msg2gui_
Definition: mvsim_node_core.h:325
MVSimNode::gui_refresh_period_ms_
int gui_refresh_period_ms_
Definition: mvsim_node_core.h:162
MVSimNode::TPubSubPerVehicle::pub_chassis_shape
rclcpp::Publisher< Msg_Polygon >::SharedPtr pub_chassis_shape
"<VEH>/chassis_shape"
Definition: mvsim_node_core.h:254
config
config
ros::Duration
MVSimNode::mvsim_world_
mvsim_node::shared_ptr< mvsim::World > mvsim_world_
Definition: mvsim_node_core.h:155
mvsim::WorldElementBase
Definition: WorldElementBase.h:27
MVSimNode::profiler_
mrpt::system::CTimeLogger profiler_
Definition: mvsim_node_core.h:344
Transform.h
MVSimNode::TPubSubPerVehicle::pub_tf
rclcpp::Publisher< Msg_TFMessage >::SharedPtr pub_tf
"<VEH>/tf", "<VEH>/tf_static"
Definition: mvsim_node_core.h:259
MVSimNode::TPubSubPerVehicle::chassis_shape_msg
Msg_MarkerArray chassis_shape_msg
Definition: mvsim_node_core.h:262
ros::NodeHandle
MVSimNode::pubsub_vehicles_
std::vector< TPubSubPerVehicle > pubsub_vehicles_
Definition: mvsim_node_core.h:268
MVSimNode::t_old_
double t_old_
Definition: mvsim_node_core.h:301
MVSimNode::loadWorldModel
void loadWorldModel(const std::string &world_xml_file)
Definition: mvsim_node.cpp:260
MVSimNode::n_
rclcpp::Node::SharedPtr n_
Definition: mvsim_node_core.h:182


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08