icub-client
subSystem_recog.cpp
Go to the documentation of this file.
1 #include <yarp/os/all.h>
3 
5  if (!yarp::os::Network::isConnected(ears_port.getName(), "/ears/rpc")) {
6  if (yarp::os::Network::connect(ears_port.getName(), "/ears/rpc")) {
7  yInfo() << "Recog connected to ears";
8  }
9  else {
10  yDebug() << "Recog didn't connect to ears";
11  }
12  }
13 
14  if (yarp::os::Network::isConnected(portRPC.getName(), "/speechRecognizer/rpc")){
15  return true;
16  }
17  else {
18  return yarp::os::Network::connect(portRPC.getName(), "/speechRecognizer/rpc");
19  }
20 }
21 
22 icubclient::SubSystem_Recog::SubSystem_Recog(const std::string &masterName) : SubSystem(masterName){
23  portRPC.open(("/" + m_masterName + "/recog:rpc").c_str());
24  ears_port.open("/" + m_masterName + "/ears:o");
26 }
27 
29  portRPC.interrupt();
30  portRPC.close();
31  ears_port.interrupt();
32  ears_port.close();
33 }
34 
36  if (!yarp::os::Network::isConnected(ears_port.getName(), "/ears/rpc")){
37  yarp::os::Network::connect(ears_port.getName(), "/ears/rpc");
38  }
39  if (yarp::os::Network::isConnected(ears_port.getName(), "/ears/rpc")) {
40  yarp::os::Bottle cmd, reply;
41  cmd.addString("listen");
42  if (on) {
43  cmd.addString("on");
44  }
45  else {
46  cmd.addString("off");
47  }
48  yDebug() << "[listen] Listen sending command" << cmd.toString();
49  ears_port.write(cmd, reply);
50  yDebug() << "[listen] Listen got reply" << reply.toString();
51  }
52  else {
53  yWarning() << "[listen] No connection to ears available...";
54  }
55 }
56 
58  if (!yarp::os::Network::isConnected(ears_port.getName(), "/ears/rpc")){
59  yarp::os::Network::connect(ears_port.getName(), "/ears/rpc");
60  }
61  if (yarp::os::Network::isConnected(ears_port.getName(), "/ears/rpc")) {
62  yarp::os::Bottle cmd, reply;
63  cmd.addString("listen");
64  cmd.addString("offShouldWait");
65  yDebug() << "[waitForEars] Listen sending command" << cmd.toString();
66  ears_port.write(cmd, reply);
67  yDebug() << "[waitForEars] Listen got reply" << reply.toString();
68  }
69  else {
70  yWarning() << "[waitForEars] No connection to ears available...";
71  }
72 }
73 
75  yarp::os::Bottle bMessenger, bReply;
76  bMessenger.addString("interrupt");
77  // send the message
78  portRPC.write(bMessenger, bReply);
79  if(bReply.get(1).asString() != "OK") {
80  yError() << "speechRecognizer was not interrupted";
81  yDebug() << "Reply from speechRecognizer:" << bReply.toString();
82  return false;
83  } else {
84  return true;
85  }
86 }
87 
88 yarp::os::Bottle icubclient::SubSystem_Recog::recogFromGrammar(std::string &sInput)
89 {
90  if (!yarp::os::Network::isConnected(portRPC.getName(), "/speechRecognizer/rpc")){
91  if (!yarp::os::Network::connect(portRPC.getName(), "/speechRecognizer/rpc")){
92  yarp::os::Bottle bReply;
93  bReply.addInt(0);
94  bReply.addString("recog not connected");
95  yWarning(" recog not connected");
96  return bReply;
97  }
98  }
99  // pause ears
100  enableEars(false);
101 
102  yarp::os::Bottle bMessenger;
103  yarp::os::Bottle bReply;
104  bMessenger.addString("recog");
105  bMessenger.addString("grammarXML");
106  bMessenger.addString(sInput);
107  portRPC.write(bMessenger, bReply);
108 
109  // resume ears
110  enableEars(true);
111 
112  return bReply;
113  // turn off the main grammar through ears
114 }
115 
116 yarp::os::Bottle icubclient::SubSystem_Recog::recogFromGrammarLoop(std::string sInput, int iLoop, bool keepEarsEnabled, bool keepEarsDisabledAfterRecog)
117 {
118  if (!yarp::os::Network::isConnected(portRPC.getName(), "/speechRecognizer/rpc")){
119  if (!yarp::os::Network::connect(portRPC.getName(), "/speechRecognizer/rpc")){
120  yarp::os::Bottle bReply;
121  bReply.addInt(0);
122  bReply.addString("recog not connected");
123  yWarning(" recog not connected");
124  return bReply;
125  }
126  }
127  std::ostringstream osError;
128  bool fGetaReply = false;
129  yarp::os::Bottle bMessenger, //to be send TO speech recog
130  bAnswer,
131  bReply, //response from speech recog without transfer information, including raw sentence
132  bOutput; // semantic information of the content of the recognition
133 
134  if (!keepEarsEnabled) {
135  enableEars(false);
137  waitForEars();
138  }
139 
140  bMessenger.clear();
141  bMessenger.addString("recog");
142  bMessenger.addString("grammarXML");
143  bMessenger.addString(sInput);
144 
145  int loop = 0;
146  if(iLoop == -1) { // loop indefinitely
147  loop = -3;
148  }
149 
150  // listen off
151 
152  while (!fGetaReply && loop < iLoop)
153  {
154  // send the message
155  portRPC.write(bMessenger, bReply);
156 
157  yInfo() << " Reply from Speech Recog : " << bReply.toString();
158 
159  if (bReply.toString() == "NACK" || bReply.size() < 2)
160  {
161  bOutput.addInt(0);
162  osError << "Check grammar";
163  bOutput.addString(osError.str());
164  yError() << " " << osError.str();
165  if (!keepEarsDisabledAfterRecog) enableEars(true);
166  return bOutput;
167  }
168  else if (bReply.get(0).toString() == "0")
169  {
170  bOutput.addInt(0);
171  osError << "Grammar not recognized";
172  bOutput.addString(osError.str());
173  yError() << " " << osError.str();
174  if (!keepEarsDisabledAfterRecog) enableEars(true);
175  return bOutput;
176  }
177  else if (bReply.get(0).toString() == "ACK")
178  {
179  if (bReply.get(1).toString() == "-1")
180  {
181  yError() << "Only found Garbage...";
182  yError() << "Check Why this happens";
183  }
184  else
185  {
186  yInfo() << "Sentence Acknowledged";
187  bAnswer = *bReply.get(1).asList();
188 
189  if (bAnswer.toString() != "" && !bAnswer.isNull())
190  {
191  fGetaReply = true;
192  bOutput.addInt(1);
193  bOutput.addList() = bAnswer;
194  }
195  }
196 
197  }
198  if (iLoop != -1)
199  loop++;
200  }
201 
202  if (!fGetaReply)
203  {
204  bOutput.addInt(0);
205  osError << "no vocal input";
206  bOutput.addString(osError.str());
207  yDebug() << osError.str();
208  }
209  if (!keepEarsDisabledAfterRecog) enableEars(true);
210  return bOutput;
211 }
std::string m_type
Definition: subSystem.h:47
yarp::os::Bottle recogFromGrammarLoop(std::string sInput, int iLoop=50, bool keepEarsEnabled=false, bool keepEarsDisabledAfterRecog=false)
From one grxml grammar, return the first non-empty sentence recognized can last for several timeout (...
yarp::os::RpcClient portRPC
Definition: subSystem.h:48
bool interruptSpeechRecognizer()
Interrupt the speech recognizer.
Abstract class to handle sub-systems of the icub-client.
Definition: subSystem.h:43
yarp::os::RpcClient ears_port
Port to /ears/rpc.
SubSystem_Recog(const std::string &masterName)
Default constructor.
yarp::os::Bottle recogFromGrammar(std::string &sInput)
From one grxml grammar, return the sentence recognized for one timeout This is not supported...
std::string m_masterName
Definition: subSystem.h:46
virtual void Close()
Clean up resources.
void enableEars(bool on)
Send a command to ears whether it should listen to the speechRecognizer.
void waitForEars()
Send a command to ears that it should stop listening to the speechRecognizer, and wait until it hands...
#define SUBSYSTEM_RECOG