开发环境

Linux系统:Ubuntu 22.04
Ros2版本:humble
Solidwords版本:2023 (2019以上版本应该都是可以的)
SolidWords插件:sw_urdf_exporter 下载

SolidWords插件使用

该功能已经有很多教程,这里不再累述。请移步此处链接

生成urdf文件之后的处理

CMakeLists文件修改

ROS2中已经没有catkin_make这样的工具,统一使用colcon来进行编译,可以看到cmake文件中包含了catkin_make。所以需要先更改此处的文件。更改之前的文件为

cmake_minimum_required(VERSION 2.8.3)

project(QY2023-00019)

find_package(catkin REQUIRED)

catkin_package()

find_package(roslaunch)

foreach(dir config launch meshes urdf)
	install(DIRECTORY ${dir}/
		DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/${dir})
endforeach(dir)

需变更为,项目名与需要构建成的包名一致

cmake_minimum_required(VERSION 3.5)
project(section_chassis)

# 默认的构建类型
if(NOT CMAKE_BUILD_TYPE)
  message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
endif()

# 找到ament_cmake
find_package(ament_cmake REQUIRED)

# 包含的目录
include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# 安装文件
foreach(dir config launch meshes urdf)
  install(DIRECTORY ${dir}/
    DESTINATION share/${PROJECT_NAME}/${dir})
endforeach(dir)

# 如果有可执行文件或库,添加它们
# add_executable(your_executable src/your_source_file.cpp)
# ament_target_dependencies(your_executable rclcpp std_msgs)
# install(TARGETS your_executable DESTINATION lib/${PROJECT_NAME})

# 创建一个包
ament_package()

package.xml变更

默认的包名直接使用3D模型的名称,这里主要是更改下包名称和版本,以及一些过时的依赖信息。sw导出后的package.xml

<package format="2">
  <name>QY2023-00019</name>
  <version>1.0.0</version>
  <description>
    <p>URDF Description package for QY2023-00019</p>
    <p>This package contains configuration data, 3D models and launch files
for QY2023-00019 robot</p>
  </description>
  <author>TODO</author>
  <maintainer email="TODO@email.com" />
  <license>BSD</license>
  <buildtool_depend>catkin</buildtool_depend>
  <depend>roslaunch</depend>
  <depend>robot_state_publisher</depend>
  <depend>rviz</depend>
  <depend>joint_state_publisher_gui</depend>
  <depend>gazebo</depend>
  <export>
    <architecture_independent />
  </export>
</package>

更改之后的,需要主要的是exec_depend项使用了urdf_launch。需要先下载编译安装运行这个包,链接地址

<?xml version="1.0"?>
<package format="3">
  <name>section_chassis</name>
  <version>1.0.0</version>
  <description>
    URDF Description package for section_chassis.
    This package contains configuration data, 3D models and launch files for the section_chassis robot.
  </description>
  <maintainer email="lvhaidong520@163.com">lvhaidong</maintainer>
  <license>BSD</license>

  <!-- Build tool dependencies -->
  <buildtool_depend>ament_cmake</buildtool_depend>

  <exec_depend>urdf_launch</exec_depend>

  <test_depend>ament_lint_auto</test_depend>

  <!-- Export tags -->
  <export>
    <build_type>ament_cmake</build_type>
    <gazebo_ros gazebo_model_path="${prefix}/.."/>
  </export>
</package>

这两项完成之后应当就能顺利使用colcon build编译,编译完成后记得source install里面的setup.bash 或者 setup.zsh的路径, 需要根据终端配置的是bash或者zsh来决定使用那个。最后可以使用ros2 pkg list 来检查下是否已经顺利将包安装到环境中, 其中section_chassis是包名称:

ros2 pkg list | grep section_chassis

Launch更改
使用sw_urdf_exporter插件导出的只有*.launch文件,所以需要自己写一个python的启动文件。我这里就直接参考了官方的urdf_tutorial的包,源码地址。记得切换下ROS2分支。以下是参考的display.launch.py文件,需要更改的部分已经用中文注释。新建这样的py文件放到导出的urdf文件夹中即可:

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
from launch_ros.substitutions import FindPackageShare

def generate_launch_description():
    ld = LaunchDescription()
    urdf_robot_path = FindPackageShare('section_chassis')	# section_chassis 更改为自己的包名
    default_model_path = PathJoinSubstitution(['urdf', 'section_chassis.urdf']) # section_chassis.urdf更改为自己的urdf文件
    default_rviz_config_path = PathJoinSubstitution([urdf_robot_path, 'rviz', 'urdf.rviz'])
    # These parameters are maintained for backwards compatibility
    gui_arg = DeclareLaunchArgument(name='gui', default_value='true', choices=['true', 'false'],
                                    description='Flag to enable joint_state_publisher_gui')
    ld.add_action(gui_arg)
    rviz_arg = DeclareLaunchArgument(name='rvizconfig', default_value=default_rviz_config_path,
                                     description='Absolute path to rviz config file')
    ld.add_action(rviz_arg)
    # This parameter has changed its meaning slightly from previous versions 
    # 描述的包名称也需要变更一下
    ld.add_action(DeclareLaunchArgument(name='model', default_value=default_model_path,
                                        description='Path to robot urdf file relative to section_chassis package'))
    ld.add_action(IncludeLaunchDescription(
        PathJoinSubstitution([FindPackageShare('urdf_launch'), 'launch', 'display.launch.py']),
        launch_arguments={
            'urdf_package': 'section_chassis', # urdf包名称
            'urdf_package_path': LaunchConfiguration('model'),
            'rviz_config': LaunchConfiguration('rvizconfig'),
            'jsp_gui': LaunchConfiguration('gui')}.items()
    ))
    return ld

由于.urdf文件路径描述ROS1中语法在ROS2中不能识别的原因,所以还需要做将.urdf中关于模型文件的路径更改。
更改前:

      <geometry>
        <mesh
          filename="package://QY2023-00019/meshes/base_link.STL" />
      </geometry>

更改后:

      <geometry>
        <mesh
          filename="package://section_chassis/meshes/base_link.STL" />
      </geometry>

或者:

      <geometry>
        <mesh
          filename="package://section_chassis/meshes/base_link.STL" />
      </geometry>

 编译后,启动文件如下:

colcon build --symlink-install
source install/set.bash
ros2 launch section_chassis display.launch.py

生成gazebo.launch.py文件, 标注中文的地方需要替换即可, 如下:

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
from launch_ros.substitutions import FindPackageShare

def generate_launch_description():
    ld = LaunchDescription()
    urdf_robot_path = FindPackageShare('section_chassis')	# section_chassis 更改为自己的包名
    default_model_path = PathJoinSubstitution(['urdf', 'section_chassis.urdf']) # section_chassis.urdf更改为自己的urdf文件
    default_rviz_config_path = PathJoinSubstitution([urdf_robot_path, 'rviz', 'urdf.rviz'])
    # These parameters are maintained for backwards compatibility
    gui_arg = DeclareLaunchArgument(name='gui', default_value='true', choices=['true', 'false'],
                                    description='Flag to enable joint_state_publisher_gui')
    ld.add_action(gui_arg)
    rviz_arg = DeclareLaunchArgument(name='rvizconfig', default_value=default_rviz_config_path,
                                     description='Absolute path to rviz config file')
    ld.add_action(rviz_arg)
    # This parameter has changed its meaning slightly from previous versions 
    # 描述的包名称也需要变更一下
    ld.add_action(DeclareLaunchArgument(name='model', default_value=default_model_path,
                                        description='Path to robot urdf file relative to section_chassis package'))
    ld.add_action(IncludeLaunchDescription(
        PathJoinSubstitution([FindPackageShare('urdf_launch'), 'launch', 'display.launch.py']),
        launch_arguments={
            'urdf_package': 'section_chassis', # urdf包名称
            'urdf_package_path': LaunchConfiguration('model'),
            'rviz_config': LaunchConfiguration('rvizconfig'),
            'jsp_gui': LaunchConfiguration('gui')}.items()
    ))
    return ld

编辑 section_chassis.udrf文件, 去掉字符编码, 变更后的格式如下:

<?xml version="1.0"?>

启动仿真:

ros2 launch section_chassis gazebo.launch.py

提示如下警告:

The root link base_link has an inertia specified in the URDF, but KDL does not support a root link with an inertia.  As a workaround, you can add an extra dummy link to your URDF

在section_chassis.urdf中添加如下信息:

<link name="dummy">
   </link>
  <joint name="dummy_joint" type="fixed">
     <parent link="dummy"/>
     <child link="base_link"/>
   </joint>

警告:

如果在python3.10版本中,使用如下命令创建的模块:

ros2 pkg create --build-type ament_python robot_msgs --license Apache-2.0  

编译的时候,提示: easy_install command is deprecated。需要降级setuptools的版本, 执行如下命令:

import setuptools
print(setuptools.__version__)
pip install setuptools==58.2.0

Logo

立足具身智能前沿赛道,致力于搭建全球化、开源化、全栈式技术交流与实践共创平台。

更多推荐