Corentin Chauvin-Hameau – 2019-2020
Coverage Path Planning for an underwater robot surveying a marine farm
clock_publisher.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * \brief Definition of a node to generate a fake trajectory and evaluate the
5  * performance of the control.
6  * \author Corentin Chauvin-Hameau
7  * \date 2020
8  */
9 
10 #include "clock_publisher.hpp"
11 #include <rosgraph_msgs/Clock.h>
12 #include <ros/ros.h>
13 #include <iostream>
14 #include <chrono>
15 #include <thread>
16 #include <ctime>
17 
18 using std::cout;
19 using std::endl;
20 
21 
22 namespace mfcpp {
23 
24 
26  nh_("~")
27 {
28  init_node();
29 }
30 
31 
33 {
34 
35 }
36 
37 
39 {
40  // ROS parameters
41  nh_.param<bool>("/use_sim_time", use_sim_time_, false);
42  nh_.param<float>("publish_rate", publish_rate_, 1.0);
43  nh_.param<float>("time_factor", time_factor_, 1.0);
44 
45  // Other variables
46 
47 
48  // ROS publishers
49  clock_pub_ = nh_.advertise<rosgraph_msgs::Clock>("/clock", 0);
50 }
51 
52 
54 {
55  if (!use_sim_time_)
56  return;
57 
58  ros::WallRate rate(publish_rate_ * time_factor_);
59  double current_time = 0.0;
60 
61  while (ros::ok()) {
62  rosgraph_msgs::Clock msg;
63  msg.clock = ros::Time(current_time);
64  clock_pub_.publish(msg);
65 
66  current_time += 1 / publish_rate_;
67  rate.sleep();
68  }
69 }
70 
71 
72 } // namespace mfcpp
73 
74 
75 
76 /**
77  * \brief Main function called before node initialisation
78  */
79 int main(int argc, char **argv)
80 {
81  ros::init(argc, argv, "clock_publisher");
82  mfcpp::ClockPublisherNode clock_publisher_node;
83  clock_publisher_node.run_node();
84 
85  return 0;
86 }
float publish_rate_
Frequency (Hz) of publish on the /clock topic (ROS frequency)
Definition: common.hpp:23
ros::NodeHandle nh_
Node handler.
To publish on the /clock topic at some frequency.
void run_node()
Runs the node.
bool use_sim_time_
Whether to publish something in the /clock topic.
ros::Publisher clock_pub_
Clock publisher.
Class to publish on the /clock topic.
void init_node()
Initialises the node and its parameters.
int main(int argc, char **argv)
Main function called before node initialisation.