6.2.2章节的launch文件我还是报错运行不了,附上我的代码看着和源代码一样
-
import launch
import launch_ros
from ament_index_python.packages import get_package_share_directory
import osdef generate_launch_description():
# 获取默认的urdf文件路径
urdf_package_path = get_package_share_directory('fishbot_description')
default_urdf_path = os.path.join(urdf_package_path, 'urdf', 'first_robot.urdf')
# 声明一个URDF目录的参数,方便修改
action_declare_arg_mode_path = launch.actions.DeclareLaunchArgument(
name = 'model',default_value=str(default_urdf_path),description='加载的模型文件路径') # 通过文件路径,获取内容,并转换成参数值对象,以供robot_state_publisher节点使用 substitutions_command_result = launch.substitutions.command(['cat ',launch.substitutions.LaunchConfiguration('model')]) robot_description_value = launch_ros.parameter_descriptions.ParameterValue(substitutions_command_result,value_type=str) action_robot_state_publisher = launch_ros.actions.Node( package='robot_state_publisher', executable='robot_state_publisher', parameters=[{'robot_description': robot_description_value}] ) action_joint_state_publisher = launch_ros.actions.Node( package='joint_state_publisher', executable='joint_state_publisher', ) action_rviz_node = launch_ros.actions.Node( package='rviz2', executable='rviz2', ) return launch.LaunchDescription([ action_declare_arg_mode_path, action_robot_state_publisher, action_joint_state_publisher, action_rviz_node, ])
以下是报错:[DEBUG] [launch.launch_context]: emitting event synchronously: 'launch.events.IncludeLaunchDescription'
[INFO] [launch]: All log files can be found below /home/jin/.ros/log/2025-04-22-16-49-27-291232-jin-Virtual-Machine-11473
[INFO] [launch]: Default logging verbosity is set to DEBUG
[DEBUG] [launch]: processing event: '<launch.events.include_launch_description.IncludeLaunchDescription object at 0x74ce4db9f730>'
[DEBUG] [launch]: processing event: '<launch.events.include_launch_description.IncludeLaunchDescription object at 0x74ce4db9f730>' ✓ '<launch.event_handlers.on_include_launch_description.OnIncludeLaunchDescription object at 0x74ce4c64fd60>'
[DEBUG] [launch]: An exception was raised in an async action/event
[DEBUG] [launch]: Traceback (most recent call last):
File "/opt/ros/humble/lib/python3.10/site-packages/launch/launch_description_sources/any_launch_file_utilities.py", line 54, in get_launch_description_from_any_launch_file
return loader(launch_file_path)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/launch_description_sources/python_launch_file_utilities.py", line 68, in get_launch_description_from_python_launch_file
return getattr(launch_file_module, 'generate_launch_description')()
File "display_robot.launch.py", line 16, in generate_launch_description
substitutions_command_result = launch.substitutions.command(['cat ',launch.substitutions.LaunchConfiguration('model')])
TypeError: 'module' object is not callableThe above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/ros/humble/lib/python3.10/site-packages/launch/launch_service.py", line 336, in run_async
raise completed_tasks_exceptions[0]
File "/opt/ros/humble/lib/python3.10/site-packages/launch/launch_service.py", line 230, in _process_one_event
await self.__process_event(next_event)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/launch_service.py", line 250, in __process_event
visit_all_entities_and_collect_futures(entity, self.__context))
File "/opt/ros/humble/lib/python3.10/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures
futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures
futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 38, in visit_all_entities_and_collect_futures
sub_entities = entity.visit(context)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/action.py", line 108, in visit
return self.execute(context)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/actions/include_launch_description.py", line 148, in execute
launch_description = self.__launch_description_source.get_launch_description(context)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/launch_description_source.py", line 84, in get_launch_description
self._get_launch_description(self.__expanded_location)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/launch_description_sources/any_launch_description_source.py", line 53, in _get_launch_description
return get_launch_description_from_any_launch_file(location)
File "/opt/ros/humble/lib/python3.10/site-packages/launch/launch_description_sources/any_launch_file_utilities.py", line 57, in get_launch_description_from_any_launch_file
raise InvalidLaunchFileError(extension, likely_errors=exceptions)
launch.invalid_launch_file_error.InvalidLaunchFileError: Caught multiple exceptions when trying to load file of format [py]:- TypeError: 'module' object is not callable
- InvalidFrontendLaunchFileError: The launch file may have a syntax error, or its format is unknown
[ERROR] [launch]: Caught exception in launch (see debug for traceback): Caught multiple exceptions when trying to load file of format [py]:
- TypeError: 'module' object is not callable
- InvalidFrontendLaunchFileError: The launch file may have a syntax error, or its format is unknown
[DEBUG] [launch.launch_context]: emitting event: 'launch.events.Shutdown'
[DEBUG] [launch]: processing event: '<launch.events.shutdown.Shutdown object at 0x74ce4db9c340>'
[DEBUG] [launch]: processing event: '<launch.events.shutdown.Shutdown object at 0x74ce4db9c340>' ✓ '<launch.event_handlers.on_shutdown.OnShutdown object at 0x74ce4db94b50>'
-
找到问题了,原来代码中的方法没有大写,错误的代码是这一行:substitutions_command_result = launch.substitutions.command(['cat ',launch.substitutions.LaunchConfiguration('model')])
正确的需要将command大写首字母同时注意cat后面需要有一个空格: substitutions_command_result = launch.substitutions.Command(['cat ',launch.substitutions.LaunchConfiguration('model')])