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
00020
00021
00022
00023
00024
00025 #include "AttentionManager.h"
00026 #include "AttentionAlgorithm.h"
00027 #include "ImageInputSystem.h"
00028 #include "SignatureDB.h"
00029 #include "XMLTag.h"
00030 #include "BoostUtils.h"
00031 #ifdef BOOST_THREAD
00032 #include "boost/bind.hpp"
00033 #define BOOST(A) A
00034 #else
00035 #define BOOST(A)
00036 #endif
00037
00038 #define XML_PROPERTY_ATTENDING "IsAttending"
00039 #define XML_NODE_ATTENTIONALGORITHMS "Attendants"
00040 #define XML_NODE_ATTENTEDOBJ "AttentedObj"
00041
00042
00043 using namespace cop;
00044 extern volatile bool g_stopall;
00045
00046
00047
00048 AttentionManager::AttentionManager ( XMLTag* config , SignatureDB& sig_db, ImageInputSystem& imginsys
00049 #ifdef LOGFILE
00050 , LogFile& log
00051 #endif
00052 ) :
00053 m_imginsys(imginsys),
00054 #ifdef LOGFILE
00055 m_attendants(config != NULL ? config->GetChild(XML_NODE_ATTENTIONALGORITHMS) : NULL, log),
00056 m_logFile(log),
00057 #else
00058 m_attendants(config != NULL ? config->GetChild(XML_NODE_ATTENTIONALGORITHMS) : NULL),
00059 #endif
00060 m_sigDB(sig_db)
00061 {
00062 m_Attending = true;
00063 if(config != NULL)
00064 {
00065 XMLTag* objs = config->GetChild(XML_NODE_ATTENTEDOBJ);
00066 if(objs != NULL)
00067 {
00068 for(unsigned int i = 0; i < objs->CountChildren(); i++)
00069 {
00070 try
00071 {
00072 XMLTag* tag2 = objs->GetChild(i);
00073 if(tag2->GetName().compare(XML_NODE_SIGNATURE) == 0)
00074 {
00075 Signature* sig = (Signature*)Elem::ElemFactory(tag2);
00076 PerceptionPrimitive* p = new PerceptionPrimitive(sig);
00077 SetObjectToAttend(p, NULL, NULL);
00078 }
00079 else
00080 {
00081 ROS_WARN("Trying to instantiate a broken XMLNode as Signature: %s\n", tag2->GetName().c_str());
00082 }
00083 }
00084 catch(const char* text)
00085 {
00086 ROS_ERROR("Error loading object to attend in AttentionManager: %s\n", text);
00087 }
00088
00089 }
00090 }
00091 }
00092
00093
00094 m_learningThread = new boost::thread( boost::bind(&AttentionManager::threadfunc, this) ) ;
00095
00096 m_attendants.SetName(XML_NODE_ATTENTIONMANAGER);
00097 }
00098
00099 AttentionManager::~AttentionManager ( )
00100 {
00101 m_Attending = false;
00102
00103 if(m_learningThread != NULL)
00104 m_learningThread->join();
00105 delete m_learningThread;
00106
00107 for(std::vector<AttendedObjects>::iterator it = m_attendedObjectPrototypes.begin();
00108 it != m_attendedObjectPrototypes.end(); )
00109 {
00110 it = m_attendedObjectPrototypes.erase(it);
00111 }
00112 }
00113
00114
00115
00116
00117
00118
00119 void AttentionManager::SetObjectToAttend (PerceptionPrimitive* prototype, PossibleLocations_t* pointOfInterest, Comm* comm)
00120 {
00121 AttendedObjects obj(prototype, pointOfInterest, comm);
00122 ROS_DEBUG("Set an object to Attent\n");
00123 m_attendedObjectPrototypes.push_back(obj);
00124 }
00125
00126 void AttentionManager::StopAttend(Comm* comm)
00127 {
00128 unsigned long actionToStop = comm->GetCommID();
00129 for(std::vector<AttendedObjects>::iterator it = m_attendedObjectPrototypes.begin();
00130 it != m_attendedObjectPrototypes.end(); it++)
00131 {
00132 if((*it).comm != NULL && (*it).comm->GetCommID() == actionToStop)
00133 {
00134 m_attendedObjectPrototypes.erase(it);
00135 break;
00136 }
00137 }
00138 }
00139
00140 void AttentionManager::PerformAttentionAlg( std::vector<Sensor*> &sensors, RelPose* pose, Signature* sig, AttendedObjects& objProto)
00141 {
00142 AttentionAlgorithm* refalg = (AttentionAlgorithm*)m_attendants.BestAlgorithm(4096, *sig, sensors);
00143 int numOfObjects = 0;
00144 double qualityMeasure = 0.0;
00145 if(refalg != NULL)
00146 {
00147 printf("Alg selected: %s\n", refalg->GetName().c_str());
00148 try
00149 {
00150
00151
00152 std::vector<Signature*> results = refalg->Perform(sensors, pose, *sig, numOfObjects, qualityMeasure);
00153 for(size_t j = 0; j < results.size(); j++)
00154 {
00155 if(objProto.comm != NULL && results[j]->GetObjectPose() != NULL)
00156 {
00157 objProto.comm->NotifyNewObject(results[j], results[j]->GetObjectPose());
00158 }
00159 printf("Adding new object: Signature with ID %ld\n", results[j]->m_ID);
00160 m_sigDB.AddSignature(results[j]);
00161 }
00162 }
00163 catch(char const* error_text)
00164 {
00165 ROS_WARN("Error in Attention System: %s\n", error_text);
00166
00167 }
00168 }
00169 }
00170
00171 void AttentionManager::threadfunc()
00172 {
00173 sleep(3);
00174 while(!g_stopall)
00175 {
00176 for(size_t i = 0 ; i < m_attendedObjectPrototypes .size(); i++)
00177 {
00178 Signature* sig = m_attendedObjectPrototypes[i].proto->GetSignature();
00179 PossibleLocations_t* area = m_attendedObjectPrototypes[i].poses;
00180 std::vector<Sensor*> sensors;
00181
00182 if(area != NULL && area->size() != 0)
00183 {
00184 PossibleLocations_t::const_iterator iter = area->begin();
00185 for( ;iter != area->end(); iter++)
00186 {
00187 RelPose* pose = (*iter).first;
00188 sensors = m_imginsys.GetBestSensor(*pose);
00189 PerformAttentionAlg(sensors, pose, sig, m_attendedObjectPrototypes[i]);
00190
00191 }
00192 }
00193 else
00194 {
00195 sensors = m_imginsys.GetAllSensors();
00196 PerformAttentionAlg(sensors, NULL, sig, m_attendedObjectPrototypes[i]);
00197 }
00198 }
00199 Sleeping(10);
00200 }
00201 }
00202
00203
00204
00205 XMLTag* AttentionManager::Save()
00206 {
00207 XMLTag* tag = new XMLTag(XML_NODE_ATTENTIONMANAGER);
00208 tag->AddChild(m_attendants.Save(XML_NODE_ATTENTIONALGORITHMS));
00209 XMLTag* objs = new XMLTag(XML_NODE_ATTENTEDOBJ);
00210
00211 for(size_t obj = 0; obj< m_attendedObjectPrototypes.size(); obj++)
00212 {
00213 XMLTag* sig = m_attendedObjectPrototypes[obj].proto->GetSignature(0)->Save();
00214 objs->AddChild(sig);
00215 }
00216 tag->AddChild(objs);
00217 return tag;
00218 }
00219
00220
00221 #ifndef WIN32
00222 #include "AlgorithmSelector.hpp"
00223 template class AlgorithmSelector<std::vector<Signature*> >;
00224 #else
00225 #include "AlgorithmSelector.hpp"
00226 template AlgorithmSelector<std::vector<Signature*> >;
00227 #endif
00228
00229
00230