mvsim_node_main.cpp
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 #include <mrpt/core/round.h>
11 
12 #include "mvsim/mvsim_node_core.h"
13 
14 /*------------------------------------------------------------------------------
15  * main()
16  * Main function to set up ROS node->
17  *----------------------------------------------------------------------------*/
18 
19 int main(int argc, char** argv)
20 {
21  // Set up ROS.
22 #if PACKAGE_ROS_VERSION == 1
23  ros::init(argc, argv, "mvsim");
25 #else
26  rclcpp::init(argc, argv);
27  auto n = rclcpp::Node::make_shared("mvsim");
28 #endif
29 
30  try
31  {
32  // Create a "Node" object.
33  mvsim_node::shared_ptr<MVSimNode> node = mvsim_node::make_shared<MVSimNode>(n);
34 
35  // Declare variables that can be modified by launch file or command
36  // line.
37  std::string world_file;
38 
39  double rate = 100.0;
40 
41 #if PACKAGE_ROS_VERSION == 1
42  // Initialize node parameters from launch file or command line.
43  // Use a private node handle so that multiple instances of the node can
44  // be run simultaneously while using different parameters.
45  ros::NodeHandle private_node_handle_("~");
46  private_node_handle_.param("simul_rate", rate, 100.0);
47  private_node_handle_.param("world_file", world_file, std::string(""));
48 #else
49  n->get_parameter("world_file", world_file);
50  n->get_parameter("simul_rate", rate);
51  ASSERT_(rate > 0);
52  const auto periodMs = mrpt::round(1e6 / static_cast<double>(rate));
53 #endif
54 
55  // Launch mvsim:
56  node->launch_mvsim_server();
57 
58  // Init world model:
59  if (!world_file.empty()) node->loadWorldModel(world_file);
60 
61  // Attach world as a mvsim communications node:
62  node->mvsim_world_->headless(node->headless_);
63  node->mvsim_world_->connectToServer();
64 
65 #if PACKAGE_ROS_VERSION == 1
66  // Set up a dynamic reconfigure server.
67  // Do this before parameter server, else some of the parameter server
68  // values can be overwritten.
69  dynamic_reconfigure::Server<mvsim::mvsimNodeConfig> dr_srv;
70  dr_srv.setCallback([&node](mvsim::mvsimNodeConfig& config, uint32_t level)
71  { return node->configCallback(config, level); });
72 #endif
73 
74  // Tell ROS how fast to run this node->
75 #if PACKAGE_ROS_VERSION == 1
76  ros::Rate r(rate);
77 
78  // Main loop.
79  while (n.ok())
80  {
81  node->spin();
82  ros::spinOnce();
83  r.sleep();
84  }
85 #else
86  const auto ros_clock = rclcpp::Clock::make_shared();
87  const auto timer_ = rclcpp::create_timer(
88  n, ros_clock, std::chrono::microseconds(periodMs),
89  [&node]()
90  {
91  if (rclcpp::ok()) node->spin();
92  });
93 
94  rclcpp::on_shutdown(
95  [&node]()
96  {
97  std::cout << "[rclcpp::on_shutdown] Destroying MVSIM node..." << std::endl;
98  node->terminateSimulation();
99  std::cout << "[rclcpp::on_shutdown] MVSIM node destroyed." << std::endl;
100  });
101 
102  rclcpp::spin(n);
103  rclcpp::shutdown();
104 #endif
105 
106  return 0;
107  }
108  catch (const std::exception& e)
109  {
110 #if PACKAGE_ROS_VERSION == 1
111  std::cerr << e.what() << std::endl;
112 #else
113  RCLCPP_ERROR_STREAM(n->get_logger(), "Exception in main node body:\n" << e.what());
114 #endif
115  return 1;
116  }
117 
118 } // end main()
mvsim_node::make_shared
std::shared_ptr< T > make_shared(Args &&... args)
Definition: mvsim_node_core.h:113
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros::spinOnce
ROSCPP_DECL void spinOnce()
mvsim_node::shared_ptr
std::shared_ptr< T > shared_ptr
Definition: mvsim_node_core.h:119
mvsim_node_core.h
main
int main(int argc, char **argv)
Definition: mvsim_node_main.cpp:19
ros::Rate::sleep
bool sleep()
ros::NodeHandle::ok
bool ok() const
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
ros::Rate
config
config
ros::NodeHandle


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