icub-client
subSystem_emotion.cpp
Go to the documentation of this file.
1 #include <yarp/os/all.h>
2 #include <algorithm>
3 #include <map>
5 
7  if(yarp::os::Network::isConnected(portRPC.getName(), "/icub/face/emotions/in")) {
8  return true;
9  } else {
10  return yarp::os::Network::connect(portRPC.getName(), "/icub/face/emotions/in");
11  }
12 }
13 
14 icubclient::SubSystem_emotion::SubSystem_emotion(const std::string &masterName) : SubSystem(masterName) {
15  portRPC.open(("/" + m_masterName + "/emotion:rpc").c_str());
17 }
18 
20  portRPC.interrupt();
21  portRPC.close();
22 }
23 
24 bool icubclient::SubSystem_emotion::setEmotion(std::string emotion, std::string part) {
25  std::pair<std::string, std::string> emPair;
26 
27  // Converting inputs to lowercase
28  std::transform(emotion.begin(), emotion.end(), emotion.begin(), ::tolower);
29  std::transform(part.begin(), part.end(), part.begin(), ::tolower);
30 
31  if(mapToEmotion.find(emotion) != mapToEmotion.end() &&
32  mapToPart.find(part) != mapToPart.end())
33  {
34  emPair.first = mapToEmotion.find(emotion)->second;
35  emPair.second = mapToPart.find(part)->second;
36 
37  return sendEmotion(emPair);
38  }
39  else if(mapToEmotion.find(emotion) == mapToEmotion.end())
40  {
41  yError() << "Emotion " << emotion << " not found";
42  return false;
43  }
44  else if(mapToPart.find(part) == mapToPart.end())
45  {
46  yError() << "Part " << part << " not found";
47  return false;
48  }
49  else
50  {
51  yError() << "Uncaught error";
52  return false;
53  }
54 }
55 
56 bool icubclient::SubSystem_emotion::sendEmotion(std::pair<std::string, std::string> emPair) {
57  yarp::os::Bottle bReq, bResp;
58 
59  bReq.addString("set");
60  bReq.addString(emPair.second);
61  bReq.addString(emPair.first);
62 
63  yInfo()<<"Sending " << bReq.toString();
64 
65  portRPC.write(bReq, bResp);
66  if (bResp.toString() == "[ok]")
67  {
68  currentEmotionPair = emPair;
69  return true;
70  }
71  else
72  {
73  yError()<<bResp.toString();
74  return false;
75  }
76 }
77 
78 std::pair<std::string, std::string> icubclient::SubSystem_emotion::getEmotion()
79 {
80  return currentEmotionPair;
81 }
#define SUBSYSTEM_EMOTION
std::string m_type
Definition: subSystem.h:47
SubSystem_emotion(const std::string &masterName)
Default constructor.
yarp::os::RpcClient portRPC
Definition: subSystem.h:48
virtual void Close()
Clean up resources.
bool setEmotion(std::string emotion, std::string part="all")
Method to switch iCub emotion for different parts of face.
Abstract class to handle sub-systems of the icub-client.
Definition: subSystem.h:43
std::pair< std::string, std::string > getEmotion()
Method to get current iCub emotion.
std::string m_masterName
Definition: subSystem.h:46