Main method not being invoked

I am starting with the wall follower code for the project, and I am trying to make sure my subscriber is working. But when I try to print anything in the code any where, nothing is being output. I tried pasting my code in the code assistant but found nothing.
Here is the code:

import rclpy
from rclpy.node import Node
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.executors import MultiThreadedExecutor

class WallFollower(Node):

    def __init__(self):
        super().__init__('wall_follower')
        self.scan = LaserScan()
        self.laser_sub = self.create_subscription(
            LaserScan,
            'scan',
            self.get_laser,
            10,
            callback_group=ReentrantCallbackGroup()
        )
        self.move_pub = self.create_publisher(Twist, 'cmd_vel', 10)
        self.move = Twist()

    def get_laser(self, msg):
        self.scan = msg.ranges
        self.get_logger().debug(f"Length of scan: {len(self.scan)}")

def main(args=None):
    rclpy.init(args=args)
    follower_node = WallFollower()
    executor = MultiThreadedExecutor(num_threads=4)
    executor.add_node(follower_node)
    executor.spin()

if __name__ == '__main__':
    main()

Hello Abboud!
I am a bit rusty on ROS2, but I would like to know if you call this node using a launch file.
If this is the case, I remember if you want to see the output of the node, you would have to set an argument typically

Node(
            package='<your_package>',
            executable='<your_executable>',
            <your_args>
            output='screen',
            emulate_tty=True
        )
    ])

Could this be your issue? Sorry if I didn’t help you, I’m sure someone will help you soon

Thank you for the help but I am not familiar with argument setting, from what I know my args is None. The thing is this never happened before so I am abit lost.

Hi @Abboud ,

I think you forgot to add rclpy.shutdown() after executor.spin().

Try adding this line and check again. If you still have issues then you might have missed something in your setup.py file.

Regards,
Girish

I added the shutdown line and it still does not enter main(). Here is my setup file:

from setuptools import setup
import os
from glob import glob

package_name = 'wall_follower'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        (os.path.join('share', package_name), glob('launch/*.launch.py'))
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='somebody very awesome',
    maintainer_email='user@user.com',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'follow_node = wall_follower.wall_follow:main'
        ],
    },
)

And this is my launch file:

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='wall_follower',
            executable='follow_node',
            output='screen'),
    ])

Hi @Abboud ,

There are no problems in your launch file or your setup.py file.

I just noted something after taking a second look into your code.
Your code does not print anything in the info logger. The only working function you have at the moment is the scan callback that prints the length in debug loggger.

Since you have not set the logging level to debug, you are unable to see the debug messages on your console output.

Fix:
Change this line:
self.get_logger().debug(f"Length of scan: {len(self.scan)}")
to this line:
self.get_logger().info(f"Length of scan: {len(self.scan)}")

I guess your main function is being triggered, since the message was debug you were unable to see them, which made you conclude that the program did not enter the main function.

Try this fix and let me know if it worked for you.

Regards,
Girish

That totally fixed the issue, I had tried normal print statements before but they did not work either. Thank you for the help !!

Hi @Abboud ,

Glad to know that your problem is resolved!

Also by the way, you can also enable the printing of debug messages in your console output.
You need to set logging level to debug in your program. This is explained in the ROS2 Basics course.

Here is a quick-look:

class MyNode(Node):

    def __init__(self):
        super().__init__("my_node_name")
        # the line below sets the logger level
        rclpy.logging.set_logger_level("my_node_name",
                                       rclpy.logging.LoggingSeverity.DEBUG)
        # other code stuff
        return None

Also set colors for the console output so debug statements will have different colors.
In your launch file:

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='wall_follower',
            executable='follow_node',
            emulate_tty=True,   # <--- add this line for colored output
            output='screen'),
    ])

That would be all!

Regards,
Girish

1 Like