/
topic_buffer_client.cpp
278 lines (246 loc) · 8.8 KB
/
topic_buffer_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <cstdio>
#include <vector>
#include <list>
#include <boost/lambda/lambda.hpp>
#include "ros/ros.h"
#include "ros/header.h"
#include "ros/console.h"
#include "std_msgs/Header.h"
#include "std_msgs/String.h"
#include "jsk_topic_tools/List.h"
#include "jsk_topic_tools/Update.h"
#include "topic_tools/shape_shifter.h"
#include "topic_tools/parse.h"
using std::string;
using std::vector;
using std::list;
using namespace topic_tools;
static bool debug;
class pub_info_t
{
public:
std::string topic_name;
ros::Publisher pub;
ros::Subscriber *sub;
bool advertised;
boost::shared_ptr<ShapeShifter > msg;
ros::Timer timer;
ros::Duration rate;
ros::Time last_time_received;
bool topic_with_header;
bool latched;
uint32_t last_seq_received;
uint32_t topic_received;
void publish(const ros::TimerEvent &event)
{
if ( advertised == false ) return;
topic_received++;
if ( topic_with_header == true ) {
std_msgs::Header header;
uint8_t buf[msg->size()];
ros::serialization::OStream ostream(buf, msg->size());
ros::serialization::IStream istream(buf, msg->size());
msg->write(ostream);
((uint32_t *)buf)[0] = last_seq_received + topic_received;
ros::Time tmp(last_time_received.toSec() + topic_received * rate.toSec());
((uint32_t *)buf)[1] = tmp.sec;
((uint32_t *)buf)[2] = tmp.nsec;
msg->read(istream);
}
if(debug){
ROS_INFO_STREAM("publishing " << topic_name);
}
pub.publish(msg);
}
};
typedef boost::shared_ptr<pub_info_t> pub_info_ref;
static list<pub_info_ref> g_pubs;
static ros::NodeHandle *g_node = NULL;
static bool use_fixed_rate;
static bool use_periodic_rate = false;
void in_cb(const boost::shared_ptr<ShapeShifter const>& msg,
boost::shared_ptr<pub_info_t> s)
{
using namespace boost::lambda;
s->msg = boost::const_pointer_cast<ShapeShifter>(msg);
s->topic_received = 0; // reset topic_received
if ( s->advertised == false ) {
s->pub = msg->advertise(*g_node, s->topic_name+string("_buffered"), 10, s->latched);
s->advertised = true;
}
if(debug){
ROS_INFO_STREAM("advertised as " << s->topic_name+string("_buffered") << " running at " << 1/(s->rate.toSec()) << "Hz");
}
// check if msg has header
{
std_msgs::Header header;
uint8_t buf[msg->size()];
ros::serialization::OStream stream(buf, msg->size());
msg->write(stream);
header.seq = ((uint32_t *)buf)[0];
header.stamp.sec = ((uint32_t *)buf)[1];
header.stamp.nsec = ((uint32_t *)buf)[2];
if ( abs((header.stamp - ros::Time::now()).toSec()) < 5.0 ) {
if(debug){
ROS_INFO_STREAM(" this message contains headers.. seq =" << header.seq << " stamp = " << header.stamp);
}
s->topic_with_header = true;
s->last_seq_received = header.seq;
s->last_time_received = header.stamp;
}
}
//g_node->createTimer(ros::Duration(0.1), [](const ros::TimerEvent event) { std::cerr << "hoge" << std::endl; });
{// at first publish once
ros::TimerEvent ev;
s->publish (ev);
}
s->timer = g_node->createTimer(s->rate, &pub_info_t::publish, s);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "topic_buffer_client", ros::init_options::AnonymousName);
ros::NodeHandle n;
ros::NodeHandle nh("~");
double fixed_rate = 0.1; // 10Hz
if (nh.hasParam("fixed_rate")) {
use_fixed_rate = true;
nh.param ("fixed_rate", fixed_rate, 0.1);
ROS_INFO("use fixed rate = %f", fixed_rate);
}
double update_rate = 10; // 0.1Hz
if (nh.hasParam("update_rate")) {
nh.param ("update_rate", update_rate, 10.0);
ROS_INFO("use update rate = %f", update_rate);
}
double periodic_rate = 0.1; // 10Hz
if (nh.hasParam("periodic_rate")) {
use_periodic_rate = true;
nh.param ("periodic_rate", periodic_rate, 0.1);
ROS_INFO("use periodic rate = %f", periodic_rate);
}
bool latched;
if (nh.hasParam("latched")) {
nh.param ("latched", latched, false);
if(latched) {
ROS_INFO("use latched");
}
}
g_node = &n;
bool use_service_p = false;
std::vector<std::string> target_topics;
// use service or parameter
// check the parameter first
XmlRpc::XmlRpcValue topics;
if (!nh.getParam ("topics", topics)) {
ROS_WARN("no ~topics is available, use service interface");
use_service_p = true;
}
else {
switch (topics.getType ()) {
case XmlRpc::XmlRpcValue::TypeArray: {
for (int d = 0; d < topics.size(); ++d) {
target_topics.push_back((std::string)(topics[d]));
}
break;
}
default: {
ROS_WARN("~topics mismatches type, it should be a list, use service interface");
use_service_p = true;
}
}
}
if (use_service_p) {
target_topics.clear();
// New service
ros::service::waitForService(string("/list"), -1);
ros::ServiceClient sc_list = n.serviceClient<jsk_topic_tools::List>(string("/list"), true);
jsk_topic_tools::List::Request req;
jsk_topic_tools::List::Response res;
ROS_INFO_STREAM("calling " << sc_list.getService());
while ( sc_list.call(req, res) == false) {
ROS_WARN_STREAM("calling " << sc_list.getService() << " fails, retry...");
ros::Duration(1).sleep();
}
ROS_WARN_STREAM("calling /list success!!!");
for(vector<std::string>::iterator it = res.topic_names.begin(); it != res.topic_names.end(); ++it) {
target_topics.push_back(*it);
}
ROS_INFO_STREAM("calling /list has done.. found " << res.topic_names.size() << " topics to publish");
}
for(size_t i = 0; i < target_topics.size(); i++) {
boost::shared_ptr<pub_info_t> pub_info(new pub_info_t);
pub_info->topic_name = target_topics[i];
if (use_fixed_rate) {
pub_info->rate = ros::Duration(fixed_rate);
}
pub_info->latched = latched;
pub_info->advertised = false;
pub_info->topic_with_header = false;
ROS_INFO_STREAM("subscribe " << pub_info->topic_name+string("_update"));
pub_info->sub = new ros::Subscriber(n.subscribe<ShapeShifter>(pub_info->topic_name+string("_update"), 10,
#if __cplusplus < 201400L
boost::bind(in_cb, _1, pub_info)
#else
[pub_info](auto& msg){ in_cb(msg, pub_info); }
#endif
));
g_pubs.push_back(pub_info);
}
ros::Rate rate_loop(100);
ros::Time last_updated;
bool use_service = true;
nh.param("use_service", use_service, true);
nh.param("debug", debug, false);
ros::ServiceClient sc_update = n.serviceClient<jsk_topic_tools::Update>(string("/update"), true);
ros::Publisher pub_update;
if (!use_service) {
pub_update = n.advertise<std_msgs::String>("/update", 1);
}
if (use_periodic_rate) {
for (list<pub_info_ref>::iterator it = g_pubs.begin();
it != g_pubs.end();
++it) {
jsk_topic_tools::Update::Request req;
jsk_topic_tools::Update::Response res;
req.topic_name = (*it)->topic_name;
req.periodic = true;
req.periodic_rate = ros::Duration(periodic_rate);
sc_update.call(req, res);
ROS_INFO_STREAM("sending request for periodic rate publish topic:" << req.topic_name << " rate:" << req.periodic_rate);
}
}
while ( ros::ok() ) {
if ( update_rate >= 0 && (ros::Time::now() - last_updated) > ros::Duration(update_rate) ) {
for (list<pub_info_ref>::iterator it = g_pubs.begin();
it != g_pubs.end();
++it) {
if (use_service) {
jsk_topic_tools::Update::Request req;
jsk_topic_tools::Update::Response res;
req.topic_name = (*it)->topic_name;
if ( sc_update.call(req, res) == false ) {
ROS_ERROR_STREAM("calling /update (" << req.topic_name << ") fails, retry...");
sc_update = n.serviceClient<jsk_topic_tools::Update>(string("/update"), true);
continue;
}
(*it)->rate = ros::Duration(res.rate);
if(debug){
ROS_INFO_STREAM("calling /update " << req.topic_name << " .. " << res.rate);
}
}
else {
std_msgs::String msg;
msg.data = (*it)->topic_name;
pub_update.publish(msg);
if(debug){
ROS_INFO_STREAM("publishing /update " << msg.data);
}
}
}
last_updated = ros::Time::now();
}
ros::spinOnce();
rate_loop.sleep();
}
return 0;
}