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