InteractiveObject.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Institute for Artificial Intelligence,
00005  *  Universität Bremen.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Institute for Artificial Intelligence,
00019  *     Universität Bremen, nor the names of its contributors may be
00020  *     used to endorse or promote products derived from this software
00021  *     without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *********************************************************************/
00036 
00040 #include <plugins/interactive/InteractiveObject.h>
00041 
00042 
00043 namespace beliefstate {
00044   InteractiveObject::InteractiveObject(std::string strName) {
00045     m_imsServer = NULL;
00046     
00047     m_imMarker.header.frame_id = "/map";
00048     m_imMarker.scale = 1;
00049     m_imMarker.name = strName;
00050     
00051     m_mkrMarker.type = visualization_msgs::Marker::CUBE;
00052     m_mkrMarker.scale.x = m_imMarker.scale * 0.45;
00053     m_mkrMarker.scale.y = m_imMarker.scale * 0.45;
00054     m_mkrMarker.scale.z = m_imMarker.scale * 0.45;
00055     m_mkrMarker.color.r = 0.5;
00056     m_mkrMarker.color.g = 0.5;
00057     m_mkrMarker.color.b = 0.5;
00058     m_mkrMarker.color.a = 1.0;
00059   }
00060   
00061   InteractiveObject::~InteractiveObject() {
00062   }
00063   
00064   void InteractiveObject::setSize(float fWidth, float fDepth, float fHeight) {
00065     m_mkrMarker.scale.x = m_imMarker.scale * fWidth;
00066     m_mkrMarker.scale.y = m_imMarker.scale * fDepth;
00067     m_mkrMarker.scale.z = m_imMarker.scale * fHeight;
00068     
00069     this->insertIntoServer(m_imsServer);
00070   }
00071   
00072   void InteractiveObject::setPose(std::string strFixedFrame, geometry_msgs::Pose posPose) {
00073     m_imMarker.header.frame_id = strFixedFrame;
00074     m_imMarker.pose = posPose;
00075     
00076     this->insertIntoServer(m_imsServer);
00077   }
00078   
00079   void InteractiveObject::setPose(geometry_msgs::Pose posPose) {
00080     this->setPose(m_imMarker.header.frame_id, posPose);
00081   }
00082   
00083   void InteractiveObject::setMarker(visualization_msgs::Marker mkrMarker) {
00084     m_mkrMarker = mkrMarker;
00085     
00086     this->insertIntoServer(m_imsServer);
00087   }
00088   
00089   void InteractiveObject::clickCallback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback) {
00090     interactive_markers::MenuHandler::EntryHandle entHandle = feedback->menu_entry_id;
00091     
00092     bool bFound = false;
00093     InteractiveMenuEntry imeEntry;
00094     
00095     for(InteractiveMenuEntry imeEntry : m_lstMenuEntries) {
00096       if(imeEntry.unMenuEntryID == entHandle) {
00097         imeEntry = imeEntry;
00098         bFound = true;
00099         
00100         break;
00101       }
00102     }
00103     
00104     if(bFound) {
00105       // Send out the symbolic event containing the callback result.
00106       InteractiveObjectCallbackResult iocrResult;
00107       iocrResult.strObject = feedback->marker_name;
00108       iocrResult.strCommand = imeEntry.strIdentifier;
00109       iocrResult.strParameter = imeEntry.strParameter;
00110       
00111       m_mtxCallbackResults.lock();
00112       m_lstCallbackResults.push_back(iocrResult);
00113       m_mtxCallbackResults.unlock();
00114     }
00115   }
00116   
00117   bool InteractiveObject::insertIntoServer(interactive_markers::InteractiveMarkerServer* imsServer) {
00118     if(ros::ok()) {
00119       if(imsServer) {
00120         m_imcControl.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00121         m_imcControl.always_visible = true;
00122       
00123         m_imcControl.markers.clear();
00124         m_imcControl.markers.push_back(m_mkrMarker);
00125       
00126         m_imMarker.controls.clear();
00127         m_imMarker.controls.push_back(m_imcControl);
00128       
00129         imsServer->insert(m_imMarker);
00130         m_mhMenu.apply(*imsServer, m_imMarker.name);
00131         imsServer->applyChanges();
00132       
00133         m_imsServer = imsServer;
00134       
00135         return true;
00136       }
00137     }
00138     
00139     return false;
00140   }
00141   
00142   bool InteractiveObject::removeFromServer() {
00143     if(ros::ok()) {
00144       if(m_imsServer) {
00145         m_imsServer->erase(m_imMarker.name);
00146         m_imsServer->applyChanges();
00147         
00148         return true;
00149       }
00150     }
00151     
00152     return false;
00153   }
00154   
00155   std::string InteractiveObject::name() {
00156     return m_imMarker.name;
00157   }
00158   
00159   void InteractiveObject::addMenuEntry(std::string strLabel, std::string strIdentifier, std::string strParameter) {
00160     InteractiveMenuEntry imeEntry;
00161     imeEntry.strLabel = strLabel;
00162     imeEntry.strIdentifier = strIdentifier;
00163     imeEntry.strParameter = strParameter;
00164     
00165     interactive_markers::MenuHandler::EntryHandle entEntry = m_mhMenu.insert(strLabel, boost::bind(&InteractiveObject::clickCallback, this, _1));
00166     m_mhMenu.setCheckState(entEntry, interactive_markers::MenuHandler::NO_CHECKBOX);
00167     
00168     imeEntry.unMenuEntryID = entEntry;
00169     
00170     m_lstMenuEntries.push_back(imeEntry);
00171     
00172     if(m_imsServer) {
00173       this->insertIntoServer(m_imsServer);
00174     }
00175   }
00176   
00177   void InteractiveObject::clearMenuEntries() {
00178     interactive_markers::MenuHandler mhMenu;
00179     
00180     m_lstMenuEntries.clear();
00181     m_mhMenu = mhMenu;
00182     
00183     if(m_imsServer) {
00184       this->insertIntoServer(m_imsServer);
00185     }
00186   }
00187   
00188   void InteractiveObject::removeMenuEntry(std::string strIdentifier, std::string strParameter) {
00189     interactive_markers::MenuHandler mhMenu;
00190     
00191     for(std::list<InteractiveMenuEntry>::iterator itIME = m_lstMenuEntries.begin();
00192         itIME != m_lstMenuEntries.end();
00193         itIME++) {
00194       if((*itIME).strIdentifier == strIdentifier && (*itIME).strParameter == strParameter) {
00195         m_lstMenuEntries.erase(itIME);
00196         
00197         break;
00198       }
00199     }
00200     
00201     for(InteractiveMenuEntry imeEntry : m_lstMenuEntries) {
00202       interactive_markers::MenuHandler::EntryHandle entEntry = m_mhMenu.insert(imeEntry.strLabel, boost::bind(&InteractiveObject::clickCallback, this, _1));
00203       m_mhMenu.setCheckState(entEntry, interactive_markers::MenuHandler::NO_CHECKBOX);
00204     }
00205     
00206     m_mhMenu = mhMenu;
00207     
00208     if(m_imsServer) {
00209       this->insertIntoServer(m_imsServer);
00210     }
00211   }
00212   
00213   std::list<InteractiveObjectCallbackResult> InteractiveObject::callbackResults() {
00214     std::list<InteractiveObjectCallbackResult> lstResults;
00215     
00216     m_mtxCallbackResults.lock();
00217     lstResults = m_lstCallbackResults;
00218     m_lstCallbackResults.clear();
00219     m_mtxCallbackResults.unlock();
00220     
00221     return lstResults;
00222   }
00223 }


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15