文章目录
- 开发环境
- SolidWords插件使用
- 生成urdf文件之后的处理
- CMakeLists文件修改
- package.xml变更
- Launch更改
- 运行
开发环境
Linux系统:Ubuntu 22.04
Ros2版本:humble
Solidwords版本:2023 (2019以上版本应该都是可以的)
SolidWords插件:sw_urdf_exporter 下载
SolidWords插件使用
该功能已经有很多教程,这里不再累述。请移步此处链接
urdf_9">生成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(urdf_robot)find_package(ament_cmake REQUIRED)install(DIRECTORY images launch meshes rviz urdfDESTINATION share/${PROJECT_NAME}
)if(BUILD_TESTING)find_package(ament_lint_auto REQUIRED)ament_lint_auto_find_test_dependencies()
endif()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"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3"><name>urdf_robot</name><version>1.1.0</version><description>This package contains a number of URDF robot.</description><maintainer email="davidvlu@gmail.com">qhf</maintainer><license>BSD 3-clause</license><url>http://ros.org/wiki/urdf_tutorial</url><author email="davidvlu@gmail.com">David V. Lu!!</author><buildtool_depend>ament_cmake</buildtool_depend><exec_depend>urdf_launch</exec_depend><test_depend>ament_lint_auto</test_depend><export><build_type>ament_cmake</build_type><gazebo_ros gazebo_model_path="${prefix}/.."/></export>
</package>
这两项完成之后应当就能顺利使用colcon build编译,编译完成后记得source install里面的setup.bash的路径。最后可以使用ros2 pkg list 来检查下是否已经顺利将包安装到环境中。
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('urdf_robot') #urdf_robot更改为自己的包名default_model_path = PathJoinSubstitution(['urdf', 'QY2023-00019.urdf']) #QY2023-00019.urdf更改为自己的urdf文件default_rviz_config_path = PathJoinSubstitution([urdf_robot_path, 'rviz', 'urdf.rviz'])# These parameters are maintained for backwards compatibilitygui_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 versionsld.add_action(DeclareLaunchArgument(name='model', default_value=default_model_path,description='Path to robot urdf file relative to urdf_robot package'))ld.add_action(IncludeLaunchDescription(PathJoinSubstitution([FindPackageShare('urdf_launch'), 'launch', 'display.launch.py']),launch_arguments={'urdf_package': 'urdf_robot','urdf_package_path': LaunchConfiguration('model'),'rviz_config': LaunchConfiguration('rvizconfig'),'jsp_gui': LaunchConfiguration('gui')}.items()))return ld
运行
编译后,使用一下命令行即可运行
ros2 launch urdf_robot display.launch.py
其中urdf_robot为自己命名的包名,display.launch.py为上述所说的启动文件名。
运行之后还会报一个错误,此错误是由于.urdf文件路径描述ROS1中语法在ROS2中不能识别的原因,所以还需要做将.urdf中关于模型文件的路径更改。
更改前:
<geometry><meshfilename="package://QY2023-00019/meshes/base_link.STL" /></geometry>
更改后:
<geometry><meshfilename="file://$(find urdf_robot)/meshes/base_link.STL" /></geometry>
所有的路径都做这样的修改。然后重新编译运行即可。