Complete parrot example.

This commit is contained in:
James Pace 2023-02-19 22:10:10 +00:00
parent ae2875d7cf
commit 878fc973ac
7 changed files with 120 additions and 16 deletions

View File

@ -9,19 +9,29 @@ endif()
find_package(ament_cmake REQUIRED) find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED) find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED) find_package(std_msgs REQUIRED)
find_package(fmt REQUIRED)
add_executable(j7s-publisher src/j7s-publisher.cpp) add_executable(j7s-publisher src/j7s-publisher.cpp)
target_include_directories(j7s-publisher PUBLIC target_include_directories(j7s-publisher PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>) $<INSTALL_INTERFACE:include>)
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) ament_target_dependencies(j7s-publisher rclcpp std_msgs)
add_executable(j7s-parrot src/j7s-parrot.cpp)
target_include_directories(j7s-parrot PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
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 install(DIRECTORY
launch launch
DESTINATION share/${PROJECT_NAME}/ DESTINATION share/${PROJECT_NAME}/
) )
install(TARGETS j7s-publisher install(TARGETS j7s-publisher j7s-parrot
DESTINATION lib/${PROJECT_NAME}) DESTINATION lib/${PROJECT_NAME})
ament_package() ament_package()

View File

@ -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 <string>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
class J7sParrot : public rclcpp::Node
{
public:
J7sParrot();
private:
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr m_publisher;
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr m_subscription;
const std::string m_name;
};

View File

@ -13,6 +13,8 @@
// limitations under the License. // limitations under the License.
#pragma once #pragma once
#include <string>
#include "rclcpp/rclcpp.hpp" #include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp" #include "std_msgs/msg/string.hpp"
@ -20,7 +22,10 @@ class J7sPublisher: public rclcpp::Node
{ {
public: public:
J7sPublisher(); J7sPublisher();
private: private:
rclcpp::TimerBase::SharedPtr m_timer; rclcpp::TimerBase::SharedPtr m_timer;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr m_publisher; rclcpp::Publisher<std_msgs::msg::String>::SharedPtr m_publisher;
const std::string m_message;
unsigned int m_count;
}; };

View File

@ -2,9 +2,26 @@ import launch
import launch_ros.actions import launch_ros.actions
def generate_launch_description(): def generate_launch_description():
return launch.LaunchDescription([ publisher_node = launch_ros.actions.Node(
launch_ros.actions.Node(
package='j7s-simple', package='j7s-simple',
executable='j7s-publisher', 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
]) ])

View File

@ -8,6 +8,7 @@
<license>Apache-2.0</license> <license>Apache-2.0</license>
<buildtool_depend>ament_cmake</buildtool_depend> <buildtool_depend>ament_cmake</buildtool_depend>
<depend>fmt</depend>
<depend>std_msgs</depend> <depend>std_msgs</depend>
<depend>rclcpp</depend> <depend>rclcpp</depend>

39
src/j7s-parrot.cpp Normal file
View File

@ -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 <fmt/core.h>
#include <chrono>
#include <j7s-simple/j7s-parrot.hpp>
using namespace std::chrono_literals;
J7sParrot::J7sParrot() : Node("j7s_parrot"), m_name{declare_parameter<std::string>("name")}
{
m_publisher = this->create_publisher<std_msgs::msg::String>("~/parrot_out", 1);
m_subscription = this->create_subscription<std_msgs::msg::String>(
"~/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<J7sParrot>());
rclcpp::shutdown();
return 0;
}

View File

@ -11,19 +11,21 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <j7s-simple/j7s-publisher.hpp> #include <fmt/core.h>
#include <chrono> #include <chrono>
#include <j7s-simple/j7s-publisher.hpp>
using namespace std::chrono_literals; using namespace std::chrono_literals;
J7sPublisher::J7sPublisher() : J7sPublisher::J7sPublisher() :
Node("j7s_publisher") Node("j7s_publisher"), m_message{declare_parameter<std::string>("message")}, m_count{0}
{
m_publisher = this->create_publisher<std_msgs::msg::String>("~/main_out", 1);
const auto callback = [this]()
{ {
m_publisher = this->create_publisher<std_msgs::msg::String>("j7s_hello", 1);
const auto callback = [this]() {
std_msgs::msg::String msg; 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_publisher->publish(msg);
}; };
m_timer = this->create_wall_timer(500ms, callback); m_timer = this->create_wall_timer(500ms, callback);