ROS2 Notes - Node

ROS2 Notes - Node

node

Study material reference ROS2 Getting Started 21 Graphic Tutorials.

A robot is a complex of various functions, each function is like a working cell of the robot, and many cells are connected together through some mechanisms to become a robot as a whole.

In ROS, we give these "cells" a name, and that is nodes.
communication model

A complete robot system may not be a physical whole, such as a robot like this:

A computer A is equipped in the robot body, which can obtain information about the external environment through the robot's eyes - the camera, and can also control the robot's legs - the wheels, so that the robot can move to where it wants to go. In addition to this, there may be another computer B, placed on your desk, which can remotely monitor the information seen by the robot, and can also remotely configure the speed and certain parameters of the robot, and can also connect a joystick , the robot is manually controlled to move back and forth, left and right.

Although these functions are located in different computers, they are all working cells of the robot, that is, nodes, and together they form a complete robot system.

The responsibility of the node in the robot system is to perform some specific tasks, which are also called processes from the point of view of the computer operating system;
Each node is an executable file that can run independently, such as executing a certain python program, or execute C++The results generated by the compilation are regarded as running a node;
Since each node is an independent executable file, it is natural to think that the programming language for obtaining this executable file can be different, such as C++,Python,and even Java,Ruby and more languages.
These nodes are cells with different functions and, depending on the system design, may be located in the computer A,may also be located on a computer B,It is also possible to run in the cloud, which is called distributed, that is, it can be distributed on different hardware carriers;
Each node needs to have a unique name. When we want to find a node, or want to query the status of a node, we can query by the node name.

Nodes can also be compared to workers who complete different tasks separately. Some of them work in the front-line workshop, and some provide support in the logistics department. They may not know each other, but together they promote the "factory" of robots. Complete more complex tasks.

Next, let's take a look at how the working cell of the node is implemented.

background

ROS2 map

A ROS graph is a ROS 2 element network that processes data simultaneously. It contains all executables and the connections between them, if you want to map and visualize them all.

Each node in ROS should be responsible for a single module purpose (e.g. one node to control the wheel motors, one node to control the laser rangefinder, etc.). Each node can send and receive data to and from other nodes through topics, services, operations, or parameters.

ROS2 node

Each node in ROS should be responsible for a single module purpose (e.g. one node to control the wheel motors, one node to control the laser rangefinder, etc.). Each node can send and receive data to and from other nodes through topics, services, operations, or parameters.
A complete robotic system consists of many nodes that work together. In ROS 2, a single executable (C++ program, Python program, etc.) can contain one or more nodes.

Case 1: Hello World node (process-oriented)

Of course, the implementation of nodes in ROS2 requires programming. We start with the Hello World routine. First, we will implement the simplest node. The function is not complicated, that is, it prints a "Hello World" string to the terminal in a loop.
Create Node Process

So in general, the implementation of the node is still these four steps, but the coding method has changed.

  • Programming interface initialization
  • Create node and initialize
  • Implement node functionality
  • Destroy the node and close the interface

Create the function package learning_node and implement the helloworld node through the following commands

$ cd ~/dev_ws/src
$ ros2 pkg create --build-type ament_python learning_node
$ cd learning_node/learning_node
$ gedit node_helloworld.py

The content of node_helloworld.py is as follows

#!/usr/bin/env python3 
# -*- coding: utf-8 -*-

"""
@author: Gu Yue Ju(www.guyuehome.com)
@illustrate: ROS2 Node example-release" Hello World"log information, Use a process-oriented implementation
"""

import rclpy                                     # ROS2 Python interface library
from rclpy.node import Node                      # ROS2 node class
import time

def main(args=None):                             # ROS2 node main entry main function
    rclpy.init(args=args)                        # ROS2 Python interface initialization
    node = Node("node_helloworld")               # Create a ROS2 node object and initialize it
    
    while rclpy.ok():                            # Is the ROS2 system running normally?
        node.get_logger().info("Hello World")    # ROS2 log output
        time.sleep(0.5)                          # Sleep Control Cycle Time
    
    node.destroy_node()                          # destroy node object    
    rclpy.shutdown()                             # Close the ROS2 Python interface

After writing the code, you need to set the compilation options of the function package to let the system know the entry point of the Python program, open the setup.py file of the function package, and add the following entry point configuration:

    entry_points={
        'console_scripts': [
        'node_helloworld       = learning_node.node_helloworld:main',
        ],
    },

Compile and execute

$ cd ~/dev_ws/src
$ colcon build
$ ros2 run learning_node node_helloworld 

[INFO] [1663995000.408755490] [node_helloworld]: Hello World
[INFO] [1663995000.909924309] [node_helloworld]: Hello World
[INFO] [1663995001.411069528] [node_helloworld]: Hello World
[INFO] [1663995001.912105173] [node_helloworld]: Hello World
[INFO] [1663995002.413409973] [node_helloworld]: Hello World
[INFO] [1663995002.914844265] [node_helloworld]: Hello World
[INFO] [1663995003.416305538] [node_helloworld]: Hello World
[INFO] [1663995003.917357029] [node_helloworld]: Hello World

Node Command Line Operations

Common operations of node commands are as follows:

$ ros2 node list               # View Node List
$ ros2 node info <node_name>   # View node information

Tags: ROS2 Robot

Posted by jdavidbakr on Mon, 26 Sep 2022 05:01:34 +0930