17
17
// If not, see <http://www.gnu.org/licenses/>.
18
18
// ========================================================================
19
19
20
- // C++ headers.
21
- #include < string >
20
+ # ifndef ROS_HELPERS_H
21
+ #define ROS_HELPERS_H
22
22
23
- // C++ Library headers.
23
+ # include < string >
24
24
#include " eigen3/Eigen/Core"
25
- #include " geometry_msgs/Point.h"
26
- #include " geometry_msgs/Vector3.h"
27
- #include " ros/ros.h"
28
- #include " std_msgs/ColorRGBA.h"
29
- #include " std_msgs/Header.h"
30
- #include " visualization_msgs/Marker.h"
31
25
32
- // Custom headers.
33
- #include " math/math_util.h"
34
-
35
- #ifndef ROS_HELPERS_H
36
- #define ROS_HELPERS_H
26
+ #ifdef ROS2
27
+ // Use ROS2 headers
28
+ #include " rclcpp/rclcpp.hpp"
29
+ #include " std_msgs/msg/color_rgba.hpp"
30
+ #include " std_msgs/msg/header.hpp"
31
+ #include " geometry_msgs/msg/point.hpp"
32
+ #include " visualization_msgs/msg/marker.hpp"
33
+ #else
34
+ // Use ROS1 headers
35
+ #include " ros/ros.h"
36
+ #include " std_msgs/ColorRGBA.h"
37
+ #include " std_msgs/Header.h"
38
+ #include " geometry_msgs/Point.h"
39
+ #include " visualization_msgs/Marker.h"
40
+ #endif
37
41
38
42
namespace ros_helpers {
39
43
40
- inline void InitRosHeader (const std::string& frame_id,
41
- std_msgs::Header* h) {
44
+ #ifdef ROS2
45
+ // Aliases for ROS2 types.
46
+ using StdColorRGBA = std_msgs::msg::ColorRGBA;
47
+ using StdHeader = std_msgs::msg::Header;
48
+ using GeoPoint = geometry_msgs::msg::Point ;
49
+ using VisMarker = visualization_msgs::msg::Marker;
50
+ #else
51
+ // Aliases for ROS1 types.
52
+ using StdColorRGBA = std_msgs::ColorRGBA;
53
+ using StdHeader = std_msgs::Header;
54
+ using GeoPoint = geometry_msgs::Point ;
55
+ using VisMarker = visualization_msgs::Marker;
56
+ #endif
57
+
58
+ // Initializes a header with the specified frame_id and current time.
59
+ inline void InitRosHeader (const std::string& frame_id, StdHeader* h) {
60
+ #ifdef ROS2
61
+ // Use a temporary clock since we don't have a node pointer here.
62
+ rclcpp::Clock clock ;
63
+ h->stamp = clock .now ();
64
+ #else
65
+ h->stamp = ros::Time::now ();
42
66
h->seq = 0 ;
67
+ #endif
43
68
h->frame_id = frame_id;
44
- h->stamp = ros::Time::now ();
45
69
}
46
70
47
- inline void ClearMarker (visualization_msgs::Marker * m) {
71
+ inline void ClearMarker (VisMarker * m) {
48
72
m->points .clear ();
49
73
m->colors .clear ();
50
74
}
51
75
52
76
template <typename Tr, typename Tg, typename Tb, typename Ta>
53
- std_msgs::ColorRGBA RosColor (const Tr& r,
54
- const Tg& g,
55
- const Tb& b,
56
- const Ta& a) {
57
- std_msgs::ColorRGBA c;
77
+ StdColorRGBA RosColor (const Tr& r, const Tg& g, const Tb& b, const Ta& a) {
78
+ StdColorRGBA c;
58
79
c.r = r;
59
80
c.g = g;
60
81
c.b = b;
@@ -63,29 +84,25 @@ std_msgs::ColorRGBA RosColor(const Tr& r,
63
84
}
64
85
65
86
template <typename Tr, typename Tg, typename Tb, typename Ta, typename RosColor>
66
- void SetRosColor (const Tr& r,
67
- const Tg& g,
68
- const Tb& b,
69
- const Ta& a,
70
- RosColor* c) {
87
+ void SetRosColor (const Tr& r, const Tg& g, const Tb& b, const Ta& a, RosColor* c) {
71
88
c->r = r;
72
89
c->g = g;
73
90
c->b = b;
74
91
c->a = a;
75
92
}
76
93
77
94
template <typename Tx, typename Ty, typename Tz>
78
- geometry_msgs:: Point RosPoint (const Tx& x, const Ty& y, const Tz& z) {
79
- geometry_msgs:: Point p;
95
+ GeoPoint RosPoint (const Tx& x, const Ty& y, const Tz& z) {
96
+ GeoPoint p;
80
97
p.x = x;
81
98
p.y = y;
82
99
p.z = z;
83
100
return p;
84
101
}
85
102
86
103
template <typename Tx, typename Ty>
87
- geometry_msgs:: Point RosPoint (const Tx& x, const Ty& y) {
88
- geometry_msgs:: Point p;
104
+ GeoPoint RosPoint (const Tx& x, const Ty& y) {
105
+ GeoPoint p;
89
106
p.x = x;
90
107
p.y = y;
91
108
p.z = 0 ;
@@ -100,11 +117,7 @@ void SetRosVector(const Tx& x, const Ty& y, const Tz& z, RosVector* v) {
100
117
}
101
118
102
119
template <typename RosVector, typename T>
103
- void SetRosQuaternion (const T& w,
104
- const T& x,
105
- const T& y,
106
- const T& z,
107
- RosVector* q) {
120
+ void SetRosQuaternion (const T& w, const T& x, const T& y, const T& z, RosVector* q) {
108
121
q->w = w;
109
122
q->x = x;
110
123
q->y = y;
@@ -120,17 +133,17 @@ void SetIdentityRosQuaternion(RosVector* q) {
120
133
}
121
134
122
135
template <typename Derived>
123
- geometry_msgs:: Point Eigen3DToRosPoint (const Eigen::DenseBase<Derived>& v) {
124
- geometry_msgs:: Point p;
136
+ GeoPoint Eigen3DToRosPoint (const Eigen::DenseBase<Derived>& v) {
137
+ GeoPoint p;
125
138
p.x = v[0 ];
126
139
p.y = v[1 ];
127
140
p.z = v[2 ];
128
141
return p;
129
142
}
130
143
131
144
template <typename Derived>
132
- geometry_msgs:: Point Eigen2DToRosPoint (const Eigen::DenseBase<Derived>& v) {
133
- geometry_msgs:: Point p;
145
+ GeoPoint Eigen2DToRosPoint (const Eigen::DenseBase<Derived>& v) {
146
+ GeoPoint p;
134
147
p.x = v[0 ];
135
148
p.y = v[1 ];
136
149
p.z = 0 ;
@@ -140,16 +153,16 @@ geometry_msgs::Point Eigen2DToRosPoint(const Eigen::DenseBase<Derived>& v) {
140
153
template <typename Derived>
141
154
void DrawEigen2DLine (const Eigen::DenseBase<Derived>& v1,
142
155
const Eigen::DenseBase<Derived>& v2,
143
- visualization_msgs::Marker * msg) {
156
+ VisMarker * msg) {
144
157
msg->points .push_back (Eigen2DToRosPoint<Derived>(v1));
145
158
msg->points .push_back (Eigen2DToRosPoint<Derived>(v2));
146
159
}
147
160
148
161
template <typename Derived>
149
162
void DrawEigen2DLine (const Eigen::DenseBase<Derived>& v1,
150
163
const Eigen::DenseBase<Derived>& v2,
151
- const std_msgs::ColorRGBA & c,
152
- visualization_msgs::Marker * msg) {
164
+ const StdColorRGBA & c,
165
+ VisMarker * msg) {
153
166
msg->points .push_back (Eigen2DToRosPoint<Derived>(v1));
154
167
msg->points .push_back (Eigen2DToRosPoint<Derived>(v2));
155
168
msg->colors .push_back (c);
@@ -159,9 +172,9 @@ void DrawEigen2DLine(const Eigen::DenseBase<Derived>& v1,
159
172
template <typename Derived>
160
173
void DrawEigen2DLine (const Eigen::DenseBase<Derived>& v1,
161
174
const Eigen::DenseBase<Derived>& v2,
162
- const std_msgs::ColorRGBA & c1,
163
- const std_msgs::ColorRGBA & c2,
164
- visualization_msgs::Marker * msg) {
175
+ const StdColorRGBA & c1,
176
+ const StdColorRGBA & c2,
177
+ VisMarker * msg) {
165
178
msg->points .push_back (Eigen2DToRosPoint<Derived>(v1));
166
179
msg->points .push_back (Eigen2DToRosPoint<Derived>(v2));
167
180
msg->colors .push_back (c1);
@@ -171,8 +184,8 @@ void DrawEigen2DLine(const Eigen::DenseBase<Derived>& v1,
171
184
template <typename Vector2>
172
185
void DrawCross (const Vector2& v,
173
186
const float size,
174
- const std_msgs::ColorRGBA & color,
175
- visualization_msgs::Marker * msg) {
187
+ const StdColorRGBA & color,
188
+ VisMarker * msg) {
176
189
msg->points .push_back (Eigen2DToRosPoint (v - Vector2 (size, size)));
177
190
msg->points .push_back (Eigen2DToRosPoint (v + Vector2 (size, size)));
178
191
msg->points .push_back (Eigen2DToRosPoint (v - Vector2 (size, -size)));
0 commit comments