icub-client
opcPopulator.cpp
Go to the documentation of this file.
1 #include <functional>
2 #include "opcPopulator.h"
5 
6 using namespace std;
7 using namespace yarp::os;
8 using namespace icubclient;
9 
10 bool opcPopulator::configure(yarp::os::ResourceFinder &rf)
11 {
12  string moduleName = rf.check("name", Value("opcPopulator")).asString().c_str();
13  setName(moduleName.c_str());
14 
15  yInfo() << moduleName << " : finding configuration files...";
16  period = rf.check("period", Value(0.1)).asDouble();
17 
18  //Create an iCub Client and check that all dependencies are here before starting
19  bool isRFVerbose = false;
20  iCub = new ICubClient(moduleName, "opcPopulator", "client.ini", isRFVerbose);
21  iCub->opc->isVerbose = false;
22  if (!iCub->connect())
23  {
24  yInfo() << " iCubClient : Some dependencies are not running...";
25  Time::delay(1.0);
26  }
27  rpc.open(("/" + moduleName + "/rpc").c_str());
28  attach(rpc);
29 
30  Bottle &bRFInfo = rf.findGroup("populateSpecific");
31 
32  X_obj = bRFInfo.check("X_obj", Value(-0.4)).asDouble();
33  Y_obj = bRFInfo.check("Y_obj", Value(0.5)).asDouble();
34  Z_obj = bRFInfo.check("Z_obj", Value(0.1)).asDouble();
35  X_ag = bRFInfo.check("X_ag", Value(-0.4)).asDouble();
36  Y_ag = bRFInfo.check("Y_ag", Value(0.5)).asDouble();
37  Z_ag = bRFInfo.check("Z_ag", Value(0.5)).asDouble();
38  noise = bRFInfo.check("noise", Value(5.1)).asDouble();
39 
40  yInfo() << "X_obj " << X_obj;
41  yInfo() << "Y_obj " << Y_obj;
42  yInfo() << "Z_obj " << Z_obj;
43  yInfo() << "X_ag " << X_ag;
44  yInfo() << "Y_ag " << Y_ag;
45  yInfo() << "Z_ag " << Z_ag;
46  yInfo() << "noise " << noise;
47 
48  iCub->home();
49 
50  yInfo() << "\n \n" << "----------------------------------------------" << "\n \n" << moduleName << " ready ! \n \n ";
51 
52 
53  return true;
54 }
55 
56 
58  iCub->close();
59  delete iCub;
60 
61  return true;
62 }
63 
64 
65 bool opcPopulator::respond(const Bottle& command, Bottle& reply) {
66  string helpMessage = string(getName().c_str()) +
67  " commands are: \n" +
68  "help \n" +
69  "quit \n" +
70  "populateEntityRandom entity_type entity_name \n" +
71  "populateSpecific \n" +
72  "populateTwoUnknowns \n" +
73  "populateRedBall \n" +
74  "addUnknownEntity entity_type\n" +
75  "setSaliencyEntity entity_name saliency_value\n" +
76  "setValueEntity entity_name value_value\n";
77 
78  reply.clear();
79 
80 
81  if (command.get(0).asString() == "quit") {
82  reply.addString("quitting");
83  return false;
84  }
85  else if (command.get(0).asString() == "populateRedBall") {
86  yInfo() << " populateRedBall";
87  (populateRedBall()) ? reply.addString("populateRedBall done !") : reply.addString("populateRedBall failed !");
88  }
89  else if (command.get(0).asString() == "populateEntityRandom") {
90  yInfo() << " populateEntityRandom";
91  (populateEntityRandom(command)) ? reply.addString("populateSpecific done !") : reply.addString("populateSpecific failed !");
92  }
93  else if (command.get(0).asString() == "populateSpecific") {
94  yInfo() << " populateSpecific";
95  (populateSpecific()) ? reply.addString("populateSpecific done !") : reply.addString("populateSpecific failed !");
96  }
97  else if (command.get(0).asString() == "populateTwoUnknowns") {
98  yInfo() << " populateTwoUnknowns";
99  (populateTwoUnknowns()) ? reply.addString("populated 2 objects !") : reply.addString("populateSpecific failed !");
100  }
101  else if (command.get(0).asString() == "addUnknownEntity") {
102  yInfo() << " addUnknownEntity";
103  (addUnknownEntity(command)) ? reply.addString("addUnknownEntity done !") : reply.addString("addUnknownEntity failed !");
104  }
105  else if (command.get(0).asString() == "setSaliencyEntity") {
106  yInfo() << " setSaliencyEntity";
107  (setAttributeEntity(command, std::mem_fn(&Object::setSaliency))) ? reply.addString("setSaliencyEntity done !") : reply.addString("setSaliencyEntity failed !");
108  }
109  else if (command.get(0).asString() == "setValueEntity") {
110  yInfo() << " setValueEntity";
111  (setAttributeEntity(command, std::mem_fn(&Object::setValue))) ? reply.addString("setValueEntity done !") : reply.addString("setValueEntity failed !");
112  }
113  else if (command.get(0).asString() == "clear") {
114  yInfo() << " clearing OPC";
115  iCub->opc->clear();
116  iCub->opc->update();
117  reply.addString("clearing OPC");
118  }
119  else {
120  yInfo() << helpMessage;
121  reply.addString("wrong command");
122  reply.addString(helpMessage);
123  }
124 
125  return true;
126 }
127 
128 /* Called periodically every getPeriod() seconds */
130  return true;
131 }
132 
133 bool opcPopulator::populateEntityRandom(const Bottle& bInput){
134 
135  if (bInput.size() != 3)
136  {
137  yWarning() << " in opcPopulator::populateEntityRandom | wrong number of input";
138  return false;
139  }
140  string sName = bInput.get(2).toString();
141 
142  if (bInput.get(1).toString() == ICUBCLIENT_OPC_ENTITY_AGENT)
143  {
144  Agent* agent = iCub->opc->addOrRetrieveEntity<Agent>(sName);
145  agent->m_ego_position[0] = (-1.5) * (Random::uniform()) - 0.5;
146  agent->m_ego_position[1] = (2) * (Random::uniform()) - 1;
147  agent->m_ego_position[2] = 0.60;
148  agent->m_present = 1.0;
149  agent->m_color[0] = Random::uniform(0, 80);
150  agent->m_color[1] = Random::uniform(180, 250);
151  agent->m_color[2] = Random::uniform(80, 180);
152  iCub->opc->commit(agent);
153  }
154  else if (bInput.get(1).toString() == ICUBCLIENT_OPC_ENTITY_OBJECT)
155  {
156  Object* obj = iCub->opc->addOrRetrieveEntity<Object>(sName);
157  obj->m_ego_position[0] = (-1.5) * (Random::uniform()) - 0.2;
158  obj->m_ego_position[1] = (2) * (Random::uniform()) - 1;
159  obj->m_ego_position[2] = 0.20;
160  obj->m_present = 1.0;
161  obj->m_color[0] = Random::uniform(100, 180);
162  obj->m_color[1] = Random::uniform(0, 80);
163  obj->m_color[2] = Random::uniform(180, 250);
164  obj->m_value = 1.5;
165  yDebug() << "value: " << obj->m_value;
166  iCub->opc->commit(obj);
167  }
168  else
169  {
170  yError() << "Entity type not supported";
171  return false;
172  }
173 
174  return true;
175 }
176 
177 
178 bool opcPopulator::addUnknownEntity(const Bottle &bInput){
179 
180  if (bInput.size() != 2)
181  {
182  yWarning() << " in opcPopulator::addUnknownEntity | wrong number of input";
183  return false;
184  }
185 
186  iCub->opc->checkout();
187  string sName = "unknown";
188  yInfo() << " to be added: " << bInput.get(1).toString() << " called " << sName;
189 
190  if (bInput.get(1).toString() == ICUBCLIENT_OPC_ENTITY_AGENT)
191  {
192  sName = "partner";
193  Agent* agent = iCub->opc->addEntity<Agent>(sName);
194  agent->m_ego_position[0] = (-1.5) * (Random::uniform()) - 0.5;
195  agent->m_ego_position[1] = (2) * (Random::uniform()) - 1;
196  agent->m_ego_position[2] = 0.60;
197  agent->m_present = 1.0;
198  agent->m_color[0] = Random::uniform(0, 80);
199  agent->m_color[1] = Random::uniform(180, 250);
200  agent->m_color[2] = Random::uniform(80, 180);
201  iCub->opc->commit(agent);
202  }
203  else if (bInput.get(1).toString() == ICUBCLIENT_OPC_ENTITY_OBJECT)
204  {
205  Object* obj = iCub->opc->addEntity<Object>(sName);
206  obj->m_ego_position[0] = (-1.5) * (Random::uniform()) - 0.2;
207  obj->m_ego_position[1] = (2) * (Random::uniform()) - 1;
208  obj->m_ego_position[2] = 0.20;
209  obj->m_present = 1.0;
210  obj->m_color[0] = Random::uniform(100, 180);
211  obj->m_color[1] = Random::uniform(0, 80);
212  obj->m_color[2] = Random::uniform(180, 250);
213  iCub->opc->commit(obj);
214  }
215  else
216  {
217  yError() << bInput.get(1).toString() << " unknown kind of entity";
218  return false;
219  }
220 
221  return true;
222 }
223 
224 bool opcPopulator::setAttributeEntity(const Bottle& bInput, std::function<void(Object*, double)> f_setter){
225 
226  if (bInput.size() != 3) {
227  yWarning() << " in opcPopulator::setAttributeEntity| wrong number of input";
228  return false;
229  }
230 
231  string sName = bInput.get(1).toString();
232  double target = bInput.get(2).asDouble();
233 
234  iCub->opc->checkout();
235 
236  Entity *e = iCub->opc->getEntity(sName);
238  f_setter(dynamic_cast<Object*>(e), target);
239  iCub->opc->commit();
240  } else{
241  yError() << "Cannot change value of the non-object entity: " << sName;
242  return false;
243  }
244 
245  return true;
246 }
247 
248 
250  iCub->opc->clear();
251 
252  double errorMargin = noise;
253 
254  Object* obj1 = iCub->opc->addOrRetrieveEntity<Object>("bottom_left");
255  obj1->m_ego_position[0] = X_obj + errorMargin * (Random::uniform() - 0.5);
256  obj1->m_ego_position[1] = -1.* Y_obj + errorMargin * (Random::uniform() - 0.5);
257  obj1->m_ego_position[2] = Z_obj + errorMargin * (Random::uniform() - 0.5);
258  obj1->m_present = 1.0;
259  obj1->m_color[0] = Random::uniform(0, 80);
260  obj1->m_color[1] = Random::uniform(80, 180);
261  obj1->m_color[2] = Random::uniform(180, 250);
262  iCub->opc->commit(obj1);
263 
264  Object* obj2 = iCub->opc->addOrRetrieveEntity<Object>("top_left");
265  obj2->m_ego_position[0] = X_ag + errorMargin * (Random::uniform() - 0.5);
266  obj2->m_ego_position[1] = -1.* Y_ag + errorMargin * (Random::uniform() - 0.5);
267  obj2->m_ego_position[2] = Z_ag + errorMargin * (Random::uniform() - 0.5);
268  obj2->m_present = 1.0;
269  obj2->m_color[0] = Random::uniform(0, 180);
270  obj2->m_color[1] = Random::uniform(0, 80);
271  obj2->m_color[2] = Random::uniform(180, 250);
272  iCub->opc->commit(obj2);
273 
274  Object* obj3 = iCub->opc->addOrRetrieveEntity<Object>("top_right");
275  obj3->m_ego_position[0] = X_ag + errorMargin * (Random::uniform() - 0.5);
276  obj3->m_ego_position[1] = Y_ag + errorMargin * (Random::uniform() - 0.5);
277  obj3->m_ego_position[2] = Z_ag + errorMargin * (Random::uniform() - 0.5);
278  obj3->m_present = 1.0;
279  obj3->m_color[0] = Random::uniform(100, 180);
280  obj3->m_color[1] = Random::uniform(80, 180);
281  obj3->m_color[2] = Random::uniform(0, 80);
282  iCub->opc->commit(obj3);
283 
284  Object* obj4 = iCub->opc->addOrRetrieveEntity<Object>("bottom_right");
285  obj4->m_ego_position[0] = X_obj + errorMargin * (Random::uniform() - 0.5);
286  obj4->m_ego_position[1] = Y_obj + errorMargin * (Random::uniform() - 0.5);
287  obj4->m_ego_position[2] = Z_obj + errorMargin * (Random::uniform() - 0.5);
288  obj4->m_present = 1.0;
289  obj4->m_color[0] = Random::uniform(100, 180);
290  obj4->m_color[1] = Random::uniform(0, 80);
291  obj4->m_color[2] = Random::uniform(180, 250);
292  iCub->opc->commit(obj4);
293 
294  return true;
295 }
296 
297 
299  iCub->opc->clear();
300 
301  Object* obj1 = iCub->opc->addOrRetrieveEntity<Object>("unknown_1");
302  obj1->m_ego_position[0] = -0.4;
303  obj1->m_ego_position[1] = 0.25;
304  obj1->m_ego_position[2] = 0;
305  obj1->m_present = 1.0;
306  obj1->m_color[0] = Random::uniform(0, 80);
307  obj1->m_color[1] = Random::uniform(80, 180);
308  obj1->m_color[2] = Random::uniform(180, 250);
309  iCub->opc->commit(obj1);
310 
311  Object* obj2 = iCub->opc->addOrRetrieveEntity<Object>("unknown_2");
312  obj2->m_ego_position[0] = -0.4;
313  obj2->m_ego_position[1] = -0.25;
314  obj2->m_ego_position[2] = 0;
315  obj2->m_present = 1.0;
316  obj2->m_color[0] = Random::uniform(100, 180);
317  obj2->m_color[1] = Random::uniform(0, 80);
318  obj2->m_color[2] = Random::uniform(180, 250);
319  iCub->opc->commit(obj2);
320 
321  return true;
322 }
323 
324 
326 
327  Object* obj1 = iCub->opc->addOrRetrieveEntity<Object>("red_ball");
328  obj1->m_ego_position[0] = -0.4;
329  obj1->m_ego_position[1] = 0.25;
330  obj1->m_ego_position[2] = 0;
331  obj1->m_present = 0.0;
332  obj1->m_color[0] = 250;
333  obj1->m_color[1] = 0;
334  obj1->m_color[2] = 0;
335  obj1->m_value = -1.0;
336  iCub->opc->commit(obj1);
337  return true;
338 }
bool populateSpecific()
populateSpecific populate four objects ("bottom_left", "bottom_right", according to X_obj...
yarp::sig::VectorOf< double > m_color
Mean color of the object (r,g,b) used mainly for debugging/displaying purposes in the iCubGUI...
Definition: object.h:61
bool populateEntityRandom(const yarp::os::Bottle &bInput)
populateEntityRandom populate entity from Bottle
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
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
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &reply)
STL namespace.
Represent any physical entity (including objects and agents) that can be stored within the OPC...
Definition: object.h:35
yarp::sig::VectorOf< double > m_ego_position
Position of the Object, in the initial ego-centered reference frame of the agent mainting the OPC (in...
Definition: object.h:46
bool setAttributeEntity(const yarp::os::Bottle &bInput, std::function< void(icubclient::Object *, double)> f_setter)
setAttributeEntity set an attribute of an object
Represent an agent.
Definition: agent.h:93
bool addUnknownEntity(const yarp::os::Bottle &bInput)
addUnknownEntity populate object / agent at pseudo-random location, pseudo-randomly colored ...
#define ICUBCLIENT_OPC_ENTITY_OBJECT
Definition: tags.h:38
bool populateRedBall()
populateRedBall populate object with name "red_ball" at location (-0.4, 0.25, 0.0); is_present=0...
#define ICUBCLIENT_OPC_ENTITY_AGENT
Definition: tags.h:40
bool populateTwoUnknowns()
populateTwoUnknowns populate two objects: "unknown_1" at (-0.4, 0.25, 0.0) and "unknown_2" at (-0...
double m_value
A measurement of the object subjective value [0,1].
Definition: object.h:84
bool configure(yarp::os::ResourceFinder &rf)
bool updateModule()
std::string entity_type() const
Return the specific type of an entity.
Definition: entity.h:121