19 #include <yarp/os/LogStream.h> 37 ICubClient::ICubClient(
const std::string &moduleName,
const std::string &context,
const std::string &clientConfigFile,
bool isRFVerbose)
39 yarp::os::ResourceFinder rfClient;
40 rfClient.setVerbose(isRFVerbose);
41 rfClient.setDefaultContext(context.c_str());
42 rfClient.setDefaultConfigFile(clientConfigFile.c_str());
43 rfClient.configure(0,
nullptr);
45 if (rfClient.check(
"robot"))
47 robot = rfClient.find(
"robot").asString();
48 yInfo(
"Robot name set to %s", robot.c_str());
53 yInfo(
"Robot name set to default, i.e. %s", robot.c_str());
57 Bottle defaultRangeMin; defaultRangeMin.fromString(
"-0.5 -0.3 -0.15");
58 Bottle defaultRangeMax; defaultRangeMax.fromString(
"-0.1 0.3 0.5");
59 Bottle *rangeMin = rfClient.find(
"reachingRangeMin").asList();
60 Bottle *rangeMax = rfClient.find(
"reachingRangeMax").asList();
61 if (rangeMin ==
nullptr) rangeMin =
new Bottle(defaultRangeMin);
62 if (rangeMax ==
nullptr) rangeMax =
new Bottle(defaultRangeMax);
63 xRangeMin = defaultRangeMin.get(0).asDouble(); xRangeMax = defaultRangeMax.get(0).asDouble();
64 yRangeMin = defaultRangeMin.get(1).asDouble(); yRangeMax = defaultRangeMax.get(1).asDouble();
65 zRangeMin = defaultRangeMin.get(2).asDouble(); zRangeMax = defaultRangeMax.get(2).asDouble();
70 string fullName = moduleName +
"/icubClient";
72 opc->isVerbose =
false;
75 if (Bottle* bSubsystems = rfClient.find(
"subsystems").asList())
77 for (
unsigned int s = 0; s < bSubsystems->size(); s++)
79 std::string currentSS = bSubsystems->get(s).asString();
80 yInfo() <<
"Trying to open subsystem : " << currentSS;
100 yError() <<
"Unknown subsystem!";
107 bool ICubClient::connectOPC(
const string &opcName)
109 bool isConnected = opc->connect(opcName);
112 yInfo() <<
"Connection to OPC: " << (isConnected ?
"successful" :
"failed");
117 bool ICubClient::connectSubSystems()
119 bool isConnected =
true;
120 for (map<string, SubSystem*>::iterator sIt = subSystems.begin(); sIt != subSystems.end(); sIt++)
122 yInfo() <<
"Connection to " << sIt->first <<
": ";
123 bool result = sIt->second->Connect();
124 yInfo() << (result ?
"successful" :
"failed");
125 isConnected &= result;
132 bool ICubClient::connect(
const string &opcName)
134 bool isConnected = connectOPC(opcName);
135 isConnected &= connectSubSystems();
140 void ICubClient::close()
145 yInfo() <<
"Terminating subsystems:";
146 for (map<string, SubSystem*>::iterator sIt = subSystems.begin(); sIt != subSystems.end(); sIt++)
148 yInfo() <<
"\t" << sIt->first;
149 sIt->second->Close();
161 ICubClient::~ICubClient() {
169 void ICubClient::updateAgent()
171 if (opc->isConnected())
173 if (this->icubAgent ==
nullptr)
175 icubAgent = opc->addOrRetrieveEntity<
Agent>(
"icub");
178 opc->update(this->icubAgent);
183 bool ICubClient::changeName(
Entity *e,
const std::string &newName) {
186 if (subSystems.find(
"agentDetector") == subSystems.end()) {
187 say(
"Could not change name of default partner of agentDetector");
188 yWarning() <<
"Could not change name of default partner of agentDetector";
189 opc->changeName(e, newName);
197 yError() <<
"Could not cast to SubSystem_agentDetector!";
203 opc->changeName(e, newName);
207 say(
"could not change default name of partner");
208 yError() <<
"[SubSystem_agentDetector] Could not change default name of partner";
216 if (subSystems.find(
"iol2opc") == subSystems.end()) {
217 say(
"iol2opc not reachable, did not change object name");
218 yWarning() <<
"iol2opc not reachable, did not change object name";
219 opc->changeName(e, newName);
226 string oldName = e->
name();
227 if (!sub_iol2opc || !sub_iol2opc->
changeName(oldName, newName)) {
228 yError() <<
"iol2opc did not change name successfuly";
229 say(
"iol2opc did not change name successfuly");
235 if (!opc->changeName(e, newName)) {
236 yError() <<
"Could not change name of entity";
237 say(
"Could not change name of entity");
245 void ICubClient::commitAgent()
247 if (opc->isConnected())
248 opc->commit(this->icubAgent);
251 std::string ICubClient::getPartnerName(
bool verbose)
253 string partnerName =
"";
254 list<shared_ptr<Entity>> lEntities = opc->EntitiesCacheCopy();
255 for (
auto& entity : lEntities) {
257 Agent* a =
dynamic_cast<Agent*
>(entity.get());
260 partnerName = a->
name();
262 yInfo() <<
"Partner found: name =" << partnerName;
269 yWarning() <<
"No partner present was found!";
274 yarp::sig::VectorOf<double> ICubClient::getPartnerBodypartLoc(std::string sBodypartName){
279 string partnerName = getPartnerName();
280 if (partnerName ==
""){
281 yWarning() <<
"[iCubclient] Called getPartnerBodypartLoc :No partner present was found: cannot look at his/her ";
286 if (
Agent *oPartner = dynamic_cast<Agent*>(opc->getEntity(partnerName,
true))){
287 if (oPartner->m_present == 1.0){
288 if (oPartner->m_body.m_parts.find(sBodypartName) != oPartner->m_body.m_parts.end()){
289 vLoc = oPartner->m_body.m_parts[sBodypartName];
290 yDebug() <<
"The bodypart " << sBodypartName <<
"of the agemt " << partnerName <<
" is at position " << vLoc.toString();
294 yError() <<
"[iCubClient] Called getPartnerBodypartLoc() on an unavalid bodypart (" << sBodypartName <<
")";
299 yError() <<
"[iCubClient] Called getPartnerBodypartLoc() on a non-present agent (" << partnerName <<
")";
307 bool ICubClient::lookAtPartner()
309 return look(getPartnerName());
312 bool ICubClient::babbling(
const string &bpName,
const string &babblingLimb,
double train_dur)
315 if (subSystems.find(
"babbling") != subSystems.end()){
318 Entity *target = opc->getEntity(bpName,
true);
321 yError() <<
"[iCubClient] Called babbling() on a unallowed entity: \"" << bpName <<
"\"";
328 if (jointNumber == -1){
329 yError() <<
"[iCubClient] Called babbling() on " << bpName <<
" which have no joint number linked to it\"";
333 return ((
SubSystem_babbling*)subSystems[
"babbling"])->babbling(jointNumber, babblingLimb, train_dur);
335 yError() <<
"Could not cast" << target <<
"to Bodypart";
340 yError() <<
"Error, babbling is not running...";
344 bool ICubClient::babbling(
int jointNumber,
const string &babblingLimb,
double train_dur)
346 if (subSystems.find(
"babbling") != subSystems.end())
347 return ((
SubSystem_babbling*)subSystems[
"babbling"])->babbling(jointNumber, babblingLimb, train_dur);
349 yError() <<
"Error, babbling is not running...";
354 std::tuple<std::string, double> ICubClient::getHighestEmotion()
356 double intensity = 0.0;
357 string emotionName =
"joy";
360 for (map<string, double>::iterator d = this->icubAgent->m_emotions_intrinsic.begin(); d != this->icubAgent->m_emotions_intrinsic.end(); d++)
363 if (d->second > intensity)
365 emotionName = d->first;
366 intensity = d->second;
370 return std::make_tuple(emotionName, intensity);
374 bool ICubClient::say(
const string &
text,
bool shouldWait)
376 if (subSystems.find(
"speech") == subSystems.end())
378 yError() <<
"Impossible, speech is not running...";
382 yDebug() <<
"iCub says" <<
text;
387 list<Action*> ICubClient::getKnownActions()
389 this->actionsKnown.clear();
391 for (list<Entity*>::iterator it = entities.begin(); it != entities.end(); it++)
393 actionsKnown.push_back(dynamic_cast<Action*>(*it));
399 list<Object*> ICubClient::getObjectsInSight()
401 list<Object*> inSight;
403 list<Entity*> allEntities = opc->EntitiesCache();
404 for (list<Entity*>::iterator it = allEntities.begin(); it != allEntities.end(); it++)
413 if (itemPosition[0] < 0)
414 inSight.push_back(dynamic_cast<Object*>(*it));
422 list<Object*> ICubClient::getObjectsInRange()
424 list<Object*> inRange;
426 list<Entity*> allEntities = opc->EntitiesCache();
427 for (list<Entity*>::iterator it = allEntities.begin(); it != allEntities.end(); it++)
435 if (isTargetInRange(itemPosition))
436 inRange.push_back(o);
444 bool ICubClient::isTargetInRange(
const Vector &target)
const 446 bool isIn = ((target[0] > xRangeMin) && (target[0] < xRangeMax) &&
447 (target[1] > yRangeMin) && (target[1]<yRangeMax) &&
448 (target[2]>zRangeMin) && (target[2] < zRangeMax));
bool changeDefaultName(const std::string &new_name)
Changes the name of the partner within agentDetector Internally used by ICubClient::changeName.
#define SUBSYSTEM_EMOTION
Abstract subSystem for speech management (Text to Speech)
Represent any entity that can be stored within the OPC.
SubSystem to deal with the actionsRenderingEngine module (a.k.a.
virtual bool isType(std::string _entityType) const
Test if an entity is inheriting a given type.
double m_present
Is the object present in the scene A value of 1.0 means that the object currently is in the scene A v...
yarp::sig::VectorOf< double > getSelfRelativePosition(const yarp::sig::VectorOf< double > &vInitialRoot) const
Express a point given in the initial ego-centered reference frame in respect to the object reference ...
void resume()
The skeleton detection of agentDetector will be resumed.
SubSystem to deal with the experimental affordance learning module (a.k.a.
Represent any physical entity (including objects and agents) that can be stored within the OPC...
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...
SubSystem for agentDetector.
int m_joint_number
Joint number of the represented body part.
#define SUBSYSTEM_AGENTDETECTOR
Represents a body part of the robot.
bool changeName(const std::string &old_name, const std::string &new_name)
Change the name of an object Internally used by ICubClient::changeName.
void pause()
Pause the agentDetector.
An OPC client using the datastructures defined within the icub-client library.
std::string name() const
Return the name of an entity (which has to be unique within the OPC)
Abstract subSystem for speech recognizer.
#define SUBSYSTEM_BABBLING
#define SUBSYSTEM_IOL2OPC
#define SUBSYSTEM_SPEECH_ESPEAK
std::string entity_type() const
Return the specific type of an entity.