mvsim_node_core.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 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 #if PACKAGE_ROS_VERSION == 1
13 #include <dynamic_reconfigure/server.h>
14 #include <mvsim/mvsimNodeConfig.h>
15 #endif
16 
17 #include <mrpt/core/WorkerThreadsPool.h>
18 #include <mrpt/obs/CObservation.h>
19 #include <mrpt/obs/obs_frwds.h>
20 #include <mrpt/system/CTicTac.h>
21 #include <mrpt/system/CTimeLogger.h>
22 #include <mvsim/Comms/Server.h>
23 #include <mvsim/World.h>
27 
28 #if PACKAGE_ROS_VERSION == 1
29 #include <geometry_msgs/Twist.h>
30 #include <ros/ros.h>
31 #include <ros/time.h>
32 #include <rosgraph_msgs/Clock.h>
33 #include <visualization_msgs/MarkerArray.h>
34 #else
35 #include <geometry_msgs/msg/polygon.hpp>
36 #include <geometry_msgs/msg/pose_array.hpp>
37 #include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
38 #include <nav_msgs/msg/map_meta_data.hpp>
39 #include <nav_msgs/msg/occupancy_grid.hpp>
40 #include <nav_msgs/msg/odometry.hpp>
41 #include <visualization_msgs/msg/marker_array.hpp>
42 
43 #include "rclcpp/clock.hpp"
44 #include "rclcpp/rclcpp.hpp"
45 #include "rclcpp/time_source.hpp"
46 #endif
47 
48 #include <atomic>
49 #include <thread>
50 
51 namespace mrpt
52 {
53 namespace obs
54 {
55 class CObservationPointCloud;
56 }
57 } // namespace mrpt
58 
61 class MVSimNode
62 {
63  public:
65 #if PACKAGE_ROS_VERSION == 1
67 #else
68  MVSimNode(rclcpp::Node::SharedPtr& n);
69 #endif
70 
72  ~MVSimNode();
73 
74  void terminateSimulation();
75 
76  void launch_mvsim_server();
77 
78  void loadWorldModel(const std::string& world_xml_file);
79 
80  void spin();
81 
82 #if PACKAGE_ROS_VERSION == 1
83 
84  void configCallback(mvsim::mvsimNodeConfig& config, uint32_t level);
85 #endif
86 
87  void onNewObservation(
88  const mvsim::Simulable& veh, const mrpt::obs::CObservation::Ptr& obs);
89 
92  std::shared_ptr<mvsim::World> mvsim_world_ =
93  std::make_shared<mvsim::World>();
94 
95  mrpt::WorkerThreadsPool ros_publisher_workers_{
96  4 /*threads*/, mrpt::WorkerThreadsPool::POLICY_FIFO};
97 
99  double realtime_factor_ = 1.0;
100  int gui_refresh_period_ms_ = 50;
101  bool headless_ = false;
102 
105  bool do_fake_localization_ = true;
106 
107  int publisher_history_len_ = 50;
108 
110  double transform_tolerance_ = 0.1;
111 
112  protected:
113  std::shared_ptr<mvsim::Server> mvsim_server_;
114 
115 #if PACKAGE_ROS_VERSION == 1
116  ros::NodeHandle& n_;
117  ros::NodeHandle localn_{"~"};
118 #else
119  rclcpp::Node::SharedPtr n_;
120 #endif
121 
122  // === ROS Publishers ====
124 #if PACKAGE_ROS_VERSION == 1
125  ros::Publisher pub_map_ros_, pub_map_metadata_;
126  // ros::Publisher pub_clock_;
127 #else
128  rclcpp::Publisher<nav_msgs::msg::OccupancyGrid>::SharedPtr pub_map_ros_;
129  rclcpp::Publisher<nav_msgs::msg::MapMetaData>::SharedPtr pub_map_metadata_;
130  rclcpp::TimeSource ts_{n_};
131  rclcpp::Clock::SharedPtr clock_;
132 #endif
133 
134 #if PACKAGE_ROS_VERSION == 1
137 #else
140 #endif
141 
143  {
144 #if PACKAGE_ROS_VERSION == 1
145  ros::Subscriber sub_cmd_vel;
146  ros::Publisher pub_odom;
147  ros::Publisher pub_ground_truth;
148 
150  ros::Publisher pub_amcl_pose, pub_particlecloud;
151 
153  std::map<std::string, ros::Publisher> pub_sensors;
154 
155  ros::Publisher pub_chassis_markers;
156  ros::Publisher pub_chassis_shape;
157 
158  visualization_msgs::MarkerArray chassis_shape_msg;
159 #else
160  rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr sub_cmd_vel;
163  rclcpp::Publisher<nav_msgs::msg::Odometry>::SharedPtr pub_odom;
165  rclcpp::Publisher<nav_msgs::msg::Odometry>::SharedPtr pub_ground_truth;
166 
168  rclcpp::Publisher<geometry_msgs::msg::PoseWithCovarianceStamped>::
169  SharedPtr pub_amcl_pose;
170  rclcpp::Publisher<geometry_msgs::msg::PoseArray>::SharedPtr
172 
174  std::map<std::string, rclcpp::PublisherBase::SharedPtr> pub_sensors;
175 
177  rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr
180  rclcpp::Publisher<geometry_msgs::msg::Polygon>::SharedPtr
182 
183  visualization_msgs::msg::MarkerArray chassis_shape_msg;
184 #endif
185  };
186 
189  std::vector<TPubSubPerVehicle> pubsub_vehicles_;
191 
194  void initPubSubs(TPubSubPerVehicle& out_pubsubs, mvsim::VehicleBase* veh);
195 
196  // === End ROS Publishers ====
197 
198  // === ROS Hooks ====
199  void onROSMsgCmdVel(
200 #if PACKAGE_ROS_VERSION == 1
201  const geometry_msgs::Twist::ConstPtr& cmd,
202 #else
203  const geometry_msgs::msg::Twist::ConstSharedPtr& cmd,
204 #endif
205  mvsim::VehicleBase* veh);
206  // === End ROS Hooks====
207 
208 #if PACKAGE_ROS_VERSION == 1
209  // rosgraph_msgs::Clock clockMsg_;
210  // ros::Time sim_time_; //!< Current simulation time
211  ros::Time base_last_cmd_;
212  ros::Duration base_watchdog_timeout_;
213 #else
214  // rclcpp::Time sim_time_; //!< Current simulation time
215  rclcpp::Time base_last_cmd_;
216  rclcpp::Duration base_watchdog_timeout_ = std::chrono::seconds(1);
217 #endif
218 
221 
223  {
224  TThreadParams() = default;
225 
226  MVSimNode* obj = nullptr;
227  std::atomic_bool closing{false};
228  };
230  mrpt::system::CTicTac realtime_tictac_;
231 
232  double t_old_ = -1;
233 
235  bool world_init_ok_ = false;
236 
239  double period_ms_publish_tf_ = 20;
240 
241  mrpt::system::CTicTac tim_publish_tf_;
242 
245  double period_ms_teleop_refresh_ = 100;
246  mrpt::system::CTicTac tim_teleop_refresh_;
247 
249  size_t teleop_idx_veh_ = 0;
251  std::string msg2gui_;
252 
253  std::thread thGUI_;
254  static void thread_update_GUI(TThreadParams& thread_params);
255 
258  void notifyROSWorldIsUpdated();
259 
261  void spinNotifyROS();
262 
265  std::string vehVarName(
266  const std::string& sVarName, const mvsim::VehicleBase& veh) const;
267 
268  void sendStaticTF(
269  const std::string& frame_id, const std::string& child_frame_id,
270  const tf2::Transform& tx,
271 #if PACKAGE_ROS_VERSION == 1
272  const ros::Time& stamp
273 #else
274  const rclcpp::Time& stamp
275 #endif
276  );
277 
278 #if PACKAGE_ROS_VERSION == 1
279  ros::Time myNow() const;
280 #else
281  rclcpp::Time myNow() const;
282 #endif
283 
284  mrpt::system::CTimeLogger profiler_{true /*enabled*/, "mvsim_node"};
285 
286  void publishWorldElements(mvsim::WorldElementBase& obj);
287  void publishVehicles(mvsim::VehicleBase& veh);
288 
289  void internalOn(
290  const mvsim::VehicleBase& veh,
291  const mrpt::obs::CObservation2DRangeScan& obs);
292  void internalOn(
293  const mvsim::VehicleBase& veh, const mrpt::obs::CObservationImage& obs);
294  void internalOn(
295  const mvsim::VehicleBase& veh,
296  const mrpt::obs::CObservation3DRangeScan& obs);
297  void internalOn(
298  const mvsim::VehicleBase& veh,
299  const mrpt::obs::CObservationPointCloud& obs);
300  void internalOn(
301  const mvsim::VehicleBase& veh, const mrpt::obs::CObservationIMU& obs);
302 
303 }; // end class
mvsim::World::TGUIKeyEvent gui_key_events_
rclcpp::Publisher< nav_msgs::msg::Odometry >::SharedPtr pub_odom
Publisher of "odom" topic.
visualization_msgs::msg::MarkerArray chassis_shape_msg
mrpt::system::CTicTac realtime_tictac_
rclcpp::Node::SharedPtr n_
rclcpp::Publisher< visualization_msgs::msg::MarkerArray >::SharedPtr pub_chassis_markers
"<VEH>/chassis_markers"
rclcpp::Publisher< nav_msgs::msg::OccupancyGrid >::SharedPtr pub_map_ros_
used for simul_map publication
rclcpp::Publisher< geometry_msgs::msg::PoseArray >::SharedPtr pub_particlecloud
rclcpp::Clock::SharedPtr clock_
std::string msg2gui_
static const Transform & getIdentity()
std::mutex pubsub_vehicles_mtx_
rclcpp::Publisher< geometry_msgs::msg::Polygon >::SharedPtr pub_chassis_shape
"<VEH>/chassis_shape"
ROSCPP_DECL void spin()
mrpt::system::CTicTac tim_teleop_refresh_
TThreadParams thread_params_
mrpt::system::CTicTac tim_publish_tf_
std::map< std::string, rclcpp::PublisherBase::SharedPtr > pub_sensors
Map <sensor_label> => publisher.
rclcpp::Publisher< nav_msgs::msg::Odometry >::SharedPtr pub_ground_truth
"base_pose_ground_truth" topic
std::thread thGUI_
rclcpp::Publisher< nav_msgs::msg::MapMetaData >::SharedPtr pub_map_metadata_
std::shared_ptr< mvsim::Server > mvsim_server_
rclcpp::Publisher< geometry_msgs::msg::PoseWithCovarianceStamped >::SharedPtr pub_amcl_pose
"fake_localization" pubs:
rclcpp::Time base_last_cmd_
received a vel_cmd (for watchdog)
std::vector< TPubSubPerVehicle > pubsub_vehicles_


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21