diff --git a/CMakeLists.txt b/CMakeLists.txt index 09ada36..d300289 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,19 +9,29 @@ endif() find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) +find_package(fmt REQUIRED) add_executable(j7s-publisher src/j7s-publisher.cpp) target_include_directories(j7s-publisher PUBLIC $ $) -target_compile_features(j7s-publisher PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17 +target_compile_features(j7s-publisher PUBLIC c_std_99 cxx_std_17) +target_link_libraries(j7s-publisher fmt::fmt) ament_target_dependencies(j7s-publisher rclcpp std_msgs) +add_executable(j7s-parrot src/j7s-parrot.cpp) +target_include_directories(j7s-parrot PUBLIC + $ + $) +target_compile_features(j7s-parrot PUBLIC c_std_99 cxx_std_17) +target_link_libraries(j7s-parrot fmt::fmt) +ament_target_dependencies(j7s-parrot rclcpp std_msgs) + install(DIRECTORY launch DESTINATION share/${PROJECT_NAME}/ ) -install(TARGETS j7s-publisher +install(TARGETS j7s-publisher j7s-parrot DESTINATION lib/${PROJECT_NAME}) ament_package() diff --git a/include/j7s-simple/j7s-parrot.hpp b/include/j7s-simple/j7s-parrot.hpp new file mode 100644 index 0000000..149abf8 --- /dev/null +++ b/include/j7s-simple/j7s-parrot.hpp @@ -0,0 +1,30 @@ +// Copyright 2023 James Pace +// +// 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. +#pragma once + +#include + +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/string.hpp" + +class J7sParrot : public rclcpp::Node +{ +public: + J7sParrot(); + +private: + rclcpp::Publisher::SharedPtr m_publisher; + rclcpp::Subscription::SharedPtr m_subscription; + const std::string m_name; +}; diff --git a/include/j7s-simple/j7s-publisher.hpp b/include/j7s-simple/j7s-publisher.hpp index ee041b9..28ba9c3 100644 --- a/include/j7s-simple/j7s-publisher.hpp +++ b/include/j7s-simple/j7s-publisher.hpp @@ -13,14 +13,19 @@ // limitations under the License. #pragma once +#include + #include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp" -class J7sPublisher: public rclcpp::Node +class J7sPublisher : public rclcpp::Node { public: J7sPublisher(); + private: - rclcpp::TimerBase::SharedPtr m_timer; - rclcpp::Publisher::SharedPtr m_publisher; + rclcpp::TimerBase::SharedPtr m_timer; + rclcpp::Publisher::SharedPtr m_publisher; + const std::string m_message; + unsigned int m_count; }; diff --git a/launch/j7s_publisher_launch.py b/launch/j7s_publisher_launch.py index b7d6fa2..4120491 100644 --- a/launch/j7s_publisher_launch.py +++ b/launch/j7s_publisher_launch.py @@ -2,9 +2,26 @@ import launch import launch_ros.actions def generate_launch_description(): - return launch.LaunchDescription([ - launch_ros.actions.Node( + publisher_node = launch_ros.actions.Node( package='j7s-simple', executable='j7s-publisher', - name='j7s_publisher') - ]) + name='j7s_publisher', + parameters=[{ + "message":"Hello" + }] + ) + parrot_node = launch_ros.actions.Node( + package='j7s-simple', + executable='j7s-parrot', + name='j7s_parrot_1', + parameters=[{ + "name":"Polly" + }], + remappings=[ + ("~/parrot_in", "/j7s_publisher/main_out") + ] + ) + return launch.LaunchDescription([ + publisher_node, + parrot_node + ]) diff --git a/package.xml b/package.xml index 37f3843..7ade5a7 100644 --- a/package.xml +++ b/package.xml @@ -8,6 +8,7 @@ Apache-2.0 ament_cmake + fmt std_msgs rclcpp diff --git a/src/j7s-parrot.cpp b/src/j7s-parrot.cpp new file mode 100644 index 0000000..ba026dc --- /dev/null +++ b/src/j7s-parrot.cpp @@ -0,0 +1,39 @@ +// Copyright 2023 James Pace +// +// 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 + +#include +#include +using namespace std::chrono_literals; + +J7sParrot::J7sParrot() : Node("j7s_parrot"), m_name{declare_parameter("name")} +{ + m_publisher = this->create_publisher("~/parrot_out", 1); + m_subscription = this->create_subscription( + "~/parrot_in", 1, + [this](const std_msgs::msg::String & msg) + { + std_msgs::msg::String new_msg; + new_msg.data = fmt::format("{} says: {}", m_name, msg.data); + m_publisher->publish(new_msg); + }); +} + +int main(int argc, char ** argv) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} diff --git a/src/j7s-publisher.cpp b/src/j7s-publisher.cpp index bfe198b..aaf08a8 100644 --- a/src/j7s-publisher.cpp +++ b/src/j7s-publisher.cpp @@ -11,19 +11,21 @@ // 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 +#include #include +#include using namespace std::chrono_literals; - -J7sPublisher::J7sPublisher(): - Node("j7s_publisher") +J7sPublisher::J7sPublisher() : + Node("j7s_publisher"), m_message{declare_parameter("message")}, m_count{0} { - m_publisher = this->create_publisher("j7s_hello", 1); - const auto callback = [this]() { + m_publisher = this->create_publisher("~/main_out", 1); + const auto callback = [this]() + { std_msgs::msg::String msg; - msg.data = "Hello from j7s!"; + msg.data = fmt::format("{} count: {}", m_message, m_count); + m_count++; m_publisher->publish(msg); }; m_timer = this->create_wall_timer(500ms, callback);