Laserscan_callback not being called

I am on lesson 3.5 and am trying to run the ROS2 node after making the addition of the subscriber callback. When I compile and run the only output I get is

[INFO] [1751294334.829143156] [obstacle_detector_node]: obstacle_detector_node Ready…

I have checked my code against the example provided in the course and can’t find the issue. My subscriber_obstacle_detector.py

  			#!/usr/bin/env python
				import rclpy
				from rclpy.node import Node
				# import the LaserScan module from sensor_msgs interface
				from sensor_msgs.msg import LaserScan
				# import Quality of Service library, to set the correct profile and reliability to read sensor data.
				from rclpy.qos import ReliabilityPolicy, QoSProfile
				 
				class ObstacleDetectorNode(Node):
				    def __init__(self, node_name="obstacle_detector_node"):
				        self._node_name = node_name
				        super().__init__(self._node_name)
				 
				                # create the subscriber object
				        # in this case, the subscriptor will be subscribed on /scan topic with a queue size of 10 messages.
				        # use the LaserScan module for /scan topic
				        # send the received info to the laserscan_callback method.
				        self.subscriber = self.create_subscription(
				            LaserScan,
				            '/laser_scan',
				            self.laserscan_callback,
				            QoSProfile(depth=10, reliability=ReliabilityPolicy.RELIABLE))  # is the most used to read LaserScan data
				 
				        self.get_logger().info(self._node_name +" Ready...")
				 
				    def laserscan_callback(self, msg):
				        # Find the minimum distance in the ranges array
				        min_distance = min(msg.ranges)
				        
				        # Log the minimum distance value
				        self.get_logger().info(f'Minimum distance: {min_distance:.2f} meters')
				        
				def main(args=None):
				    rclpy.init(args=args)
				    node = ObstacleDetectorNode()
				    rclpy.spin(node)
				    rclpy.shutdown()
				 
				if __name__ == '__main__':
                    main()

I’m not sure what the issue is. The launch and setup files also seem correct, but clearly I have gone wrong somewhere.

My mars_rover_tasks/launch.py

from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return([
Node(
package = ‘mars_rover_tasks’,
executable = ‘subscriber_obstacle_detector_executable’,
output = ‘screen’)
])

my setup.py

from setuptools import find_packages, setup
import os
from glob import glob
package_name = ‘mars_rover_tasks’
setup(
name=package_name,
version=‘0.0.0’,
packages=find_packages(exclude=[‘test’]),
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=‘user’,
maintainer_email=‘user@todo.todo’,
description=‘TODO: Package description’,
license=‘TODO: License declaration’,
tests_require=[‘pytest’],
entry_points={
‘console_scripts’: [
‘subscriber_obstacle_detector_executable = mars_rover_tasks.subscriber_obstacle_detector:main’,
],
},
)

Hello @abulot

I just tested your code and it works fine for me

Did you compiled your package to apply the latest changes by doing:

cd ~/ros2_ws/
source install/setup.bash
colcon build --packages-select mars_rover_tasks
source install/setup.bash

and then test your code using:

cd ~/ros2_ws/
source install/setup.bash
ros2 run mars_rover_tasks subscriber_obstacle_detector_executable.

Please note that these names are provided from the course and you need toi change accordingly if you have chnaged the file names. Check it and please update. Make sure the simulation is also working properly.

Its working now! I had closed the simulator tab while writing the code to give me more space and assumed it was still running in the background, turns out it wasn’t. Lesson learned LOL!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.