5. ROS Node

In the previous part, we already mentioned the node. In this chapter, we will get into the details.

Let’s start by taking a look at the complete file again:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""OnboardingNode."""

from simulation.utils.ros_base.node_base import NodeBase


class OnboardingNode(NodeBase):
    """ROS node to teach new members."""

    def __init__(self):
        """Initialize the node."""
        super().__init__(name="onboarding_node")

        # Start running node
        # This will do the following
        # 1) Call self.start()
        # 2) Call self.steer() 60 times a second!
        # 3) Call self.stop() when ROS is shutting down
        self.run(function=self.steer, rate=60)

    def start(self):
        """Start node."""
        # When overwriting a function, ensure that the original function (NodeBase.start())
        # is also called
        super().start()

    def stop(self):
        """Turn off node."""
        # When overwriting a function, ensure that the original function (NodeBase.stop())
        # is also called
        super().stop()

    def steer(self):
        """Control the car's pose to drive along the road."""
        pass

The OnboardingNode class is a subclass of NodeBase (Go check it out). This speeds up the development process by handling all rospy related functions.

5.1. __init__

Let’s start by looking at __init__():

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

    def __init__(self):
        """Initialize the node."""
        super().__init__(name="onboarding_node")

        # Start running node
        # This will do the following
        # 1) Call self.start()
        # 2) Call self.steer() 60 times a second!
        # 3) Call self.stop() when ROS is shutting down
        self.run(function=self.steer, rate=60)

The function super() returns the next super class of self. (You can take a look at one of our short talks for more: Inheritance in Python.) In this case, it’s NodeBase. The method __init__() gets called with the argument name. This introduces the node to ROS and loads parameters specified inside onboarding_node.launch.

Next, self.run() is called. It is defined within NodeBase and will correctly start and shutdown the node by calling self.start() and self.stop(). When passing function=self.steer and rate=60 as arguments, NodeBase will also call the function self.steer() 60 times a second.

5.2. start

start() is called before when the node is started. In this example, nothing happens, but the supers’ start() method gets called. At the moment it doesn’t do a thing but you will need to add code here later.

1
2
3
4
5
6
7

    def start(self):
        """Start node."""
        # When overwriting a function, ensure that the original function (NodeBase.start())
        # is also called
        super().start()

5.3. stop

stop() is much like start. It is called when the node is shutting down. And again it doesn’t do anything at the moment.

1
2
3
4
5
6
7

    def stop(self):
        """Turn off node."""
        # When overwriting a function, ensure that the original function (NodeBase.stop())
        # is also called
        super().stop()

5.4. steer

As described earlier, steer() is called 60 times a second. It is empty, but you will add code to it later in this tutorial.

1
2
3
4

    def steer(self):
        """Control the car's pose to drive along the road."""
        pass