|
18 | 18 | #include "dynamic_graph_bridge/ros_init.hh"
|
19 | 19 | #include "ros_publish.hh"
|
20 | 20 |
|
| 21 | +#define ENABLE_RT_LOG |
| 22 | +#include <dynamic-graph/real-time-logger.h> |
| 23 | + |
21 | 24 | namespace dynamicgraph
|
22 | 25 | {
|
| 26 | + |
23 | 27 | DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(RosPublish, "RosPublish");
|
24 | 28 | const double RosPublish::ROS_JOINT_STATE_PUBLISHER_RATE = 0.01;
|
25 | 29 |
|
@@ -142,12 +146,22 @@ namespace dynamicgraph
|
142 | 146 | trigger_ (boost::bind (&RosPublish::trigger, this, _1, _2),
|
143 | 147 | sotNOSIGNAL,
|
144 | 148 | MAKE_SIGNAL_STRING(name, true, "int", "trigger")),
|
145 |
| - rate_ (ROS_JOINT_STATE_PUBLISHER_RATE), |
| 149 | + rate_ (0,10000000), |
146 | 150 | nextPublication_ ()
|
147 | 151 | {
|
148 |
| - |
| 152 | + aofs_.open("/tmp/ros_publish.txt"); |
| 153 | + dgADD_OSTREAM_TO_RTLOG (aofs_); |
| 154 | + |
149 | 155 | try {
|
150 |
| - nextPublication_ = ros::Time::now (); |
| 156 | + if (ros::Time::isSimTime()) |
| 157 | + nextPublication_ = ros::Time::now (); |
| 158 | + else |
| 159 | + { |
| 160 | + struct timeval timeofday; |
| 161 | + gettimeofday(&timeofday,NULL); |
| 162 | + nextPublicationRT_.tv_sec = timeofday.tv_sec; |
| 163 | + nextPublicationRT_.tv_usec = timeofday.tv_usec; |
| 164 | + } |
151 | 165 | } catch (const std::exception& exc) {
|
152 | 166 | throw std::runtime_error ("Failed to call ros::Time::now ():" +
|
153 | 167 | std::string (exc.what ()));
|
@@ -201,6 +215,7 @@ namespace dynamicgraph
|
201 | 215 |
|
202 | 216 | RosPublish::~RosPublish ()
|
203 | 217 | {
|
| 218 | + aofs_.close(); |
204 | 219 | }
|
205 | 220 |
|
206 | 221 | void RosPublish::display (std::ostream& os) const
|
@@ -256,13 +271,44 @@ namespace dynamicgraph
|
256 | 271 |
|
257 | 272 | int& RosPublish::trigger (int& dummy, int t)
|
258 | 273 | {
|
| 274 | + |
259 | 275 | typedef std::map<std::string, bindedSignal_t>::iterator iterator_t;
|
| 276 | + ros::Time aTime; |
| 277 | + struct timeval aTimeRT; |
| 278 | + if (ros::Time::isSimTime()) |
| 279 | + { |
| 280 | + aTime= ros::Time::now(); |
| 281 | + dgRTLOG() << "nextPublication_:" |
| 282 | + << " " << nextPublication_.sec |
| 283 | + << " " << nextPublication_.nsec |
| 284 | + << " " << ros::Time::isValid() |
| 285 | + << " " << ros::Time::isSimTime(); |
| 286 | + |
| 287 | + if (aTime <= nextPublication_) |
| 288 | + return dummy; |
| 289 | + |
| 290 | + nextPublication_ = aTime + rate_; |
| 291 | + } |
| 292 | + else |
| 293 | + { |
| 294 | + struct timeval aTimeRT; |
| 295 | + gettimeofday(&aTimeRT,NULL); |
| 296 | + // dgRTLOG() << "nextPublicationRT_:" |
| 297 | + // << " " << nextPublicationRT_.tv_sec |
| 298 | + // << " " << nextPublicationRT_.tv_usec |
| 299 | + // << " " << ros::Time::isValid() |
| 300 | + // << " " << ros::Time::isSimTime() |
| 301 | + // << std::endl; |
| 302 | + nextPublicationRT_.tv_sec = aTimeRT.tv_sec + rate_.sec; |
| 303 | + nextPublicationRT_.tv_usec = aTimeRT.tv_usec + rate_.nsec/1000; |
| 304 | + if (nextPublicationRT_.tv_usec>1000000) |
| 305 | + { |
| 306 | + nextPublicationRT_.tv_usec-=1000000; |
| 307 | + nextPublicationRT_.tv_sec+=1; |
| 308 | + } |
| 309 | + } |
260 | 310 |
|
261 |
| - if (ros::Time::now() <= nextPublication_) |
262 |
| - return dummy; |
263 |
| - |
264 |
| - nextPublication_ = ros::Time::now() + rate_; |
265 |
| - |
| 311 | + // dgRTLOG() << "after nextPublication_" << std::endl; |
266 | 312 | boost::mutex::scoped_lock lock (mutex_);
|
267 | 313 |
|
268 | 314 | for (iterator_t it = bindedSignal_.begin ();
|
|
0 commit comments