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

  1. Programming interface initialization
  2. Create nodes and initialize them
  3. Implement node functions
  4. 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

3.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:

image-20231023163116372

Delete the original helloworld.py code and write the following code:

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:

image-20231023172134879

3.3. Compile function package

3.4. Run the node

After running successfully, you can see the effect of printing the "Hello World" string in a loop in the terminal:

image-20231023163749024