Welcome to MicroROS Self-balancing Robot Car repository
6.ROS2 node
6.ROS2 node
1. Node introduction
During communication, no matter which method is used, the construction of communication objects depends on nodes. In ROS2, generally each node corresponds to a single functional module.(For example: a radar driver node may be responsible for publishing radar messages, and a camera driver node may be responsible for publishing image messages).A complete robot system may consist of many nodes working together, and a single executable file (C++ program or Python program) in ROS2 can contain one or more nodes.
2. Create node process
- Programming interface initialization
- Create nodes and initialize them
- Implement node functions
- Destroy nodes and close interfaces
3. Hello World node case
Here we take the python function package as an example to explain
3.1. Create python function package
cd ~/yahboomcar_ros2_ws/yahboomcar_ws/srcros2 pkg create pkg_helloworld_py --build-type ament_python --dependencies rclpy --node-name helloworld3.2. Writing code
After executing the above command, pkg_helloworld_py will be created, and the helloworld.py file will be created to write the node:
Delete the original helloworld.py code and write the following code:
ximport rclpy # ROS2 Python interface libraryfrom rclpy.node import Node # ROS2 Node classimport time"""Create a HelloWorld node and output the "hello world" log during initialization"""class HelloWorldNode(Node):def __init__(self, name):super().__init__(name) # ROS2 node parent class initializationwhile rclpy.ok(): # Whether the ROS2 system is running normallyself.get_logger().info("Hello World") # ROS2 log outputtime.sleep(0.5) # sleep control loop timedef main(args=None): # ROS2 node main entrance main functionrclpy.init(args=args) # ROS2 Python interface initializationnode = HelloWorldNode("helloworld") # Create a ROS2 node object and initialize itrclpy.spin(node) # Loop waiting for ROS2 to exitnode.destroy_node() # Destroy node objectrclpy.shutdown() # Close the ROS2 Python interface
After completing the writing of the code, you need to set the compilation options of the function package to let the system know the entrance of the Python program. Open the setup.py file of the function package and add the following entry point configuration:
3.3. Compile function package
xxxxxxxxxxcd ~/yahboomcar_ros2_ws/yahboomcar_wscolcon build --packages-select pkg_helloworld_pysource install/setup.bash3.4. Run the node
xxxxxxxxxxros2 run pkg_helloworld_py helloworld
After running successfully, you can see the effect of printing the "Hello World" string in a loop in the terminal: