0.前言
以下參考:摘自兩個月前我的筆記
但是我已經都不記得啥了,加上我當時仿真用的是UR5,但是我現在僅有一臺UR10,你可以先將前言部分劃過,往下看,
我本篇blog主要內容是:ROS下Rviz聯合Momeit實作UR10機器人的直線和圓弧軌跡規劃
如果你在實作程序出現bug,可以去看我上邊的筆記摘抄,應該有大部分bug的處理方法,希望今天我不會再一次處理他們,
2020/12月軌跡規劃學習筆記
一、直線軌跡規劃的實作
1.思路:通過moveit的官方檔案,實作ur5機器人在rviz+moveit下的笛卡爾空間的直線和圓弧軌跡規劃,我想做平面的
2.運行panda機器人的c++檔案
roslaunch panda_moveit_config demo.launch
roslaunch moveit_tutorials move_group_interface_tutorial.launch
3.更改.cpp檔案move_group_interface_tutorial.cpp
①中間若是目標節點不在作業空間,會出現軌跡規劃失敗
②四元數的定義
個四元數可以表示為q = w + xi + yj + zk, 給定一個單位長度的旋轉軸(x, y, z)和一個角度θ,對應的四元數為:q=((x,y,z)sinθ2, cosθ2)
③這個.cpp檔案實作蠻多功能的呢
PTP(兩種實作方法)→路徑限制的PTP →cartesian 路徑規劃 →抓取或者松開物體 →避障礙物規劃
4.根據panda的c++控制檔案寫一個ur5的
新建檔案路徑為ws_moveit/src/moveit_tutorials/doc/move_group_interface
①先把ur5_e_moveit_config功能包弄過來,看看是不是可以正常運行
報錯:No such file or directory: /opt/ros/melodic/share/ur_description/urdf/ur5.urdf.xacro
原因:之前的share處的ur5描述檔案被我更改過
解決:cd /opt/ros/melodic/share/
sudo rm -rf ur_description/
sudo cp -rf ~/catkin_UR/src/universal_robot/ur_description/* /opt/ros/melodic/share
(注意這行代碼是把ur_description下的檔案夾復制到share路徑下,所以我稍微手動處理了一部分)
備注:這個部位我應該誤洗掉了share下的檔案,導致出現以下報錯(catkin build 失敗)
CMake Error at /opt/ros/melodic/share/kdl_parser/cmake/kdl_parserConfig.cmake:197 (find_package):
Could not find a package configuration file provided by “urdf” with any of
the following names:urdfConfig.cmake urdf-config.cmake Add the installation prefix of "urdf" to CMAKE_PREFIX_PATH or set“urdf_DIR” to a directory containing one of the above files. If
“urdf” provides a separate development package or SDK, be sure it
has been installed.解決:由于這個ROS我越處理出現的問題越多,我最后決定重裝ROS正好溫習一下(微笑) 會把安裝ROS以及各個插件的流程放在“二、”中
②上文中①我把/opt/ros/melodic改出問題來了,所以重新裝了ros和moveit 運行ur5的demo.launch
要先確保安裝了ur機器人,這樣share檔案夾下可以找到ur_description②添加c++檔案,用moveit控制ur5做直線軌跡規劃
添加ws_moveit/src/moveit_tutorials/doc/move_group_interface/launch/move_group_interface_ur5.launch
添加ws_moveit/src/moveit_tutorials/doc/move_group_interface/src/move_group_interface_ur5.cpp
修改ws_moveit/src/moveit_tutorials/doc/move_group_interface/Cmake.lists
在結尾加上三行 基于.cpp生成devel和build下的可執行檔案 catkin build 運行c++的可執行檔案:
roslaunch ur5_moveit_config demo.launch
二、卸載和安裝ROS
1.卸載 sudo apt-get remove ros-*
2.安裝 1)安裝ROS設定源: sudo sh -c ‘. /etc/lsb-release && echo “deb
http://mirrors.ustc.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main” >
/etc/apt/sources.list.d/ros-latest.list’ 設定秘鑰:sudo apt-key adv
–keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654 安裝ROS: sudo apt-get update sudo apt-get install ros-melodic-desktop-full
初始化rosdep: sudo apt install python-rosdep2 sudo rosdep init
如果這個位置還是報錯打不開網頁 嘗試: #打開hosts檔案 sudo gedit /etc/hosts #在檔案末尾添加
151.101.84.133 raw.githubusercontent.com #保存后退出再嘗試 rosdep update 安裝rosinstall: sudo apt-get install python-rosinstall
python-rosinstall-generator python-wstool build-essential添加環境配置 echo
“source /opt/ros/melodic/setup.bash” >> ~/.bashrc source ~/.bashrc
備注:注意要是想打開bashrc 命令為 gedit ~/.bashrc 更改之后要生效需要source ~/.bashrc sudo
apt-get install ros-melodic-desktop 測驗:roscore 2)安裝moveit
安裝catkin的build系統: sudo apt-get install ros-melodic-catkin
python-catkin-tools 安裝moveit: sudo apt install ros-melodic-moveit
打開你的作業空間: cd ~/ws_moveit/src rosdep install -y --from-paths .
–ignore-src --rosdistro melodiccd ~/ws_moveit catkin config --extend /opt/ros/${ROS_DISTRO}
–cmake-args -DCMAKE_BUILD_TYPE=Release catkin build
1.通信建立
1.1先下載catkin_UR功能包和ws_moveit功能包(我希望你是一個懂得很多道理的人兒了,比如怎么下載這兩個功能包)
1.2檢測ws_moveit功能包是否正常
roslaunch panda_moveit_config demo.launch
roslaunch moveit_tutorials move_group_interface_tutorial.launch

我這個代碼我改過(所以我的運行結果是寫王字),我把他原來的官方教程網址放在下邊,你要是還沒接觸,并且以后要用moveit的話可以先學一下
http://docs.ros.org/en/melodic/api/moveit_tutorials/html/doc
1.3.添加UR10機器人功能包
這個地方我們用了兩個官方教程
1)UR功能包,使用rviz的面板控制ur機器人的運動
2)moveit功能包,提供了C++和Python借口控制panda機器人做軌跡規劃
因為目標是實作moveit的c++借口控制UR10機器人實作直線和圓弧軌跡規劃
所以進行如下操作
①要先確保安裝了ur機器人功能包,這樣share檔案夾下可以找到ur_description
將ur10_moveit_config復制到ws_moveit/src下
②添加c++檔案,用moveit控制ur10做直線軌跡規劃
添加ws_moveit/src/moveit_tutorials/doc/move_group_interface/launch/move_group_interface_ur10.launch
<launch>
<node name="move_group_interface_ur10" pkg="moveit_tutorials" type="move_group_interface_ur10" respawn="false" output="screen">
</node>
</launch>
③添加ws_moveit/src/moveit_tutorials/doc/move_group_interface/src/move_group_interface_ur10.cpp
#include <moveit/move_group_interface/move_group_interface.h>
#include <moveit/planning_scene_interface/planning_scene_interface.h>
#include <moveit_msgs/DisplayRobotState.h>
#include <moveit_msgs/DisplayTrajectory.h>
#include <moveit_msgs/AttachedCollisionObject.h>
#include <moveit_msgs/CollisionObject.h>
#include <moveit_visual_tools/moveit_visual_tools.h>
int main(int argc, char** argv)
{
ros::init(argc, argv, "move_group_interface_ur10");
ros::NodeHandle node_handle;
ros::AsyncSpinner spinner(1);
spinner.start();
//initialize
static const std::string PLANNING_GROUP = "manipulator";
moveit::planning_interface::MoveGroupInterface move_group(PLANNING_GROUP);
moveit::planning_interface::PlanningSceneInterface planning_scene_interface;
const robot_state::JointModelGroup* joint_model_group =
move_group.getCurrentState()->getJointModelGroup(PLANNING_GROUP);
// Visualization
// ^^^^^^^^^^^^^
namespace rvt = rviz_visual_tools;
moveit_visual_tools::MoveItVisualTools visual_tools("fixed_base");
visual_tools.deleteAllMarkers();
visual_tools.loadRemoteControl();
Eigen::Isometry3d text_pose = Eigen::Isometry3d::Identity();
text_pose.translation().z() = 1.75;
visual_tools.publishText(text_pose, "MoveGroupInterface Demo", rvt::WHITE, rvt::XLARGE);
// Batch publishing is used to reduce the number of messages being sent to RViz for large visualizations
visual_tools.trigger();
// Getting Basic Information
// ^^^^^^^^^^^^^^^^^^^^^^^^^
//
// We can print the name of the reference frame for this robot.
ROS_INFO_NAMED("tutorial", "Planning frame: %s", move_group.getPlanningFrame().c_str());
// We can also print the name of the end-effector link for this group.
ROS_INFO_NAMED("tutorial", "End effector link: %s", move_group.getEndEffectorLink().c_str());
// We can get a list of all the groups in the robot:
ROS_INFO_NAMED("tutorial", "Available Planning Groups:");
std::copy(move_group.getJointModelGroupNames().begin(), move_group.getJointModelGroupNames().end(),
std::ostream_iterator<std::string>(std::cout, ", "));
// Start the demo
// ^^^^^^^^^^^^^^^^^^^^^^^^^
visual_tools.prompt("Press 'next' in the RvizVisualToolsGui window to start the demo");
moveit::core::RobotStatePtr current_state = move_group.getCurrentState();
cartesian path
robot_state::RobotState start_state(*move_group.getCurrentState());
geometry_msgs::Pose start_pose2;
/*start_pose2.orientation.w= 1.0;
start_pose2.position.x = 0.81724;
start_pose2.position.y = 0.109157;
start_pose2.position.z = -0.005;*/
start_pose2.orientation.w= 1.0;
start_pose2.position.x = 0.67253;
start_pose2.position.y = 0.10914;
start_pose2.position.z = 0.5;
start_state.setFromIK(joint_model_group, start_pose2);
move_group.setStartState(start_state);
std::vector<geometry_msgs::Pose> waypoints;
//waypoints.push_back(start_pose2);
geometry_msgs::Pose target_pose3 = start_pose2;
/code core
target_pose3.orientation.w= 1.0;
target_pose3.position.x = 0.67253;
target_pose3.position.y = 0.10914;
target_pose3.position.z =0.5;
waypoints.push_back(target_pose3);// 1
// Cartesian motions are frequently needed to be slower for actions such as approach and retreat
// grasp motions. Here we demonstrate how to reduce the speed of the robot arm via a scaling factor
// of the maxiumum speed of each joint. Note this is not the speed of the end effector point.
move_group.setMaxVelocityScalingFactor(0.1);
// We want the Cartesian path to be interpolated at a resolution of 1 cm
// which is why we will specify 0.01 as the max step in Cartesian
// translation. We will specify the jump threshold as 0.0, effectively disabling it.
// Warning - disabling the jump threshold while operating real hardware can cause
// large unpredictable motions of redundant joints and could be a safety issue
moveit_msgs::RobotTrajectory trajectory;
const double jump_threshold = 0.0;
const double eef_step = 0.01;
double fraction = move_group.computeCartesianPath(waypoints, eef_step, jump_threshold, trajectory);
ROS_INFO_NAMED("tutorial", "Visualizing plan 4 (Cartesian path) (%.2f%% acheived)", fraction * 100.0);
// Visualize the plan in RViz
// visual_tools.deleteAllMarkers();
visual_tools.publishText(text_pose, "Joint Space Goal", rvt::WHITE, rvt::XLARGE);
visual_tools.publishPath(waypoints, rvt::LIME_GREEN, rvt::SMALL);
visual_tools.publishPath(waypoints, rvt::LIME_GREEN, rvt::SMALL);
for (std::size_t i = 0; i < waypoints.size(); ++i)
visual_tools.publishAxisLabeled(waypoints[i], "pt" + std::to_string(i), rvt::SMALL);
visual_tools.trigger();
visual_tools.prompt("Press 'next' in the RvizVisualToolsGui window to continue the demo");
// END_TUTORIAL
ros::shutdown();
return 0;
}
④修改ws_moveit/src/moveit_tutorials/doc/move_group_interface/Cmake.lists 在結尾加上三行 基于.cpp生成devel和build下的可執行檔案
add_executable(move_group_interface_ur10 src/move_group_interface_ur10.cpp)
target_link_libraries(move_group_interface_ur10 ${catkin_LIBRARIES} ${Boost_LIBRARIES})
install(TARGETS move_group_interface_ur10 DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
install(DIRECTORY launch DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION})
⑤終端輸入
cd ~/ws_moveit
source ./devel/setup.bash
catkin build
⑥以上搭建完通信了
檢驗一下
cd ~/ws_moveit
source ./devel/setup.bash
roslaunch ur10_moveit_config demo.launch
打開一個新的tab
cd ~/ws_moveit
source ./devel/setup.bash
roslaunch moveit_tutorials move_group_interface_tutorial.launch
2.實作直線軌跡規劃(PTP)
修改ws_moveit/src/moveit_tutorials/doc/move_group_interface/src/move_group_interface_ur10.cpp代碼如下
#include <moveit/move_group_interface/move_group_interface.h>
#include <moveit/planning_scene_interface/planning_scene_interface.h>
#include <moveit_msgs/DisplayRobotState.h>
#include <moveit_msgs/DisplayTrajectory.h>
#include <moveit_msgs/AttachedCollisionObject.h>
#include <moveit_msgs/CollisionObject.h>
#include <moveit_visual_tools/moveit_visual_tools.h>
int main(int argc, char** argv)
{
ros::init(argc, argv, "move_group_interface_ur10");
ros::NodeHandle node_handle;
ros::AsyncSpinner spinner(1);
spinner.start();
//initialize
static const std::string PLANNING_GROUP = "manipulator";
moveit::planning_interface::MoveGroupInterface move_group(PLANNING_GROUP);
moveit::planning_interface::PlanningSceneInterface planning_scene_interface;
const robot_state::JointModelGroup* joint_model_group =
move_group.getCurrentState()->getJointModelGroup(PLANNING_GROUP);
// Visualization
// ^^^^^^^^^^^^^
namespace rvt = rviz_visual_tools;
moveit_visual_tools::MoveItVisualTools visual_tools("fixed_base");
visual_tools.deleteAllMarkers();
visual_tools.loadRemoteControl();
Eigen::Isometry3d text_pose = Eigen::Isometry3d::Identity();
text_pose.translation().z() = 1.75;
visual_tools.publishText(text_pose, "MoveGroupInterface Demo", rvt::WHITE, rvt::XLARGE);
// Batch publishing is used to reduce the number of messages being sent to RViz for large visualizations
visual_tools.trigger();
// // Getting Basic Information
// ^^^^^^^^^^^^^^^^^^^^^^^^^
//
// We can print the name of the reference frame for this robot.
ROS_INFO_NAMED("tutorial", "Planning frame: %s", move_group.getPlanningFrame().c_str());
// We can also print the name of the end-effector link for this group.
ROS_INFO_NAMED("tutorial", "End effector link: %s", move_group.getEndEffectorLink().c_str());
// We can get a list of all the groups in the robot:
ROS_INFO_NAMED("tutorial", "Available Planning Groups:");
std::copy(move_group.getJointModelGroupNames().begin(), move_group.getJointModelGroupNames().end(),
std::ostream_iterator<std::string>(std::cout, ", "));
// Start the demo
// ^^^^^^^^^^^^^^^^^^^^^^^^^
visual_tools.prompt("Press 'next' in the RvizVisualToolsGui window to start the demo");
moveit::core::RobotStatePtr current_state = move_group.getCurrentState();
cartesian path
robot_state::RobotState start_state(*move_group.getCurrentState());
geometry_msgs::Pose start_pose2;
start_pose2.orientation.w = 1.0;
start_pose2.position.x = 0.35;
start_pose2.position.y = -0.05;
start_pose2.position.z = 0.6;
start_state.setFromIK(joint_model_group, start_pose2);
move_group.setStartState(start_state);
std::vector<geometry_msgs::Pose> waypoints;
waypoints.push_back(start_pose2);
geometry_msgs::Pose target_pose3 = start_pose2;
/code core
target_pose3.position.y += 0.2;
waypoints.push_back(target_pose3); // 1
target_pose3.position.y -= 0.4;
waypoints.push_back(target_pose3); //2
target_pose3.position.y += 0.2;
waypoints.push_back(target_pose3); // 3
target_pose3.position.x += 0.2;
waypoints.push_back(target_pose3); // 4
target_pose3.position.y += 0.2;
waypoints.push_back(target_pose3); // 5
target_pose3.position.y -= 0.4;
waypoints.push_back(target_pose3); //6
target_pose3.position.y += 0.2;
waypoints.push_back(target_pose3); //7
target_pose3.position.x += 0.2;
waypoints.push_back(target_pose3); // 8
target_pose3.position.y += 0.2;
waypoints.push_back(target_pose3); // 9
target_pose3.position.y -= 0.4;
waypoints.push_back(target_pose3); //10
target_pose3.position.y += 0.2;
waypoints.push_back(target_pose3); //11
// Cartesian motions are frequently needed to be slower for actions such as approach and retreat
// grasp motions. Here we demonstrate how to reduce the speed of the robot arm via a scaling factor
// of the maxiumum speed of each joint. Note this is not the speed of the end effector point.
move_group.setMaxVelocityScalingFactor(0.1);
// We want the Cartesian path to be interpolated at a resolution of 1 cm
// which is why we will specify 0.01 as the max step in Cartesian
// translation. We will specify the jump threshold as 0.0, effectively disabling it.
// Warning - disabling the jump threshold while operating real hardware can cause
// large unpredictable motions of redundant joints and could be a safety issue
moveit_msgs::RobotTrajectory trajectory;
const double jump_threshold = 0.0;
const double eef_step = 0.01;
double fraction = move_group.computeCartesianPath(waypoints, eef_step, jump_threshold, trajectory);
ROS_INFO_NAMED("tutorial", "Visualizing plan 4 (Cartesian path) (%.2f%% acheived)", fraction * 100.0);
// Visualize the plan in RViz
visual_tools.deleteAllMarkers();
visual_tools.publishText(text_pose, "Joint Space Goal", rvt::WHITE, rvt::XLARGE);
visual_tools.publishPath(waypoints, rvt::LIME_GREEN, rvt::SMALL);
for (std::size_t i = 0; i < waypoints.size(); ++i)
visual_tools.publishAxisLabeled(waypoints[i], "pt" + std::to_string(i), rvt::SMALL);
visual_tools.trigger();
visual_tools.prompt("Press 'next' in the RvizVisualToolsGui window to continue the demo");
// END_TUTORIAL
ros::shutdown();
return 0;
}
運行結果

顯而易見我寫了個漢字
3.圓弧軌跡規劃
(這個坑我明天會填的,會的~)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/259976.html
標籤:AI
