39 #include <gtest/gtest.h>
43 #include <rclcpp/rclcpp.hpp>
44 #include <rclcpp_action/rclcpp_action.hpp>
45 #include <tf2_ros/buffer.h>
47 #include <moveit_msgs/action/hybrid_planner.hpp>
48 #include <moveit_msgs/msg/display_robot_state.hpp>
49 #include <moveit_msgs/msg/motion_plan_response.hpp>
53 using namespace std::chrono_literals;
60 action_successful_ =
false;
61 action_aborted_ =
false;
62 action_complete_ =
false;
64 executor_.add_node(node_);
66 std::string hybrid_planning_action_name =
"";
67 node_->declare_parameter(
"hybrid_planning_action_name",
"");
68 if (node_->has_parameter(
"hybrid_planning_action_name"))
70 node_->get_parameter<std::string>(
"hybrid_planning_action_name", hybrid_planning_action_name);
74 RCLCPP_ERROR(node_->get_logger(),
"hybrid_planning_action_name parameter was not defined");
75 std::exit(EXIT_FAILURE);
79 rclcpp_action::create_client<moveit_msgs::action::HybridPlanner>(node_, hybrid_planning_action_name);
82 global_solution_subscriber_ = node_->create_subscription<moveit_msgs::msg::MotionPlanResponse>(
83 "global_trajectory", rclcpp::SystemDefaultsQoS(),
84 [
this](
const moveit_msgs::msg::MotionPlanResponse::SharedPtr ) {});
86 RCLCPP_INFO(node_->get_logger(),
"Initialize Planning Scene Monitor");
87 tf_buffer_ = std::make_shared<tf2_ros::Buffer>(node_->get_clock());
89 planning_scene_monitor_ = std::make_shared<planning_scene_monitor::PlanningSceneMonitor>(node_,
"robot_description",
90 "planning_scene_monitor");
91 if (!planning_scene_monitor_->getPlanningScene())
93 RCLCPP_ERROR(node_->get_logger(),
"The planning scene was not retrieved!");
94 std::exit(EXIT_FAILURE);
98 planning_scene_monitor_->startStateMonitor();
99 planning_scene_monitor_->setPlanningScenePublishingFrequency(100);
102 planning_scene_monitor_->startSceneMonitor();
105 if (!planning_scene_monitor_->waitForCurrentRobotState(node_->now(), 5))
107 RCLCPP_ERROR(node_->get_logger(),
"Timeout when waiting for /joint_states updates. Is the robot running?");
108 std::exit(EXIT_FAILURE);
111 if (!hp_action_client_->wait_for_action_server(20s))
113 RCLCPP_ERROR(node_->get_logger(),
"Hybrid planning action server not available after waiting");
114 std::exit(EXIT_FAILURE);
118 const std::string planning_group =
"panda_arm";
123 const auto robot_state = std::make_shared<moveit::core::RobotState>(robot_model);
127 robot_state->setToDefaultValues(joint_model_group,
"ready");
128 robot_state->update();
132 locked_planning_scene->setCurrentState(*robot_state);
137 std::vector<double> joint_values = { 0.0, 0.0, 0.0, 0.0, 0.0, 1.571, 0.785 };
144 [robot_state, planning_group, goal_state, joint_model_group]() {
147 goal_motion_request.group_name = planning_group;
148 goal_motion_request.num_planning_attempts = 10;
149 goal_motion_request.max_velocity_scaling_factor = 0.1;
150 goal_motion_request.max_acceleration_scaling_factor = 0.1;
151 goal_motion_request.allowed_planning_time = 2.0;
152 goal_motion_request.planner_id =
"ompl";
153 goal_motion_request.pipeline_id =
"ompl";
154 goal_motion_request.goal_constraints.resize(1);
155 goal_motion_request.goal_constraints[0] =
157 return goal_motion_request;
161 moveit_msgs::msg::MotionSequenceItem sequence_item;
162 sequence_item.req = goal_motion_request;
163 sequence_item.blend_radius = 0.0;
165 moveit_msgs::msg::MotionSequenceRequest sequence_request;
166 sequence_request.items.push_back(sequence_item);
168 goal_action_request_ = moveit_msgs::action::HybridPlanner::Goal();
169 goal_action_request_.planning_group = planning_group;
170 goal_action_request_.motion_sequence = sequence_request;
172 send_goal_options_ = rclcpp_action::Client<moveit_msgs::action::HybridPlanner>::SendGoalOptions();
173 send_goal_options_.result_callback =
174 [
this](
const rclcpp_action::ClientGoalHandle<moveit_msgs::action::HybridPlanner>::WrappedResult& result) {
177 case rclcpp_action::ResultCode::SUCCEEDED:
178 action_successful_ =
true;
179 RCLCPP_INFO(node_->get_logger(),
"Hybrid planning goal succeeded");
181 case rclcpp_action::ResultCode::ABORTED:
182 action_aborted_ =
true;
183 RCLCPP_ERROR(node_->get_logger(),
"Hybrid planning goal was aborted");
185 case rclcpp_action::ResultCode::CANCELED:
186 RCLCPP_ERROR(node_->get_logger(),
"Hybrid planning goal was canceled");
189 RCLCPP_ERROR(node_->get_logger(),
"Unknown hybrid planning result code");
192 action_complete_ =
true;
195 send_goal_options_.feedback_callback =
196 [
this](rclcpp_action::ClientGoalHandle<moveit_msgs::action::HybridPlanner>::SharedPtr ,
197 const std::shared_ptr<const moveit_msgs::action::HybridPlanner::Feedback> feedback) {
198 RCLCPP_INFO(node_->get_logger(), feedback->feedback.c_str());
222 std::thread run_thread([
this]() {
224 auto goal_handle_future = hp_action_client_->async_send_goal(goal_action_request_, send_goal_options_);
227 rclcpp::Rate rate(10);
228 while (!action_complete_)
230 executor_.spin_once();
234 ASSERT_TRUE(action_successful_);
240 std::thread run_thread([
this]() {
242 auto goal_handle_future = hp_action_client_->async_send_goal(goal_action_request_, send_goal_options_);
245 hp_action_client_->async_cancel_all_goals();
248 rclcpp::Rate rate(10);
249 while (!action_complete_)
251 executor_.spin_once();
255 ASSERT_FALSE(action_successful_);
256 ASSERT_TRUE(action_aborted_);
260 int main(
int argc,
char** argv)
262 rclcpp::init(argc, argv);
263 ::testing::InitGoogleTest(&argc, argv);
265 int ret = RUN_ALL_TESTS();
Representation of a robot's state. This includes position, velocity, acceleration and effort.
void setJointGroupPositions(const std::string &joint_group_name, const double *gstate)
Given positions for the variables that make up a group, in the order found in the group (including va...
planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_
rclcpp_action::Client< moveit_msgs::action::HybridPlanner >::SendGoalOptions send_goal_options_
std::atomic_bool action_aborted_
std::shared_ptr< tf2_ros::Buffer > tf_buffer_
rclcpp::Subscription< moveit_msgs::msg::MotionPlanResponse >::SharedPtr global_solution_subscriber_
rclcpp::executors::MultiThreadedExecutor executor_
moveit_msgs::action::HybridPlanner::Goal goal_action_request_
std::atomic_bool action_complete_
rclcpp_action::Client< moveit_msgs::action::HybridPlanner >::SharedPtr hp_action_client_
rclcpp::Node::SharedPtr node_
std::atomic_bool action_successful_
This is a convenience class for obtaining access to an instance of a locked PlanningScene.
@ UPDATE_SCENE
The entire scene was updated.
moveit_msgs::msg::Constraints constructGoalConstraints(const moveit::core::RobotState &state, const moveit::core::JointModelGroup *jmg, double tolerance_below, double tolerance_above)
Generates a constraint message intended to be used as a goal constraint for a joint group....
void robotStateToRobotStateMsg(const RobotState &state, moveit_msgs::msg::RobotState &robot_state, bool copy_attached_bodies=true)
Convert a MoveIt robot state to a robot state message.
TEST_F(HybridPlanningFixture, ActionCompletion)
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
int main(int argc, char **argv)