2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
#include < chrono>
5
+ #include < geometry_msgs/msg/point.hpp>
6
+ #include < geometry_msgs/msg/point32.hpp>
5
7
#include < rclcpp/rclcpp.hpp>
6
8
#include < ros_babel_fish/babel_fish.hpp>
7
9
#include < ros_babel_fish/macros.hpp>
8
- #include < thread>
9
- #include < utility>
10
10
11
11
/* !
12
12
* The following example demonstrates how to subscribe to a topic of any type,
@@ -28,9 +28,12 @@ class AnySubscriber : public rclcpp::Node
28
28
void init ()
29
29
{
30
30
fish = ros_babel_fish::BabelFish::make_shared ();
31
+ RCLCPP_INFO ( this ->get_logger (), " Started. Waiting for topic '%s' to become available." ,
32
+ topic_.c_str () );
31
33
subscription_ = fish->create_subscription (
32
34
*this , topic_, 10 ,
33
35
[this ]( ros_babel_fish::CompoundMessage::SharedPtr msg ) { topic_callback ( msg ); } );
36
+ RCLCPP_INFO ( this ->get_logger (), " Subscribed to topic '%s'" , topic_.c_str () );
34
37
}
35
38
36
39
private:
@@ -43,7 +46,7 @@ class AnySubscriber : public rclcpp::Node
43
46
try {
44
47
std::cout << " Type: " << msg->name () << std::endl;
45
48
dumpMessageContent ( *msg );
46
- std::cout << std::endl << " ---" << std::endl;
49
+ std::cout << " ---" << std::endl;
47
50
} catch ( ros_babel_fish::BabelFishException &ex ) {
48
51
RCLCPP_ERROR ( this ->get_logger (), " Got a BabelFishException during translation: %s" , ex.what () );
49
52
}
@@ -70,6 +73,24 @@ int main( int argc, char *argv[] )
70
73
}
71
74
72
75
// Here's where the dumping happens
76
+ void printPoint ( const ros_babel_fish::CompoundMessage &point )
77
+ {
78
+ // This method demonstrates how to get a known message type out of a CompoundMessage
79
+ // Note that this is only possible if the CompoundMessage actually contains a message of the given type
80
+ // Otherwise, a BabelFishException will be thrown
81
+ // Also, this message is a reference to the original message, so any modifications to either will affect the other
82
+ auto point_msg = point.message <geometry_msgs::msg::Point>();
83
+ std::cout << " Point { x: " << point_msg->x << " , y: " << point_msg->y << " , z: " << point_msg->z
84
+ << " }" << std::endl;
85
+ }
86
+
87
+ void printPoint32 ( const ros_babel_fish::CompoundMessage &point )
88
+ {
89
+ // Again for Point32 so it can be tested with geometry_msgs/msg/Polygon
90
+ auto point_msg = point.message <geometry_msgs::msg::Point32>();
91
+ std::cout << " Point { x: " << point_msg->x << " , y: " << point_msg->y << " , z: " << point_msg->z
92
+ << " }" << std::endl;
93
+ }
73
94
74
95
template <typename T>
75
96
void printToStdOut ( const T &val )
@@ -134,20 +155,27 @@ void dumpMessageContent( const ros_babel_fish::Message &message, const std::stri
134
155
// We can either just dump the message or demonstrate the time conversion
135
156
if ( compound.isTime () ) {
136
157
rclcpp::Time time = compound.value <rclcpp::Time>();
137
- std::cout << " Time { seconds: " << time.seconds () << " }" ;
158
+ std::cout << " Time { seconds: " << time.seconds () << " }" << std::endl ;
138
159
return ;
139
160
}
140
161
if ( compound.isDuration () ) {
141
162
auto duration = compound.value <rclcpp::Duration>();
142
- std::cout << " Duration { seconds: " << duration.seconds () << " }" ;
163
+ std::cout << " Duration { seconds: " << duration.seconds () << " }" << std::endl;
164
+ return ;
165
+ }
166
+ if ( compound.name () == " geometry_msgs/msg/Point" ) {
167
+ printPoint ( compound );
168
+ return ;
169
+ }
170
+ if ( compound.name () == " geometry_msgs/msg/Point32" ) {
171
+ printPoint32 ( compound );
143
172
return ;
144
173
}
145
174
std::cout << std::endl;
146
175
for ( size_t i = 0 ; i < compound.keys ().size (); ++i ) {
147
176
std::cout << prefix << compound.keys ()[i] << " : " ;
148
177
dumpMessageContent ( *compound.values ()[i], prefix + " " );
149
- if ( i != compound.keys ().size () - 1 )
150
- std::cout << std::endl;
178
+ std::cout << std::endl;
151
179
}
152
180
} else if ( message.type () == MessageTypes::Array ) {
153
181
auto &base = message.as <ArrayMessageBase>();
0 commit comments