RosLoService.cpp
Go to the documentation of this file.
00001 /**********************************************************************************************/
00002 /**********************************************************************************************/
00025 #include "RosLoService.h"
00026 
00027 #include "lo/ServiceLocatedObject.h"
00028 
00029 #include <tf/transform_listener.h>
00030 #include <string>
00031 #include <boost/bind.hpp>
00032 #include <boost/thread.hpp>
00033 
00034 #define IDQUERY "idquery"
00035 #define NAMEQUERY "namequery"
00036 #define FRAMEQUERY "framequery"
00037 #define DELETE "del"
00038 #define UPDATE "update"
00039 #define COUTLO "coutlo"
00040 #ifdef _DEBUG
00041 #define PRINTF_DEBUG(A) printf(A)
00042 #else
00043 #define PRINTF_DEBUG(A) printf(A)
00044 #endif
00045 
00046 std::queue<unsigned long> RosLoService::m_queueOfLosToPublish;
00047 
00048 RosLoService::RosLoService(const char* nodename, ros::NodeHandle &n, std::string configFile)
00049     : jlo::ServiceInterface(configFile.c_str()),
00050       located_object_service( n.advertiseService("/located_object", &RosLoService::ServiceCallback, this) )/*,
00051       located_object_callback_reagister_service( n.advertiseService("/register_jlo_callback", &RosLoService::CallbackRegisterService, this) )*/
00052 {
00053   using namespace std;
00054 
00058   printf("Advertise Service \"/located_object\"\n");
00059 
00063   if( n.hasParam( "tf_blacklist" ) )
00064   {
00065       using namespace XmlRpc;
00066 
00067       XmlRpcValue blacklist_param_value;
00068       n.getParam( "tf_blacklist", blacklist_param_value );
00069       if( blacklist_param_value.getType() != XmlRpcValue::TypeArray )
00070           ROS_WARN("tf_blacklist has invalid type.");
00071       else
00072       {
00073           for(int i=0; i<blacklist_param_value.size(); i++)
00074           {
00075               if( blacklist_param_value[i].getType() != XmlRpcValue::TypeString )
00076               {
00077                   ROS_WARN("Ignoring blacklist entry. Not a string");
00078                   continue;
00079               }
00080               string blacklist_entry = blacklist_param_value[i];
00081               tf_blacklist.insert( blacklist_entry );
00082               ROS_INFO("Adding '%s' to tf blacklist", blacklist_entry.c_str());
00083           }
00084       }
00085   }
00086 
00087   string tf_topic;
00088   n.param<string>( "tf_topic", tf_topic, "/tf" );
00089   printf("Subscribe \"%s\"\n", tf_topic.c_str());
00090   tf_subscription = n.subscribe<tf::tfMessage>( tf_topic, 5, boost::bind(&RosLoService::tf_subscription_callback, this, _1) );
00091   /*boost::thread(boost::bind(&RosLoService::UpdateEventHandler, this));*/
00092 }
00093 
00094 RosLoService::~RosLoService()
00095 {
00096   SaveList()->WriteToFile("bla.ini");
00097   printf("Wrote data to bla.ini\n");
00098 }
00099 
00100 bool PutLoIntoPartialLo( jlo::ServiceLocatedObject* lo, vision_msgs::partial_lo&  out_lo, bool incRef = true)
00101 {
00102   try
00103   {
00104     out_lo.id = lo->m_uniqueID;
00105     out_lo.parent_id = lo->m_parentID;
00106     int width = 4;
00107     Matrix m = lo->GetMatrix();
00108     Matrix cov = lo->GetCovarianceMatrix();
00109     if(incRef)
00110       lo->IncreaseReferenceCounter();
00111     for(int r = 0; r < width; r++)
00112     {
00113       for(int c = 0; c < width; c++)
00114       {
00115           out_lo.pose[r * width + c] = m.element(r,c);
00116       }
00117     }
00118     width = 6;
00119     for(int r = 0; r < width; r++)
00120     {
00121       for(int c = 0; c < width; c++)
00122       {
00123           out_lo.cov[r * width + c] = cov.element(r,c);
00124       }
00125     }
00126     out_lo.type = lo->GetLOType();
00127     out_lo.name = lo->m_mapstring;
00128   }
00129   catch(...)
00130   {
00131     printf("Critical Error in PutLoIntoResponse \n");
00132     return false;
00133   }
00134   return true;
00135 }
00136 
00137 bool PutLoIntoResponse( jlo::ServiceLocatedObject* lo, vision_srvs::srvjlo::Response&  answer)
00138 {
00139   vision_msgs::partial_lo& out_lo = answer.answer;
00140   return PutLoIntoPartialLo(lo, out_lo);
00141 }
00142 
00143 std::map<std::string, int> s_tester;
00144 std::map<std::string, int> s_tester_max;
00145 
00146 void IncreaseTester(std::string callerid)
00147 {
00148   s_tester[callerid] += 1;
00149   if(s_tester[callerid] >= 100)
00150   {
00151     s_tester_max[callerid] += s_tester[callerid];
00152     s_tester[callerid] = 0;
00153     ROS_WARN("Warning: the jlo caller %s registered %d jlos without deleting", callerid.c_str(), s_tester_max[callerid] );
00154   }
00155 }
00156 
00157 void DecreaseTester(std::string callerid)
00158 {
00159   s_tester[callerid] -= 1;
00160 }
00161 
00162 
00163 bool RosLoService::ServiceCallback(vision_srvs::srvjlo::Request& request, vision_srvs::srvjlo::Response&  answer)
00164 {
00165 std::string callerid = (*request.__connection_header)["callerid"];
00166  try
00167  {
00168    if(request.command.compare(IDQUERY) == 0)
00169    {
00170      /* case 1: One number can only mean an id: ID-Query*/
00171      jlo::ServiceLocatedObject* lo = GetServiceLocatedObject(request.query.id);
00172      /*printf("Requested ID: %d => %p\n", (int)request.query.id, lo);*/
00173      if (lo == NULL)
00174      {
00175        ROS_WARN("Error in IDquery: Id %ld does not exist! (Caller: %s)\n", request.query.id, callerid.c_str());
00176        answer.error = "Error in IDquery: Id does not exist!\n";
00177        return true;
00178      }
00179      IncreaseTester(callerid);
00180      PutLoIntoResponse(lo, answer);
00181    }
00182    else if(request.command.compare(NAMEQUERY) == 0)
00183    {
00184      long id = GetServiceLocatedObjectID(request.query.name);
00185      if(id >= ID_WORLD)
00186      {
00187        jlo::ServiceLocatedObject* lo = GetServiceLocatedObject(id);
00188        if (lo == NULL)
00189        {
00190          ROS_WARN("Error in NameQuery: Id %s does not exist!(Caller: %s)\n", request.query.name.c_str(), callerid.c_str());
00191          answer.error = "Error in NameQuery: Id does not exist!\n";
00192          return true;
00193        }
00194        PutLoIntoResponse(lo, answer);
00195      }
00196      else
00197      {
00198         ROS_WARN("Error in NameQuery: Name %s does not exist!(Caller: %s)\n", request.query.name.c_str(), callerid.c_str());
00199         answer.error = std::string("Error in NameQuery: Name does not exist: ").append(request.query.name);
00200         answer.answer.id = 0;
00201         return true;
00202       }
00203   }
00204   else if (request.command.compare(FRAMEQUERY) == 0)
00205   {
00206     try
00207     {
00208       unsigned long parentID = request.query.parent_id;
00209       jlo::ServiceLocatedObject* lo = GetServiceLocatedObject(request.query.id);
00210       if(lo != NULL && request.query.id == parentID)
00211       {
00212          answer.error = "Warning Parsing Input: Self reference ignored!\n";
00213          IncreaseTester(callerid);
00214          PutLoIntoResponse(lo, answer);
00215       }
00216       else
00217       {
00218         if (lo == NULL)
00219         {
00220           ROS_WARN("Error in Framequery: Id %ld does not exist! (Caller %s)\n", request.query.id, callerid.c_str());
00221           answer.error = "Error in Framequery: Id does not exist!\n";
00222           return true;
00223         }
00224         if(lo->m_uniqueID != ID_WORLD && lo->m_relation->m_uniqueID == (unsigned long)parentID)
00225         {
00226           IncreaseTester(callerid);
00227           PutLoIntoResponse(lo, answer);
00228         }
00229         else
00230         {
00231           jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(parentID);
00232           if (parent == NULL)
00233           {
00234              ROS_WARN("Error in Framequery: Parent Id %ld does not exist! (Caller: %s)\n", parentID, callerid.c_str());
00235              answer.error = "Error in Framequery: Parent Id does not exist!\n";
00236              return true;
00237           }
00238           Matrix mat = lo->GetMatrix(*parent);
00239           Matrix cov = lo->GetCovarianceMatrix(*parent);
00240           IncreaseTester(callerid);
00241           PutLoIntoResponse(FServiceLocatedObject(parent, mat, cov, 0), answer);
00242         }
00243       }
00244     }
00245     catch(...)
00246     {
00247       printf("Critical error in FrameQuery\n");
00248       return true;
00249     }
00250   }
00251   else if(request.command.compare(DELETE) == 0)
00252   {
00253     unsigned long id = request.query.id;
00254     if(id != ID_WORLD)
00255     {
00256       jlo::ServiceLocatedObject* obj  = GetServiceLocatedObject(id);
00257       if(obj == NULL)
00258       {
00259          ROS_WARN("Error in delete: Object %ld does not exist! (Caller: %s)\n", id, callerid.c_str());
00260          answer.error = "Error in delete: Object does not exist!\n";
00261          return true;
00262       }
00263       else
00264       {
00265         DecreaseTester(callerid);
00266         unsigned long ref_count = obj->GetReferenceCounter();
00267         /*ROS_DEBUG("Delete request from node %s for id %ld, parent %ld", callerid.c_str(), id, obj->m_parentID);*/
00268         FreeServiceLocatedObject(obj);
00269         if(ref_count > 1)
00270         {
00271           answer.error = "Warning in delete: Object is still referenced.";
00272         }
00273       }
00274     }
00275     else
00276     {
00277       PRINTF_DEBUG("Error in delete: Can't delete ID_WORLD!\n");
00278        answer.error = "Error in delete: Can't delete ID_WORLD!\n";
00279       return true;
00280     }
00281   }
00282  /*case error */
00283 /*semi complete*/
00284   else if(request.command.compare(UPDATE) == 0)
00285   {
00286     unsigned long parentID, id;
00287     bool update = false;
00288     unsigned long type = request.query.type;
00289     id =  request.query.id;
00290     parentID =  request.query.parent_id;
00291     if(id != 0)
00292     {
00293       update = true;
00294     }
00295     Matrix mat(4,4), cov(6,6);
00296 
00297     int width = 4;
00298     for(int r = 0; r < width; r++)
00299     {
00300       for(int c = 0; c < width; c++)
00301       {
00302           if((unsigned)(r * width + c) < request.query.pose.size())
00303              mat.element(r,c) = request.query.pose[r * width + c];
00304           else
00305             printf("Wrong length of pose\n");
00306 
00307       }
00308     }
00309     width = 6;
00310     for(int r = 0; r < width; r++)
00311     {
00312       for(int c = 0; c < width; c++)
00313       {
00314           if((unsigned)(r * width + c) < request.query.cov.size())
00315              cov.element(r,c) = request.query.cov[r * width + c];
00316           else
00317             printf("Wrong length of cov\n");
00318       }
00319     }
00320     if(update)
00321     {
00322       jlo::ServiceLocatedObject* pose = GetServiceLocatedObject(id);
00323       if(pose == NULL)
00324       {
00325         jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(parentID);
00326         if(parent == NULL)
00327         {
00328           PRINTF_DEBUG("Error in Update: Requested parent does not exist!\n");
00329           answer.error = "Error in Update: Requested parent does not exist!\n";
00330           return true;
00331         }
00332         jlo::ServiceLocatedObject* pose = FServiceLocatedObject(parent, mat, cov, type);
00333         IncreaseTester(callerid);
00334         if(request.query.name.length() > 0 && GetServiceLocatedObjectID(request.query.name) < ID_WORLD)
00335         {
00336           ServiceInterface::AddMapString(pose, request.query.name);
00337         }
00338         PutLoIntoResponse(pose, answer);
00339       }
00340       else
00341       {
00342         jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(parentID);
00343         if(parent == NULL || (parent->m_uniqueID == ID_WORLD && pose->m_uniqueID == ID_WORLD))
00344         {
00345           ROS_WARN("Error in Update: Requested parent %ld does not exist!\n", parentID);
00346            answer.error = "Error in Update: Requested parent does not exist!\n";
00347           return true;
00348         }
00349         if((parent->m_uniqueID == ID_WORLD && pose->m_uniqueID == ID_WORLD))
00350         {
00351             answer.error = "Error in Update: Asked for world in world\n";
00352             return true;
00353         }
00354         if(parent->m_uniqueID != pose->m_parentID)
00355         {
00356           ROS_WARN("Updated parent, dangerous in terms of circles (Old parent: %ld New Parent %ld)", pose->m_parentID, parent->m_uniqueID);
00357           pose->UpdateParent(parent);
00358         }
00359         pose->Update(mat, cov, ServiceInterface::FServiceLocatedObjectCopy, ServiceInterface::FreeServiceLocatedObject, &RosLoService::UpdateEventNotifier);
00360         if(request.query.name.length() > 0 && GetServiceLocatedObjectID(request.query.name) < ID_WORLD)
00361         {
00362           if(pose->m_mapstring.compare(request.query.name) != 0)
00363           {
00364             ServiceInterface::RemoveMapString(pose->m_mapstring);
00365           }
00366           ServiceInterface::AddMapString(pose, request.query.name);
00367         }
00368         IncreaseTester(callerid);
00369         PutLoIntoResponse(pose, answer);
00370       }
00371     }
00372     else
00373     {
00374       jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(parentID);
00375       if(parent == NULL)
00376       {
00377         ROS_WARN("Error in Update: Requested parent %ld does not exist!\n", parentID);
00378         answer.error = "Error in Update: Requested parent does not exist!\n";
00379         return true;
00380       }
00381       jlo::ServiceLocatedObject* pose = FServiceLocatedObject(parent, mat, cov, type);
00382       if(request.query.name.length() > 0 && GetServiceLocatedObjectID(request.query.name) < ID_WORLD)
00383       {
00384          ServiceInterface::AddMapString(pose, request.query.name);
00385       }
00386       IncreaseTester(callerid);
00387       PutLoIntoResponse(pose, answer);
00388     }
00389   }
00390   else if (request.command.compare(COUTLO) == 0)
00391   {
00392 
00393     jlo::ServiceLocatedObject* lo = GetServiceLocatedObject(request.query.id);
00394     /*printf("Requested ID: %d => %p\n", (int)request.query.id, lo);*/
00395     if (lo == NULL)
00396     {
00397       ROS_WARN("Error Parsing Input: Id %ld does not exist!\n", request.query.id);
00398       answer.error = "Error Parsing Input: Id does not exist!\n";
00399       return true;
00400     }
00401     IncreaseTester(callerid);
00402     PutLoIntoResponse(lo, answer);
00403     std::string st = lo->SaveComplete()->WriteToString();
00404     cout << st << endl;
00405   }
00406   else
00407   {
00408     printf("Command %s not accepted\n", request.command.c_str());
00409     PRINTF_DEBUG("Error: Requested command does not exist!\n");
00410     answer.error = "Erro: Requested command does not exist!\n";
00411     return true;
00412   }
00413   return true;
00414  }
00415  catch(...)
00416  {
00417   printf("An exception occured during parsing the message!\n");
00418   answer.error = "Error in jlo: Exception happened during execution!\n";
00419   return true;
00420  }
00421 }
00422 
00423 boost::condition_variable cond;boost::mutex mut;
00424 
00425 bool RosLoService::PublishUpdate(unsigned long id, PubParentFilterStruct& obj_to_pub)
00426 {
00427   jlo::ServiceLocatedObject* lo = GetServiceLocatedObject(id);
00428   if(obj_to_pub.parent_id != 0)
00429   {
00430   jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(obj_to_pub.parent_id);
00431   if (parent != NULL)
00432   {
00433     Matrix mat = lo->GetMatrix(*parent);
00434     Matrix cov = lo->GetCovarianceMatrix(*parent);
00435     lo = FServiceLocatedObject(parent, mat, cov, 0);
00436   }
00437   else
00438   {
00439      PRINTF_DEBUG("Error in Framequery: Parent Id does not exist!\n");
00440      lo = NULL;
00441   }
00442   }
00443   if(lo == NULL)
00444   {
00445     return false;
00446   }
00447   else
00448   {
00450     if(obj_to_pub.last_value_set)
00451     {
00452       Matrix pos = lo->GetMatrix(lo->m_relation->m_uniqueID);
00453       ColumnVector rot = irpy(pos);
00454       ColumnVector xyz(3);
00455       xyz << pos.element(0,3)<< pos.element(1,3) << pos.element(2,3);
00456       if(fabs(obj_to_pub.last_val[0] - rot.element(0)) < obj_to_pub.filter[0] &&
00457          fabs(obj_to_pub.last_val[1] - rot.element(1)) < obj_to_pub.filter[1] &&
00458          fabs(obj_to_pub.last_val[2] - rot.element(2)) < obj_to_pub.filter[2] &&
00459          fabs(obj_to_pub.last_val[3] - xyz.element(0)) < obj_to_pub.filter[3] &&
00460          fabs(obj_to_pub.last_val[4] - xyz.element(1)) < obj_to_pub.filter[4] &&
00461          fabs(obj_to_pub.last_val[5] - xyz.element(2)) < obj_to_pub.filter[5])
00462       {
00463         return true;
00464       }
00465       else
00466       {
00467         obj_to_pub.last_val[0] = rot.element(0);
00468         obj_to_pub.last_val[1] = rot.element(1);
00469         obj_to_pub.last_val[2] = rot.element(2);
00470         obj_to_pub.last_val[3] = xyz.element(0);
00471         obj_to_pub.last_val[4] = xyz.element(1);
00472         obj_to_pub.last_val[5] = xyz.element(2);
00473       }
00474     }
00475     else
00476     {
00477       Matrix pos = lo->GetMatrix(lo->m_relation->m_uniqueID);
00478       ColumnVector rot = irpy(pos);
00479       ColumnVector xyz(3);
00480       obj_to_pub.last_val.push_back(rot.element(0));
00481       obj_to_pub.last_val.push_back(rot.element(1));
00482       obj_to_pub.last_val.push_back(rot.element(2));
00483       obj_to_pub.last_val.push_back(xyz.element(0));
00484       obj_to_pub.last_val.push_back(xyz.element(1));
00485       obj_to_pub.last_val.push_back(xyz.element(2));
00486       obj_to_pub.last_value_set = true;
00487     }
00488   }
00489   vision_msgs::partial_lo answer;
00490   PutLoIntoPartialLo(lo, answer, false);
00491 
00492   for(std::vector<ros::Publisher*>::iterator it = obj_to_pub.publisher.begin();
00493        it != obj_to_pub.publisher.end(); it++)
00494   {
00495    (*it)->publish(answer);
00496   }
00497   return true;
00498 }
00499 
00500 void RosLoService::UpdateEventHandler()
00501 {
00502   while(true)
00503   {
00504     boost::unique_lock<boost::mutex> lock(mut);
00505     while(m_queueOfLosToPublish.size() > 0 )
00506     {
00507       unsigned long id = m_queueOfLosToPublish.front();
00508       m_queueOfLosToPublish.pop();
00509       std::map<unsigned long, PubParentFilterStruct>::iterator iter =
00510          m_jlotopicMap.find(id);
00511       if(iter != m_jlotopicMap.end())
00512       {
00513         PubParentFilterStruct& obj_to_pub = (*iter).second;
00514         if(!PublishUpdate(id, obj_to_pub))
00515            m_jlotopicMap.erase(iter);
00516       }
00518       std::map<unsigned long, unsigned long>::iterator iter_parent = m_parentToRegisteredJlo.find(id);
00519       if(iter_parent != m_parentToRegisteredJlo.end())
00520       {
00521         std::map<unsigned long, PubParentFilterStruct>::iterator iter_child =
00522            m_jlotopicMap.find(m_parentToRegisteredJlo[id]);
00523         if(iter_child != m_jlotopicMap.end())
00524         {
00525           PubParentFilterStruct& obj_to_pub = (*iter_child).second;
00526           if(!PublishUpdate(id, obj_to_pub))
00527             m_jlotopicMap.erase(iter_child);
00528 
00529         }
00530         else
00531         {
00532           m_parentToRegisteredJlo.erase(iter_parent);
00533         }
00534       }
00535     }
00536     cond.wait(lock);
00537   }
00538 }
00539 
00540 void RosLoService::UpdateEventNotifier(unsigned long id)
00541 {
00542   boost::lock_guard<boost::mutex> lock(mut);
00543   m_queueOfLosToPublish.push(id);
00544   cond.notify_one();
00545 }
00546 
00547 
00548 bool RosLoService::CallbackRegisterService(vision_srvs::register_jlo_callback::Request& request, vision_srvs::register_jlo_callback::Response&  answer)
00549 {
00550   jlo::ServiceLocatedObject* obj = GetServiceLocatedObject(request.jlo_id);
00551   if(obj != NULL)
00552   {
00553     ros::NodeHandle nh;
00554     std::vector<double> filter(request.filter_delta_cov.size());
00555     for(size_t i = 0; i <  request.filter_delta_cov.size(); i++)
00556       filter[i] = request.filter_delta_cov[i];
00557     if(m_jlotopicMap.find(request.jlo_id) == m_jlotopicMap.end())
00558     {
00559       m_jlotopicMap[request.jlo_id] = RosLoService::PubParentFilterStruct::PubParentFilterStruct(std::vector<ros::Publisher*>(), request.parent_id, filter);
00560       m_parentToRegisteredJlo[request.parent_id] = request.jlo_id;
00561     }
00562     else
00563     {
00564       if(request.parent_id != m_jlotopicMap[request.jlo_id].parent_id)
00565         answer.error = "Error: Object will be puplished with wrong parent";
00566       else
00567         answer.error = "Error: Object will be most probbably filtered incorrect";
00568     }
00569     std::map<std::string, ros::Publisher>::iterator it = m_topicPublisherMap.find(request.topic_name);
00570     if(it == m_topicPublisherMap.end())
00571     {
00572       m_topicPublisherMap[request.topic_name] = (nh.advertise<vision_msgs::partial_lo>(request.topic_name, 20));
00573     }
00574     m_jlotopicMap[request.jlo_id].publisher.push_back(&m_topicPublisherMap[request.topic_name]);
00575   }
00576   else
00577     answer.error = "Error: Object does not exist";
00578   return true;
00579 }
00580 
00581 
00582 void RosLoService::tf_subscription_callback(const tf::tfMessage::ConstPtr &msg_in_)
00583 {
00584   for (unsigned int i = 0; i < msg_in_->transforms.size(); i++)
00585   {
00586     tf::StampedTransform trans;
00587     /*Obsolete: TransformStampedMsgToTF, if you get here an error cause the following function is not defined, pls update tf*/
00588     transformStampedMsgToTF(msg_in_->transforms[i], trans);
00589     if( tf_blacklist.find( trans.child_frame_id_ ) != tf_blacklist.end() )
00590         continue;
00591 
00592 
00593  /*   printf("got matrix:= \n[");
00594     for(int i = 0; i < 16; i++)
00595     {
00596       printf("%f ", matrix[i]);
00597       if(i % 4  == 3)
00598         printf("\n ");
00599     }
00600     printf("]\n");*/
00601     try
00602     {
00603       std::map<std::string, std::string>* msg_header_map = msg_in_->__connection_header.get();
00604       std::string authority;
00605       std::map<std::string, std::string>::iterator it = msg_header_map->find("callerid");
00606       if (it == msg_header_map->end())
00607       {
00608         ROS_WARN("Message recieved without callerid");
00609         authority = "no callerid";
00610       }
00611       else
00612       {
00613         authority = it->second;
00614       }
00615       /*setTransform(trans, authority);*/
00616     }
00617     catch(...)
00618     {
00619        ROS_ERROR("Failure to set recieved transform\n");
00620     }
00621     if(trans.frame_id_.length() == 0)
00622     {
00623       printf("Empty tf\n");
00624       continue;
00625     }
00626     else
00627     {
00628       //printf("Getting tf %s parent %s\n", trans.frame_id_.c_str(), trans.parent_id_.c_str());
00629     }
00630 
00631     //if ((trans.child_frame_id_.find("torso") != -1 ) || (trans.frame_id_.find("torso") != -1 ))
00632     //  ROS_ERROR("Getting tf -%s- parent -%s-\n", trans.child_frame_id_.c_str(), trans.frame_id_.c_str());
00633 
00634     unsigned long id = (long)GetServiceLocatedObjectID(trans.child_frame_id_);
00635     unsigned long parentid = (long)GetServiceLocatedObjectID(trans.frame_id_);
00636     if(parentid < ID_WORLD && trans.frame_id_.compare("/map") != 0)
00637     {
00638       printf("Error reading tf: Requested parent does not exist! (%s in %s)\n", trans.child_frame_id_.c_str(), trans.frame_id_.c_str());
00639       continue;
00640       /*jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(parentid);
00641       parent->m_mapstring = trans.parent_id_;*/
00642     }
00643     if(parentid < ID_WORLD)
00644     {
00645       parentid = ID_WORLD;
00646       printf("Setting name of world to %s\n", trans.frame_id_.c_str());
00647       jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(parentid);
00648       ServiceInterface::AddMapString(parent, trans.frame_id_);
00649     }
00650     jlo::ServiceLocatedObject* pose = NULL;
00651 
00652     btScalar matrix[16];
00653     trans.getOpenGLMatrix(matrix);
00654 
00655     Matrix mat(4,4);
00656     mat << matrix[0] << matrix[4]<< matrix[8]<< matrix[12]
00657         << matrix[1]<< matrix[5]<< matrix[9]<< matrix[13]
00658         << matrix[2]<< matrix[6]<< matrix[10]<< matrix[14]
00659         << matrix[3]<< matrix[7]<< matrix[11]<< matrix[15];
00660 
00661     Matrix cov(6,6);
00662 
00663     cov << 0 << 0 << 0 << 0 << 0 << 0
00664         << 0 << 0 << 0 << 0 << 0 << 0
00665         << 0 << 0 << 0 << 0 << 0 << 0
00666         << 0 << 0 << 0 << 0 << 0 << 0
00667         << 0 << 0 << 0 << 0 << 0 << 0
00668         << 0 << 0 << 0 << 0 << 0 << 0;
00669 
00670     int type = LO_TYPE_PHYSICAL;
00671 
00672     if(id < ID_WORLD)
00673     {
00674       jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(parentid);
00675       if(parent == NULL)
00676       {
00677           printf("Error tf: Requested parent does not exist! (%s -> %ld)\n", trans.child_frame_id_.c_str(),  parentid);
00678           continue;
00679       }
00680       pose = FServiceLocatedObject(parent, mat, cov, type);
00681       ServiceInterface::AddMapString(pose, trans.child_frame_id_);
00682     }
00683     else
00684     {
00685       pose = GetServiceLocatedObject(id);
00686       if (pose == NULL)
00687       {
00688         ROS_ERROR("Error tf: Reject overwritten frame %s -> %ld\n", trans.child_frame_id_.c_str(), id);
00689         continue;
00690       }
00691 
00692       if(parentid == pose->m_parentID)
00693       {
00694           Matrix m = pose->GetMatrix();
00695           Matrix diff =  mat - m;
00696           if((fabs(diff.element(0,0)) < 0.000001 &&
00697               fabs(diff.element(0,1)) < 0.000001 &&
00698               fabs(diff.element(0,2)) < 0.000001 &&
00699               fabs(diff.element(0,3)) < 0.000001 &&
00700               fabs(diff.element(1,0)) < 0.000001 &&
00701               fabs(diff.element(1,1)) < 0.000001 &&
00702               fabs(diff.element(1,2)) < 0.000001 &&
00703               fabs(diff.element(1,3)) < 0.000001 &&
00704               fabs(diff.element(2,0)) < 0.000001 &&
00705               fabs(diff.element(2,1)) < 0.000001 &&
00706               fabs(diff.element(2,2)) < 0.000001 &&
00707               fabs(diff.element(2,3)) < 0.000001) || pose->m_uniqueID == ID_WORLD)
00708           {
00709             continue;
00710           }
00711           pose->Update(mat, cov, ServiceInterface::FServiceLocatedObjectCopy, ServiceInterface::FreeServiceLocatedObject, &RosLoService::UpdateEventNotifier);
00712 
00713           //if  ((trans.child_frame_id_.find("torso") != -1 ) || (trans.frame_id_.find("torso") != -1 ))
00714           //   ROS_ERROR("Getting tf -%s- parent -%s-\n", trans.child_frame_id_.c_str(), trans.frame_id_.c_str());
00715       }
00716       else
00717       {
00718           printf("tf from  %s with parent %s does not correspond with the actual situation %ld with parentID %ld\n", trans.child_frame_id_.c_str(), trans.frame_id_.c_str(), pose->m_uniqueID, pose->m_parentID);
00719           pose->m_mapstring = "";
00720           jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(parentid);
00721           if(parent == NULL || parent->m_uniqueID != parentid)
00722           {
00723               printf("Error tf: Requested parent does not exist! (%s -> %ld)\n", trans.frame_id_.c_str(), parentid);
00724               continue;
00725           }
00726           pose = FServiceLocatedObject(parent, mat, cov, type);
00727           ServiceInterface::AddMapString(pose, trans.child_frame_id_);
00728           pose->IncreaseReferenceCounter();
00729       }
00730     }
00731 
00732     /*catch (TransformException& ex)
00733     {
00735       std::string temp = ex.what();
00736       ROS_ERROR("Failure to set recieved transform %s to %s with error: %s\n", msg_in_.transforms[i].header.frame_id.c_str(), msg_in_.transforms[i].parent_id.c_st
00737 r(), temp.c_str());
00738     }*/
00739   }
00740 
00741 
00742 
00743 };
00744 
00745 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


jlo
Author(s): Ulrich F Klank
autogenerated on Thu May 23 2013 10:38:09