Nav2 Localization error when launching the file

Hello the construct team,

I hope all of u are fine

I am facing a problem in the rosject Project of the course ( Ros2 NAV in 5 Days ) in the 2nd part localization in point number 5 we are asked to create :

Create a service that saves these spots into a file
so firstly we should create a pacakge where we will create our srv in it after that we will create other pkg or we can use the localization one in my situation i created other pkg in this pkg we will write a code that will do the thing that we are asked for and i will use this srv that i created in my code then i will create a launch file for this code and run the launch file so then at each point i move the robot i call the service to save the points at the end i will type end and the file will be saved.

So my issue is that when i launch the launch file that has this code it gives me this error which really i did not undrestand it :

So as u this the error msg for more informations i will share my package configuration files :

spots_to_file.py :


from custom_interfaces.srv import MyServiceMessage
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import PoseWithCovarianceStamped
from std_msgs.msg import String

class Service(Node):
    def __init__(self):
        super().__init__('spot_recorder')
        self.service = self.create_service(MyServiceMessage, 'save_spot', self.custom_service_callback)
        self.subscription = self.create_subscription(
            PoseWithCovarianceStamped,
            'amcl_pose',
            self.listener_callback,
            10)
        self.subscription  # prevent unused variable warning
        self.robot_pose = PoseWithCovarianceStamped()
        self.pose_received = 0
        self.text_to_save = str()

    def listener_callback(self, msg):
        self.robot_pose = msg.pose.pose
        self.get_logger().info('I heard a pose')
        self.pose_received += 1
        self.get_logger().info('I heard: "%s"' % self.robot_pose.position.x)

    def custom_service_callback(self, request, response):
        if not self.pose_received:
            self.get_logger().info('I don\'t have a pose to save')
            response.navigation_successful = False
            response.message = 'No pose to save'
            return response

        if request.label == 'end':
            file_path = '/ros2_ws/src/spot_recorder_service/config/spots.txt'
            with open('spots.txt', 'w') as file:
                file.write(self.text_to_save)
            response.navigation_successful = True
            response.message = 'File saved successfully'
        else:
            self.text_to_save += 'Label: {}\n'.format(request.label)
            self.text_to_save += 'Position:\n'
            self.text_to_save += '- x: {}\n'.format(self.robot_pose.position.x)
            self.text_to_save += '- y: {}\n'.format(self.robot_pose.position.y)
            self.text_to_save += '- z: {}\n'.format(self.robot_pose.position.z)
            self.text_to_save += 'Orientation:\n'
            self.text_to_save += '- x: {}\n'.format(self.robot_pose.orientation.x)
            self.text_to_save += '- y: {}\n'.format(self.robot_pose.orientation.y)
            self.text_to_save += '- z: {}\n'.format(self.robot_pose.orientation.z)
            self.text_to_save += '- w: {}\n\n'.format(self.robot_pose.orientation.w)
            response.navigation_successful = True
            response.message = 'Spot added'
            self.get_logger().info('Spot added')

        return response

def main(args=None):
    rclpy.init(args=args)
    service = Service()
    rclpy.spin(service)
    service.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

Spot_recorder_file.launch.py :


from launch import LaunchDescription
from launch_ros.actions import Node

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

Setup.py


from setuptools import setup
import os
from glob import glob

package_name = 'spot_recorder_service'

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='user',
    maintainer_email='user@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
        
        'spots_to_file = spot_recorder_service.spots_to_file:main',

        ],
    },
)

I hope anyone can help me ,

For more details feel free to ask,

Thank you

Ghassan

Hi @ghassan11 ,

I need your complete package file tree. You can expand all the folders in your IDE left panel.
Please post a picture of only the expanded contents of this package.

You need to include the config folder path in your setup.py file, just like you have done for launch folder.

Regards,
Girish

YOU mean this one :sweat_smile: :grin:

I fix the error u told me but now when i launch it
i get this error :

file ‘spot_recorder_file.launch.py’ was found more than once in the share directory of package ‘spot_recorder_service’: [‘/home/user/ros2_ws/install/spot_recorder_service/share/spot_recorder_service/spot_recorder_file.launch.py’, ‘/home/user/ros2_ws/install/spot_recorder_service/share/spot_recorder_service/launch/spot_recorder_file.launch.py’]

Hi @ghassan11 ,

Thanks for sharing the image,

You get the executable ‘spot_recorder’ not found error because you have not defined such an executable.

Looking into your setup.py file, in the following line:

'spots_to_file = spot_recorder_service.spots_to_file:main',
# generic formula
'<executable_name> = <package_name>.<python_file_name>:main'

The executable name that you have defined is spots_to_file and not spot_recorder.

You should modify your launch file this way:

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='spot_recorder_service',
            # executable='spot_recorder',   # <--- this line is wrong
            executable='spots_to_file',   # <--- this line is correct
            output='screen'),
    ])

Also, like I have told you in the previous post, you need to add config folder path in setup.py file:

(os.path.join('share', package_name), glob('config/**'))

These 3 steps should fix your issue.

You need to know how to debug your issues. For this, you need to carefully follow the actions that you do while learning the course materials. For this you need to be very attentive when learning the course!

Regards,
Girish

Sure i will try to be more attentive

Thank you

1 Like

Hi @girishkumar.kannan

Mr i did as u told me but i got this error message now

Hi @ghassan11 ,

Recompile your ROS2 workspace from scratch.

cd ~/ros2_ws
rm -rf ./build ./install ./log
colcon build
source install/setup.bash

Then launch your programs using ros2 launch ....

Regards,
Girish

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