icub-client
main.cpp
Go to the documentation of this file.
1 /*
2 * Copyright(C) 2016 WYSIWYD Consortium, European Commission FP7 Project ICT - 612139
3 * Authors: Ugo Pattacini
4 * email : ugo.pattacini@iit.it
5 * Permission is granted to copy, distribute, and / or modify this program
6 * under the terms of the GNU General Public License, version 2 or any
7 * later version published by the Free Software Foundation.
8 *
9 * A copy of the license can be found at
10 * icub-client / license / gpl.txt
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General
15 * Public License for more details
16 */
17 
18 #include <string>
19 #include <list>
20 
21 #include <yarp/os/all.h>
22 #include <yarp/sig/all.h>
25 
26 using namespace std;
27 using namespace yarp::os;
28 using namespace yarp::sig;
29 using namespace icubclient;
30 
35 /*******************************************************/
36 class Recorder : public RFModule
37 {
38  Stamp dumpStamp;
39  BufferedPort<Bottle> dumpPort;
40  RpcServer rpcPort;
41  Mutex mutex;
43 
44  string agentName;
45  string actionTag;
46  string objectTag;
47  double period;
48  int gate;
49  int logcounter;
50 
51 public:
52  /*******************************************************/
53  Recorder() : icubclient("actionRecogDataDumper", "actionRecogDataDumper"), period(0.05), gate(0), logcounter(0) { }
54 
55  /*******************************************************/
56  bool configure(ResourceFinder &rf)
57  {
58  agentName=rf.check("agent-name",Value("partner")).asString();
59  period=rf.check("period",Value(0.05)).asDouble();
60 
61  if (!icubclient.connect())
62  {
63  yError() << " iCubClient : Some dependencies are not running (OPC?)...";
64  return false;
65  }
66 
67  dumpPort.open("/actionRecogDataDumper/data/dump:o");
68  rpcPort.open("/actionRecogDataDumper/rpc");
69  attach(rpcPort);
70 
71  actionTag="none";
72  objectTag="none";
73 
74  return true;
75  }
76 
77  /*******************************************************/
78  double getPeriod()
79  {
80  return period;
81  }
82 
83  /*******************************************************/
84  bool updateModule()
85  {
86  LockGuard lg(mutex);
87 
88  double dumpTime=Time::now();
89  Bottle &bDump=dumpPort.prepare();
90  bDump.clear();
91 
92  bDump.addString(actionTag);
93  bDump.addString(objectTag);
94 
95  icubclient.opc->checkout();
96 
97  // agent body + position
98  agentName = icubclient.getPartnerName(false);
99  if (Entity *e=icubclient.opc->getEntity(agentName))
100  {
101  if (Agent *agent=dynamic_cast<Agent*>(e))
102  {
103  bDump.addList()=agent->m_body.asBottle();
104  Bottle &bAgent=bDump.addList();
105  bAgent.addString(agent->name());
106  bAgent.addDouble(agent->m_ego_position[0]);
107  bAgent.addDouble(agent->m_ego_position[1]);
108  bAgent.addDouble(agent->m_ego_position[2]);
109  bAgent.addInt(agent->m_present==1.0?1:0);
110  }
111  }
112 
113  // objects position
114  list<Entity*> lEntity=icubclient.opc->EntitiesCache();
115  for (list<Entity*>::iterator itEnt=lEntity.begin(); itEnt!=lEntity.end(); itEnt++)
116  {
117  string entityName=(*itEnt)->name();
118  string entityType=(*itEnt)->entity_type();
119  if (entityType==ICUBCLIENT_OPC_ENTITY_OBJECT)
120  {
121  if (Object *object=dynamic_cast<Object*>(*itEnt))
122  {
123  Bottle &bObject=bDump.addList();
124  bObject.addString(object->name());
125  bObject.addDouble(object->m_ego_position[0]);
126  bObject.addDouble(object->m_ego_position[1]);
127  bObject.addDouble(object->m_ego_position[2]);
128  bObject.addInt(object->m_present==1.0?1:0);
129  }
130  }
131  }
132 
133  bDump.addInt(gate);
134 
135  dumpStamp.update(dumpTime);
136  dumpPort.setEnvelope(dumpStamp);
137  dumpPort.writeStrict();
138 
139  if(logcounter>20) { // only log every 20 iterations
140  yInfo()<<bDump.toString();
141  logcounter=0;
142  } else {
143  logcounter++;
144  }
145 
146  return true;
147  }
148 
149  /*******************************************************/
150  bool respond(const Bottle &command, Bottle &reply)
151  {
152  LockGuard lg(mutex);
153  int cmd=command.get(0).asVocab();
154  int ack=Vocab::encode("ack");
155  int nack=Vocab::encode("nack");
156 
157  if (cmd==Vocab::encode("start"))
158  {
159  if (command.size()>=3)
160  {
161  actionTag=command.get(1).asString();
162  objectTag=command.get(2).asString();
163  gate=1;
164  if (command.size()>=4)
165  {
166  int g=command.get(3).asInt();
167  if (g>0)
168  gate=g;
169  }
170  reply.addVocab(ack);
171  }
172  else
173  reply.addVocab(nack);
174  return true;
175  }
176  else if (cmd==Vocab::encode("stop"))
177  {
178  actionTag="none";
179  objectTag="none";
180  gate=0;
181  reply.addVocab(ack);
182  return true;
183  }
184  else if (cmd==Vocab::encode("get"))
185  {
186  reply.addVocab(ack);
187  reply.addString(actionTag);
188  reply.addString(objectTag);
189  reply.addInt(gate);
190  return true;
191  }
192  else
193  return RFModule::respond(command,reply);
194  }
195 
196  /*******************************************************/
197  bool close()
198  {
199  rpcPort.interrupt();
200  rpcPort.close();
201  dumpPort.interrupt();
202  dumpPort.close();
203  icubclient.opc->interrupt();
204  icubclient.opc->close();
205  icubclient.close();
206  return true;
207  }
208 
210  {
211  rpcPort.interrupt();
212  dumpPort.interrupt();
213  icubclient.opc->interrupt();
214  return true;
215  }
216 };
217 
218 
219 /*******************************************************/
220 int main(int argc, char *argv[])
221 {
222  Network yarp;
223  if (!yarp.checkNetwork())
224  {
225  yError()<<"YARP network seems unavailable!";
226  return 1;
227  }
228 
229  ResourceFinder rf;
230  rf.configure(argc,argv);
231 
232  Recorder recorder;
233  return recorder.runModule(rf);
234 }
235 
236 
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
void close()
Close the client ports.
Definition: opcClient.cpp:69
Grants access to high level motor commands (grasp, touch, look, goto, etc) of the robot as well as it...
Definition: icubClient.h:66
bool connect(const std::string &opcName="OPC")
Try to connect all functionalities.
Definition: icubClient.cpp:132
STL namespace.
Recorder()
Definition: main.cpp:53
bool close()
Definition: main.cpp:197
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
bool respond(const Bottle &command, Bottle &reply)
Definition: main.cpp:150
std::list< Entity * > EntitiesCache() const
Getter of the list of entities stored locally.
Definition: opcClient.cpp:905
bool updateModule()
Definition: main.cpp:84
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
int main()
Definition: main.cpp:32
bool interruptModule()
Definition: main.cpp:209
void close()
Properly closes all ports which were opened.
Definition: icubClient.cpp:140
void interrupt()
Interrupt communications of the client ports.
Definition: opcClient.cpp:64
#define ICUBCLIENT_OPC_ENTITY_OBJECT
Definition: tags.h:38
void checkout(bool updateCache=true)
Poll the OPC for all entities and relations and store them locally.
Definition: opcClient.cpp:754
std::string getPartnerName(bool verbose=true)
Extract the name of the agent interaction with the iCub (present, not iCub nor &#39;unnamed&#39; partner) ...
Definition: icubClient.cpp:251
bool configure(ResourceFinder &rf)
Definition: main.cpp:56
double getPeriod()
Definition: main.cpp:78