icub-client
behaviorManager.cpp
Go to the documentation of this file.
1 #include "behaviorManager.h"
2 
3 #include "dummy.h"
4 #include "tagging.h"
5 #include "pointing.h"
6 #include "recognitionOrder.h"
7 #include "moveObject.h"
8 
9 using namespace std;
10 using namespace yarp::os;
11 
13 {
14  rpc_in_port.interrupt();
15 
16  for(auto& beh : behaviors) {
17  beh->interrupt_ports();
18  }
19  return true;
20 }
21 
23 {
24  rpc_in_port.interrupt();
25  rpc_in_port.close();
26  iCub->close();
27 
28  for(auto& beh : behaviors) {
29  beh->interrupt_ports();
30  beh->close_ports();
31  delete beh;
32  }
33  behaviors.clear();
34 
35  delete iCub;
36 
37  return true;
38 }
39 
40 bool BehaviorManager::configure(yarp::os::ResourceFinder &rf)
41 {
42  moduleName = rf.check("name",Value("BehaviorManager")).asString();
43  setName(moduleName.c_str());
44  yInfo()<<moduleName<<": finding configuration files...";//<<endl;
45  period = rf.check("period",Value(1.0)).asDouble();
46 
47  Bottle grp = rf.findGroup("BEHAVIORS");
48  Bottle behaviorList = *grp.find("behaviors").asList();
49 
50  rpc_in_port.open("/" + moduleName + "/trigger:i");
51  yInfo() << "RPC_IN : " << rpc_in_port.getName();
52 
53  //Create an iCub Client and check that all dependencies are here before starting
54  bool isRFVerbose = false;
55  iCub = new icubclient::ICubClient(moduleName, "behaviorManager","client.ini",isRFVerbose);
56 
57  if (!iCub->connect())
58  {
59  yInfo() << "iCubClient : Some dependencies are not running...";
60  Time::delay(1.0);
61  }
62 
63  for (unsigned int i = 0; i<behaviorList.size(); i++)
64  {
65  std::string behavior_name = behaviorList.get(i).asString();
66  if (behavior_name == "tagging") {
67  behaviors.push_back(new Tagging(&mut, rf, iCub, "tagging"));
68  } else if (behavior_name == "pointing") {
69  behaviors.push_back(new Pointing(&mut, rf, iCub, "pointing"));
70  } else if (behavior_name == "dummy") {
71  behaviors.push_back(new Dummy(&mut, rf, iCub, "dummy"));
72  } else if (behavior_name == "dummy2") {
73  behaviors.push_back(new Dummy(&mut, rf, iCub, "dummy2"));
74  } else if (behavior_name == "recognitionOrder") {
75  behaviors.push_back(new RecognitionOrder(&mut, rf, iCub, "recognitionOrder"));
76  } else if (behavior_name == "moveObject") {
77  behaviors.push_back(new MoveObject(&mut, rf, iCub, "moveObject"));
78  // other behaviors here
79  } else {
80  yDebug() << "Behavior " + behavior_name + " not implemented";
81  return false;
82  }
83  }
84 
85  while (!Network::connect("/ears/behavior:o", rpc_in_port.getName())) {
86  yWarning() << "Ears is not reachable";
87  yarp::os::Time::delay(0.5);
88  }
89 
90  for(auto& beh : behaviors) {
91  beh->configure();
92  beh->openPorts(moduleName);
93 
94  if (beh->from_sensation_port_name != "None") {
95  while (!Network::connect(beh->from_sensation_port_name, beh->sensation_port_in.getName())) {
96  yInfo()<<"Connecting "<< beh->from_sensation_port_name << " to " << beh->sensation_port_in.getName();// <<endl;
97  yarp::os::Time::delay(0.5);
98  }
99  }
100  if (beh->external_port_name != "None") {
101  while (!Network::connect(beh->rpc_out_port.getName(), beh->external_port_name)) {
102  yInfo()<<"Connecting "<< beh->rpc_out_port.getName() << " to " << beh->external_port_name;// <<endl;
103  yarp::os::Time::delay(0.5);
104  }
105  }
106 
107  }
108 
109  attach(rpc_in_port);
110  yInfo("Init done");
111 
112  return true;
113 }
114 
115 
117 {
118  return true;
119 }
120 
121 
122 bool BehaviorManager::respond(const Bottle& cmd, Bottle& reply)
123 {
124  yDebug() << "RPC received in BM";
125  yDebug() << cmd.toString();
126 
127  reply.clear();
128 
129  if (cmd.get(0).asString() == "help" )
130  { string help = "\n";
131  help += " ['behavior_name'] : Triggers corresponding behavior \n";
132  reply.addString(help);
133  }
134  else if (cmd.get(0).asString() == "names" ) {
135  Bottle names;
136  for(auto& beh : behaviors) {
137  names.addString(beh->behaviorName);
138  }
139  reply.addList() = names;
140  } else if (cmd.get(0).asString() == "is_available" ) {
141  if (mut.try_lock()) {
142  mut.unlock();
143  reply.addInt(1);
144  } else {
145  reply.addInt(0);
146  }
147  }
148  else
149  {
150  bool behavior_triggered = false;
151  for(auto& beh : behaviors) {
152  if (cmd.get(0).asString() == beh->behaviorName) {
153  bool aux;
154  if (beh->from_sensation_port_name != "None") {
155  if (!Network::isConnected(beh->from_sensation_port_name, beh->sensation_port_in.getName())) {
156  aux =Network::connect(beh->from_sensation_port_name, beh->sensation_port_in.getName());
157  yInfo()<< "The sensations port for "<< beh->behaviorName <<" was not connected. \nReconnection status: " << aux;
158  }
159  }
160  if (beh->external_port_name != "None") {
161  if (!Network::isConnected(beh->rpc_out_port.getName(), beh->external_port_name)) {
162  aux = Network::connect(beh->rpc_out_port.getName(), beh->external_port_name);
163  yInfo()<< "The external port for "<< beh->behaviorName <<" was not connected. \nReconnection status: " << aux;
164  }
165  }
166 
167  Bottle args;
168  if (cmd.size()>1){
169  args = cmd.tail();
170  }
171  yDebug() << "arguments are " << args.toString().c_str();
172  behavior_triggered = beh->trigger(args);
173  }
174  }
175  if (behavior_triggered) {
176  reply.addString("ack");
177  } else {
178  reply.addString("nack");
179  yDebug()<< "Behavior ' " << cmd.get(0).asString() << " ' cant be triggered. \nSend 'names' to see a list of available behaviors. ";
180  }
181  }
182  yDebug() << "End of BehaviorManager::respond";
183  return true;
184 }
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &reply)
Grants access to high level motor commands (grasp, touch, look, goto, etc) of the robot as well as it...
Definition: icubClient.h:66
STL namespace.
Definition: dummy.h:12
bool configure(yarp::os::ResourceFinder &rf)