Skip to content

Commit d80dd4d

Browse files
Add gettimeofday for time computation.
Revert "Remove useless thread to start and stop dynamic graph" This reverts commit 924c301.
1 parent c4b21d4 commit d80dd4d

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

src/ros_publish.cpp

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
#include "dynamic_graph_bridge/ros_init.hh"
1919
#include "ros_publish.hh"
2020

21+
#define ENABLE_RT_LOG
22+
#include <dynamic-graph/real-time-logger.h>
23+
2124
namespace dynamicgraph
2225
{
26+
2327
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(RosPublish, "RosPublish");
2428
const double RosPublish::ROS_JOINT_STATE_PUBLISHER_RATE = 0.01;
2529

@@ -142,12 +146,22 @@ namespace dynamicgraph
142146
trigger_ (boost::bind (&RosPublish::trigger, this, _1, _2),
143147
sotNOSIGNAL,
144148
MAKE_SIGNAL_STRING(name, true, "int", "trigger")),
145-
rate_ (ROS_JOINT_STATE_PUBLISHER_RATE),
149+
rate_ (0,10000000),
146150
nextPublication_ ()
147151
{
148-
152+
aofs_.open("/tmp/ros_publish.txt");
153+
dgADD_OSTREAM_TO_RTLOG (aofs_);
154+
149155
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+
}
151165
} catch (const std::exception& exc) {
152166
throw std::runtime_error ("Failed to call ros::Time::now ():" +
153167
std::string (exc.what ()));
@@ -201,6 +215,7 @@ namespace dynamicgraph
201215

202216
RosPublish::~RosPublish ()
203217
{
218+
aofs_.close();
204219
}
205220

206221
void RosPublish::display (std::ostream& os) const
@@ -256,13 +271,44 @@ namespace dynamicgraph
256271

257272
int& RosPublish::trigger (int& dummy, int t)
258273
{
274+
259275
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+
}
260310

261-
if (ros::Time::now() <= nextPublication_)
262-
return dummy;
263-
264-
nextPublication_ = ros::Time::now() + rate_;
265-
311+
// dgRTLOG() << "after nextPublication_" << std::endl;
266312
boost::mutex::scoped_lock lock (mutex_);
267313

268314
for (iterator_t it = bindedSignal_.begin ();

src/ros_publish.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
# include "converter.hh"
1818
# include "sot_to_ros.hh"
19+
# include <fstream>
1920

2021
namespace dynamicgraph
2122
{
@@ -95,6 +96,8 @@ namespace dynamicgraph
9596
ros::Duration rate_;
9697
ros::Time nextPublication_;
9798
boost::mutex mutex_;
99+
std::ofstream aofs_;
100+
struct timeval nextPublicationRT_;
98101
};
99102
} // end of namespace dynamicgraph.
100103

0 commit comments

Comments
 (0)