Go to the documentation of this file.00001 #ifndef REMOTEATTENTION_H
00002 #define REMOTEATTENTION_H
00003
00004
00005 #include <ros/ros.h>
00006 #include "XMLTag.h"
00007 #include "AttentionAlgorithm.h"
00008
00009 #define XML_NODE_REMOTEATTENTION "RemoteAttention"
00010
00011 extern volatile bool g_stopall;
00012
00013 namespace cop
00014 {
00019 template <typename Message>
00020 class RemoteAttention : public AttentionAlgorithm
00021 {
00022 public:
00023
00027 RemoteAttention () : m_bStarted(false), m_neededObjectType(ELEM){};
00028
00032 virtual ~RemoteAttention ( ){};
00033
00034 typedef boost::mutex::scoped_lock lock;
00035
00046 virtual std::vector<Signature*> Perform(std::vector<Sensor*> sensors, RelPose* pose,
00047 Signature& prototype, int &numOfObjects, double& qualityMeasure)
00048 {
00049 boost::xtime t;
00050 if(!m_bStarted && !g_stopall)
00051 Start();
00052 lock lk(m_mutexNewData);
00053 ROS_INFO("Waiting for new data\n");
00054
00055 while(!m_bNew && !g_stopall)
00056 {
00057 try
00058 {
00059 boost::xtime_get(&t, boost::TIME_UTC);
00060 t.sec += 1;
00061 m_newDataArrived.timed_wait(lk, t);
00062 }
00063 catch(...)
00064 {
00065 ROS_ERROR("Error: condition variable error in RemoteAttention\n");
00066 }
00067 }
00068 ROS_INFO("Got %snew data\n", m_bNew ? "" : "no ");
00069 m_bNew = false;
00070 if(!g_stopall)
00071 {
00072 std::vector<Signature*> copy = m_latestResult;
00073 m_latestResult.clear();
00074 numOfObjects = copy.size();
00075 printf("returning %d objects\n", numOfObjects);
00076 qualityMeasure = 0.5;
00077 return copy;
00078 }
00079
00080 return std::vector<Signature*>();
00081 }
00082
00083
00088 virtual double CheckSignature(const Signature& object, const std::vector<Sensor*> &sensors)
00089 {
00090 lock lk(m_mutexNewData);
00091
00092
00093 if(!m_bStarted || m_bNew)
00094 {
00095 if(m_neededObjectType == ELEM || object.GetElement(0, m_neededObjectType) != NULL)
00096 {
00097 return 1.00;
00098 }
00099 else
00100 return 0.0;
00101 }
00102 return 0.0;
00103 }
00104
00105 virtual XMLTag* Save()
00106 {
00107 XMLTag* tag = new XMLTag(XML_NODE_REMOTEATTENTION);
00108 SetData(tag);
00109 return tag;
00110 }
00111
00112 void SaveRemoteProps(XMLTag* tag){tag->AddProperty(XML_ATTRIBUTE_TOPICNAME, m_stTopic);}
00113
00114 void Start()
00115 {
00116 m_bStarted = true;
00117 try
00118 {
00119 ros::NodeHandle node;
00120 printf("Subscribe to %s\n", m_stTopic.c_str());
00121 m_subs = node.subscribe<Message>(
00122 m_stTopic, 1,
00123 boost::bind(&RemoteAttention::CallbackToTopic, this, _1));
00124 }
00125 catch(...)
00126 {
00127 ROS_ERROR("cop::RemoteAttention: Could not subscribe to %s\n", m_stTopic.c_str());
00128 }
00129 }
00130
00131 virtual void Stop()
00132 {
00133 m_subs.shutdown();
00134 }
00135
00136 virtual void SetData(XMLTag* tag)
00137 {
00138 m_stTopic = tag->GetProperty(XML_ATTRIBUTE_TOPICNAME, "/cop/attention");
00139 }
00140
00141 void CallbackToTopic(boost::shared_ptr<Message const> msg)
00142 {
00143 printf("Got something\n");
00144 std::vector<Signature*> new_sigs = MessageToSignature(msg);
00145 m_latestResult.insert(m_latestResult.end(), new_sigs.begin(), new_sigs.end());
00146 lock lk(m_mutexNewData);
00147 m_bNew = true;
00148 m_newDataArrived.notify_all();
00149 }
00150 virtual std::vector<Signature*> MessageToSignature(boost::shared_ptr<Message const> msg)
00151 {
00152 throw "Not implemented";
00153 }
00154
00155 void SetObjectType(ElemType_t neededObjectType) {m_neededObjectType = neededObjectType;}
00156
00157 std::string m_stTopic;
00158 std::vector<Signature*> m_latestResult;
00159 bool m_bStarted;
00160 bool m_bNew;
00161 ros::Subscriber m_subs;
00162 boost::condition m_newDataArrived;
00163 boost::mutex m_mutexNewData;
00164 ElemType_t m_neededObjectType;
00165
00166 };
00167 }
00168 #endif