|
| 1 | +// Copyright 2016 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "rclcpp/rclcpp.hpp" |
| 16 | +#include "std_msgs/msg/float64.hpp" |
| 17 | +#include "std_msgs/msg/int32.hpp" |
| 18 | +#include "std_msgs/msg/string.hpp" |
| 19 | +using std::placeholders::_1; |
| 20 | + |
| 21 | +class ExternalNode : public rclcpp::Node |
| 22 | +{ |
| 23 | +public: |
| 24 | + ExternalNode() : Node("control") |
| 25 | + { |
| 26 | + engine_power_publisher_ = this->create_publisher<std_msgs::msg::Int32>("std_msgs_msg_Int32"); |
| 27 | + warn_publisher_ = this->create_publisher<std_msgs::msg::String>("std_msgs_msg_String"); |
| 28 | + |
| 29 | + altitude_subscription_ = this->create_subscription<std_msgs::msg::Float64>("std_msgs_msg_Float64", std::bind(&ExternalNode::topic_callback, this, _1)); |
| 30 | + } |
| 31 | + |
| 32 | +private: |
| 33 | + rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr engine_power_publisher_; |
| 34 | + rclcpp::Publisher<std_msgs::msg::String>::SharedPtr warn_publisher_; |
| 35 | + rclcpp::Subscription<std_msgs::msg::Float64>::SharedPtr altitude_subscription_; |
| 36 | + |
| 37 | + void topic_callback(const std_msgs::msg::Float64::SharedPtr msg) |
| 38 | + { |
| 39 | + // Warning |
| 40 | + if (msg->data <= 500) |
| 41 | + { |
| 42 | + { |
| 43 | + auto message = std_msgs::msg::String(); |
| 44 | + message.data = "Failure!!"; |
| 45 | + RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()) |
| 46 | + this->warn_publisher_->publish(message); |
| 47 | + } |
| 48 | + |
| 49 | + { |
| 50 | + auto message = std_msgs::msg::Int32(); |
| 51 | + message.data = 10; |
| 52 | + this->engine_power_publisher_->publish(message); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + // Failure |
| 58 | + else if (msg->data <= 1000) |
| 59 | + { |
| 60 | + { |
| 61 | + auto message = std_msgs::msg::String(); |
| 62 | + message.data = "Warning!!"; |
| 63 | + RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()) |
| 64 | + this->warn_publisher_->publish(message); |
| 65 | + } |
| 66 | + |
| 67 | + { |
| 68 | + auto message = std_msgs::msg::Int32(); |
| 69 | + message.data = 5; |
| 70 | + this->engine_power_publisher_->publish(message); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +int main(int argc, char * argv[]) |
| 78 | +{ |
| 79 | + rclcpp::init(argc, argv); |
| 80 | + rclcpp::spin(std::make_shared<ExternalNode>()); |
| 81 | + rclcpp::shutdown(); |
| 82 | + return 0; |
| 83 | +} |
0 commit comments