42#include <object_recognition_msgs/action/object_recognition.hpp>
44#include <tf2_eigen/tf2_eigen.hpp>
46#include "ui_motion_planning_rviz_plugin_frame.h"
52void MotionPlanningFrame::detectObjectsButtonClicked()
70void MotionPlanningFrame::processDetectedObjects()
72 pick_object_name_.clear();
74 std::vector<std::string> object_ids;
75 double min_x =
ui_->roi_center_x->value() -
ui_->roi_size_x->value() / 2.0;
76 double min_y =
ui_->roi_center_y->value() -
ui_->roi_size_y->value() / 2.0;
77 double min_z =
ui_->roi_center_z->value() -
ui_->roi_size_z->value() / 2.0;
79 double max_x =
ui_->roi_center_x->value() +
ui_->roi_size_x->value() / 2.0;
80 double max_y =
ui_->roi_center_y->value() +
ui_->roi_size_y->value() / 2.0;
81 double max_z =
ui_->roi_center_z->value() +
ui_->roi_size_z->value() / 2.0;
83 rclcpp::Time start_time = rclcpp::Clock().now();
84 while (object_ids.empty() && (rclcpp::Clock().now() - start_time) <= rclcpp::Duration::from_seconds(3))
89 const collision_detection::WorldConstPtr& world = ps->getWorld();
91 for (
const auto&
object : *world)
93 const auto& position =
object.second->pose_.translation();
94 if (position.x() >= min_x && position.x() <= max_x && position.y() >= min_y && position.y() <= max_y &&
95 position.z() >= min_z && position.z() <= max_z)
97 object_ids.push_back(
object.first);
100 if (!object_ids.empty())
103 rclcpp::sleep_for(std::chrono::milliseconds(500));
106 RCLCPP_DEBUG(logger_,
"Found %d objects",
static_cast<int>(object_ids.size()));
107 updateDetectedObjectsList(object_ids);
110void MotionPlanningFrame::selectedDetectedObjectChanged()
112 QList<QListWidgetItem*> sel =
ui_->detected_objects_list->selectedItems();
115 RCLCPP_INFO(logger_,
"No objects to select");
119 std_msgs::msg::ColorRGBA pick_object_color;
120 pick_object_color.r = 1.0;
121 pick_object_color.g = 0.0;
122 pick_object_color.b = 0.0;
123 pick_object_color.a = 1.0;
127 if (!selected_object_name_.empty())
128 ps->removeObjectColor(selected_object_name_);
129 selected_object_name_ = sel[0]->text().toStdString();
130 ps->setObjectColor(selected_object_name_, pick_object_color);
134void MotionPlanningFrame::detectedObjectChanged(QListWidgetItem* )
138void MotionPlanningFrame::triggerObjectDetection()
140 if (!object_recognition_client_)
142 object_recognition_client_ = rclcpp_action::create_client<object_recognition_msgs::action::ObjectRecognition>(
144 if (!object_recognition_client_->wait_for_action_server(std::chrono::seconds(3)))
146 RCLCPP_ERROR(logger_,
"Object recognition action server not responsive");
151 object_recognition_msgs::action::ObjectRecognition::Goal goal;
152 auto goal_handle_future = object_recognition_client_->async_send_goal(goal);
153 goal_handle_future.wait();
154 if (goal_handle_future.get()->get_status() != rclcpp_action::GoalStatus::STATUS_SUCCEEDED)
156 RCLCPP_ERROR(logger_,
"ObjectRecognition client: send goal call failed");
161void MotionPlanningFrame::listenDetectedObjects(
162 const object_recognition_msgs::msg::RecognizedObjectArray::ConstSharedPtr& )
164 rclcpp::sleep_for(std::chrono::seconds(1));
168void MotionPlanningFrame::updateDetectedObjectsList(
const std::vector<std::string>& object_ids)
170 ui_->detected_objects_list->setUpdatesEnabled(
false);
171 bool old_state =
ui_->detected_objects_list->blockSignals(
true);
172 ui_->detected_objects_list->clear();
174 for (std::size_t i = 0; i < object_ids.size(); ++i)
176 QListWidgetItem* item =
177 new QListWidgetItem(QString::fromStdString(object_ids[i]),
ui_->detected_objects_list,
static_cast<int>(i));
178 item->setToolTip(item->text());
179 Qt::ItemFlags flags = item->flags();
180 flags &= ~(Qt::ItemIsUserCheckable);
181 item->setFlags(flags);
182 ui_->detected_objects_list->addItem(item);
185 ui_->detected_objects_list->blockSignals(old_state);
186 ui_->detected_objects_list->setUpdatesEnabled(
true);
187 if (!object_ids.empty())
188 ui_->pick_button->setEnabled(
true);
192void MotionPlanningFrame::updateTables()
194 RCLCPP_DEBUG(logger_,
"Update table callback");
198void MotionPlanningFrame::publishTables()
206void MotionPlanningFrame::selectedSupportSurfaceChanged()
208 QList<QListWidgetItem*> sel =
ui_->support_surfaces_list->selectedItems();
211 RCLCPP_INFO(logger_,
"No tables to select");
215 std_msgs::msg::ColorRGBA selected_support_surface_color;
216 selected_support_surface_color.r = 0.0;
217 selected_support_surface_color.g = 0.0;
218 selected_support_surface_color.b = 1.0;
219 selected_support_surface_color.a = 1.0;
223 if (!selected_support_surface_name_.empty())
224 ps->removeObjectColor(selected_support_surface_name_);
225 selected_support_surface_name_ = sel[0]->text().toStdString();
226 ps->setObjectColor(selected_support_surface_name_, selected_support_surface_color);
230void MotionPlanningFrame::updateSupportSurfacesList()
242 std::vector<std::string> support_ids;
243 RCLCPP_INFO(logger_,
"%d Tables in collision world",
static_cast<int>(support_ids.size()));
245 ui_->support_surfaces_list->setUpdatesEnabled(
false);
246 bool old_state =
ui_->support_surfaces_list->blockSignals(
true);
247 ui_->support_surfaces_list->clear();
249 for (std::size_t i = 0; i < support_ids.size(); ++i)
251 QListWidgetItem* item =
252 new QListWidgetItem(QString::fromStdString(support_ids[i]),
ui_->support_surfaces_list,
static_cast<int>(i));
253 item->setToolTip(item->text());
254 Qt::ItemFlags flags = item->flags();
255 flags &= ~(Qt::ItemIsUserCheckable);
256 item->setFlags(flags);
257 ui_->support_surfaces_list->addItem(item);
260 ui_->support_surfaces_list->blockSignals(old_state);
261 ui_->support_surfaces_list->setUpdatesEnabled(
true);
265void MotionPlanningFrame::pickObjectButtonClicked()
267 RCLCPP_WARN_STREAM(logger_,
"Pick & Place capability isn't supported yet");
304void MotionPlanningFrame::placeObjectButtonClicked()
306 QList<QListWidgetItem*> sel_table =
ui_->support_surfaces_list->selectedItems();
309 if (!sel_table.empty())
311 support_surface_name_ = sel_table[0]->text().toStdString();
315 support_surface_name_.clear();
316 RCLCPP_ERROR(logger_,
"Need to specify table to place object on");
320 ui_->pick_button->setEnabled(
false);
321 ui_->place_button->setEnabled(
false);
323 std::vector<const moveit::core::AttachedBody*> attached_bodies;
327 RCLCPP_ERROR(logger_,
"No planning scene");
332 ps->getCurrentState().getAttachedBodies(attached_bodies, jmg);
334 if (attached_bodies.empty())
336 RCLCPP_ERROR(logger_,
"No bodies to place");
340 geometry_msgs::msg::Quaternion upright_orientation;
341 upright_orientation.w = 1.0;
344 place_poses_.clear();
std::string getCurrentPlanningGroup() const
Ui::MotionPlanningUI * ui_
MotionPlanningDisplay * planning_display_
planning_scene_monitor::LockedPlanningSceneRO getPlanningSceneRO() const
get read-only access to planning scene
planning_scene_monitor::LockedPlanningSceneRW getPlanningSceneRW()
get write access to planning scene
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)
This is a convenience class for obtaining access to an instance of a locked PlanningScene.
This is a convenience class for obtaining access to an instance of a locked PlanningScene.
const std::string OBJECT_RECOGNITION_ACTION