emulation_clock.cpp
Go to the documentation of this file.
1 #include <ctime>
2 #include <boost/asio.hpp>
3 #include <boost/bind.hpp>
4 #include <boost/date_time/posix_time/posix_time.hpp>
5 
6 #include <ros/ros.h>
7 #include <rosgraph_msgs/Clock.h>
8 
9 
11 {
12 public:
13  EmulationClock(boost::asio::io_service& io, int dt_ms, double time_factor)
14  : dt_ms_(dt_ms),
15  time_factor_(time_factor),
16  timer_(io, boost::posix_time::milliseconds(dt_ms))
17  {
18  t_ = (double)std::time(NULL);
19  pub_ = nh_.advertise<rosgraph_msgs::Clock>("/clock", 1);
20  timer_.async_wait(boost::bind(&EmulationClock::timer_cb, this));
21  }
22 
23  void timer_cb()
24  {
25  t_ += time_factor_*dt_ms_/1000;
26  ROS_DEBUG_STREAM("WALL: "<<(double)std::time(NULL)<<"; ROS: "<<t_<<"; dt_ms: "<<dt_ms_<<"; time_factor: "<<time_factor_);
27  rosgraph_msgs::Clock msg;
28  msg.clock = ros::Time(t_);
29  pub_.publish(msg);
30 
31  timer_.expires_at(timer_.expires_at() + boost::posix_time::milliseconds(dt_ms_));
32  timer_.async_wait(boost::bind(&EmulationClock::timer_cb, this));
33  }
34 
37 
38  int dt_ms_;
39  double time_factor_;
40  double t_;
41 
42 private:
43  boost::asio::deadline_timer timer_;
44 };
45 
46 int main(int argc, char **argv)
47 {
48  ros::init(argc, argv, "emulation_clock");
49  ros::NodeHandle nh;
50 
51  int dt_ms;
52  double time_factor;
53  nh.param<int>("/emulation_dt_ms", dt_ms, 10);
54  nh.param<double>("/emulation_time_factor", time_factor, 1.0);
55 
56  boost::asio::io_service io;
57  EmulationClock ec(io, dt_ms, time_factor);
58  io.run();
59 
60  return 0;
61 }
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
EmulationClock(boost::asio::io_service &io, int dt_ms, double time_factor)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
boost::asio::deadline_timer timer_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_DEBUG_STREAM(args)
ros::NodeHandle nh_
ros::Publisher pub_


cob_hardware_emulation
Author(s): Florian Weisshardt
autogenerated on Thu Apr 8 2021 02:39:41