dynamic_graph_manager
converter.hh
Go to the documentation of this file.
1 
9 #ifndef DYNAMIC_GRAPH_ROS_CONVERTER_HH
10 #define DYNAMIC_GRAPH_ROS_CONVERTER_HH
11 
12 #include <stdexcept>
13 #include "dg_to_ros.hh"
14 
15 #include <ros/time.h>
16 #include <std_msgs/Header.h>
17 
18 // not used?
19 //# include <LinearMath/btMatrix3x3.h>
20 //# include <LinearMath/btQuaternion.h>
21 
22 #ifdef MAC_OS
23 // On MAC_OS a macro "tolower" when __APPLE__ is defined.
24 // For compatibility reason we need to undefine it as it
25 // interfere with the std::tolower function used in boost/date_time
26 #undef tolower
27 #endif // MAC_OS
28 
29 #include <boost/date_time/date.hpp>
30 #include <boost/date_time/posix_time/posix_time.hpp>
31 #include <boost/static_assert.hpp>
32 
33 #define DG_TO_ROS_IMPL(T) \
34  template <> \
35  inline void converter(DgToRos<T>::ros_t& dst, const DgToRos<T>::dg_t& src)
36 
37 #define ROS_TO_DG_IMPL(T) \
38  template <> \
39  inline void converter(DgToRos<T>::dg_t& dst, const DgToRos<T>::ros_t& src)
40 
41 namespace dynamic_graph
42 {
43 inline void makeHeader(std_msgs::Header& header)
44 {
45  header.seq = 0;
46  header.stamp = ros::Time::now();
47  header.frame_id = "/dynamic_graph/world";
48 }
49 
57 template <typename D, typename S>
58 void converter(D& dst, const S& src);
59 
60 // Double
61 DG_TO_ROS_IMPL(double)
62 {
63  dst.data = src;
64 }
65 
66 ROS_TO_DG_IMPL(double)
67 {
68  dst = src.data;
69 }
70 
71 // Unsigned
72 DG_TO_ROS_IMPL(unsigned int)
73 {
74  dst.data = src;
75 }
76 
77 ROS_TO_DG_IMPL(unsigned int)
78 {
79  dst = src.data;
80 }
81 
82 // Vector
83 DG_TO_ROS_IMPL(Vector)
84 {
85  dst.data.resize(src.size());
86  for (int i = 0; i < src.size(); ++i) dst.data[i] = src(i);
87 }
88 
89 ROS_TO_DG_IMPL(Vector)
90 {
91  dst.resize(src.data.size());
92  for (unsigned int i = 0; i < src.data.size(); ++i) dst(i) = src.data[i];
93 }
94 
95 // Vector3
96 DG_TO_ROS_IMPL(specific::Vector3)
97 {
98  if (src.size() > 0)
99  {
100  dst.x = src(0);
101  if (src.size() > 1)
102  {
103  dst.y = src(1);
104  if (src.size() > 2) dst.z = src(2);
105  }
106  }
107 }
108 
109 ROS_TO_DG_IMPL(specific::Vector3)
110 {
111  dst.resize(3);
112  dst(0) = src.x;
113  dst(1) = src.y;
114  dst(2) = src.z;
115 }
116 
117 // Matrix
118 DG_TO_ROS_IMPL(Matrix)
119 {
120  // TODO: Confirm Ros Matrix Storage order. It changes the RosMatrix to
121  // ColMajor.
122  dst.width = (unsigned int)src.rows();
123  dst.data.resize(src.cols() * src.rows());
124  for (int i = 0; i < src.cols() * src.rows(); ++i)
125  dst.data[i] = src.data()[i];
126 }
127 
128 ROS_TO_DG_IMPL(Matrix)
129 {
130  dst.resize(src.width,
131  (unsigned int)src.data.size() / (unsigned int)src.width);
132  for (unsigned i = 0; i < src.data.size(); ++i) dst.data()[i] = src.data[i];
133 }
134 
135 // Homogeneous matrix.
136 DG_TO_ROS_IMPL(MatrixHomogeneous)
137 {
138  VectorQuaternion q(src.linear());
139  dst.translation.x = src.translation()(0);
140  dst.translation.y = src.translation()(1);
141  dst.translation.z = src.translation()(2);
142 
143  dst.rotation.x = q.x();
144  dst.rotation.y = q.y();
145  dst.rotation.z = q.z();
146  dst.rotation.w = q.w();
147 }
148 
149 ROS_TO_DG_IMPL(MatrixHomogeneous)
150 {
151  VectorQuaternion q(
152  src.rotation.w, src.rotation.x, src.rotation.y, src.rotation.z);
153  dst.linear() = q.matrix();
154 
155  // Copy the translation component.
156  dst.translation()(0) = src.translation.x;
157  dst.translation()(1) = src.translation.y;
158  dst.translation()(2) = src.translation.z;
159 }
160 
161 // Twist.
162 DG_TO_ROS_IMPL(specific::Twist)
163 {
164  if (src.size() != 6)
165  throw std::runtime_error("failed to convert invalid twist");
166  dst.linear.x = src(0);
167  dst.linear.y = src(1);
168  dst.linear.z = src(2);
169  dst.angular.x = src(3);
170  dst.angular.y = src(4);
171  dst.angular.z = src(5);
172 }
173 
174 ROS_TO_DG_IMPL(specific::Twist)
175 {
176  dst.resize(6);
177  dst(0) = src.linear.x;
178  dst(1) = src.linear.y;
179  dst(2) = src.linear.z;
180  dst(3) = src.angular.x;
181  dst(4) = src.angular.y;
182  dst(5) = src.angular.z;
183 }
184 
188 #define DG_BRIDGE_TO_ROS_MAKE_STAMPED_IMPL(T, ATTRIBUTE, EXTRA) \
189  template <> \
190  inline void converter(DgToRos<std::pair<T, Vector> >::ros_t& dst, \
191  const DgToRos<std::pair<T, Vector> >::dg_t& src) \
192  { \
193  makeHeader(dst.header); \
194  converter<DgToRos<T>::ros_t, DgToRos<T>::dg_t>(dst.ATTRIBUTE, src); \
195  do \
196  { \
197  EXTRA \
198  } while (0); \
199  } \
200  struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
201 
202 DG_BRIDGE_TO_ROS_MAKE_STAMPED_IMPL(specific::Vector3, vector, ;);
203 DG_BRIDGE_TO_ROS_MAKE_STAMPED_IMPL(MatrixHomogeneous,
204  transform,
205  dst.child_frame_id = "";);
206 DG_BRIDGE_TO_ROS_MAKE_STAMPED_IMPL(specific::Twist, twist, ;);
207 
214 #define DG_BRIDGE_MAKE_SHPTR_IMPL(T) \
215  template <> \
216  inline void converter( \
217  DgToRos<T>::dg_t& dst, \
218  const boost::shared_ptr<DgToRos<T>::ros_t const>& src) \
219  { \
220  converter<DgToRos<T>::dg_t, DgToRos<T>::ros_t>(dst, *src); \
221  } \
222  struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
223 
225 DG_BRIDGE_MAKE_SHPTR_IMPL(unsigned int);
227 DG_BRIDGE_MAKE_SHPTR_IMPL(specific::Vector3);
229 DG_BRIDGE_MAKE_SHPTR_IMPL(MatrixHomogeneous);
230 DG_BRIDGE_MAKE_SHPTR_IMPL(specific::Twist);
231 
236 #define DG_BRIDGE_MAKE_STAMPED_IMPL(T, ATTRIBUTE, EXTRA) \
237  template <> \
238  inline void converter(DgToRos<std::pair<T, Vector> >::dg_t& dst, \
239  const DgToRos<std::pair<T, Vector> >::ros_t& src) \
240  { \
241  converter<DgToRos<T>::dg_t, DgToRos<T>::ros_t>(dst, src.ATTRIBUTE); \
242  do \
243  { \
244  EXTRA \
245  } while (0); \
246  } \
247  struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
248 
249 DG_BRIDGE_MAKE_STAMPED_IMPL(specific::Vector3, vector, ;);
250 DG_BRIDGE_MAKE_STAMPED_IMPL(MatrixHomogeneous, transform, ;);
251 DG_BRIDGE_MAKE_STAMPED_IMPL(specific::Twist, twist, ;);
252 
257 #define DG_BRIDGE_MAKE_STAMPED_SHPTR_IMPL(T, ATTRIBUTE, EXTRA) \
258  template <> \
259  inline void converter( \
260  DgToRos<std::pair<T, Vector> >::dg_t& dst, \
261  const boost::shared_ptr<DgToRos<std::pair<T, Vector> >::ros_t const>& \
262  src) \
263  { \
264  converter<DgToRos<T>::dg_t, DgToRos<T>::ros_t>(dst, src->ATTRIBUTE); \
265  do \
266  { \
267  EXTRA \
268  } while (0); \
269  } \
270  struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
271 
272 DG_BRIDGE_MAKE_STAMPED_SHPTR_IMPL(specific::Vector3, vector, ;);
273 DG_BRIDGE_MAKE_STAMPED_SHPTR_IMPL(MatrixHomogeneous, transform, ;);
274 DG_BRIDGE_MAKE_STAMPED_SHPTR_IMPL(specific::Twist, twist, ;);
275 
286 template <typename U, typename V>
287 inline void converter(U& dst, V& src)
288 {
289  // This will always fail if instantiated.
290  BOOST_STATIC_ASSERT(sizeof(U) == 0);
291 }
292 
293 typedef boost::posix_time::ptime ptime;
294 typedef boost::posix_time::seconds seconds;
295 typedef boost::posix_time::microseconds microseconds;
296 typedef boost::posix_time::time_duration time_duration;
297 typedef boost::gregorian::date date;
298 
299 boost::posix_time::ptime rosTimeToPtime(const ros::Time& rosTime);
300 
301 ros::Time pTimeToRostime(const boost::posix_time::ptime& time);
302 } // end of namespace dynamic_graph.
303 
304 #endif
#define DG_BRIDGE_MAKE_STAMPED_SHPTR_IMPL(T, ATTRIBUTE, EXTRA)
This macro generates a converter for a shared pointer on a stamped type.
Definition: converter.hh:257
#define DG_BRIDGE_MAKE_STAMPED_IMPL(T, ATTRIBUTE, EXTRA)
This macro generates a converter for a stamped type.
Definition: converter.hh:236
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.
#define DG_BRIDGE_MAKE_SHPTR_IMPL(T)
This macro generates a converter for a shared pointer on a ROS type to a dynamic-graph type...
Definition: converter.hh:214
#define DG_BRIDGE_TO_ROS_MAKE_STAMPED_IMPL(T, ATTRIBUTE, EXTRA)
This macro generates a converter for a stamped type from dynamic-graph to ROS.
Definition: converter.hh:188