✅ Summary: After making a node, the standard workflow is:
setup.py
→ declare dependencies in package.xml
→ rebuild workspace → source setup.bash → run with ros2 run
or ros2 launch
.mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_python my_package
This gives you package.xml
, setup.py
, and a Python module folder (my_package/
).
Inside ~/ros2_ws/src/my_package/my_package/
create my_node.py
:
#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class MinimalNode(Node):
def __init__(self):
super().__init__('minimal_node')
self.publisher_ = self.create_publisher(String, 'chatter', 10)
self.timer = self.create_timer(1.0, self.timer_callback)
def timer_callback(self):
msg = String()
msg.data = "Hello ROS2!"
self.publisher_.publish(msg)
self.get_logger().info(f'Publishing: "{msg.data}"')
def main():
rclpy.init()
node = MinimalNode()
rclpy.spin(node)
rclpy.shutdown()
if __name__ == '__main__':
main()
Make it executable:
chmod +x ~/ros2_ws/src/my_package/my_package/my_node.py
setup.py
Tell ROS how to run your node:
entry_points={
'console_scripts': [
'talker = my_package.my_node:main',
],
},
This means:
ros2 run my_package talker
will run the main()
function from my_node.py
.