环境ubuntu20.04 ROS-noetic
国内少有搭建Moveit和Gazebo联合仿真的教程,对于搭建双臂等复杂的仿真平台更是鲜有资料,因此想要把自己的见解分享出来供大家参考,共同提高。
本文提出了两种方法实现Moveit 对双臂的规划,并在gazebo中进行仿真
方案一
首先是方案一,主要思路是:使用moveit setup assistant配置规划组的时候,创建一个父组,包含两条臂的规划组。
先说方案一的优缺点:
优点:两条臂互相知道对方的存在,Moveit进行规划的时候会考虑到两条臂之间的避障
缺点:Moveit不能实现对两条臂的独立控制,如果对单独臂的规划组进行操作时,规划动作的执行是阻塞的,即一条臂执行完,另一条臂才能继续执行;如果对含有两个臂的规划组的父组进行操作时,存在的问题是:Moveit规划时总是保证两个臂的运行时间是一致的,即两个臂的轨迹长度相差较大时,Moveit使两个臂的运行速度相差很大,保证两者同时开始,同时结束。
这样不能实现我想要的两个臂比较独立的控制,但是实现起来比较简单。
实现的效果展示:
主要过程参考moveit官方教程:Multiple Robot Arms — moveit_tutorials Noetic documentation
但是官方教程中,并未详细说明如何控制双臂同时规划,所以这里我们对该部分详细说明,教程中已有的部分不再赘述,建议读者先仔细阅读教程。
Moveit setup assistant配置
进入正文,首先在 moveit setup assistant配置部分,我们在完成教程已有的内容后,还需要增加一个group,我这里命令为dual_arm(ps:这里前后图片规划组的名称一个是dual_arm 一个是dual_arms,是我两次实践过程中命名不同而已,读者要注意统一,别被我误导)
选择 add subgroups 然后把之前创建的四个group(这部分内容看官方教程)分别是right_arm 、left_arm、left_hand、right_hand添加进去
然后是关于eef末端执行器的配置,制定两个末端执行器,这里是为了方便后续编程同时控制双臂,指明两个臂各自的末端执行器,后续才能通过编程接口进行控制。
这里moveit setup assistant配置我们需要额外添加的部分(或者与教程不同的部分)就全部完成了,生成moveit_config包,我这里命名的是 dual_arm_moveit_config,生成之后进行编译,运行demo.launch ,运行效果如下图所示:
当rviz界面左下角MotionPlanning插件,Planning Group选择dual_arms(dual_arm)时,可以看到两条臂都有可以被拖动的标记,拖动 goal state到一个你想要的位置,然后plan execute,可以看到两条臂同时运动了!
如果两条臂处于碰撞状体时,接触的相应部分也会变红,这说明Moveit在规划时会考虑两条臂之间的碰撞。
当然Planning Group选择right_arm或者left_arm时也能实现对单独臂进行控制。
编程接口
刚才仅是在rviz中的可视化界面实现了控制,下面介绍如何通过编程接口进行对双臂的控制。
先给出代码,主要的介绍都在注释中。
#include <ros/ros.h>
#include <moveit/move_group_interface/move_group_interface.h>
//包含需要的头文件
int main(int argc, char** argv)
{
ros::init(argc,argv,"demo");
ros::NodeHandle node_handle;
//ros节点初始化
//开辟线程,主要作用是为move group节点获取当前机器人状态
ros::AsyncSpinner spinner(1);
spinner.start();
//这些string都是之前我们在setup assistant配置中定义的,一定要保持一致
static const std::string right_arm_group = "right_arm";
static const std::string left_arm_group = "left_arm";
static const std::string dual_arm_group = "dual_arms";
static const std::string right_end_effector_link = "right_arm_link8";
static const std::string left_end_effector_link = "left_arm_link8";
//实例化move group接口
moveit::planning_interface::MoveGroupInterface right_arm_move_group_interface(right_arm_group);
moveit::planning_interface::MoveGroupInterface left_arm_move_group_interface(left_arm_group);
moveit::planning_interface::MoveGroupInterface dual_arm_move_group_interface(dual_arm_group);
//这个ready也是在setup assistant配置中提前定义好的pose,详细过程见官方教程
right_arm_move_group_interface.setNamedTarget("ready");
left_arm_move_group_interface.setNamedTarget("ready");
//实例化一些plan
moveit::planning_interface::MoveGroupInterface::Plan right_arm_plan;
moveit::planning_interface::MoveGroupInterface::Plan left_arm_plan;
moveit::planning_interface::MoveGroupInterface::Plan dual_arm_plan;
//对机械臂运行到ready位姿进行规划和执行,可以看到机械臂的运动是阻塞执行的,直到right arm执行完,才去执行left arm,因此这是我们需要dual arm group的原因
bool rgt_success = (right_arm_move_group_interface.plan(right_arm_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
if(rgt_success)
{
right_arm_move_group_interface.execute(right_arm_plan);
}
bool lft_success = (left_arm_move_group_interface.plan(left_arm_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
if(lft_success)
{
left_arm_move_group_interface.execute(left_arm_plan);
}
//实例化两个pose,从当前pose进行修改,获得target pose
geometry_msgs::PoseStamped current_right_arm_pose = right_arm_move_group_interface.getCurrentPose();
geometry_msgs::PoseStamped current_left_arm_pose = left_arm_move_group_interface.getCurrentPose();
geometry_msgs::PoseStamped target_right_arm_pose = current_right_arm_pose;
target_right_arm_pose.pose.position.z -= 0.3;
target_right_arm_pose.pose.position.x += 0.4;
target_right_arm_pose.pose.position.x = -target_right_arm_pose.pose.position.x;
geometry_msgs::PoseStamped target_left_arm_pose = current_left_arm_pose;
target_left_arm_pose.pose.position.z += 0.2;
//设置双臂的规划目标,可以看到setPoseTarget函数通过指定了末端执行器的link来区分两个臂,可以看到两个臂是同时运行的dual_arm_move_group_interface.setPoseTarget(target_right_arm_pose,right_end_effector_link);
dual_arm_move_group_interface.setPoseTarget(target_left_arm_pose,left_end_effector_link);
bool dual_success = (dual_arm_move_group_interface.plan(dual_arm_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
if(dual_success)
{
dual_arm_move_group_interface.execute(dual_arm_plan);
}
//结束
ros::shutdown();
return 0;
}
这是官网code API文档介绍:Source Code & API | MoveIt
cmakelist:文章来源:https://www.toymoban.com/news/detail-401490.html
cmake_minimum_required(VERSION 3.0.2)
project(dual_arms)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
geometry_msgs
moveit_ros_planning_interface
moveit_ros_planning
)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )
## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# std_msgs # Or other packages containing msgs
# )
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES dual_arms
# CATKIN_DEPENDS other_catkin_pkg
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
include
${catkin_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/dual_arms.cpp
# )
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/dual_arms_node.cpp)
add_executable(demo src/demo.cpp)
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_dependencies(demo ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
# ${catkin_LIBRARIES}
# )
target_link_libraries(demo
${catkin_LIBRARIES}
)
#############
## Install ##
#############
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# catkin_install_python(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
#############
## Testing ##
#############
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_dual_arms.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
效果视频
Moveit+Gazebo:搭建双臂仿真平台(方案一)_哔哩哔哩_bilibili 文章来源地址https://www.toymoban.com/news/detail-401490.html
到了这里,关于Moveit + Gazebo:搭建双臂仿真平台(方案一)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!