|
1 | | -// Copyright 2020 Open Source Robotics Foundation, Inc. |
| 1 | +// Copyright 2021 Open Source Robotics Foundation, Inc. |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
|
15 | 15 | #include <cassert> |
16 | 16 | #include <memory> |
17 | 17 | #include <vector> |
18 | | -// #include <string> |
19 | | -// #include <thread> |
20 | 18 |
|
21 | | -#include "example_interfaces/srv/add_two_ints.hpp" |
22 | 19 | #include "rclcpp/rclcpp.hpp" |
23 | 20 | #include "std_msgs/msg/string.hpp" |
24 | 21 |
|
25 | 22 | int main(int argc, char * argv[]) |
26 | 23 | { |
27 | 24 | rclcpp::init(argc, argv); |
28 | 25 |
|
29 | | - auto do_nothing = [](std_msgs::msg::String::UniquePtr) {assert(false);}; |
30 | | - |
31 | 26 | auto node = std::make_shared<rclcpp::Node>("wait_set_example_node"); |
32 | 27 |
|
33 | | - auto guard_condition = std::make_shared<rclcpp::GuardCondition>(); |
| 28 | + auto do_nothing = [](std_msgs::msg::String::UniquePtr) {assert(false);}; |
| 29 | + auto sub1 = node->create_subscription<std_msgs::msg::String>("~/chatter", 10, do_nothing); |
| 30 | + auto sub2 = node->create_subscription<std_msgs::msg::String>("~/chatter", 10, do_nothing); |
| 31 | + std::vector<decltype(sub1)> sub_vector = {sub1, sub2}; |
| 32 | + auto guard_condition1 = std::make_shared<rclcpp::GuardCondition>(); |
34 | 33 | auto guard_condition2 = std::make_shared<rclcpp::GuardCondition>(); |
35 | 34 |
|
36 | | - auto sub = node->create_subscription<std_msgs::msg::String>("~/chatter", 10, do_nothing); |
37 | | - rclcpp::SubscriptionOptions so; |
38 | | - so.use_intra_process_comm = rclcpp::IntraProcessSetting::Enable; |
39 | | - auto sub2 = node->create_subscription<std_msgs::msg::String>("~/chatter", 10, do_nothing, so); |
40 | | - |
| 35 | + // FIXME: removing sub if it was added in the ctor leads to a failure |
| 36 | + // terminate called after throwing an instance of 'std::runtime_error' |
| 37 | + // what(): waitable not in wait set |
41 | 38 | rclcpp::ThreadSafeWaitSet wait_set( |
42 | | - std::vector<rclcpp::ThreadSafeWaitSet::SubscriptionEntry>{{sub}}, |
43 | | - std::vector<rclcpp::GuardCondition::SharedPtr>{guard_condition}); |
| 39 | + // std::vector<rclcpp::WaitSet::SubscriptionEntry>{{sub}}, |
| 40 | + {}, |
| 41 | + std::vector<rclcpp::GuardCondition::SharedPtr>{guard_condition1}); |
| 42 | + |
| 43 | + wait_set.add_subscription(sub1); // FIXME: add it in the ctor |
44 | 44 | wait_set.add_subscription(sub2); |
45 | 45 | wait_set.add_guard_condition(guard_condition2); |
46 | 46 |
|
47 | | - { |
| 47 | + auto wait_and_print_results = [&]() { |
| 48 | + RCLCPP_INFO(node->get_logger(), "Waiting..."); |
48 | 49 | auto wait_result = wait_set.wait(std::chrono::seconds(1)); |
49 | | - assert(wait_result.kind() == rclcpp::WaitResultKind::Timeout); |
50 | | - } |
51 | | - |
52 | | - guard_condition->trigger(); |
53 | | - |
54 | | - { |
55 | | - auto wait_result = wait_set.wait(std::chrono::seconds(1)); |
56 | | - assert(wait_result.kind() == rclcpp::WaitResultKind::Ready); |
57 | | - assert(wait_result.get_wait_set().get_rcl_wait_set().guard_conditions[0] != nullptr); |
58 | | - assert(wait_result.get_wait_set().get_rcl_wait_set().guard_conditions[1] == nullptr); |
59 | | - assert(wait_result.get_wait_set().get_rcl_wait_set().subscriptions[0] == nullptr); |
60 | | - assert(wait_result.get_wait_set().get_rcl_wait_set().subscriptions[1] == nullptr); |
61 | | - } |
| 50 | + if (wait_result.kind() == rclcpp::WaitResultKind::Ready) { |
| 51 | + int guard_conditions_num = wait_set.get_rcl_wait_set().size_of_guard_conditions; |
| 52 | + int subscriptions_num = wait_set.get_rcl_wait_set().size_of_subscriptions; |
| 53 | + |
| 54 | + for (int i=0; i<guard_conditions_num; i++) { |
| 55 | + if (wait_result.get_wait_set().get_rcl_wait_set().guard_conditions[i]) { |
| 56 | + RCLCPP_INFO(node->get_logger(), "guard_condition %d triggered", i+1); |
| 57 | + } |
| 58 | + } |
| 59 | + for (int i=0; i<subscriptions_num; i++) { |
| 60 | + if (wait_result.get_wait_set().get_rcl_wait_set().subscriptions[i]) { |
| 61 | + RCLCPP_INFO(node->get_logger(), "subscription %d triggered", i+1); |
| 62 | + std_msgs::msg::String msg; |
| 63 | + rclcpp::MessageInfo msg_info; |
| 64 | + if (sub_vector.at(i)->take(msg, msg_info)) { |
| 65 | + RCLCPP_INFO(node->get_logger(), |
| 66 | + "subscription %d: I heard '%s'", i+1, msg.data.c_str()); |
| 67 | + } else { |
| 68 | + RCLCPP_INFO(node->get_logger(), "subscription %d: No message", i+1); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } else if (wait_result.kind() == rclcpp::WaitResultKind::Timeout) { |
| 73 | + RCLCPP_INFO(node->get_logger(), "wait-set waiting failed with timeout"); |
| 74 | + } else if (wait_result.kind() == rclcpp::WaitResultKind::Empty) { |
| 75 | + RCLCPP_INFO(node->get_logger(), "wait-set waiting failed because wait-set is empty"); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + RCLCPP_INFO(node->get_logger(), "Action: Nothing triggered"); |
| 80 | + wait_and_print_results(); |
| 81 | + |
| 82 | + RCLCPP_INFO(node->get_logger(), "Action: Trigger Guard condition 1"); |
| 83 | + guard_condition1->trigger(); |
| 84 | + wait_and_print_results(); |
| 85 | + |
| 86 | + RCLCPP_INFO(node->get_logger(), "Action: Trigger Guard condition 2"); |
| 87 | + guard_condition2->trigger(); |
| 88 | + wait_and_print_results(); |
| 89 | + |
| 90 | + RCLCPP_INFO(node->get_logger(), "Action: Message published"); |
| 91 | + auto pub = node->create_publisher<std_msgs::msg::String>("~/chatter", 1); |
| 92 | + pub->publish(std_msgs::msg::String().set__data("test")); |
| 93 | + wait_and_print_results(); |
62 | 94 |
|
| 95 | + RCLCPP_INFO(node->get_logger(), "Action: Guard condition 1 removed"); |
| 96 | + RCLCPP_INFO(node->get_logger(), "Action: Guard condition 2 removed"); |
| 97 | + wait_set.remove_guard_condition(guard_condition1); |
63 | 98 | wait_set.remove_guard_condition(guard_condition2); |
| 99 | + wait_and_print_results(); |
64 | 100 |
|
65 | | - { |
66 | | - // still fails with timeout |
67 | | - auto wait_result = wait_set.wait(std::chrono::seconds(1)); |
68 | | - assert(wait_result.kind() == rclcpp::WaitResultKind::Timeout); |
69 | | - } |
70 | | - |
71 | | - wait_set.remove_guard_condition(guard_condition); |
72 | | - |
73 | | - { |
74 | | - // still fails with timeout |
75 | | - auto wait_result = wait_set.wait(std::chrono::seconds(1)); |
76 | | - assert(wait_result.kind() == rclcpp::WaitResultKind::Timeout); |
77 | | - } |
78 | | - |
| 101 | + RCLCPP_INFO(node->get_logger(), "Action: Subscription 2 removed"); |
79 | 102 | wait_set.remove_subscription(sub2); |
| 103 | + wait_and_print_results(); |
80 | 104 |
|
81 | | - { |
82 | | - // still fails with timeout |
83 | | - auto wait_result = wait_set.wait(std::chrono::seconds(1)); |
84 | | - assert(wait_result.kind() == rclcpp::WaitResultKind::Timeout); |
85 | | - } |
86 | | - |
87 | | - auto pub = node->create_publisher<std_msgs::msg::String>("~/chatter", 1); |
88 | | - pub->publish(std_msgs::msg::String().set__data("test")); |
| 105 | + RCLCPP_INFO(node->get_logger(), "Action: Subscription 1 removed"); |
| 106 | + wait_set.remove_subscription(sub1); |
| 107 | + wait_and_print_results(); |
89 | 108 |
|
90 | | - { |
91 | | - auto wait_result = wait_set.wait(std::chrono::seconds(1)); |
92 | | - assert(wait_result.kind() == rclcpp::WaitResultKind::Ready); |
93 | | - assert(wait_result.get_wait_set().get_rcl_wait_set().subscriptions[0] != nullptr); |
94 | | - std_msgs::msg::String msg; |
95 | | - rclcpp::MessageInfo msg_info; |
96 | | - assert(sub->take(msg, msg_info)); |
97 | | - assert(msg.data == "test"); |
98 | | - } |
99 | | - |
100 | | - wait_set.remove_subscription(sub); |
101 | | - |
102 | | - { |
103 | | - // still will not fail, because thread-safe policy we are using adds its |
104 | | - // own guard condition and therefore it will never be empty |
105 | | - auto wait_result = wait_set.wait(std::chrono::seconds(1)); |
106 | | - assert(wait_result.kind() == rclcpp::WaitResultKind::Timeout); |
107 | | - } |
108 | 109 |
|
109 | 110 | return 0; |
110 | 111 | } |
0 commit comments