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 matched event demo for rclcpp and rclpy #607

Merged
merged 8 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions demo_nodes_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ custom_executable(parameters parameter_events)
custom_executable(parameters parameter_event_handler)
custom_executable(parameters set_and_get_parameters_async)

# Tutorials of events
custom_executable(events matched_event_detect)

add_library(timers_library SHARED
src/timers/one_off_timer.cpp
src/timers/reuse_timer.cpp)
Expand Down Expand Up @@ -175,6 +178,7 @@ if(BUILD_TESTING)
parameter_events
set_and_get_parameters_async
set_and_get_parameters
matched_event_detect
"talker:listener")
set(service_tutorial_tests
"add_two_ints_server:add_two_ints_client"
Expand Down
28 changes: 28 additions & 0 deletions demo_nodes_cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This package consists of the following examples:
22. `listener`
23. `parameter_events_async`
24. `listener_best_effort`
25. `matched_event_detect`

## **Build**

Expand Down Expand Up @@ -274,6 +275,18 @@ This runs `loaned_message_talker` ROS 2 node that publishes unique messages whic
ros2 run demo_nodes_cpp talker_loaned_message
```

### Matched Event Detect

This runs 3 ROS2 nodes.
`matched_event_detect_node` node that set matched event callback for publisher and subscription separately to output connection or disconnection information.
`multi_sub_node` create/destroy subscriptions which connect the publisher of `matched_event_detect_node`.
`multi_pub_node` create/destroy publishers which connect the subscription of `matched_event_detect_node`.

```bash
# Open new terminal
ros2 run demo_nodes_cpp matched_event_detect
```

## **Verify**

### Basic Talker & Listener
Expand Down Expand Up @@ -671,6 +684,21 @@ When executed correctly, strings should be printed to terminal similar to what i

> Note that Fast-DDS does not support the loaned messages. The loaned message API is used in iceoryx right now, which workes with CycloneDDS.

### Matched Event Detect

When executed correctly, strings should be printed to terminal similar to what is shown below:
```bash
# In terminal running matched_event_detect
[INFO] [1679745306.353845963] [matched_event_detect_node]: First subscription is connected.
[INFO] [1679745306.355642312] [matched_event_detect_node]: The changed number of connected subscription is 1 and current number of connected subscription is 2.
[INFO] [1679745306.357680594] [matched_event_detect_node]: The changed number of connected subscription is -1 and current number of connected subscription is 1.
[INFO] [1679745306.358833833] [matched_event_detect_node]: Last subscription is disconnected.
[INFO] [1679745306.360239274] [matched_event_detect_node]: First publisher is connected.
[INFO] [1679745306.361822463] [matched_event_detect_node]: The changed number of connected publisher is 1 and Current number of connected publisher is 2.
[INFO] [1679745306.363446300] [matched_event_detect_node]: The changed number of connected publisher is -1 and Current number of connected publisher is 1.
[INFO] [1679745306.364523970] [matched_event_detect_node]: Last publisher is disconnected.
```

## **FAQ**

`Q`: Encountered the following error in terminal when running **Loaned Message Talker**:
Expand Down
223 changes: 223 additions & 0 deletions demo_nodes_cpp/src/events/matched_event_detect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// Copyright 2023 Sony Group Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <chrono>
#include <string>
#include <vector>

#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/string.hpp"

#include "demo_nodes_cpp/visibility_control.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove it if not to build the following classes into a library.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Should remove it.
Thanks.


using namespace std::chrono_literals;

// This demo program shows matched event work.
// Matched event occurs when publisher and subscription establishes the connection.
// Class MatchedEventDetectNode output connection information of publisher and subscription.
// Class MultiSubNode is used to create/destroy subscription to connect/disconnect the publisher of
// MatchedEventDetectNode.
// Class MultiPubNode is used to created/destroy publisher to connect/disconnect the subscription
// of MatchedEventDetectNode.

class MatchedEventDetectNode : public rclcpp::Node
{
public:
DEMO_NODES_CPP_PUBLIC explicit MatchedEventDetectNode(
Copy link
Contributor

Choose a reason for hiding this comment

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

If using the main in this file, which means this file will not be built into a library, I think it's unnecessary to add DEMO_NODES_CPP_PUBLIC.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

const std::string & pub_topic_name,
const std::string & sub_topic_name)
: Node("matched_event_detect_node")
{
rclcpp::PublisherOptions pub_options;
pub_options.event_callbacks.matched_callback =
[this](rclcpp::MatchedInfo & s) {
if (any_subscription_connected_) {
if (s.current_count == 0) {
RCLCPP_INFO(this->get_logger(), "Last subscription is disconnected.");
any_subscription_connected_ = false;
} else {
RCLCPP_INFO(
this->get_logger(),
"The changed number of connected subscription is %d and current number of connected"
" subscription is %lu.", s.current_count_change, s.current_count);
}
} else {
if (s.current_count != 0) {
RCLCPP_INFO(this->get_logger(), "First subscription is connected.");
any_subscription_connected_ = true;
}
}
};

pub_ = create_publisher<std_msgs::msg::String>(
pub_topic_name, 10, pub_options);

rclcpp::SubscriptionOptions sub_options;
sub_options.event_callbacks.matched_callback =
[this](rclcpp::MatchedInfo & s) {
if (any_publisher_connected_) {
if (s.current_count == 0) {
RCLCPP_INFO(this->get_logger(), "Last publisher is disconnected.");
any_publisher_connected_ = false;
} else {
RCLCPP_INFO(
this->get_logger(),
"The changed number of connected publisher is %d and Current number of connected"
" publisher is %lu.", s.current_count_change, s.current_count);
}
} else {
if (s.current_count != 0) {
RCLCPP_INFO(this->get_logger(), "First publisher is connected.");
any_publisher_connected_ = true;
}
}
};
sub_ = create_subscription<std_msgs::msg::String>(
sub_topic_name,
10,
[](std_msgs::msg::String::ConstSharedPtr) {},
sub_options);
}

private:
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub_;
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub_;
bool any_subscription_connected_{false};
bool any_publisher_connected_{false};
};

class MultiSubNode : public rclcpp::Node
{
public:
DEMO_NODES_CPP_PUBLIC explicit MultiSubNode(const std::string & topic_name)
: Node("multi_sub_node"),
topic_name_(topic_name)
{}

DEMO_NODES_CPP_PUBLIC void create_one_sub(void)
{
auto sub = create_subscription<std_msgs::msg::String>(
topic_name_,
10,
[](std_msgs::msg::String::ConstSharedPtr) {});
subs_.emplace_back(sub);
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to output an info message that there is a subscription coming, which will make the whole output a little more clear. (destroy_one_sub as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay.

}

DEMO_NODES_CPP_PUBLIC void destroy_one_sub(void)
Copy link
Contributor

Choose a reason for hiding this comment

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

Even if it's a demo that we can control the whole process, I think we should design a better behavior in this method. It's just a suggestion,

rclcpp::Subscription<std_msgs::msg::String>::WeakPtr create_one_sub(void);
void destroy_one_sub(rclcpp::Subscription<std_msgs::msg::String>::WeakPtr sub);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you means test as the below

  auto sub1 = multi_sub_node->create_one_sub();
  executor.spin_some(200ms);

  auto sub2 = multi_sub_node->create_one_sub();
  executor.spin_some(200ms);

  multi_sub_node->destroy_one_sub(sub1);
  executor.spin_some(200ms);

  multi_sub_node->destroy_one_sub(sub2);
  executor.spin_some(200ms);

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes.

{
subs_.erase(subs_.begin());
}

private:
std::string topic_name_;
std::vector<rclcpp::Subscription<std_msgs::msg::String>::SharedPtr> subs_;
};

class MultiPubNode : public rclcpp::Node
{
public:
DEMO_NODES_CPP_PUBLIC explicit MultiPubNode(const std::string & topic_name)
: Node("multi_pub_node"),
topic_name_(topic_name)
{}

DEMO_NODES_CPP_PUBLIC void create_one_pub(void)
{
auto pub = create_publisher<std_msgs::msg::String>(topic_name_, 10);
pubs_.emplace_back(pub);
}

DEMO_NODES_CPP_PUBLIC void destroy_one_pub(void)
{
pubs_.erase(pubs_.begin());
}

private:
std::string topic_name_;
std::vector<rclcpp::Publisher<std_msgs::msg::String>::SharedPtr> pubs_;
};

int main(int argc, char ** argv)
{
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
rclcpp::init(argc, argv);

std::string topic_name_for_detect_pub_matched_event = "pub_topic_matched_event_detect";
std::string topic_name_for_detect_sub_matched_event = "sub_topic_matched_event_detect";

rclcpp::executors::SingleThreadedExecutor executor;

auto matched_event_detect_node = std::make_shared<MatchedEventDetectNode>(
topic_name_for_detect_pub_matched_event,
topic_name_for_detect_sub_matched_event);

auto multi_sub_node = std::make_shared<MultiSubNode>(
topic_name_for_detect_pub_matched_event);

auto multi_pub_node = std::make_shared<MultiPubNode>(
topic_name_for_detect_sub_matched_event);

executor.add_node(matched_event_detect_node);
executor.add_node(multi_sub_node);
executor.add_node(multi_pub_node);
executor.spin_some(200ms);

// MatchedEventDetectNode will output:
// First subscription is connected.
multi_sub_node->create_one_sub();
executor.spin_some(200ms);

// MatchedEventDetectNode will output:
// The changed number of connected subscription is 1 and current number of connected subscription
// is 2.
multi_sub_node->create_one_sub();
executor.spin_some(200ms);

// MatchedEventDetectNode will output:
// The changed number of connected subscription is -1 and current number of connected subscription
// is 1.
multi_sub_node->destroy_one_sub();
executor.spin_some(200ms);

// MatchedEventDetectNode will output:
// Last subscription is disconnected.
multi_sub_node->destroy_one_sub();
executor.spin_some(200ms);

// MatchedEventDetectNode will output:
// First publisher is connected.
multi_pub_node->create_one_pub();
executor.spin_some(200ms);

// MatchedEventDetectNode will output:
// The changed number of connected publisher is 1 and Current number of connected publisher
// is 2.
multi_pub_node->create_one_pub();
executor.spin_some(200ms);

// MatchedEventDetectNode will output:
// The changed number of connected publisher is -1 and Current number of connected publisher
// is 1.
multi_pub_node->destroy_one_pub();
executor.spin_some(200ms);

// MatchedEventDetectNode will output:
// Last publisher is disconnected.
multi_pub_node->destroy_one_pub();
executor.spin_some(200ms);

rclcpp::shutdown();
return 0;
}
8 changes: 8 additions & 0 deletions demo_nodes_cpp/test/matched_event_detect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
First subscription is connected.
The changed number of connected subscription is 1 and current number of connected subscription is 2.
The changed number of connected subscription is -1 and current number of connected subscription is 1.
Last subscription is disconnected.
First publisher is connected.
The changed number of connected publisher is 1 and Current number of connected publisher is 2.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
The changed number of connected publisher is 1 and Current number of connected publisher is 2.
The changed number of connected publisher is 1 and current number of connected publisher is 2.

please update README.md and matched_event_detect.cpp as well.

The changed number of connected publisher is -1 and Current number of connected publisher is 1.
Last publisher is disconnected.
Empty file.
Loading