环境
- Solidwoks2021 SP5;
 - Ubuntu22.04;
 - ROS2 Humble;
 
步骤
基本步骤参考:Solidworks导出URDF总结(Noetic)
 本文只介绍不同之处。
将solidworks生成的文件夹(我这里为wuwei2)移动到/ws_moveit2/src.
修改CMakeLists.txt 确认project(wuwei2)为自己的名称。
cmake_minimum_required(VERSION 3.5)
project(wuwei2)
find_package(ament_cmake REQUIRED)
install(
  DIRECTORY images launch meshes rviz urdf
  DESTINATION share/${PROJECT_NAME}
)
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()
ament_package()
 
修改package.xml:确认name为自己的名称。
<package format="2">
  <name>wuwei2</name>
  <version>1.0.0</version>
  <description>
    <p>URDF Description package for wuwei2</p>
    <p>This package contains configuration data, 3D models and launch files
for wuwei2 robot</p>
  </description>
  <author>TODO</author>
  <maintainer email="TODO@email.com" />
  <license>BSD</license>
  
  <buildtool_depend>ament_cmake</buildtool_depend>
  <exec_depend>joint_state_publisher</exec_depend>
  <exec_depend>joint_state_publisher_gui</exec_depend>
  <exec_depend>robot_state_publisher</exec_depend>
  <exec_depend>rviz2</exec_depend>
  <exec_depend>xacro</exec_depend>
  <test_depend>ament_lint_auto</test_depend>
  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>
 
修改/launch/display.launch:确认get_package_share_path(‘wuwei2’),urdf_tutorial_path / 'urdf/wuwei2.urdf’为自己的名称。
from ament_index_python.packages import get_package_share_path
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.conditions import IfCondition, UnlessCondition
from launch.substitutions import Command, LaunchConfiguration
from launch_ros.actions import Node
from launch_ros.parameter_descriptions import ParameterValue
def generate_launch_description():
    urdf_tutorial_path = get_package_share_path('wuwei2')
    default_model_path = urdf_tutorial_path / 'urdf/wuwei2.urdf'
    default_rviz_config_path = urdf_tutorial_path / 'rviz/urdf.rviz'
    gui_arg = DeclareLaunchArgument(name='gui', default_value='true', choices=['true', 'false'],
                                    description='Flag to enable joint_state_publisher_gui')
    model_arg = DeclareLaunchArgument(name='model', default_value=str(default_model_path),
                                      description='Absolute path to robot urdf file')
    rviz_arg = DeclareLaunchArgument(name='rvizconfig', default_value=str(default_rviz_config_path),
                                     description='Absolute path to rviz config file')
    robot_description = ParameterValue(Command(['xacro ', LaunchConfiguration('model')]),
                                       value_type=str)
    robot_state_publisher_node = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        parameters=[{'robot_description': robot_description}]
    )
    # Depending on gui parameter, either launch joint_state_publisher or joint_state_publisher_gui
    joint_state_publisher_node = Node(
        package='joint_state_publisher',
        executable='joint_state_publisher',
        condition=UnlessCondition(LaunchConfiguration('gui'))
    )
    joint_state_publisher_gui_node = Node(
        package='joint_state_publisher_gui',
        executable='joint_state_publisher_gui',
        condition=IfCondition(LaunchConfiguration('gui'))
    )
    rviz_node = Node(
        package='rviz2',
        executable='rviz2',
        name='rviz2',
        output='screen',
        arguments=['-d', LaunchConfiguration('rvizconfig')],
    )
    return LaunchDescription([
        gui_arg,
        model_arg,
        rviz_arg,
        joint_state_publisher_node,
        joint_state_publisher_gui_node,
        robot_state_publisher_node,
        rviz_node
    ])
 
新建images文件夹。
 新建rviz文件夹,并新建urdf.rviz:
Panels:
  - Class: rviz_common/Displays
    Name: Displays
  - Class: rviz_common/Views
    Name: Views
Visualization Manager:
  Class: ""
  Displays:
    - Class: rviz_default_plugins/Grid
      Name: Grid
      Value: true
    - Alpha: 0.8
      Class: rviz_default_plugins/RobotModel
      Description Source: Topic
      Description Topic:
        Value: /robot_description
      Enabled: true
      Name: RobotModel
      Value: true
    - Class: rviz_default_plugins/TF
      Name: TF
      Value: true
  Global Options:
    Fixed Frame: base_link
    Frame Rate: 30
  Name: root
  Tools:
    - Class: rviz_default_plugins/MoveCamera
  Value: true
  Views:
    Current:
      Class: rviz_default_plugins/Orbit
      Distance: 1.7
      Name: Current View
      Pitch: 0.33
      Value: Orbit (rviz)
      Yaw: 5.5
Window Geometry:
  Height: 800
  Width: 1200
 
编译:
colcon build --packages-select wuwei2
. install/local_setup.bash
ros2 launch wuwei2 display.launch
 
效果:Robot Model滚动条可以显示转动移动效果。
 
参考
https://github.com/ros/urdf_tutorial/tree/ros2
 https://github.com/xiaoming-sun6/sw2urdf_ros2(尝试过,但是不显示模型)
 https://zhuanlan.zhihu.com/p/465398486
 https://blog.csdn.net/cxyhjl/article/details/120922447



















