datetime:2025/01/24 14:15
author:nzb
功能目录
- 碰撞检测
- 自碰撞检测
- 修改状态
- 以组进行碰撞检测,比如对
panda_arm
的hand
组进行碰撞检测
- 获取碰撞检测结果信息
- 修改碰撞检测矩阵
- 自身碰撞检测和环境碰撞检测
- 约束检测
代码解读
#include <rclcpp/rclcpp.hpp>
#include <moveit/robot_model_loader/robot_model_loader.hpp>
#include <moveit/planning_scene/planning_scene.hpp>
#include <moveit/kinematic_constraints/utils.hpp>
bool stateFeasibilityTestExample(const moveit::core::RobotState& robot_state, bool )
{
const double* joint_values = robot_state.getJointPositions("panda_joint1");
return (joint_values[0] > 0.0);
}
static const rclcpp::Logger LOGGER = rclcpp::get_logger("planning_scene_tutorial");
int main(int argc, char** argv)
{
rclcpp::init(argc, argv);
rclcpp::NodeOptions node_options;
node_options.automatically_declare_parameters_from_overrides(true);
auto planning_scene_tutorial_node = rclcpp::Node::make_shared("planning_scene_tutorial", node_options);
rclcpp::executors::SingleThreadedExecutor executor;
executor.add_node(planning_scene_tutorial_node);
std::thread([&executor]() { executor.spin(); }).detach();
robot_model_loader::RobotModelLoader robot_model_loader(planning_scene_tutorial_node, "robot_description");
const moveit::core::RobotModelPtr& kinematic_model = robot_model_loader.getModel();
planning_scene::PlanningScene planning_scene(kinematic_model);
collision_detection::CollisionRequest collision_request;
collision_detection::CollisionResult collision_result;
planning_scene.checkSelfCollision(collision_request, collision_result);
RCLCPP_INFO_STREAM(LOGGER, "Test 1: Current state is " << (collision_result.collision ? "in" : "not in")
<< " self collision");
moveit::core::RobotState& current_state = planning_scene.getCurrentStateNonConst();
current_state.setToRandomPositions();
collision_result.clear();
planning_scene.checkSelfCollision(collision_request, collision_result);
RCLCPP_INFO_STREAM(LOGGER, "Test 2: Current state is " << (collision_result.collision ? "in" : "not in")
<< " self collision");
collision_request.group_name = "hand";
current_state.setToRandomPositions();
collision_result.clear();
planning_scene.checkSelfCollision(collision_request, collision_result);
RCLCPP_INFO_STREAM(LOGGER, "Test 3: Current state is " << (collision_result.collision ? "in" : "not in")
<< " self collision");
std::vector<double> joint_values = { 0.0, 0.0, 0.0, -2.9, 0.0, 1.4, 0.0 };
const moveit::core::JointModelGroup* joint_model_group = current_state.getJointModelGroup("panda_arm");
current_state.setJointGroupPositions(joint_model_group, joint_values);
RCLCPP_INFO_STREAM(LOGGER, "Test 4: Current state is "
<< (current_state.satisfiesBounds(joint_model_group) ? "valid" : "not valid"));
collision_request.contacts = true;
collision_request.max_contacts = 1000;
collision_result.clear();
planning_scene.checkSelfCollision(collision_request, collision_result);
RCLCPP_INFO_STREAM(LOGGER, "Test 5: Current state is " << (collision_result.collision ? "in" : "not in")
<< " self collision");
collision_detection::CollisionResult::ContactMap::const_iterator it;
for (it = collision_result.contacts.begin(); it != collision_result.contacts.end(); ++it)
{
RCLCPP_INFO(LOGGER, "Contact between: %s and %s", it->first.first.c_str(), it->first.second.c_str());
}
collision_detection::AllowedCollisionMatrix acm = planning_scene.getAllowedCollisionMatrix();
moveit::core::RobotState copied_state = planning_scene.getCurrentState();
collision_detection::CollisionResult::ContactMap::const_iterator it2;
for (it2 = collision_result.contacts.begin(); it2 != collision_result.contacts.end(); ++it2)
{
acm.setEntry(it2->first.first, it2->first.second, true);
}
collision_result.clear();
planning_scene.checkSelfCollision(collision_request, collision_result, copied_state, acm);
RCLCPP_INFO_STREAM(LOGGER, "Test 6: Current state is " << (collision_result.collision ? "in" : "not in")
<< " self collision");
collision_result.clear();
planning_scene.checkCollision(collision_request, collision_result, copied_state, acm);
RCLCPP_INFO_STREAM(LOGGER, "Test 7: Current state is " << (collision_result.collision ? "in" : "not in")
<< " self collision");
std::string end_effector_name = joint_model_group->getLinkModelNames().back();
geometry_msgs::msg::PoseStamped desired_pose;
desired_pose.pose.orientation.w = 1.0;
desired_pose.pose.position.x = 0.3;
desired_pose.pose.position.y = -0.185;
desired_pose.pose.position.z = 0.5;
desired_pose.header.frame_id = "panda_link0";
moveit_msgs::msg::Constraints goal_constraint =
kinematic_constraints::constructGoalConstraints(end_effector_name, desired_pose);
copied_state.setToRandomPositions();
copied_state.update();
bool constrained = planning_scene.isStateConstrained(copied_state, goal_constraint);
RCLCPP_INFO_STREAM(LOGGER, "Test 8: Random state is " << (constrained ? "constrained" : "not constrained"));
kinematic_constraints::KinematicConstraintSet kinematic_constraint_set(kinematic_model);
kinematic_constraint_set.add(goal_constraint, planning_scene.getTransforms());
bool constrained_2 = planning_scene.isStateConstrained(copied_state, kinematic_constraint_set);
RCLCPP_INFO_STREAM(LOGGER, "Test 9: Random state is " << (constrained_2 ? "constrained" : "not constrained"));
kinematic_constraints::ConstraintEvaluationResult constraint_eval_result =
kinematic_constraint_set.decide(copied_state);
RCLCPP_INFO_STREAM(LOGGER, "Test 10: Random state is "
<< (constraint_eval_result.satisfied ? "constrained" : "not constrained"));
planning_scene.setStateFeasibilityPredicate(stateFeasibilityTestExample);
bool state_feasible = planning_scene.isStateFeasible(copied_state);
RCLCPP_INFO_STREAM(LOGGER, "Test 11: Random state is " << (state_feasible ? "feasible" : "not feasible"));
bool state_valid = planning_scene.isStateValid(copied_state, kinematic_constraint_set, "panda_arm");
RCLCPP_INFO_STREAM(LOGGER, "Test 12: Random state is " << (state_valid ? "valid" : "not valid"));
rclcpp::shutdown();
return 0;
}
输出
blues@pasture-10:~/vscode_projects/cyan_demos/moveit_ws$ ros2 launch moveit2_tutorials planning_scene_tutorial.launch.py
[INFO] [launch]: All log files can be found below /home/blues/.ros/log/2025-01-24-14-23-35-317348-pasture-10-38088
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [planning_scene_tutorial-1]: process started with pid [38089]
[planning_scene_tutorial-1] [INFO] [1737699815.402008812] [moveit_rdf_loader.rdf_loader]: Loaded robot model in 0.00102517 seconds
[planning_scene_tutorial-1] [INFO] [1737699815.402037223] [moveit_robot_model.robot_model]: Loading robot model 'panda'...
[planning_scene_tutorial-1] [INFO] [1737699815.410305944] [planning_scene_tutorial]: Test 1: Current state is in self collision
[planning_scene_tutorial-1] [INFO] [1737699815.410370753] [planning_scene_tutorial]: Test 2: Current state is not in self collision
[planning_scene_tutorial-1] [INFO] [1737699815.410379780] [planning_scene_tutorial]: Test 3: Current state is not in self collision
[planning_scene_tutorial-1] [INFO] [1737699815.410382780] [planning_scene_tutorial]: Test 4: Current state is valid
[planning_scene_tutorial-1] [INFO] [1737699815.410423584] [planning_scene_tutorial]: Test 5: Current state is in self collision
[planning_scene_tutorial-1] [INFO] [1737699815.410425702] [planning_scene_tutorial]: Contact between: panda_leftfinger and panda_link1
[planning_scene_tutorial-1] [INFO] [1737699815.410427868] [planning_scene_tutorial]: Contact between: panda_link1 and panda_rightfinger
[planning_scene_tutorial-1] [INFO] [1737699815.410443083] [planning_scene_tutorial]: Test 6: Current state is not in self collision
[planning_scene_tutorial-1] [INFO] [1737699815.410452560] [planning_scene_tutorial]: Test 7: Current state is not in self collision
[planning_scene_tutorial-1] [INFO] [1737699815.410500323] [planning_scene_tutorial]: Test 8: Random state is not constrained
[planning_scene_tutorial-1] [INFO] [1737699815.410506990] [planning_scene_tutorial]: Test 9: Random state is not constrained
[planning_scene_tutorial-1] [INFO] [1737699815.410509393] [planning_scene_tutorial]: Test 10: Random state is not constrained
[planning_scene_tutorial-1] [INFO] [1737699815.410511847] [planning_scene_tutorial]: Test 11: Random state is not feasible
[planning_scene_tutorial-1] [INFO] [1737699815.410532895] [planning_scene_tutorial]: Test 12: Random state is not valid
[INFO] [planning_scene_tutorial-1]: process has finished cleanly [pid 38089]
启动文件
from launch import LaunchDescription
from launch_ros.actions import Node
from moveit_configs_utils import MoveItConfigsBuilder
def generate_launch_description():
moveit_config = MoveItConfigsBuilder("moveit_resources_panda").to_moveit_configs()
planning_scene_tutorial = Node(
name="planning_scene_tutorial",
package="moveit2_tutorials",
executable="planning_scene_tutorial",
output="screen",
parameters=[
moveit_config.robot_description,
moveit_config.robot_description_semantic,
moveit_config.robot_description_kinematics,
],
)
return LaunchDescription([planning_scene_tutorial])