ocra-recipes
Doxygen documentation for the ocra-recipes repository
ControllerClient.cpp
Go to the documentation of this file.
2 
3 using namespace ocra_recipes;
4 
5 int ControllerClient::CONTROLLER_CLIENT_COUNT = 0;
6 
8 : yarp::os::RateThread(DEFAULT_LOOP_PERIOD)
9 , isReady(false)
10 , expectedPeriod(DEFAULT_LOOP_PERIOD)
11 {
12 
13 }
14 
15 ControllerClient::ControllerClient(ocra::Model::Ptr derivedModelPtr, const int loopPeriod)
16 : yarp::os::RateThread(loopPeriod)
17 , model(derivedModelPtr)
18 , expectedPeriod(loopPeriod)
19 {
20  clientNumber = ++ControllerClient::CONTROLLER_CLIENT_COUNT;
21 
22  clientComs = std::make_shared<ClientCommunications>();
23  clientComs->open();
24 
25  std::string statesPort_Name = "/ControllerClient/"+ std::to_string(clientNumber) +"/states:i";
26 
27  statesPort.open(statesPort_Name.c_str());
28 
29  stateCallback = StateListener(model);
30 
31  statesPort.setReader(stateCallback);
32 
33  yarp.connect("/ControllerServer/states:o", statesPort_Name.c_str());
34 
35  isReady = true;
36 }
37 
39 {
40 
41 }
42 
44 {
45  clientThreadHasBeenReleased = false;
46  if(isReady)
47  return initialize();
48  else
49  return false;
50 }
51 
53 {
54  loop();
55 }
56 
58 {
59  release();
60  clientThreadHasBeenReleased = true;
61 }
62 
63 bool ControllerClient::removeTask(const std::string& taskName)
64 {
65  yarp::os::Bottle request;
66  request.addInt(REMOVE_TASK);
67  request.addString(taskName);
68 
69  return (clientComs->queryController(request).get(0).asInt() == SUCCESS);
70 }
71 
72 bool ControllerClient::removeTasks(const std::vector<std::string>& taskNameVector)
73 {
74  return false;
75 }
76 
77 
78 void ControllerClient::addTasks(const std::string& pathToXmlFile, bool overwrite)
79 {
81 
82  std::vector<ocra::TaskBuilderOptions> taskOptsVec = factory.parseTaskOptionsFromXml(pathToXmlFile);
83  std::vector<ocra::TaskBuilderOptions> addTaskOptsVec;
84  std::vector<ocra::TaskBuilderOptions> overwriteTaskOptsVec;
85  for (auto taskOpts : taskOptsVec) {
86  if(!checkIfTaskExists(taskOpts)) {
87  addTaskOptsVec.push_back(taskOpts);
88  } else {
89  if (overwrite) {
90  overwriteTaskOptsVec.push_back(taskOpts);
91  }
92  }
93  }
94 
95  yarp::os::Bottle request;
96  request.addInt(ADD_TASKS);
97  request.addInt(addTaskOptsVec.size());
98  for (auto taskOpts : addTaskOptsVec) {
99  taskOpts.putIntoBottle(request);
100  }
101  if(clientComs->queryController(request).get(0).asInt() != SUCCESS)
102  {
103  std::cout << "Didn't work." << std::endl;
104  }
105  // Code to send the tm opts to the server.
106 }
107 
109 {
110  std::vector<std::string> tmNames = getTaskNames();
111  for (auto name : tmNames) {
112  if (tmOpts.taskName == name) {
113  TaskConnection tCon(tmOpts.taskName);
114  if (tmOpts.taskType == tCon.getTaskTypeAsString()) {
115  return true;
116  }
117  else {
118  yLog.warning() << "You are trying to add a task with the same name as another task. I am gonna rename it for you big dummy.";
119  tmOpts.taskName += "_" + std::to_string(clientNumber);
120  return false;
121  }
122  }
123  }
124  return false;
125 }
126 
127 
128 std::vector<std::string> ControllerClient::getTaskTypes()
129 {
130  return clientComs->getTaskTypes();
131 }
132 
133 std::vector<std::string> ControllerClient::getTaskNames()
134 {
135  return clientComs->getTaskNames();
136 }
137 
138 bool ControllerClient::changeFixedLink(std::string newFixedLink, int isInLeftSupport, int isInRightSupport)
139 {
140  yarp::os::Bottle request;
141  if (!newFixedLink.compare("r_sole") || !newFixedLink.compare("right")) {
142  request.addInt(CHANGE_FIXED_LINK_RIGHT);
143  OCRA_INFO("Changed fixed link to right sole");
144  } else {
145  if (!newFixedLink.compare("l_sole") || !newFixedLink.compare("left")) {
146  request.addInt(CHANGE_FIXED_LINK_LEFT);
147  OCRA_INFO("Changed fixed link to left sole");
148  } else {
149  OCRA_ERROR("The new fixed link you specify is not supported yet. Please try r_sole, right, l_sole or left.");
150  return false;
151  }
152  }
153  OCRA_INFO("sending contact state (" << isInLeftSupport <<", " <<isInRightSupport << ")");
154  request.addInt(isInLeftSupport);
155  request.addInt(isInRightSupport);
156 
157 
158  if(clientComs->queryController(request).get(0).asInt() != SUCCESS)
159  {
160  OCRA_ERROR("Communication with ocra-icub-server didn't work. Requested change to right foot link");
161  return false;
162  }
163 
164  return true;
165 }
#define OCRA_ERROR(msg)
Definition: ErrorsHelper.h:32
void addTasks(const std::string &pathToXmlFile, bool overwrite)
ClientCommunications::Ptr clientComs
std::vector< std::string > getTaskNames()
std::vector< TaskBuilderOptions > parseTaskOptionsFromXml(const std::string &optionsXmlFilePath)
#define OCRA_INFO(msg)
Definition: ErrorsHelper.h:38
A callback for a port listing to robot states.
Definition: RobotState.h:57
bool checkIfTaskExists(ocra::TaskBuilderOptions &tmOpts)
bool removeTasks(const std::vector< std::string > &taskNameVector)
std::vector< std::string > getTaskTypes()
bool changeFixedLink(std::string newFixedLink, int isInLeftSupport, int isInRightSupport)
bool removeTask(const std::string &taskName)