44 #include <std_srvs/srv/empty.hpp>
45 #include <moveit_msgs/msg/robot_state.hpp>
46 #include <tf2_eigen/tf2_eigen.hpp>
49 #include "ui_motion_planning_rviz_plugin_frame.h"
54 void MotionPlanningFrame::planButtonClicked()
56 publishSceneIfNeeded();
60 void MotionPlanningFrame::executeButtonClicked()
62 ui_->execute_button->setEnabled(
false);
67 void MotionPlanningFrame::planAndExecuteButtonClicked()
69 publishSceneIfNeeded();
70 ui_->plan_and_execute_button->setEnabled(
false);
71 ui_->execute_button->setEnabled(
false);
76 void MotionPlanningFrame::stopButtonClicked()
78 ui_->stop_button->setEnabled(
false);
82 void MotionPlanningFrame::allowReplanningToggled(
bool checked)
88 void MotionPlanningFrame::allowLookingToggled(
bool checked)
94 void MotionPlanningFrame::pathConstraintsIndexChanged(
int index)
100 std::string c =
ui_->path_constraints_combo_box->itemText(index).toStdString();
102 RCLCPP_WARN_STREAM(logger_,
"Unable to set the path constraints: " << c);
109 void MotionPlanningFrame::onClearOctomapClicked()
111 auto req = std::make_shared<std_srvs::srv::Empty::Request>();
112 auto result = clear_octomap_service_client_->async_send_request(req);
114 if (result.wait_for(std::chrono::seconds(0)) != std::future_status::ready)
116 RCLCPP_ERROR(logger_,
"Failed to call clear_octomap_service");
118 ui_->clear_octomap_button->setEnabled(
false);
121 bool MotionPlanningFrame::computeCartesianPlan()
123 rclcpp::Time start = rclcpp::Clock().now();
126 std::vector<geometry_msgs::msg::Pose> waypoints;
127 const std::string& link_name =
move_group_->getEndEffectorLink();
131 RCLCPP_ERROR_STREAM(logger_,
"Failed to determine unique end-effector link: " << link_name);
137 double cart_step_size = 0.01;
138 double cart_jump_thresh = 0.0;
139 bool avoid_collisions =
true;
142 moveit_msgs::msg::RobotTrajectory trajectory;
144 move_group_->computeCartesianPath(waypoints, cart_step_size, cart_jump_thresh, trajectory, avoid_collisions);
148 RCLCPP_INFO(logger_,
"Achieved %f %% of Cartesian path", fraction * 100.);
153 rt.setRobotTrajectoryMsg(*
move_group_->getCurrentState(), trajectory);
155 bool success = time_parameterization.
computeTimeStamps(rt,
ui_->velocity_scaling_factor->value(),
156 ui_->acceleration_scaling_factor->value());
157 RCLCPP_INFO(logger_,
"Computing time stamps %s", success ?
"SUCCEEDED" :
"FAILED");
160 current_plan_ = std::make_shared<moveit::planning_interface::MoveGroupInterface::Plan>();
162 current_plan_->planning_time = (rclcpp::Clock().now() - start).seconds();
168 bool MotionPlanningFrame::computeJointSpacePlan()
170 current_plan_ = std::make_shared<moveit::planning_interface::MoveGroupInterface::Plan>();
174 void MotionPlanningFrame::computePlanButtonClicked()
180 ui_->result_label->setText(
"Planning...");
182 configureForPlanning();
184 bool success = (
ui_->use_cartesian_path->isEnabled() &&
ui_->use_cartesian_path->checkState()) ?
185 computeCartesianPlan() :
186 computeJointSpacePlan();
190 ui_->execute_button->setEnabled(
true);
191 ui_->result_label->setText(QString(
"Time: ").
append(QString::number(
current_plan_->planning_time,
'f', 3)));
196 ui_->result_label->setText(
"Failed");
201 void MotionPlanningFrame::computeExecuteButtonClicked()
204 moveit::planning_interface::MoveGroupInterfacePtr mgi(
move_group_);
207 ui_->stop_button->setEnabled(
true);
208 bool success = mgi->execute(*
current_plan_) == moveit::core::MoveItErrorCode::SUCCESS;
209 onFinishedExecution(success);
213 void MotionPlanningFrame::computePlanAndExecuteButtonClicked()
216 moveit::planning_interface::MoveGroupInterfacePtr mgi(
move_group_);
219 configureForPlanning();
223 mgi->setStartStateToCurrentState();
224 ui_->stop_button->setEnabled(
true);
225 if (
ui_->use_cartesian_path->isEnabled() &&
ui_->use_cartesian_path->checkState())
227 if (computeCartesianPlan())
228 computeExecuteButtonClicked();
232 bool success = mgi->move() == moveit::core::MoveItErrorCode::SUCCESS;
233 onFinishedExecution(success);
235 ui_->plan_and_execute_button->setEnabled(
true);
238 void MotionPlanningFrame::computeStopButtonClicked()
244 void MotionPlanningFrame::onFinishedExecution(
bool success)
249 ui_->result_label->setText(
"Executed");
253 ui_->result_label->setText(!
ui_->stop_button->isEnabled() ?
"Stopped" :
"Failed");
256 ui_->stop_button->setEnabled(
false);
259 if (
ui_->start_state_combo_box->currentText() ==
"<current>")
260 startStateTextChanged(
ui_->start_state_combo_box->currentText());
264 if (
ui_->goal_state_combo_box->currentText() ==
"<previous>")
265 goalStateTextChanged(
ui_->goal_state_combo_box->currentText());
268 void MotionPlanningFrame::onNewPlanningSceneState()
271 if (
ui_->start_state_combo_box->currentText() ==
"<current>")
276 if (
ui_->goal_state_combo_box->currentText() ==
"<current>")
280 void MotionPlanningFrame::startStateTextChanged(
const QString& start_state)
284 "update start state");
287 void MotionPlanningFrame::startStateTextChangedExec(
const std::string& start_state)
290 updateQueryStateHelper(start, start_state);
294 void MotionPlanningFrame::goalStateTextChanged(
const QString& goal_state)
298 "update goal state");
301 void MotionPlanningFrame::goalStateTextChangedExec(
const std::string& goal_state)
304 updateQueryStateHelper(goal, goal_state);
308 void MotionPlanningFrame::planningGroupTextChanged(
const QString& planning_group)
317 configureWorkspace();
324 if (v ==
"<random valid>")
326 configureWorkspace();
332 static const int MAX_ATTEMPTS = 100;
333 int attempt_count = 0;
334 while (attempt_count < MAX_ATTEMPTS)
348 if (attempt_count >= MAX_ATTEMPTS)
349 RCLCPP_WARN(logger_,
"Unable to find a random collision free configuration after %d attempts", MAX_ATTEMPTS);
358 if (v ==
"<current>")
360 rclcpp::Time t = node_->now();
364 state = ps->getCurrentState();
368 if (v ==
"<same as goal>")
374 if (v ==
"<same as start>")
380 if (v ==
"<previous>")
391 void MotionPlanningFrame::populatePlannersList(
const std::vector<moveit_msgs::msg::PlannerInterfaceDescription>& desc)
393 ui_->planning_pipeline_combo_box->clear();
396 size_t default_planner_index = 0;
399 QString item_text(
d.pipeline_id.c_str());
403 if (item_text.isEmpty())
404 item_text = QString::fromStdString(
d.name);
405 default_planner_index =
ui_->planning_pipeline_combo_box->count();
407 ui_->planning_pipeline_combo_box->addItem(item_text);
411 ui_->planning_pipeline_combo_box->setItemData(default_planner_index, font, Qt::FontRole);
415 ui_->planning_pipeline_combo_box->setCurrentIndex(default_planner_index);
418 void MotionPlanningFrame::populatePlannerDescription(
const moveit_msgs::msg::PlannerInterfaceDescription& desc)
421 RCLCPP_DEBUG(logger_,
"Found %zu planners for group '%s' and pipeline '%s'", desc.planner_ids.size(),
group.c_str(),
422 desc.pipeline_id.c_str());
423 ui_->planning_algorithm_combo_box->clear();
426 ui_->library_label->setText(QString::fromStdString(desc.name));
427 ui_->library_label->setStyleSheet(
"QLabel { color : green; font: bold }");
429 bool found_group =
false;
433 for (
const std::string& planner_id : desc.planner_ids)
435 RCLCPP_DEBUG(logger_,
"planner id: %s", planner_id.c_str());
436 if (planner_id ==
group)
440 else if (planner_id.substr(0,
group.length()) ==
group)
442 if (planner_id.size() >
group.length() && planner_id[
group.length()] ==
'[')
444 std::string
id = planner_id.substr(
group.length());
447 id.resize(
id.length() - 1);
448 ui_->planning_algorithm_combo_box->addItem(QString::fromStdString(
id.substr(1)));
454 if (
ui_->planning_algorithm_combo_box->count() == 0 && !found_group)
456 for (
const std::string& planner_id : desc.planner_ids)
457 ui_->planning_algorithm_combo_box->addItem(QString::fromStdString(planner_id));
460 ui_->planning_algorithm_combo_box->insertItem(0,
"<unspecified>");
463 const std::string& default_planner_config =
move_group_->getDefaultPlannerId(found_group ?
group : std::string());
464 int default_index =
ui_->planning_algorithm_combo_box->findText(QString::fromStdString(default_planner_config));
465 if (default_index < 0)
467 ui_->planning_algorithm_combo_box->setCurrentIndex(default_index);
471 ui_->planning_algorithm_combo_box->setItemData(default_index, font, Qt::FontRole);
474 void MotionPlanningFrame::populateConstraintsList()
480 void MotionPlanningFrame::populateConstraintsList(
const std::vector<std::string>& constr)
482 ui_->path_constraints_combo_box->clear();
483 ui_->path_constraints_combo_box->addItem(
"None");
484 for (
const std::string& constraint : constr)
485 ui_->path_constraints_combo_box->addItem(QString::fromStdString(constraint));
491 mreq.num_planning_attempts =
ui_->planning_attempts->value();
492 mreq.allowed_planning_time =
ui_->planning_time->value();
493 mreq.max_velocity_scaling_factor =
ui_->velocity_scaling_factor->value();
494 mreq.max_acceleration_scaling_factor =
ui_->acceleration_scaling_factor->value();
496 mreq.workspace_parameters.min_corner.x =
ui_->wcenter_x->value() -
ui_->wsize_x->value() / 2.0;
497 mreq.workspace_parameters.min_corner.y =
ui_->wcenter_y->value() -
ui_->wsize_y->value() / 2.0;
498 mreq.workspace_parameters.min_corner.z =
ui_->wcenter_z->value() -
ui_->wsize_z->value() / 2.0;
499 mreq.workspace_parameters.max_corner.x =
ui_->wcenter_x->value() +
ui_->wsize_x->value() / 2.0;
500 mreq.workspace_parameters.max_corner.y =
ui_->wcenter_y->value() +
ui_->wsize_y->value() / 2.0;
501 mreq.workspace_parameters.max_corner.z =
ui_->wcenter_z->value() +
ui_->wsize_z->value() / 2.0;
506 mreq.goal_constraints.resize(1);
511 void MotionPlanningFrame::configureWorkspace()
531 if (psm && psm->getRobotModelLoader() && psm->getRobotModelLoader()->getModel())
533 const moveit::core::RobotModelPtr& robot_model = psm->getRobotModelLoader()->getModel();
534 const std::vector<moveit::core::JointModel*>& jm = robot_model->getJointModels();
539 joint->setVariableBounds(joint->getName() +
"/" + joint->getLocalVariableNames()[0], bx);
540 joint->setVariableBounds(joint->getName() +
"/" + joint->getLocalVariableNames()[1], by);
544 joint->setVariableBounds(joint->getName() +
"/" + joint->getLocalVariableNames()[0], bx);
545 joint->setVariableBounds(joint->getName() +
"/" + joint->getLocalVariableNames()[1], by);
546 joint->setVariableBounds(joint->getName() +
"/" + joint->getLocalVariableNames()[2], bz);
552 void MotionPlanningFrame::configureForPlanning()
557 move_group_->setNumPlanningAttempts(
ui_->planning_attempts->value());
558 move_group_->setMaxVelocityScalingFactor(
ui_->velocity_scaling_factor->value());
559 move_group_->setMaxAccelerationScalingFactor(
ui_->acceleration_scaling_factor->value());
560 configureWorkspace();
565 void MotionPlanningFrame::remotePlanCallback(
const std_msgs::msg::Empty::ConstSharedPtr& )
570 void MotionPlanningFrame::remoteExecuteCallback(
const std_msgs::msg::Empty::ConstSharedPtr& )
572 executeButtonClicked();
575 void MotionPlanningFrame::remoteStopCallback(
const std_msgs::msg::Empty::ConstSharedPtr& )
580 void MotionPlanningFrame::remoteUpdateStartStateCallback(
const std_msgs::msg::Empty::ConstSharedPtr& )
594 void MotionPlanningFrame::remoteUpdateGoalStateCallback(
const std_msgs::msg::Empty::ConstSharedPtr& )
608 void MotionPlanningFrame::remoteUpdateCustomStartStateCallback(
const moveit_msgs::msg::RobotState::ConstSharedPtr& msg)
610 moveit_msgs::msg::RobotState msg_no_attached(*msg);
611 msg_no_attached.attached_collision_objects.clear();
612 msg_no_attached.is_diff =
true;
619 auto state = std::make_shared<moveit::core::RobotState>(ps->getCurrentState());
626 void MotionPlanningFrame::remoteUpdateCustomGoalStateCallback(
const moveit_msgs::msg::RobotState::ConstSharedPtr& msg)
628 moveit_msgs::msg::RobotState msg_no_attached(*msg);
629 msg_no_attached.attached_collision_objects.clear();
630 msg_no_attached.is_diff =
true;
637 auto state = std::make_shared<moveit::core::RobotState>(ps->getCurrentState());
A joint from the robot. Models the transform that this joint applies in the kinematic chain....
std::vector< VariableBounds > Bounds
The datatype for the joint bounds.
A link from the robot. Contains the constant transform applied to the link and its geometry.
Representation of a robot's state. This includes position, velocity, acceleration and effort.
const Eigen::Isometry3d & getGlobalLinkTransform(const std::string &link_name)
Get the link transform w.r.t. the root link (model frame) of the RobotModel. This is typically the ro...
const JointModelGroup * getJointModelGroup(const std::string &group) const
Get the model of a particular joint group.
void setToRandomPositions()
Set all joints to random values. Values will be within default bounds.
void update(bool force=false)
Update all transforms.
void setToDefaultValues()
Set all joints to their default positions. The default position is 0, or if that is not within bounds...
void changePlanningGroup(const std::string &group)
std::string getCurrentPlanningGroup() const
void setQueryGoalState(const moveit::core::RobotState &goal)
void setQueryStartState(const moveit::core::RobotState &start)
void dropVisualizedTrajectory()
moveit::core::RobotStateConstPtr getQueryStartState() const
void rememberPreviousStartState()
const moveit::core::RobotState & getPreviousState() const
moveit::core::RobotStateConstPtr getQueryGoalState() const
Ui::MotionPlanningUI * ui_
moveit::planning_interface::MoveGroupInterface::PlanPtr current_plan_
std::vector< moveit_msgs::msg::PlannerInterfaceDescription > planner_descriptions_
std::string default_planning_pipeline_
moveit::planning_interface::MoveGroupInterfacePtr move_group_
void constructPlanningRequest(moveit_msgs::msg::MotionPlanRequest &mreq)
MotionPlanningDisplay * planning_display_
planning_scene_monitor::LockedPlanningSceneRO getPlanningSceneRO() const
get read-only access to planning scene
void spawnBackgroundJob(const std::function< void()> &job)
void addMainLoopJob(const std::function< void()> &job)
queue the execution of this function for the next time the main update() loop gets called
void addBackgroundJob(const std::function< void()> &job, const std::string &name)
bool waitForCurrentRobotState(const rclcpp::Time &t)
wait for robot state more recent than t
const planning_scene_monitor::PlanningSceneMonitorPtr & getPlanningSceneMonitor()
This is a convenience class for obtaining access to an instance of a locked PlanningScene.
Maintain a sequence of waypoints and the time durations between these waypoints.
bool computeTimeStamps(robot_trajectory::RobotTrajectory &trajectory, const double max_velocity_scaling_factor=1.0, const double max_acceleration_scaling_factor=1.0) const override
Compute a trajectory with waypoints spaced equally in time (according to resample_dt_)....
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.
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::msg::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
Convert a robot state msg (with accompanying extra transforms) to a MoveIt robot state.
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
std::string append(const std::string &left, const std::string &right)