icub-client
opcSensation.cpp
Go to the documentation of this file.
1 #include "opcSensation.h"
4 
5 using namespace icubclient;
6 using namespace yarp::os;
7 using namespace std;
8 
10 {
11  bool isRFVerbose = false;
12  iCub = new ICubClient("opcSensation","sensation","client.ini",isRFVerbose);
13  iCub->opc->isVerbose = false;
14  while (!iCub->connect())
15  {
16  cout<<"iCubClient : Some dependencies are not running..."<<endl;
17  Time::delay(1.0);
18  }
19 
20  homeoPort.open("/opcSensation/toHomeo:o");
21  unknown_entities_port.open("/opcSensation/unknown_entities:o");
22  known_entities_port.open( "/opcSensation/known_entities:o");
23 
24  yInfo() << "Configuration done.";
25 
26 }
27 
29 {
30  Bottle res = handleEntities();
31 
32  if (!Network::isConnected("/opcSensation/toHomeo:o", "/homeostasis/fromSensations:i"))
33  Network::connect("/opcSensation/toHomeo:o", "/homeostasis/fromSensations:i");
34  yarp::os::Bottle &bot = homeoPort.prepare();
35  bot.clear();
36  yarp::os::Bottle key;
37  key.clear();
38  key.addString("unknown");
39  key.addInt(res.get(0).asInt());
40  bot.addList()=key;
41  key.clear();
42  key.addString("known");
43  key.addInt(res.get(2).asInt());
44  bot.addList()=key;
45  homeoPort.write();
46 
47 
48  yarp::os::Bottle &unkn = unknown_entities_port.prepare();
49  unkn.clear();
50  unkn.append(*res.get(1).asList());
51  unknown_entities_port.write();
52 
53  yarp::os::Bottle &kn = known_entities_port.prepare();
54  kn.clear();
55  kn.append(*res.get(3).asList());
56  known_entities_port.write();
57 }
58 
59 void OpcSensation::addToEntityList(Bottle& list, string type, string name) {
60  Bottle item;
61  item.addString(type);
62  item.addString(name);
63  list.addList() = item;
64 }
65 
66 Bottle OpcSensation::handleEntities()
67 {
68  LockGuard lg(m_entity_bottles);
69  iCub->opc->checkout();
70  list<Entity*> lEntities = iCub->opc->EntitiesCache();
71 
72  k_entities.clear();
73  kp_entities.clear();
74  u_entities.clear();
75  up_entities.clear();
76  o_positions.clear();
77  p_entities.clear();
78 
79  for (auto& entity : lEntities)
80  {
81  if(entity->entity_type() == "object") {
82  Object* o = dynamic_cast<Object*>(entity);
83  if(o) {
84  addToEntityList(o_positions, o->objectAreaAsString(), entity->name());
85  }
86  }
87 
88  if (entity->name().find("unknown") == 0) {
89  if (entity->entity_type() == "object")
90  {
91  Object* o = dynamic_cast<Object*>(entity);
92  if(o && (o->m_present==1.0)) {
93  addToEntityList(up_entities, entity->entity_type(), entity->name());
94  }
95  addToEntityList(u_entities, entity->entity_type(), entity->name());
96  }
97  else if(entity->entity_type() == "bodypart") {
98  addToEntityList(u_entities, entity->entity_type(), entity->name());
99  addToEntityList(up_entities, entity->entity_type(), entity->name());
100  }
101  }
102  else if (entity->entity_type() == "object")
103  {
104  Object* o = dynamic_cast<Object*>(entity);
105  if(o && (o->m_present==1.0)) {
106  addToEntityList(kp_entities, entity->entity_type(), entity->name());
107  }
108  addToEntityList(k_entities, entity->entity_type(), entity->name());
109  }
110  else if (entity->name() == "partner" && entity->entity_type() == "agent") {
111  Agent* a = dynamic_cast<Agent*>(entity);
112  if(a && (a->m_present==1.0)) {
113  addToEntityList(up_entities, entity->entity_type(), entity->name());
114  }
115  addToEntityList(u_entities, entity->entity_type(), entity->name());
116  }
117  else {
118  if (entity->entity_type() == "bodypart")
119  {
120  Bodypart* bp = dynamic_cast<Bodypart*>(entity);
121  if(bp) {
122  if(bp->m_tactile_number == -1) {
123  addToEntityList(u_entities, entity->entity_type(), entity->name());
124  addToEntityList(up_entities, entity->entity_type(), entity->name());
125  }
126  }
127  }
128  }
129  Object * o = dynamic_cast<Object*>(entity);
130  if (o && o->m_present == 1.0)
131  {
132  addToEntityList(p_entities, entity->entity_type(), entity->name());
133  }
134  }
135 
136  Bottle out;
137  out.addInt(up_entities.size());
138  out.addList()=up_entities;
139  out.addInt(kp_entities.size());
140  out.addList()=kp_entities;
141 
142  return out;
143 }
144 
145 int OpcSensation::get_property(string name,string property)
146 {
147  LockGuard lg(m_entity_bottles);
148  Bottle b;
149  bool check_position=false;
150 
151  if (property == "known")
152  {
153  b = k_entities;
154  }
155  else if (property == "unknown")
156  {
157  b = u_entities;
158  }
159  else if (property == "present")
160  {
161  b = p_entities;
162  }
163  else
164  {
165  b = o_positions;
166  check_position=true;
167  }
168  if (check_position)
169  {
170  yDebug()<<"Checking object position"<<name<<property;
171  for (unsigned int i=0;i<b.size();i++)
172  {
173  if (name == "any"){
174  if (b.get(i).asList()->get(0).asString()==property)
175  {
176  yDebug() << "check_position, name==any, return 1";
177  return 1;
178  }
179  }else{
180  if (b.get(i).asList()->get(1).asString()==name && b.get(i).asList()->get(0).asString()==property)
181  {
182  yDebug() << "check_position, name!=any, return 1 " << b.toString();
183  return 1;
184  }
185  }
186  }
187  yDebug() << "check_position, return 0";
188  return 0;
189  }else{
190  if (name == "any"){
191  if (b.size()!=0){
192  yDebug() << "not check_position, name==any, return 1";
193  return 1;
194  }else{
195  yDebug() << "not check_position, name==any, return 0";
196  return 0;
197  }
198  }else{
199  for (unsigned int i=0;i<b.size();i++)
200  {
201  if (b.get(i).asList()->get(1).asString()==name)
202  {
203  yDebug() << "not check_position, name!=any, return 1";
204  return 1;
205  }
206  }
207  yDebug() << "not check_position, name!=any, return 0";
208  return 0;
209  }
210  }
211 }
212 
214  unknown_entities_port.interrupt();
215  unknown_entities_port.close();
216  homeoPort.interrupt();
217  homeoPort.close();
218  known_entities_port.interrupt();
219  known_entities_port.close();
220  iCub->close();
221  delete iCub;
222 }
std::string objectAreaAsString() const
Definition: object.h:106
int get_property(std::string name, std::string property)
get_property quickly answers a boolean query
Grants access to high level motor commands (grasp, touch, look, goto, etc) of the robot as well as it...
Definition: icubClient.h:66
double m_present
Is the object present in the scene A value of 1.0 means that the object currently is in the scene A v...
Definition: object.h:69
int m_tactile_number
Tactile number of the represented body part.
Definition: bodypart.h:43
STL namespace.
Represent any physical entity (including objects and agents) that can be stored within the OPC...
Definition: object.h:35
Represent an agent.
Definition: agent.h:93
void publish()
send sensation data to port
void configure()
Definition: opcSensation.cpp:9
Represents a body part of the robot.
Definition: bodypart.h:29
void close_ports()