@43996173
display_robot.launch.py代码:

import launch import launch_ros from ament_index_python.packages import get_package_share_directory def generate_launch_description(): urdf_tutorial_path = get_package_share_directory('fishbot_description') default_model_path = urdf_tutorial_path + '/urdf/first_robot.urdf' #print (str(default_model_path)) default_rviz_config_path = urdf_tutorial_path + '/config/rviz/display_model.rviz' action_declare_arg_mode_path = launch.actions.DeclareLaunchArgument( name='model', default_value=str(default_model_path), description='URDF的绝对路径') robot_description=launch_ros.parameter_descriptions.ParameterValue( launch.substitutions.Command( ['xacro ', launch.substitutions.LaunchConfiguration('model')]), value_type=str) robot_state_publisher_node = launch_ros.actions.Node ( package='robot_state_publisher', executable='robot_state_publisher', parameters=[{'robot_description':robot_description}]) joint_state_publisher_node = launch_ros.actions.Node( package='joint_state_publisher', executable='joint_state_publisher', ) rviz_node = launch_ros.actions.Node( package='rviz2', executable='rviz2', arguments=['-d', default_rviz_config_path] ) return launch.LaunchDescription([ action_declare_arg_mode_path, joint_state_publisher_node, robot_state_publisher_node, rviz_node ])

first_robot.urdf.xacro代码:

<?xml version="1.0"?> <robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="first_robot"> <!--声明base模块--> <xacro:macro name="base_link" params="length radius"> <link name="base_link"> <visual> <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/> <geometry> <cylinder radius="${length}" length="${length}"/> </geometry> <material name="white"> <color rgba="1.0 1.0 1.0 0.5"/> </material> </visual> </link> </xacro:macro> <!--声明imu模块--> <xacro:macro name="imu_link" params="imu_name xyz"> <link name="${imu_name}_link"> <visual> <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/> <geometry> <box size="0.02 0.02 0.02"/> </geometry> </visual> <material name="black"> <color rgba="0.0 0.0 0.0 0.5"/> </material> </link> <joint name="${imu_name}_joint" type="fixed"> <parent link="base_link"/> <child link="${imu_name}_link"/> <origin xyz="${xyz}"/> </joint> </xacro:macro> <!--传递参数调用base_link模块--> <xacro:base_link length="0.12" radius="0.1"/> <!--传递参数调用imu模块--> <xacro:imu_link imu_name="imu_up" xyz="0 0 0.02" /> <xacro:imu_link imu_name="imu_down" xyz="0.0 0.0 -0.02" /> </robot>