PluginInteractive.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/PluginInteractive.h>
00041 
00042 
00043 namespace beliefstate {
00044   namespace plugins {
00045     PLUGIN_CLASS::PLUGIN_CLASS() {
00046       this->addDependency("ros");
00047       this->setPluginVersion("0.4");
00048       
00049       m_imsServer = NULL;
00050     }
00051     
00052     PLUGIN_CLASS::~PLUGIN_CLASS() {
00053       if(m_imsServer) {
00054         delete m_imsServer;
00055       }
00056     }
00057     
00058     Result PLUGIN_CLASS::init(int argc, char** argv) {
00059       Result resInit = defaultResult();
00060       
00061       // Initialize server
00062       m_imsServer = new interactive_markers::InteractiveMarkerServer("interactive_beliefstate", "", false);
00063       
00064       // Subscribe to internal events
00065       this->setSubscribedToEvent("symbolic-add-object", true);
00066       this->setSubscribedToEvent("symbolic-remove-object", true);
00067       this->setSubscribedToEvent("symbolic-update-object-pose", true);
00068       this->setSubscribedToEvent("symbolic-set-interactive-object-menu", true);
00069       
00070       return resInit;
00071     }
00072     
00073     InteractiveObject* PLUGIN_CLASS::updatePoseForInteractiveObject(std::string strName, geometry_msgs::Pose posUpdate) {
00074       InteractiveObject* ioUpdate = this->interactiveObjectForName(strName);
00075       
00076       if(!ioUpdate) {
00077         ioUpdate = this->addInteractiveObject(strName);
00078       }
00079       
00080       ioUpdate->setPose(posUpdate);
00081       
00082       return ioUpdate;
00083     }
00084     
00085     InteractiveObject* PLUGIN_CLASS::addInteractiveObject(std::string strName) {
00086       return this->addInteractiveObject(new InteractiveObject(strName));
00087     }
00088     
00089     InteractiveObject* PLUGIN_CLASS::addInteractiveObject(InteractiveObject* ioAdd) {
00090       m_lstInteractiveObjects.push_back(ioAdd);
00091       ioAdd->insertIntoServer(m_imsServer);
00092       
00093       return ioAdd;
00094     }
00095     
00096     InteractiveObject* PLUGIN_CLASS::interactiveObjectForName(std::string strName) {
00097       for(InteractiveObject* ioCurrent : m_lstInteractiveObjects) {
00098         if(ioCurrent->name() == strName) {
00099           return ioCurrent;
00100         }
00101       }
00102       
00103       return NULL;
00104     }
00105     
00106     bool PLUGIN_CLASS::removeInteractiveObject(std::string strName) {
00107       for(std::list<InteractiveObject*>::iterator itIO = m_lstInteractiveObjects.begin();
00108           itIO != m_lstInteractiveObjects.end();
00109           itIO++) {
00110         if((*itIO)->name() == strName) {
00111           (*itIO)->removeFromServer();
00112           m_lstInteractiveObjects.erase(itIO);
00113           
00114           return true;
00115         }
00116       }
00117       
00118       return false;
00119     }
00120     
00121     Result PLUGIN_CLASS::deinit() {
00122       Result resReturn = defaultResult();
00123       
00124       for(InteractiveObject* ioDelete : m_lstInteractiveObjects) {
00125         delete ioDelete;
00126       }
00127       
00128       return resReturn;
00129     }
00130     
00131     Result PLUGIN_CLASS::cycle() {
00132       Result resCycle = defaultResult();
00133       
00134       for(InteractiveObject* ioCurrent : m_lstInteractiveObjects) {
00135         list<InteractiveObjectCallbackResult> lstCBResults = ioCurrent->callbackResults();
00136         
00137         for(InteractiveObjectCallbackResult iocrResult : lstCBResults) {
00138           this->info("Interactive callback for object '" + iocrResult.strObject + "': '" + iocrResult.strCommand + "', '" + iocrResult.strParameter + "'");
00139           
00140           Event evCallback = defaultEvent("interactive-callback");
00141           evCallback.cdDesignator = new CDesignator();
00142           evCallback.cdDesignator->setType(ACTION);
00143           evCallback.cdDesignator->setValue("object", iocrResult.strObject);
00144           evCallback.cdDesignator->setValue("command", iocrResult.strCommand);
00145           evCallback.cdDesignator->setValue("parameter", iocrResult.strParameter);
00146           
00147           this->deployEvent(evCallback);
00148         }
00149       }
00150       
00151       this->deployCycleData(resCycle);
00152       
00153       return resCycle;
00154     }
00155     
00156     void PLUGIN_CLASS::consumeEvent(Event evEvent) {
00157       if(evEvent.strEventName == "symbolic-add-object") {
00158         if(evEvent.cdDesignator) {
00159           std::string strObjectName = evEvent.cdDesignator->stringValue("name");
00160           
00161           if(strObjectName != "") {
00162             InteractiveObject* ioNew = this->addInteractiveObject(strObjectName);
00163             
00164             bool bPoseGiven = false;
00165             if(evEvent.cdDesignator->childForKey("pose") != NULL) {
00166               ioNew->setPose(evEvent.cdDesignator->poseValue("pose"));
00167               bPoseGiven = true;
00168             }
00169             
00170             if(evEvent.cdDesignator->childForKey("pose-stamped") != NULL) {
00171               geometry_msgs::PoseStamped psPose = evEvent.cdDesignator->poseStampedValue("pose-stamped");
00172               ioNew->setPose(psPose.header.frame_id, psPose.pose);
00173               bPoseGiven = true;
00174             }
00175             
00176             if(!bPoseGiven) {
00177               this->info("No pose given for object '" + strObjectName + "'.");
00178             }
00179             
00180             float fWidth = 0.1;
00181             float fDepth = 0.1;
00182             float fHeight = 0.1;
00183             
00184             if(evEvent.cdDesignator->childForKey("width") != NULL &&
00185                evEvent.cdDesignator->childForKey("depth") != NULL &&
00186                evEvent.cdDesignator->childForKey("height") != NULL) {
00187               fWidth = evEvent.cdDesignator->floatValue("width");
00188               fDepth = evEvent.cdDesignator->floatValue("depth");
00189               fHeight = evEvent.cdDesignator->floatValue("height");
00190             } else {
00191               this->info("No dimension (width, depth, height) given for object '" + strObjectName + "', assuming default (0.1, 0.1, 0.1).");
00192             }
00193             
00194             ioNew->setSize(fWidth, fDepth, fHeight);
00195             
00196             if(evEvent.cdDesignator->childForKey("menu") != NULL) {
00197               ioNew->clearMenuEntries();
00198               
00199               CKeyValuePair* ckvpMenu = evEvent.cdDesignator->childForKey("menu");
00200               std::list<std::string> lstMenuKeys = ckvpMenu->keys();
00201               
00202               for(string strKey : lstMenuKeys) {
00203                 CKeyValuePair* ckvpMenuEntry = ckvpMenu->childForKey(strKey);
00204                 std::string strLabel = ckvpMenuEntry->stringValue("label");
00205                 std::string strParameter = ckvpMenuEntry->stringValue("parameter");
00206                 
00207                 ioNew->addMenuEntry(strLabel, strKey, strParameter);
00208                 this->info("Added menu entry '" + strKey + "': '" + strLabel + "'");
00209               }
00210             } else {
00211               this->info("No menu given for object '" + strObjectName + "'.");
00212             }
00213             
00214             this->info("Registered interactive object '" + strObjectName + "'.");
00215           } else {
00216             this->warn("No name given when adding interactive object!");
00217           }
00218         } else {
00219           this->warn("No designator given when adding interactive object!");
00220         }
00221       } else if(evEvent.strEventName == "symbolic-remove-object") {
00222         if(evEvent.cdDesignator) {
00223           std::string strObjectName = evEvent.cdDesignator->stringValue("name");
00224           
00225           if(strObjectName != "") {
00226             if(!this->removeInteractiveObject(strObjectName)) {
00227               this->warn("Tried to unregister non-existing interactive object '" + strObjectName + "'.");
00228             }
00229           }
00230         }
00231       } else if(evEvent.strEventName == "symbolic-update-object-pose") {
00232         if(evEvent.cdDesignator) {
00233           std::string strObjectName = evEvent.cdDesignator->stringValue("name");
00234           
00235           if(strObjectName != "") {
00236             geometry_msgs::Pose psSet = evEvent.cdDesignator->poseValue("pose");
00237             
00238             this->updatePoseForInteractiveObject(strObjectName, psSet);
00239           }
00240         }
00241       } else if(evEvent.strEventName == "symbolic-set-interactive-object-menu") {
00242         if(evEvent.cdDesignator) {
00243           std::string strObjectName = evEvent.cdDesignator->stringValue("name");
00244           
00245           if(strObjectName != "") {
00246             InteractiveObject* ioNew = this->interactiveObjectForName(strObjectName);
00247             
00248             if(ioNew) {
00249               ioNew->clearMenuEntries();
00250               
00251               CKeyValuePair* ckvpMenu = evEvent.cdDesignator->childForKey("menu");
00252               std::list<std::string> lstMenuKeys = ckvpMenu->keys();
00253               
00254               for(string strKey : lstMenuKeys) {
00255                 CKeyValuePair* ckvpMenuEntry = ckvpMenu->childForKey(strKey);
00256                 std::string strLabel = ckvpMenuEntry->stringValue("label");
00257                 std::string strParameter = ckvpMenuEntry->stringValue("parameter");
00258                 
00259                 ioNew->addMenuEntry(strLabel, strKey, strParameter);
00260                 this->info("Added menu entry '" + strKey + "': '" + strLabel + "'");
00261               }
00262             } else {
00263               this->warn("Interactive object '" + strObjectName + "' not known when setting menu!");
00264             }
00265           } else {
00266             this->warn("No name given when setting interactive object menu!");
00267           }
00268         } else {
00269           this->warn("No designator given when setting interactive object menu!");
00270         }
00271       }
00272     }
00273   }
00274   
00275   extern "C" plugins::PLUGIN_CLASS* createInstance() {
00276     return new plugins::PLUGIN_CLASS();
00277   }
00278   
00279   extern "C" void destroyInstance(plugins::PLUGIN_CLASS* icDestroy) {
00280     delete icDestroy;
00281   }
00282 }


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