- `resource`: holds files that do not need executable permissions like config, launch files, data files etc.
- python executables **NEED executable permissions**, so they need to placed in `dummy_test`
setup.py: so we can create exportable modules (along with ament_python_install_package(${PROJECT_NAME})). It’s NOT used for creating python nodes
importrclpyfromrclpy.nodeimportNodefromstd_msgs.msgimportStringclassMinimalPublisher(Node):def__init__(self):super().__init__('minimal_publisher')self.publisher_=self.create_publisher(String,'topic',10)timer_period=0.5# seconds
self.timer=self.create_timer(timer_period,self.timer_callback)self.i=0deftimer_callback(self):msg=String()msg.data='Hello World: %d'%self.iself.publisher_.publish(msg)self.get_logger().info('Publishing: "%s"'%msg.data)self.i+=1defmain(args=None):rclpy.init(args=args)minimal_publisher=MinimalPublisher()rclpy.spin(minimal_publisher)# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
minimal_publisher.destroy_node()rclpy.shutdown()
#include<memory>
#include<string>#include"rclcpp/rclcpp.hpp"
#include"std_msgs/msg/string.hpp"classListener:publicrclcpp::Node{public:Listener():Node("listener"){sub_=this->create_subscription<std_msgs::msg::String>("chatter",10,[this](std_msgs::msg::String::UniquePtrmsg){RCLCPP_INFO(this->get_logger(),"I heard: '%s'",msg->data.c_str());});}private:rclcpp::Subscription<std_msgs::msg::String>::SharedPtrsub_;};intmain(intargc,char*argv[]){// same SIGINT handler setup as the publisherrclcpp::init(argc,argv);autonode=std::make_shared<Listener>();rclcpp::spin(node);rclcpp::shutdown();return0;}
rclcpp::init(argc, argv); only flips the bool rclcpp::ok() reads. It does not kill the process. In the case of the publisher, rclcpp::spin() already recognizes that.
Custom Messages
You can define constants in a .msg for its field values
There’s an open design effort (and PR #685 in rosidl) to add proper enum support to the underlying IDL, but it isn’t released yet. Until then, you must either wrap or validate in C++ if you want real enforcement. You can do msg.code = 42; and nobody stops you. So we need to define a enum class: