10 #ifndef DYNAMIC_GRAPH_ROS_QUEUED_SUBSCRIBE_HXX 11 #define DYNAMIC_GRAPH_ROS_QUEUED_SUBSCRIBE_HXX 12 #include <dynamic-graph/linear-algebra.h> 13 #include <dynamic-graph/signal-cast-helper.h> 14 #include <dynamic-graph/signal-caster.h> 15 #include <std_msgs/Float64.h> 16 #include <boost/bind.hpp> 17 #include <boost/date_time/posix_time/posix_time.hpp> 19 #include "dynamic_graph_manager/Matrix.h" 20 #include "dynamic_graph_manager/Vector.h" 23 typedef boost::mutex::scoped_lock scoped_lock;
29 static const int BUFFER_SIZE = 1 << 10;
34 void operator()(RosQueuedSubscribe& rosSubscribe,
35 const std::string& type,
36 const std::string& signal,
37 const std::string& topic)
39 typedef typename DgToRos<T>::dg_t dg_t;
40 typedef typename DgToRos<T>::ros_const_ptr_t ros_const_ptr_t;
41 typedef BindedSignal<dg_t, BUFFER_SIZE> BindedSignal_t;
42 typedef typename BindedSignal_t::Signal_t Signal_t;
45 BindedSignal_t* bs =
new BindedSignal_t(&rosSubscribe);
46 DgToRos<T>::setDefault(bs->last);
49 boost::format signalName(
"RosQueuedSubscribe(%1%)::output(%2%)::%3%");
50 signalName % rosSubscribe.getName() % type % signal;
52 bs->signal.reset(
new Signal_t(signalName.str()));
53 bs->signal->setFunction(
54 boost::bind(&BindedSignal_t::reader, bs, _1, _2));
55 rosSubscribe.signalRegistration(*bs->signal);
58 typedef boost::function<void(const ros_const_ptr_t& data)>
callback_t;
59 callback_t callback = boost::bind(
60 &BindedSignal_t::template writer<ros_const_ptr_t>, bs, _1);
64 bs->subscriber = boost::make_shared<ros::Subscriber>(
65 rosSubscribe.nh().subscribe(topic, BUFFER_SIZE, callback));
67 RosQueuedSubscribe::bindedSignal_t bindedSignal(bs);
68 rosSubscribe.bindedSignal()[signal] = bindedSignal;
73 template <
typename T,
int N>
75 void BindedSignal<T, N>::writer(
const R& data)
78 boost::mutex::scoped_lock lock(wmutex);
82 frontIdx = (frontIdx + 1) % N;
92 last = buffer[backIdx];
95 backIdx = (backIdx + 1) % N;
98 template <
typename T,
int N>
99 T& BindedSignal<T, N>::reader(T& data,
int time)
103 scoped_lock lock(rmutex, boost::try_to_lock);
104 if (!lock.owns_lock() || entity->readQueue() == -1 ||
105 time < entity->readQueue())
115 data = buffer[frontIdx];
116 frontIdx = (frontIdx + 1) % N;
124 template <
typename T>
125 void RosQueuedSubscribe::add(
const std::string& type,
126 const std::string& signal,
127 const std::string& topic)
129 internal::Add<T>()(*
this, type, signal, topic);
boost::function< void(int)> callback_t
Simple shortcut for code writing convenience.
Definition: ros_robot_state_publisher.hpp:43
this is this package namespace in order to avoid naming conflict
Definition: device.hh:22
void converter(D &dst, const S &src)
Handle ROS <-> dynamic-graph conversion.
Definition of the command.
Definition: ros_robot_state_publisher.hpp:164