Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00039
00040
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
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
00099
00100
00101
00102 class YarpRpcComm : public Comm, public jlo::LazyLocatedObjectLoader
00103 {
00104 public:
00105
00106
00107
00108
00109
00110
00111 YarpRpcComm(std::string st)
00112 {
00113 yarp::os::Network::init();
00114
00115 {
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
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
00142
00143
00144
00145 ~YarpRpcComm(void)
00146 {
00147 }
00148
00149
00150
00151
00152
00153
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
00261 #endif
00262