-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carlos San Vicente <carlos.sanvicente@apex.ai>
- Loading branch information
Showing
8 changed files
with
208 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2021, Apex.AI Inc. | ||
// | ||
// 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 <rclcpp/rclcpp.hpp> | ||
#include <std_msgs/msg/string.hpp> | ||
#include "random_listener.hpp" | ||
#include "random_talker.hpp" | ||
|
||
/* For this example, we will be creating a talker node with three publishers which will | ||
* publish the topics A, B, C in random order each time. In this case the messages are handled | ||
* using an executor. The order in which the messages are handled will depend on the message | ||
* arrival and the type of messages available at the moment. | ||
*/ | ||
|
||
int32_t main(const int32_t argc, char ** const argv) | ||
{ | ||
rclcpp::init(argc, argv); | ||
|
||
// We run the talker and the listener in different threads to simulate inter-process | ||
// communications. | ||
// Note the order of execution will be different if both nodes are spun in the same executor. | ||
// This is because the executor handles the messages in the order in which the | ||
// subscriptions were created. Using difference threads the handling order depends on the | ||
// message arrival and type of messages available. | ||
auto thread = std::thread([]() {rclcpp::spin(std::make_shared<RandomTalker>());}); | ||
rclcpp::spin(std::make_shared<Listener>()); | ||
|
||
rclcpp::shutdown(); | ||
thread.join(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2021, Apex.AI Inc. | ||
// | ||
// 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 <rclcpp/rclcpp.hpp> | ||
#include <std_msgs/msg/string.hpp> | ||
#include <random> | ||
|
||
class Listener : public rclcpp::Node | ||
{ | ||
using subscription_list = std::vector<rclcpp::Subscription<std_msgs::msg::String>::SharedPtr>; | ||
|
||
public: | ||
Listener() | ||
: Node("random_listener") | ||
{ | ||
auto print_msg = [this](std_msgs::msg::String::UniquePtr msg) { | ||
RCLCPP_INFO(this->get_logger(), "I heard: %s", msg->data.c_str()); | ||
}; | ||
sub1_ = this->create_subscription<std_msgs::msg::String>("topicA", 10, print_msg); | ||
sub2_ = this->create_subscription<std_msgs::msg::String>("topicB", 10, print_msg); | ||
sub3_ = this->create_subscription<std_msgs::msg::String>("topicC", 10, print_msg); | ||
} | ||
|
||
subscription_list get_subscriptions() const | ||
{ | ||
return {sub1_, sub2_, sub3_}; | ||
} | ||
|
||
private: | ||
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub1_; | ||
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub2_; | ||
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub3_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2021, Apex.AI Inc. | ||
// | ||
// 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 <rclcpp/rclcpp.hpp> | ||
#include <std_msgs/msg/string.hpp> | ||
#include <random> | ||
|
||
using namespace std::chrono_literals; | ||
|
||
class RandomTalker : public rclcpp::Node | ||
{ | ||
public: | ||
RandomTalker() | ||
: Node("random_talker"), | ||
pub1_(this->create_publisher<std_msgs::msg::String>("topicA", 10)), | ||
pub2_(this->create_publisher<std_msgs::msg::String>("topicB", 10)), | ||
pub3_(this->create_publisher<std_msgs::msg::String>("topicC", 10)), | ||
rand_engine_(std::chrono::system_clock::now().time_since_epoch().count()) | ||
{ | ||
publish_functions_.emplace_back( | ||
([this]() { | ||
std_msgs::msg::String msg; | ||
msg.data = "A"; | ||
RCLCPP_INFO(this->get_logger(), "Publishing: %s", msg.data.c_str()); | ||
pub1_->publish(msg); | ||
})); | ||
publish_functions_.emplace_back( | ||
([this]() { | ||
std_msgs::msg::String msg; | ||
msg.data = "B"; | ||
RCLCPP_INFO(this->get_logger(), "Publishing: %s", msg.data.c_str()); | ||
pub2_->publish(msg); | ||
})); | ||
publish_functions_.emplace_back( | ||
([this]() { | ||
std_msgs::msg::String msg; | ||
msg.data = "C"; | ||
RCLCPP_INFO(this->get_logger(), "Publishing: %s", msg.data.c_str()); | ||
pub3_->publish(msg); | ||
})); | ||
auto timer_callback = | ||
[this]() -> void { | ||
std::shuffle(publish_functions_.begin(), publish_functions_.end(), rand_engine_); | ||
for (const auto & f : publish_functions_) {f();} | ||
}; | ||
timer_ = this->create_wall_timer(1s, timer_callback); | ||
} | ||
|
||
private: | ||
rclcpp::TimerBase::SharedPtr timer_; | ||
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub1_; | ||
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub2_; | ||
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub3_; | ||
std::vector<std::function<void()>> publish_functions_; | ||
std::default_random_engine rand_engine_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.