sim_time.cpp
Go to the documentation of this file.
00001 /***************************************************************************/ 
00037 #include "ros/ros.h"
00038 #include "rosgraph_msgs/Clock.h"
00039 
00040 #include <sys/time.h>
00041 
00042 #define SIM_TIME_INCREMENT_US 10000
00043 
00044 /*
00045  * This node publishes increments of 1ms in time to the /clock topic. It does so
00046  * at a rate determined by sim_speedup (simulation speedup factor), which should
00047  * be passed
00048  * in as a private parameter.
00049  */
00050 
00051 int main(int argc, char** argv)
00052 {
00053   ros::init(argc, argv, "sim_time_source");
00054   ros::NodeHandle sim_time_node;
00055 
00056   // support integral multiples of wallclock time for simulation speedup
00057   int sim_speedup;  // integral factor by which to speed up simulation
00058   ros::NodeHandle node_priv("~");
00059   node_priv.param<int>("sim_speedup", sim_speedup, 1);
00060 
00061   // get the current time & populate sim_time with it
00062   struct timeval start;
00063   int rv = gettimeofday(&start, NULL);
00064   usleep(1000);
00065   struct timeval now;
00066   rv = gettimeofday(&now, NULL);
00067   if (0 != rv)
00068   {
00069     ROS_ERROR("Invalid return from gettimeofday: %d", rv);
00070     return -1;
00071   }
00072 
00073   rosgraph_msgs::Clock sim_time;
00074   sim_time.clock.sec = now.tv_sec - start.tv_sec;
00075   sim_time.clock.nsec = now.tv_usec * 1000;
00076   ros::Publisher sim_time_pub = sim_time_node.advertise<rosgraph_msgs::Clock>("clock", 1);
00077 
00078   ROS_INFO("Starting simulation time publisher at time: %d.%d", sim_time.clock.sec, sim_time.clock.nsec);
00079 
00080   while (ros::ok())
00081   {
00082     sim_time_pub.publish(sim_time);
00083 
00084     sim_time.clock.nsec = sim_time.clock.nsec + SIM_TIME_INCREMENT_US * 1000;
00085     while (sim_time.clock.nsec > 1000000000)
00086     {
00087       sim_time.clock.nsec -= 1000000000;
00088       ++sim_time.clock.sec;
00089     }
00090 
00091     usleep(SIM_TIME_INCREMENT_US / sim_speedup);
00092     ros::spinOnce();
00093   }
00094 }


pid
Author(s): Andy Zelenak , Paul Bouchier
autogenerated on Mon Apr 15 2019 02:40:47