valid_state_publisher.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef COB_COLLISION_MONITOR_VALID_STATE_PUBLISHER_H
19 #define COB_COLLISION_MONITOR_VALID_STATE_PUBLISHER_H
20 
21 #include <exception>
22 
24 #include <std_msgs/Bool.h>
25 
26 #include <moveit/collision_detection/collision_common.h>
27 
28 namespace cob_collision_monitor{
30  planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_;
34  std_msgs::Bool data_;
35  bool verbose_;
36  moveit_msgs::PlanningScene diff_msg_;
37 public:
38  ValidStatePublisher(ros::NodeHandle nh, planning_scene_monitor::PlanningSceneMonitorPtr psm)
39  : planning_scene_monitor_(psm),
40  was_complete_(false),
41  max_age_(nh.param("max_age", 1.0)),
42  pub_(nh.advertise<std_msgs::Bool>("state_is_valid", 1)),
43  data_(),
44  verbose_(nh.param("verbose", false))
45  {
46  double ground_size = nh.param("ground_size", 10.0);
47 
48  if(ground_size != 0.0){
49 
50  moveit_msgs::CollisionObject co;
51  co.header.frame_id = nh.param("ground_link",std::string("base_link"));
52  co.id ="_ground_plane_";
53 
54  if(ground_size > 0){
55  co.primitives.resize(1);
56  co.primitives.front().type = shape_msgs::SolidPrimitive::BOX;
57  co.primitives.front().dimensions.resize(3);
58  co.primitives.front().dimensions[0] = ground_size;
59  co.primitives.front().dimensions[1] = ground_size;
60  co.primitives.front().dimensions[2] = 0.01;
61  co.primitive_poses.resize(1);
62  co.primitive_poses.front().position.z = -0.005 + nh.param("ground_height",-0.001);
63  co.primitive_poses.front().orientation.w = 1.0;
64  }else{
65  co.planes.resize(1);
66  co.planes.front().coef[2] = 1; // only z, parallel to xy-plane
67  co.plane_poses.resize(1);
68  co.plane_poses.front().position.z = nh.param("ground_height",-0.001);
69  co.plane_poses.front().orientation.w = 1.0;
70  }
71  diff_msg_.world.collision_objects.push_back(co);
72  diff_msg_.is_diff = true;
73  }
74 
75  data_.data = true;
76 
77  planning_scene_monitor_->addUpdateCallback(boost::bind(&ValidStatePublisher::updatedScene,this,_1));
78  }
79 
80 
82  try
83  {
84  planning_scene_monitor::CurrentStateMonitorPtr csm = planning_scene_monitor_->getStateMonitor();
85  bool complete = max_age_.isZero() ? csm->haveCompleteState() : csm->haveCompleteState(max_age_);
86  if(complete){
87  was_complete_ = true;
88  planning_scene_monitor::LockedPlanningSceneRO ps(planning_scene_monitor_);
89  planning_scene::PlanningScenePtr diff_ps = ps->diff(diff_msg_);
90  data_.data = !diff_ps->isStateColliding("", verbose_);
91  pub_.publish(data_);
92 
93  if(verbose_)
94  {
97  diff_ps->getCollidingPairs(contacts);
98  ROS_DEBUG("#Collisions: %zu", contacts.size());
99  for (collision_detection::CollisionResult::ContactMap::iterator map_it=contacts.begin(); map_it!=contacts.end(); ++map_it)
100  {
101  ROS_ERROR("Collision between %s and %s", map_it->first.first.c_str(), map_it->first.second.c_str());
102  ROS_DEBUG("#Contacts: %zu", map_it->second.size());
103  for (std::vector<collision_detection::Contact>::iterator vec_it=map_it->second.begin(); vec_it!=map_it->second.end(); ++vec_it)
104  {
105  ROS_DEBUG("Depth: %f", vec_it->depth);
106  }
107  }
108  }
109  }else if(was_complete_){
110  ROS_DEBUG("was not complete");
111  data_.data = false;
112  pub_.publish(data_);
113  }
114  }
115  catch(std::exception &ex)
116  {
117  ROS_DEBUG("std::exception: %s", ex.what());
118  return;
119  }
120  catch (...) {
121  ROS_DEBUG("Exception occurred");
122  return;
123  }
124  }
125 };
126 }
127 
128 #endif
bool param(const std::string &param_name, T &param_val, const T &default_val)
void publish(const boost::shared_ptr< M > &message) const
planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_
ValidStatePublisher(ros::NodeHandle nh, planning_scene_monitor::PlanningSceneMonitorPtr psm)
std::map< std::pair< std::string, std::string >, std::vector< Contact > > ContactMap
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void updatedScene(planning_scene_monitor::PlanningSceneMonitor::SceneUpdateType type)
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


cob_collision_monitor
Author(s): Mathias Lüdtke
autogenerated on Sun Dec 6 2020 03:25:44