icub-client
subSystem_ARE.cpp
Go to the documentation of this file.
1 #include <yarp/os/all.h>
3 
4 void icubclient::SubSystem_ARE::appendCartesianTarget(yarp::os::Bottle &b, const yarp::sig::VectorOf<double> &t)
5 {
6  yarp::os::Bottle &sub=b.addList();
7  sub.addString("cartesian");
8  for (size_t i=0; i<t.length(); i++)
9  sub.addDouble(t[i]);
10 }
11 
12 void icubclient::SubSystem_ARE::selectHandCorrectTarget(yarp::os::Bottle &options, yarp::sig::VectorOf<double> &target, const std::string& objName, const std::string handToUse)
13 {
14  std::string hand="";
15  for (unsigned int i=0; i<options.size(); i++)
16  {
17  yarp::os::Value val=options.get(i);
18  if (val.isString())
19  {
20  std::string item=val.asString();
21  if ((item=="left") || (item=="right"))
22  {
23  hand=item;
24  break;
25  }
26  }
27  }
28 
29  // always choose hand
30  if (hand.empty())
31  {
32  if (handToUse.empty())
33  {
34  hand=(target[1]>0.0?"right":"left");
35  options.addString(hand.c_str());
36  }
37  else
38  hand=handToUse;
39  }
40 
41  // apply 3D correction
42  if (portCalib.getOutputCount()>0)
43  {
44  yarp::os::Bottle cmd,reply;
45  cmd.addString("get_location");
46  cmd.addString(hand);
47  cmd.addString(objName);
48  cmd.addString("iol-"+hand);
49  portCalib.write(cmd,reply);
50  target[0]=reply.get(1).asDouble();
51  target[1]=reply.get(2).asDouble();
52  target[2]=reply.get(3).asDouble();
53 
54  yarp::os::Bottle opt;
55  opt.addString("fixate");
56  look(target,opt);
57  }
58 
59  lastlyUsedHand=hand;
60 }
61 
62 bool icubclient::SubSystem_ARE::sendCmd(const yarp::os::Bottle &cmd)
63 {
64  bool ret=false;
65 
66  yDebug() << "Send to ARE: " << cmd.toString();
67  yarp::os::Bottle bReply;
68  if (portCmd.write(const_cast<yarp::os::Bottle&>(cmd),bReply))
69  ret=(bReply.get(0).asVocab()==yarp::os::Vocab::encode("ack"));
70 
71  yDebug() << "Reply from ARE: " << bReply.toString();
72 
73  return ret;
74 }
75 
77 {
78  if (!yarp::os::Network::isConnected(portCalib.getName(),"/iolReachingCalibration/rpc")) {
79  if (yarp::os::Network::connect(portCalib.getName(),"/iolReachingCalibration/rpc")) {
80  yInfo()<<"ARE connected to calibrator";
81  } else {
82  yWarning()<<"ARE didn't connect to calibrator";
83  }
84  }
85 
86  bool ret=true;
87  if(!yarp::os::Network::isConnected(portCmd.getName(),"/actionsRenderingEngine/cmd:io")) {
88  ret&=yarp::os::Network::connect(portCmd.getName(),"/actionsRenderingEngine/cmd:io");
89  }
90  if(!yarp::os::Network::isConnected(portRPC.getName(),"/actionsRenderingEngine/rpc")) {
91  ret&=yarp::os::Network::connect(portRPC.getName(),"/actionsRenderingEngine/rpc");
92  }
93  if(!yarp::os::Network::isConnected(portGet.getName(),"/actionsRenderingEngine/get:io")) {
94  ret&=yarp::os::Network::connect(portGet.getName(),"/actionsRenderingEngine/get:io");
95  }
96 
97  opc->connect("OPC");
98 
99  return ret;
100 }
101 
102 icubclient::SubSystem_ARE::SubSystem_ARE(const std::string &masterName) : SubSystem(masterName)
103 {
104  portCmd.open(("/" + masterName + "/" + SUBSYSTEM_ARE + "/cmd:io").c_str());
105  portRPC.open(("/" + masterName + "/" + SUBSYSTEM_ARE + "/rpc").c_str());
106  portGet.open(("/" + masterName + "/" + SUBSYSTEM_ARE + "/get:io").c_str());
107  portCalib.open(("/" + masterName + "/" + SUBSYSTEM_ARE + "/calib:io").c_str());
109  lastlyUsedHand="";
110 
111  opc = new OPCClient(m_masterName+"/opc_from_ARE");
112 }
113 
115 {
116  opc->interrupt();
117  opc->close();
118 
119  portCmd.interrupt();
120  portRPC.interrupt();
121  portGet.interrupt();
122  portCalib.interrupt();
123 
124  portCmd.close();
125  portRPC.close();
126  portGet.close();
127  portCalib.close();
128 }
129 
131 {
132  yarp::os::Bottle bCmd, bReply;
133  bCmd.addVocab(yarp::os::Vocab::encode("get"));
134  bCmd.addVocab(yarp::os::Vocab::encode("table"));
135  portGet.write(bCmd, bReply);
136 
137  yarp::os::Value vHeight = bReply.find("table_height");
138  if (vHeight.isNull()) {
139  yError("No table height specified in ARE!");
140  return false;
141  }
142  else {
143  height = vHeight.asDouble();
144  return true;
145  }
146 }
147 
148 yarp::sig::VectorOf<double> icubclient::SubSystem_ARE::applySafetyMargins(const yarp::sig::VectorOf<double> &in)
149 {
150  yarp::sig::VectorOf<double> out=in;
151  out[0]=std::min(out[0],-0.1);
152 
153  double height;
154  if (getTableHeight(height))
155  out[2]=std::max(out[2],height);
156 
157  return out;
158 }
159 
160 bool icubclient::SubSystem_ARE::home(const std::string &part)
161 {
162  yDebug() << "ARE::home start";
163  yarp::os::Bottle bCmd;
164  bCmd.addVocab(yarp::os::Vocab::encode("home"));
165  bCmd.addString(part.c_str());
166 
167  bool bReturn = sendCmd(bCmd);
168  std::string status;
169  bReturn ? status = "success" : status = "failed";
170  yDebug() << "ARE::home stop";
171 
172  return bReturn;
173 }
174 
175 bool icubclient::SubSystem_ARE::take(const std::string &sName, const yarp::os::Bottle &options)
176 {
177  yDebug() << "ARE::take start";
178 
179  Entity* e = opc->getEntity(sName, true);
180  Object *o;
181  if(e) {
182  o = dynamic_cast<Object*>(e);
183  }
184  else {
185  yError() << sName << " is not an Entity";
186  return false;
187  }
188  if(!o) {
189  yError() << "Could not cast" << e->name() << "to Object";
190  return false;
191  }
192 
193  yarp::os::Bottle bCmd;
194  bCmd.addVocab(yarp::os::Vocab::encode("take"));
195 
196  yarp::sig::VectorOf<double> target=o->m_ego_position;
197  yarp::os::Bottle opt=options;
198  selectHandCorrectTarget(opt,target,sName);
199  target=applySafetyMargins(target);
200  appendCartesianTarget(bCmd,target);
201  bCmd.append(opt);
202 
203  bool bReturn = sendCmd(bCmd);
204  std::string status;
205  bReturn ? status = "success" : status = "failed";
206  yDebug() << "ARE::take stop";
207 
208  return bReturn;
209 }
210 
211 bool icubclient::SubSystem_ARE::push(const std::string &sName, const yarp::os::Bottle &options)
212 {
213  yDebug() << "ARE::push start";
214 
215  Entity* e = opc->getEntity(sName, true);
216  Object *o;
217  if(e) {
218  o = dynamic_cast<Object*>(e);
219  }
220  else {
221  yError() << sName << " is not an Entity";
222  return false;
223  }
224  if(!o) {
225  yError() << "Could not cast" << e->name() << "to Object";
226  return false;
227  }
228 
229  yarp::os::Bottle bCmd;
230  bCmd.addVocab(yarp::os::Vocab::encode("push"));
231 
232  yarp::sig::VectorOf<double> target=o->m_ego_position;
233  yarp::os::Bottle opt=options;
234  selectHandCorrectTarget(opt,target,sName);
235  target=applySafetyMargins(target);
236  appendCartesianTarget(bCmd,target);
237  bCmd.append(opt);
238 
239  bool bReturn = sendCmd(bCmd);
240  std::string status;
241  bReturn ? status = "success" : status = "failed";
242 
243  yDebug() << "ARE::push stop";
244  return bReturn;
245 
246 }
247 
248 bool icubclient::SubSystem_ARE::point(const yarp::sig::VectorOf<double> &targetUnsafe, const yarp::os::Bottle &options)
249 {
250  yDebug() << "ARE::point start";
251  yarp::sig::VectorOf<double> target=applySafetyMargins(targetUnsafe);
252 
253  yarp::os::Bottle bCmd;
254  if(target[0]<-0.31) { // everything further than 31cm should be pointed at using the "point far" of ARE
255  yDebug() << "Use ARE::pfar for pointing";
256  bCmd.addVocab(yarp::os::Vocab::encode("pfar"));
257  } else {
258  yDebug() << "Use ARE::point for pointing";
259  bCmd.addVocab(yarp::os::Vocab::encode("point"));
260  }
261 
262  appendCartesianTarget(bCmd,target);
263  bCmd.append(options);
264 
265  bool bReturn = sendCmd(bCmd);
266  std::string status;
267  bReturn ? status = "success" : status = "failed";
268 
269  yDebug() << "ARE::point stop";
270  return bReturn;
271 }
272 
273 bool icubclient::SubSystem_ARE::drop(const yarp::os::Bottle &options)
274 {
275  yDebug() << "ARE::drop start";
276 
277  // we don't need d2k correction
278  // because the drop takes place
279  // on a random location
280  yarp::os::Bottle bCmd;
281  bCmd.addVocab(yarp::os::Vocab::encode("drop"));
282  bCmd.append(options);
283  bool bReturn = sendCmd(bCmd);
284  std::string status;
285  bReturn ? status = "success" : status = "failed";
286 
287  yDebug() << "ARE::drop stop";
288  return bReturn;
289 }
290 
291 bool icubclient::SubSystem_ARE::dropOn(const yarp::sig::VectorOf<double> &targetUnsafe, const yarp::os::Bottle &options)
292 {
293  yDebug() << "ARE::dropOn start";
294 
295  yarp::os::Bottle bCmd;
296  bCmd.addVocab(yarp::os::Vocab::encode("drop"));
297  bCmd.addString("over");
298 
299  yarp::sig::VectorOf<double> target=targetUnsafe;
300  yarp::os::Bottle opt=options;
302  target=applySafetyMargins(target);
303  appendCartesianTarget(bCmd,target);
304  bCmd.append(opt);
305 
306  bool bReturn = sendCmd(bCmd);
307  std::string status;
308  bReturn ? status = "success" : status = "failed";
309 
310  yDebug() << "ARE::dropOn stop";
311  return bReturn;
312 }
313 
314 bool icubclient::SubSystem_ARE::observe(const yarp::os::Bottle &options)
315 {
316  yDebug() << "ARE::observe start";
317 
318  yarp::os::Bottle bCmd;
319  bCmd.addVocab(yarp::os::Vocab::encode("observe"));
320  bCmd.append(options);
321  bool bReturn = sendCmd(bCmd);
322  std::string status;
323  bReturn ? status = "success" : status = "failed";
324 
325  yDebug() << "ARE::observe stop";
326 
327  return bReturn;
328 }
329 
330 bool icubclient::SubSystem_ARE::expect(const yarp::os::Bottle &options)
331 {
332  yDebug() << "ARE::expect start";
333 
334  yarp::os::Bottle bCmd;
335  bCmd.addVocab(yarp::os::Vocab::encode("expect"));
336  bCmd.append(options);
337  bool bReturn = sendCmd(bCmd);
338  std::string status;
339  bReturn ? status = "success" : status = "failed";
340 
341  yDebug() << "ARE::expect stop";
342 
343  return bReturn;
344 }
345 
346 bool icubclient::SubSystem_ARE::give(const yarp::os::Bottle &options)
347 {
348  yDebug() << "ARE::give start";
349 
350  yarp::os::Bottle bCmd;
351  bCmd.addVocab(yarp::os::Vocab::encode("give"));
352  bCmd.append(options);
353  bool bReturn = sendCmd(bCmd);
354  std::string status;
355  bReturn ? status = "success" : status = "failed";
356 
357  yDebug() << "ARE::give stop";
358  return bReturn;
359 }
360 
362 {
363  yDebug() << "ARE::waving start";
364 
365  yarp::os::Bottle bCmd;
366  bCmd.addVocab(yarp::os::Vocab::encode("waveing"));
367  bCmd.addString(sw ? "on" : "off");
368  bool bReturn = portRPC.asPort().write(bCmd);
369  std::string status;
370  bReturn ? status = "success" : status = "failed";
371 
372  yDebug() << "ARE::waving stop";
373  return bReturn;
374 }
375 
376 bool icubclient::SubSystem_ARE::look(const yarp::sig::VectorOf<double> &target, const yarp::os::Bottle &options)
377 {
378  yDebug() << "ARE::look start";
379 
380  yarp::os::Bottle bCmd;
381  bCmd.addVocab(yarp::os::Vocab::encode("look"));
382  appendCartesianTarget(bCmd, target);
383  bCmd.append(options);
384  bool bReturn = sendCmd(bCmd);
385  std::string status;
386  bReturn ? status = "success" : status = "failed";
387 
388  yarp::os::Time::delay(0.1);
389 
390  yDebug() << "ARE::look stop";
391  return bReturn;
392 }
393 
394 bool icubclient::SubSystem_ARE::track(const yarp::sig::VectorOf<double> &target, const yarp::os::Bottle &options)
395 {
396  // track() is meant for streaming => no point in gating the activity continuously
397  yarp::os::Bottle bCmd;
398  bCmd.addVocab(yarp::os::Vocab::encode("track"));
399  appendCartesianTarget(bCmd, target);
400  bCmd.append(options);
401  return sendCmd(bCmd);
402 }
403 
405 {
406  yarp::os::Bottle bCmd;
407  bCmd.addVocab(yarp::os::Vocab::encode("impedance"));
408  bCmd.addString(sw ? "on" : "off");
409  return portRPC.asPort().write(bCmd);
410 }
411 
412 bool icubclient::SubSystem_ARE::setExecTime(const double execTime)
413 {
414  yarp::os::Bottle bCmd;
415  bCmd.addVocab(yarp::os::Vocab::encode("time"));
416  bCmd.addDouble(execTime);
417  return portRPC.asPort().write(bCmd);
418 }
419 
421 {
422  delete opc;
423 }
bool sendCmd(const yarp::os::Bottle &cmd)
Sends a command to ARE&#39;s cmdPort.
yarp::os::RpcClient portRPC
Port to /ARE/rpc.
Definition: subSystem_ARE.h:47
std::string lastlyUsedHand
The hand which was used for the last action.
Definition: subSystem_ARE.h:51
std::string m_type
Definition: subSystem.h:47
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
yarp::os::RpcClient portCalib
Port to iolReachingCalibration.
Definition: subSystem_ARE.h:49
void close()
Close the client ports.
Definition: opcClient.cpp:69
bool push(const std::string &sName, const yarp::os::Bottle &options=yarp::os::Bottle())
Reach the specified [target] from one side and then push it laterally.
bool look(const yarp::sig::VectorOf< double > &target, const yarp::os::Bottle &options=yarp::os::Bottle())
Look at the specified [target].
SubSystem_ARE(const std::string &masterName)
Default constructor.
bool expect(const yarp::os::Bottle &options=yarp::os::Bottle())
Put one hand forward with the palm facing up and wait for an object.
#define SUBSYSTEM_ARE
Definition: subSystem_ARE.h:30
bool connect(const std::string &opcName)
Try to connect the client to an OPC server.
Definition: opcClient.h:157
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 impedance(const bool sw)
Enable/disable impedance control.
bool point(const yarp::sig::VectorOf< double > &targetUnsafe, const yarp::os::Bottle &options=yarp::os::Bottle())
Point at the specified [target] with the index finger.
yarp::os::RpcClient portGet
Port to /ARE/get:io.
Definition: subSystem_ARE.h:48
bool home(const std::string &part="all")
Put the specified part ih home position.
bool getTableHeight(double &height)
Gets the table height from ARE.
yarp::os::RpcClient portCmd
Port to /ARE/cmd:io.
Definition: subSystem_ARE.h:46
Entity * getEntity(const std::string &name, bool forceUpdate=false)
Gets an entity based on its name, but do no create it if it doesn&#39;t exist yet.
Definition: opcClient.cpp:134
Abstract class to handle sub-systems of the icub-client.
Definition: subSystem.h:43
void Close()
Clean up resources.
bool track(const yarp::sig::VectorOf< double > &target, const yarp::os::Bottle &options=yarp::os::Bottle())
Track the specified [target].
An OPC client using the datastructures defined within the icub-client library.
Definition: opcClient.h:35
bool observe(const yarp::os::Bottle &options=yarp::os::Bottle())
Bring the hand in the visual field and move it with the purpose of visual exploration.
bool take(const std::string &sName, const yarp::os::Bottle &options=yarp::os::Bottle())
Reach the specified [target] and grasp it.
yarp::sig::VectorOf< double > applySafetyMargins(const yarp::sig::VectorOf< double > &in)
Applies safety margins, i.e.
std::string name() const
Return the name of an entity (which has to be unique within the OPC)
Definition: entity.h:93
void interrupt()
Interrupt communications of the client ports.
Definition: opcClient.cpp:64
void appendCartesianTarget(yarp::os::Bottle &b, const yarp::sig::VectorOf< double > &t)
Appends a target vector t to the Bottle b
void selectHandCorrectTarget(yarp::os::Bottle &options, yarp::sig::VectorOf< double > &target, const std::string &objName, const std::string handToUse="")
bool dropOn(const yarp::sig::VectorOf< double > &targetUnsafe, const yarp::os::Bottle &options=yarp::os::Bottle())
Drop the object on a given target.
bool drop(const yarp::os::Bottle &options=yarp::os::Bottle())
If an object is held, bring it over the table and drop it on a random position.
bool give(const yarp::os::Bottle &options=yarp::os::Bottle())
Put one hand forward with the palm facing up and open the fingers so that the object held in the hand...
bool waving(const bool sw)
Enable/disable arms waving.
std::string m_masterName
Definition: subSystem.h:46
bool setExecTime(const double execTime)
Change default arm movement execution time.