valid_state_publisher.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #ifndef COB_COLLISION_MONITOR_VALID_STATE_PUBLISHER_H
00019 #define COB_COLLISION_MONITOR_VALID_STATE_PUBLISHER_H
00020 
00021 #include <exception>
00022 
00023 #include <moveit/planning_scene_monitor/planning_scene_monitor.h>
00024 #include <std_msgs/Bool.h>
00025 
00026 #include <moveit/collision_detection/collision_common.h>
00027 
00028 namespace cob_collision_monitor{
00029 class ValidStatePublisher{
00030     planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_;
00031     bool was_complete_;
00032     ros::Duration max_age_;
00033     ros::Publisher pub_;
00034     std_msgs::Bool data_;
00035     bool verbose_;
00036     moveit_msgs::PlanningScene diff_msg_;
00037 public:
00038     ValidStatePublisher(ros::NodeHandle nh, planning_scene_monitor::PlanningSceneMonitorPtr psm)
00039     : planning_scene_monitor_(psm),
00040       was_complete_(false),
00041       max_age_(nh.param("max_age", 1.0)),
00042       pub_(nh.advertise<std_msgs::Bool>("state_is_valid", 1)),
00043       data_(),
00044       verbose_(nh.param("verbose", false))
00045     {
00046         double ground_size = nh.param("ground_size", 10.0);
00047          
00048         if(ground_size != 0.0){
00049 
00050             moveit_msgs::CollisionObject co;
00051             co.header.frame_id = nh.param("ground_link",std::string("base_link"));
00052             co.id ="_ground_plane_";
00053 
00054             if(ground_size > 0){
00055                 co.primitives.resize(1);
00056                 co.primitives.front().type = shape_msgs::SolidPrimitive::BOX;
00057                 co.primitives.front().dimensions.resize(3);
00058                 co.primitives.front().dimensions[0] = ground_size;
00059                 co.primitives.front().dimensions[1] = ground_size;
00060                 co.primitives.front().dimensions[2] = 0.01;
00061                 co.primitive_poses.resize(1);
00062                 co.primitive_poses.front().position.z = -0.005 + nh.param("ground_height",-0.001);
00063                 co.primitive_poses.front().orientation.w = 1.0;
00064             }else{
00065                 co.planes.resize(1);
00066                 co.planes.front().coef[2] = 1; // only z, parallel to xy-plane
00067                 co.plane_poses.resize(1);
00068                 co.plane_poses.front().position.z = nh.param("ground_height",-0.001);
00069                 co.plane_poses.front().orientation.w = 1.0;
00070             }
00071             diff_msg_.world.collision_objects.push_back(co);
00072             diff_msg_.is_diff = true;
00073         }
00074 
00075         data_.data = true;
00076 
00077         planning_scene_monitor_->addUpdateCallback(boost::bind(&ValidStatePublisher::updatedScene,this,_1));
00078     }
00079 
00080 
00081     void updatedScene(planning_scene_monitor::PlanningSceneMonitor::SceneUpdateType type){
00082         try
00083         {
00084             planning_scene_monitor::CurrentStateMonitorPtr csm = planning_scene_monitor_->getStateMonitor();
00085             bool complete = max_age_.isZero() ? csm->haveCompleteState() : csm->haveCompleteState(max_age_);
00086             if(complete){
00087                 was_complete_ = true;
00088                 planning_scene_monitor::LockedPlanningSceneRO ps(planning_scene_monitor_);
00089                 planning_scene::PlanningScenePtr diff_ps = ps->diff(diff_msg_);
00090                 data_.data = !diff_ps->isStateColliding("", verbose_);
00091                 pub_.publish(data_);
00092 
00093                 if(verbose_)
00094                 {
00096                     collision_detection::CollisionResult::ContactMap contacts;
00097                     diff_ps->getCollidingPairs(contacts);
00098                     ROS_DEBUG("#Collisions: %zu", contacts.size());
00099                     for (collision_detection::CollisionResult::ContactMap::iterator map_it=contacts.begin(); map_it!=contacts.end(); ++map_it)
00100                     {
00101                         ROS_ERROR("Collision between %s and %s", map_it->first.first.c_str(), map_it->first.second.c_str());
00102                         ROS_DEBUG("#Contacts: %zu", map_it->second.size());
00103                         for (std::vector<collision_detection::Contact>::iterator vec_it=map_it->second.begin(); vec_it!=map_it->second.end(); ++vec_it)
00104                         {
00105                             ROS_DEBUG("Depth: %f", vec_it->depth);
00106                         }
00107                     }
00108                 }
00109             }else if(was_complete_){
00110                 ROS_DEBUG("was not complete");
00111                 data_.data = false;
00112                 pub_.publish(data_);
00113             }
00114         }
00115         catch(std::exception &ex)
00116         {
00117             ROS_DEBUG("std::exception: %s", ex.what());
00118             return;
00119         }
00120         catch (...) {
00121             ROS_DEBUG("Exception occurred");
00122             return;
00123         }
00124     }
00125 };
00126 }
00127 
00128 #endif


cob_collision_monitor
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 21:22:41