dynamic_graph_manager
ros_subscribe.hxx
Go to the documentation of this file.
1 
10 #ifndef DYNAMIC_GRAPH_ROS_SUBSCRIBE_HXX
11 #define DYNAMIC_GRAPH_ROS_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>
18 #include <vector>
19 #include "dynamic_graph_manager/Matrix.h"
20 #include "dynamic_graph_manager/Vector.h"
21 #include "ros_time.hh"
22 
23 namespace dg = dynamicgraph;
24 
25 namespace dynamic_graph
26 {
27 template <typename R, typename S>
28 void RosSubscribe::callback(
29  boost::shared_ptr<dynamicgraph::SignalPtr<S, int> > signal, const R& data)
30 {
31  typedef S dg_t;
32  dg_t value;
33  converter(value, data);
34  signal->setConstant(value);
35 }
36 
37 template <typename R>
38 void RosSubscribe::callbackTimestamp(
39  boost::shared_ptr<dynamicgraph::SignalPtr<ptime, int> > signal,
40  const R& data)
41 {
42  ptime time = rosTimeToPtime(data->header.stamp);
43  signal->setConstant(time);
44 }
45 
46 namespace internal
47 {
48 template <typename T>
49 struct Add
50 {
51  void operator()(RosSubscribe& RosSubscribe,
52  const std::string& signal,
53  const std::string& topic)
54  {
55  typedef typename DgToRos<T>::dg_t dg_t;
56  typedef typename DgToRos<T>::ros_const_ptr_t ros_const_ptr_t;
57  typedef typename DgToRos<T>::signalIn_t signal_t;
58 
59  // Initialize the bindedSignal object.
60  RosSubscribe::bindedSignal_t bindedSignal;
61 
62  // Initialize the signal.
63  boost::format signalName("RosSubscribe(%1%)::%2%");
64  signalName % RosSubscribe.getName() % signal;
65 
66  boost::shared_ptr<signal_t> signal_(new signal_t(0, signalName.str()));
67  DgToRos<T>::setDefault(*signal_);
68  bindedSignal.first = signal_;
69  RosSubscribe.signalRegistration(*bindedSignal.first);
70 
71  // Initialize the subscriber.
72  typedef boost::function<void(const ros_const_ptr_t& data)> callback_t;
73  callback_t callback =
74  boost::bind(&RosSubscribe::callback<ros_const_ptr_t, dg_t>,
75  &RosSubscribe,
76  signal_,
77  _1);
78 
79  bindedSignal.second = boost::make_shared<ros::Subscriber>(
80  RosSubscribe.nh().subscribe(topic, 1, callback));
81 
82  RosSubscribe.bindedSignal()[signal] = bindedSignal;
83  }
84 };
85 
86 template <typename T>
87 struct Add<std::pair<T, dg::Vector> >
88 {
89  void operator()(RosSubscribe& RosSubscribe,
90  const std::string& signal,
91  const std::string& topic)
92  {
93  typedef std::pair<T, dg::Vector> type_t;
94 
95  typedef typename DgToRos<type_t>::dg_t dg_t;
96  typedef typename DgToRos<type_t>::ros_const_ptr_t ros_const_ptr_t;
97  typedef typename DgToRos<type_t>::signalIn_t signal_t;
98 
99  // Initialize the bindedSignal object.
100  RosSubscribe::bindedSignal_t bindedSignal;
101 
102  // Initialize the signal.
103  boost::format signalName("RosSubscribe(%1%)::%2%");
104  signalName % RosSubscribe.getName() % signal;
105 
106  boost::shared_ptr<signal_t> signal_(new signal_t(0, signalName.str()));
107  DgToRos<T>::setDefault(*signal_);
108  bindedSignal.first = signal_;
109  RosSubscribe.signalRegistration(*bindedSignal.first);
110 
111  // Initialize the publisher.
112  typedef boost::function<void(const ros_const_ptr_t& data)> callback_t;
113  callback_t callback =
114  boost::bind(&RosSubscribe::callback<ros_const_ptr_t, dg_t>,
115  &RosSubscribe,
116  signal_,
117  _1);
118 
119  bindedSignal.second = boost::make_shared<ros::Subscriber>(
120  RosSubscribe.nh().subscribe(topic, 1, callback));
121 
122  RosSubscribe.bindedSignal()[signal] = bindedSignal;
123 
124  // Timestamp.
125  typedef dynamicgraph::SignalPtr<RosSubscribe::ptime, int>
126  signalTimestamp_t;
127  std::string signalTimestamp =
128  (boost::format("%1%%2%") % signal % "Timestamp").str();
129 
130  // Initialize the bindedSignal object.
131  RosSubscribe::bindedSignal_t bindedSignalTimestamp;
132 
133  // Initialize the signal.
134  boost::format signalNameTimestamp("RosSubscribe(%1%)::%2%");
135  signalNameTimestamp % RosSubscribe.name % signalTimestamp;
136 
137  boost::shared_ptr<signalTimestamp_t> signalTimestamp_(
138  new signalTimestamp_t(0, signalNameTimestamp.str()));
139 
140  RosSubscribe::ptime zero(rosTimeToPtime(ros::Time(0, 0)));
141  signalTimestamp_->setConstant(zero);
142  bindedSignalTimestamp.first = signalTimestamp_;
143  RosSubscribe.signalRegistration(*bindedSignalTimestamp.first);
144 
145  // Initialize the publisher.
146  typedef boost::function<void(const ros_const_ptr_t& data)> callback_t;
147  callback_t callbackTimestamp =
148  boost::bind(&RosSubscribe::callbackTimestamp<ros_const_ptr_t>,
149  &RosSubscribe,
150  signalTimestamp_,
151  _1);
152 
153  bindedSignalTimestamp.second = boost::make_shared<ros::Subscriber>(
154  RosSubscribe.nh().subscribe(topic, 1, callbackTimestamp));
155 
156  RosSubscribe.bindedSignal()[signalTimestamp] = bindedSignalTimestamp;
157  }
158 };
159 } // end of namespace internal.
160 
161 template <typename T>
162 void RosSubscribe::add(const std::string& signal, const std::string& topic)
163 {
164  internal::Add<T>()(*this, signal, topic);
165 }
166 } // namespace dynamic_graph
167 
168 #endif
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
Definition: ros_queued_subscribe.hh:66
DgToRos trait type.
Definition: dg_to_ros.hh:64
void converter(D &dst, const S &src)
Handle ROS <-> dynamic-graph conversion.
Definition of the command.
Definition: ros_robot_state_publisher.hpp:164