icub-client
main.cpp
Go to the documentation of this file.
1 /*
2 * Copyright(C) 2015 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>
24 
25 using namespace std;
26 using namespace yarp::os;
27 using namespace icubclient;
28 
29 
30 /*******************************************************/
31 class Recorder : public RFModule
32 {
33  BufferedPort<Bottle> dumpPort;
34  RpcClient verbPort;
35  RpcServer rpcPort;
36  OPCClient opc;
37  Mutex mutex;
38 
39  string agentName;
40  string actionTag;
41  string objectTag;
42  double period;
43  int gate;
44 
45 public:
46  /*******************************************************/
47  Recorder() : opc("test_verbRec/opc") { }
48 
49  /*******************************************************/
50  bool configure(ResourceFinder &rf)
51  {
52  agentName=rf.check("agent-name",Value("partner")).asString();
53  period=rf.check("period",Value(0.1)).asDouble();
54 
55  if (!opc.connect("OPC"))
56  {
57  yError()<<"OPC seems unavailabe!";
58  return false;
59  }
60 
61  dumpPort.open("/test_verbRec/dump:o");
62  verbPort.open("/test_verbRec/verb:rpc");
63  rpcPort.open("/test_verbRec/rpc");
64  attach(rpcPort);
65 
66  actionTag="none";
67  objectTag="none";
68  gate=0;
69 
70  return true;
71  }
72 
73  /*******************************************************/
74  double getPeriod()
75  {
76  return period;
77  }
78 
79  /*******************************************************/
80  bool updateModule()
81  {
82  LockGuard lg(mutex);
83 
84  Bottle &bDump=dumpPort.prepare();
85  bDump.clear();
86 
87  bDump.addString(actionTag);
88  bDump.addString(objectTag);
89 
90  opc.checkout();
91 
92  // agent body + position
93  if (Entity *e=opc.getEntity(agentName))
94  {
95  if (Agent *agent=dynamic_cast<Agent*>(e))
96  {
97  bDump.addList()=agent->m_body.asBottle();
98  Bottle &bAgent=bDump.addList();
99  bAgent.addString(agent->name());
100  bAgent.addDouble(agent->m_ego_position[0]);
101  bAgent.addDouble(agent->m_ego_position[1]);
102  bAgent.addDouble(agent->m_ego_position[2]);
103  bAgent.addInt(agent->m_present==1.0?1:0);
104  }
105  }
106 
107  // objects position
108  list<Entity*> lEntity=opc.EntitiesCache();
109  for (list<Entity*>::iterator itEnt=lEntity.begin(); itEnt!=lEntity.end(); itEnt++)
110  {
111  string entityName=(*itEnt)->name();
112  string entityType=(*itEnt)->entity_type();
113  if (entityType==ICUBCLIENT_OPC_ENTITY_OBJECT)
114  {
115  if (Object *object=dynamic_cast<Object*>(*itEnt))
116  {
117  Bottle &bObject=bDump.addList();
118  bObject.addString(object->name());
119  bObject.addDouble(object->m_ego_position[0]);
120  bObject.addDouble(object->m_ego_position[1]);
121  bObject.addDouble(object->m_ego_position[2]);
122  bObject.addInt(object->m_present==1.0?1:0);
123  }
124  }
125  }
126 
127  bDump.addInt(gate);
128 
129  // query verbRec
130  if (verbPort.getOutputCount()>0)
131  {
132  Bottle cmd=bDump;
133  Bottle reply;
134  verbPort.write(cmd,reply);
135  Bottle &response=bDump.addList();
136  response.addString("response");
137  response.append(reply);
138  }
139 
140  yInfo()<<bDump.toString();
141  dumpPort.writeStrict();
142 
143  return true;
144  }
145 
146  /*******************************************************/
147  bool respond(const Bottle &command, Bottle &reply)
148  {
149  LockGuard lg(mutex);
150  int cmd=command.get(0).asVocab();
151  int ack=Vocab::encode("ack");
152  int nack=Vocab::encode("nack");
153 
154  if (cmd==Vocab::encode("start"))
155  {
156  if (command.size()>=3)
157  {
158  actionTag=command.get(1).asString();
159  objectTag=command.get(2).asString();
160  gate=1;
161  if (command.size()>=4)
162  {
163  int g=command.get(3).asInt();
164  if (g>0)
165  gate=g;
166  }
167  reply.addVocab(ack);
168  }
169  else
170  reply.addVocab(nack);
171  return true;
172  }
173  else if (cmd==Vocab::encode("stop"))
174  {
175  actionTag="none";
176  objectTag="none";
177  gate=0;
178  reply.addVocab(ack);
179  return true;
180  }
181  else if (cmd==Vocab::encode("get"))
182  {
183  reply.addVocab(ack);
184  reply.addString(actionTag);
185  reply.addString(objectTag);
186  reply.addInt(gate);
187  return true;
188  }
189  else
190  return RFModule::respond(command,reply);
191  }
192 
193  /*******************************************************/
194  bool close()
195  {
196  rpcPort.close();
197  verbPort.close();
198  dumpPort.close();
199  opc.close();
200  return true;
201  }
202 };
203 
204 
205 /*******************************************************/
206 int main(int argc, char *argv[])
207 {
208  Network yarp;
209  if (!yarp.checkNetwork())
210  {
211  yError()<<"YARP network seems unavailable!";
212  return 1;
213  }
214 
215  ResourceFinder rf;
216  rf.configure(argc,argv);
217 
218  Recorder recorder;
219  return recorder.runModule(rf);
220 }
221 
222 
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
void close()
Close the client ports.
Definition: opcClient.cpp:69
STL namespace.
Recorder()
Definition: main.cpp:47
bool close()
Definition: main.cpp:194
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
Represent an agent.
Definition: agent.h:93
bool respond(const Bottle &command, Bottle &reply)
Definition: main.cpp:147
std::list< Entity * > EntitiesCache() const
Getter of the list of entities stored locally.
Definition: opcClient.cpp:905
bool updateModule()
Definition: main.cpp:80
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
An OPC client using the datastructures defined within the icub-client library.
Definition: opcClient.h:35
#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
bool configure(ResourceFinder &rf)
Definition: main.cpp:50
double getPeriod()
Definition: main.cpp:74