YarpRpcComm.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
00003  * 
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018  
00019 #ifdef USE_YARP_COMM
00020 #ifndef YARPRPCCOMM_H
00021 #define YARPRPCCOMM_H
00022 
00023 #include "RelPose.h"
00024 #include "Comm.h"
00025 
00026 
00027 #include <yarp/os/all.h>
00028 
00029 #define STD_LO_RPC_PORT "/lo/in"
00030 
00031 #ifdef BOOST_THREAD
00032 #include <boost/thread.hpp>
00033 #include <boost/bind.hpp>
00034 using namespace boost;
00035 #endif
00036 
00037 /********************************************************************
00038 *     CreatePoseFromBottle                                         */
00039 /********************************************************************
00040 *   @brief Creates a lo from a bottle
00041 *********************************************************************/
00042 RelPose* ReadPoseFromBottle(yarp::os::Bottle& lo, jlo::LazyLocatedObjectLoader* loca)
00043 {
00044     RelPose* result = NULL;
00045     if(!lo.get(0).isInt() || !lo.get(1).isInt())
00046     {
00047       printf("Bottle that should contain a lo is invalid: no id or no parent id\n");
00048       return result;
00049     }
00051     int id = lo.get(0).asInt();
00052     int parent = lo.get(1).asInt();
00054     Matrix m(4,4), cov(6,6);
00055     /* Bottle per matrix*/
00056     if(lo.get(2).isList())
00057     {
00058       yarp::os::Bottle* matrix = lo.get(2).asList();
00059       int width = 4;
00060       for(int r = 0; r < width; r++)
00061       {
00062         for(int c = 0; c < width; c++)
00063         {
00064           yarp::os::Value& val = matrix->get(r * width + c);
00065           if(val.isDouble())
00066             m.element(r,c) = val.asDouble();
00067           else
00068             m.element(r,c) = 0;
00069         }
00070       }
00071     }
00072     else return result;
00073     if(lo.get(3).isList())
00074     {
00075       int width = 6;
00076       yarp::os::Bottle* matrix = lo.get(3).asList();
00077       for(int r = 0; r < width; r++)
00078       {
00079         for(int c = 0; c < width; c++)
00080         {
00081           yarp::os::Value& val = matrix->get(r * width + c);
00082           if(val.isDouble())
00083             cov.element(r,c) = val.asDouble();
00084           else
00085             cov.element(r,c) = 0;
00086         }
00087       }
00088     }
00089     else
00090     {
00091       printf("Bottle that should contain a lo is invalid: one of the matrices missing\n");
00092       return result;
00093     }
00094     return new RelPose(loca, id, parent, m, cov);
00095 }
00096 
00097 /*****************************************************************************************
00098 *  class YarpRpcComm                                                                             */
00099 /*****************************************************************************************
00100 *  Class organizing the answer system to a specifie yarp port
00101 ******************************************************************************************/
00102 class YarpRpcComm : public Comm, public jlo::LazyLocatedObjectLoader
00103 {
00104 public:
00105 
00106   /*****************************************************************************************
00107   *  YarpRpcComm                                                                             */
00108   /*****************************************************************************************
00109   *  Constructor
00110   ******************************************************************************************/
00111   YarpRpcComm(std::string st)
00112   {
00113     yarp::os::Network::init();
00114    /* while(!yarp::os::Network::checkNetwork())*/
00115     {
00116 /*      printf("Waiting for the yarp server to initialize\n");
00117 #ifdef BOOST_THREAD
00118                         #ifdef BOOST_1_35
00119   BOOST(boost::system_time t);
00120 #else
00121   boost::xtime t;
00122 #endif
00123 
00124                         #ifdef BOOST_1_35
00125   BOOST(t = get_system_time());
00126 #else
00127   boost::xtime_get(&t, boost::TIME_UTC);
00128 #endif
00129 
00130                         t.sec += 4;
00131                         boost::thread::sleep(t);
00132 #else
00133 #endif*/
00134     }
00135     m_port.open(st.length() == 0 ? STD_LO_RPC_PORT_INTERNAL : st.c_str());
00136     yarp::os::Network::connect(st.length() == 0 ? STD_LO_RPC_PORT_INTERNAL : st.c_str(), STD_LO_RPC_PORT);
00137   }
00138 
00139 
00140   /*****************************************************************************************
00141   *  ~YarpRpcComm                                                                             */
00142   /*****************************************************************************************
00143   *  Destructor
00144   ******************************************************************************************/
00145   ~YarpRpcComm(void)
00146   {
00147   }
00148 
00149   /*****************************************************************************************
00150   *  NotifyPoseUpdate                                                                        */
00151   /*****************************************************************************************
00152   *  Call back that is called whenever a new pose is set for a certain model
00153   *  This callback must be told the signature that is tracked
00154   ******************************************************************************************/
00155   virtual void NotifyPoseUpdate(RelPose* pose, bool sendObjectRelation = true)
00156   {
00157         yarp::os::Bottle query_bottle;
00158         yarp::os::Bottle port_answer_bottle;
00159         query_bottle.clear();
00160         port_answer_bottle.clear();
00161         PutPoseIntoABottle(query_bottle, pose);
00162         sendObjectRelation;
00163         if(!m_port.write(query_bottle, port_answer_bottle))
00164         {
00165           throw "Error in LO service!\n";
00166         }
00167   }
00168   virtual RelPose* CreateNewPose(RelPose* pose, Matrix* mat, Matrix* cov)
00169   {
00170         yarp::os::Bottle query_bottle;
00171         yarp::os::Bottle port_answer_bottle;
00172         query_bottle.clear();
00173         port_answer_bottle.clear();
00174         if(pose != NULL)
00175           query_bottle.addInt(pose->m_uniqueID);
00176         else
00177           query_bottle.addInt(ID_WORLD);
00178         AddMatrixToABottle(4, query_bottle, *mat);
00179         AddMatrixToABottle(6, query_bottle, *cov);
00180         if(!m_port.write(query_bottle, port_answer_bottle))
00181         {
00182           throw "Error in LO service!\n";
00183         }
00184         if(port_answer_bottle.size() > 0)
00185         {
00186           return ReadPoseFromBottle(port_answer_bottle, this);
00187         }
00188         else
00189           return NULL;
00190   }
00191   virtual RelPose* GetPose(int poseId)
00192   {
00193         yarp::os::Bottle query_bottle;
00194         yarp::os::Bottle port_answer_bottle;
00195         query_bottle.clear();
00196         port_answer_bottle.clear();
00197         query_bottle.addInt(poseId);
00198         if(!m_port.write(query_bottle, port_answer_bottle))
00199         {
00200           printf("Could not reach LO service\n");
00201           throw "Error in LO service!\n";
00202         }
00203         if(port_answer_bottle.size() > 0)
00204         {
00205           return ReadPoseFromBottle(port_answer_bottle, this);
00206         }
00207         else
00208         {
00209           printf("Empty answer from LO service (Wrong query?)\n");
00210           throw "Error in LO service!\n";
00211         }
00212   };
00213 
00214   virtual jlo::LocatedObject* GetParent(const jlo::LocatedObject& child)
00215   {
00216     if(child.m_uniqueID == ID_WORLD)
00217       return NULL;
00218     return GetPose(child.m_parentID);
00219   };
00220 
00221   virtual RelPose* GetPoseRelative(int poseId, int parentPoseId)
00222   {
00223         yarp::os::Bottle query_bottle;
00224         yarp::os::Bottle port_answer_bottle;
00225         query_bottle.clear();
00226         port_answer_bottle.clear();
00227         query_bottle.addInt(poseId);
00228         query_bottle.addInt(parentPoseId);
00229         if(!m_port.write(query_bottle, port_answer_bottle))
00230         {
00231           throw "Error in LO service!\n";
00232         }
00233         if(port_answer_bottle.size() > 0)
00234         {
00235           return ReadPoseFromBottle(port_answer_bottle, this);
00236         }
00237         else
00238           return NULL;
00239   };
00240 
00241   virtual void FreePose(int poseId)
00242   {
00243         yarp::os::Bottle query_bottle;
00244         yarp::os::Bottle port_answer_bottle;
00245         query_bottle.clear();
00246         port_answer_bottle.clear();
00247         query_bottle.addString("del");
00248         query_bottle.addInt(poseId);
00249         if(!m_port.write(query_bottle, port_answer_bottle))
00250         {
00251           throw "Error in LO service!\n";
00252         }
00253   }
00254 
00255 private:
00256   yarp::os::Port m_port;
00257   YarpRpcComm& operator=(YarpRpcComm&){}
00258 };
00259 
00260 #endif /*YARPRPCCOMM_H*/
00261 #endif /*USE_YARP_COMM*/
00262 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Thu May 23 2013 07:38:35