Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a demo of content filter listener #557

Merged
merged 10 commits into from
May 2, 2022
Prev Previous commit
Next Next commit
use = instead of like
Signed-off-by: Chen Lihui <[email protected]>
Chen Lihui committed Apr 13, 2022
commit ce4131709b5fcb23610863ba85f6fcd2e1e58a7b
16 changes: 7 additions & 9 deletions demo_nodes_cpp/src/topics/listener_content_filter.cpp
Original file line number Diff line number Diff line change
@@ -30,25 +30,23 @@ class ListenerContentFilter : public rclcpp::Node
explicit ListenerContentFilter(const rclcpp::NodeOptions & options)
: Node("content_filter_listener", options)
{
// Create a callback function for when messages are received.
// Variations of this function also exist using, for example UniquePtr for zero-copy transport.
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
// Create a callback function for when messages are received.
auto callback =
[this](std_msgs::msg::String::ConstSharedPtr msg) -> void
[this](const std_msgs::msg::String & msg) -> void
{
RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg->data.c_str());
RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg.data.c_str());
};
// Create a subscription to the topic which can be matched with one or more compatible ROS
// publishers.
// Note that not all publishers on the same topic with the same type will be compatible:
// they must have compatible Quality of Service policies.

// Initialize a subscription with a content filter to receive messages that contain a prefix
// with 'Hello World: 1', which is similar to the SQL statement `data like 'Hello World: 1%'`.
// NOTE: rmw_connextdds does not support `like` operation currently.
// Initialize a subscription with a content filter to receive messages that are
// 'Hello World: 10'.
rclcpp::SubscriptionOptions sub_options;
sub_options.content_filter_options.filter_expression = "data like %0";
sub_options.content_filter_options.expression_parameters = {"'Hello World: 1%'"};
sub_options.content_filter_options.filter_expression = "data = %0";
sub_options.content_filter_options.expression_parameters = {"'Hello World: 10'"};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the number (currently 10) could be configurable by the user.
Maybe that makes the demo more interesting (?).
Feel free to ignore the comment though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually agree with this comment. probably string chatter example is interesting enough to show this feature. how about filtering specific range of data? that would be one of the most common case?


sub_ = create_subscription<std_msgs::msg::String>("chatter", 10, callback, sub_options);

wjwwood marked this conversation as resolved.
Show resolved Hide resolved