CMakeLists.txt
cmake_minimum_required(VERSION 3.5) project(my_pkg_cpp) # Default to C99 if(NOT CMAKE_C_STANDARD) set(CMAKE_C_STANDARD 99) endif() # Default to C++14 if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 14) endif() if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) # uncomment the following section in order to fill in # further dependencies manually. # find_package(<dependency> REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) add_executable(pub_node_led src/my_pub_led.cpp) ament_target_dependencies(pub_node_led rclcpp std_msgs) add_executable(sub_node_led src/my_sub_led.cpp) ament_target_dependencies(sub_node_led rclcpp std_msgs) target_link_libraries(sub_node_led wiringPi) install(TARGETS pub_node_led sub_node_led DESTINATION lib/${PROJECT_NAME}) if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) # the following line skips the linter which checks for copyrights # uncomment the line when a copyright and license is not present in all source files #set(ament_cmake_copyright_FOUND TRUE) # the following line skips cpplint (only works in a git repo) # uncomment the line when this package is not in a git repo #set(ament_cmake_cpplint_FOUND TRUE) ament_lint_auto_find_test_dependencies() endif() ament_package() |
package.xml
<?xml version="1.0"?> <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> <package format="3"> <name>my_pkg_cpp</name> <version>0.0.0</version> <description>TODO: Package description</description> <maintainer email="ubuntu@todo.todo">ubuntu</maintainer> <license>TODO: License declaration</license> <buildtool_depend>ament_cmake</buildtool_depend> <depend>rclcpp</depend> <depend>std_msgs</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <export> <build_type>ament_cmake</build_type> </export> </package> |
my_pub_led.cpp
#include <chrono> #include <functional> #include <memory> #include <string> #include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp" using namespace std::chrono_literals; /* This example creates a subclass of Node and uses std::bind() to register a * member function as a callback from the timer. */ class PublisherLed : public rclcpp::Node { public: PublisherLed() : Node("publisher_led"), count_(0) { publisher_ = this->create_publisher<std_msgs::msg::String>("topic_led", 10); timer_ = this->create_wall_timer( 3000ms, std::bind(&PublisherLed::timer_callback, this)); } private: void timer_callback() { auto message = std_msgs::msg::String(); message.data = "LED ON " + std::to_string(count_++); RCLCPP_INFO(this->get_logger(), "Published: '%s'", message.data.c_str()); publisher_->publish(message); } rclcpp::TimerBase::SharedPtr timer_; rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_; size_t count_; }; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<PublisherLed>()); rclcpp::shutdown(); return 0; } |
my_sub_led.cpp
#include <iostream> #include <memory> #include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp" using std::placeholders::_1; #include <wiringPi.h> class SubscriberLed : public rclcpp::Node { public: SubscriberLed() : Node("subscriber_led") { subscription_ = this->create_subscription<std_msgs::msg::String>( "topic_led", 10, std::bind(&SubscriberLed::topic_callback, this, _1)); } private: void topic_callback(const std_msgs::msg::String::SharedPtr msg) const { // Setup for RPi4B auto LED26 = 26; wiringPiSetupGpio(); pinMode(LED26, OUTPUT); digitalWrite(LED26, HIGH); delay(1000); digitalWrite(LED26, LOW); RCLCPP_INFO(this->get_logger(), "Subscribed: '%s'", msg->data.c_str()); } rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_; }; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<SubscriberLed>()); rclcpp::shutdown(); return 0; } |
ros2 run my_pkg_cpp pub_node_led
ros2 run my_pkg_cpp sub_node_led
'ROS > ROS2FoxyUbuntu20.04RPi4B' 카테고리의 다른 글
Motor Drive with Camera(OpenCV) on RPi4B using ROS2 Topic (0) | 2022.05.13 |
---|---|
Motor Drive Control on RPi4B using ROS2 Service (0) | 2022.05.12 |
Motor Drive Control on RPi4B using ROS2 Topic (0) | 2022.05.12 |
LED Control on RPi4B using ROS2 Action (0) | 2022.05.11 |
LED Control on RPi4B using ROS2 Service (0) | 2022.05.11 |