ocra-recipes
Doxygen documentation for the ocra-recipes repository
TaskConnection.cpp
Go to the documentation of this file.
2 
3 using namespace ocra_recipes;
4 int TaskConnection::TASK_CONNECTION_COUNT = 0;
5 
7 {
8 
9 }
10 
11 TaskConnection::TaskConnection(const std::string& destinationTaskName)
12 : taskName(destinationTaskName)
13 , controlPortsAreOpen(false)
14 , firstUpdateOfTaskStateHasOccured(false)
15 , firstUpdateOfTaskDesiredStateHasOccured(false)
16 {
17  taskConnectionNumber = ++TaskConnection::TASK_CONNECTION_COUNT;
18 
19  // std::shared_ptr<ClientCommunications> ccComs = std::make_shared<ClientCommunications>();
20  // ccComs->open();
21  // taskRpcServerName = ccComs->getTaskPortName(taskName);
22  // ccComs->close();
23 
24  taskRpcServerName = "/Task/"+taskName+"/rpc:i";
25 
26 
27  while(yarp.exists(("/TaskConnection/"+std::to_string(taskConnectionNumber)+"/"+taskName+"/rpc:o")))
28  {
29  ++taskConnectionNumber;
30  }
31 
32 
33  this->taskRpcClientName = "/TaskConnection/"+std::to_string(taskConnectionNumber)+"/"+taskName+"/rpc:o";
34  this->taskRpcClient.open(taskRpcClientName);
35 
36  this->yarp.connect(taskRpcClientName, taskRpcServerName);
37 }
38 
40 {
41  this->taskRpcClient.close();
42  this->closeControlPorts();
43  --TaskConnection::TASK_CONNECTION_COUNT;
44 }
45 
47 {
48  yarp.disconnect(taskRpcClientName, taskRpcServerName);
49  if (controlPortsAreOpen) {
50  yarp.disconnect(taskOutputPortName, inputPortName);
51  yarp.disconnect(outputPortName, taskInputPortName);
52  }
53  if (yarp.isConnected(taskRpcClientName, taskRpcServerName)) {
54  OCRA_WARNING("The ports "<< taskRpcClientName << " and " << taskRpcServerName << " are still connected.")
55  } else {
56  OCRA_INFO("Disconnected "<< taskRpcClientName << " from " << taskRpcServerName)
57  }
58 }
59 
61 {
62  if (! this->yarp.isConnected(taskRpcClientName, taskRpcServerName)) {
63  OCRA_INFO("Reconnecting "<< taskRpcClientName << " to " << taskRpcServerName)
64  while (!this->yarp.connect(taskRpcClientName, taskRpcServerName)){yarp::os::Time::delay(0.01);}
65  } else {
66  OCRA_WARNING("The ports "<< taskRpcClientName << " and " << taskRpcServerName << " are already connected.")
67  }
68 }
69 
71 {
72  yarp::os::Bottle message, reply;
73  message.addInt(ocra::TASK_MESSAGE::ACTIVATE);
74  this->taskRpcClient.write(message, reply);
75  if(reply.get(0).asInt() != ocra::TASK_MESSAGE::OCRA_SUCCESS) {
76  yLog.error() << "Could not activate " << taskName;
77  return false;
78  }
79  return true;
80 }
81 
83 {
84  yarp::os::Bottle message, reply;
85  message.addInt(ocra::TASK_MESSAGE::DEACTIVATE);
86  this->taskRpcClient.write(message, reply);
87  if(reply.get(0).asInt() != ocra::TASK_MESSAGE::OCRA_SUCCESS) {
88  yLog.error() << "Could not deactivate " << taskName;
89  return false;
90  }
91  return true;
92 }
93 
95 {
96  yarp::os::Bottle message, reply;
98  this->taskRpcClient.write(message, reply);
99  return reply.get(0).asString();
100 }
101 
103 {
104  yarp::os::Bottle message, reply;
106  this->taskRpcClient.write(message, reply);
107  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::TASK_IS_ACTIVATED) {
108  return true;
109  } else {
110  return false;
111  }
112 }
113 
115 {
116  yarp::os::Bottle message, reply;
117  message.addInt(ocra::TASK_MESSAGE::GET_TASK_ERROR);
118  this->taskRpcClient.write(message, reply);
119  if(reply.get(0).asInt() != 0) {
120  int dummy;
121  return ocra::util::pourBottleIntoEigenVector(reply, dummy);
122  } else {
123  return Eigen::VectorXd::Zero(0);
124  }
125 }
126 
128 {
129  return this->getTaskError().norm();
130 }
131 
133 {
134  yarp::os::Bottle message, reply;
135  message.addInt(ocra::TASK_MESSAGE::SET_STIFFNESS);
136  message.addDouble(K);
137  this->taskRpcClient.write(message, reply);
138  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_FAILURE) {
139  yLog.error() << "setStiffness() did not work.";
140  }
141 }
142 
143 void TaskConnection::setStiffness(const Eigen::VectorXd& K)
144 {
145  yarp::os::Bottle message, reply;
148  this->taskRpcClient.write(message, reply);
149  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_FAILURE) {
150  yLog.error() << "setStiffness() did not work.";
151  }
152 }
153 
154 void TaskConnection::setStiffness(const Eigen::MatrixXd& K)
155 {
156  yarp::os::Bottle message, reply;
159  this->taskRpcClient.write(message, reply);
160  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_FAILURE) {
161  yLog.error() << "setStiffness() did not work.";
162  }
163 }
164 
166 {
167  return this->getStiffnessMatrix()(0,0);
168 }
169 
171 {
172  yarp::os::Bottle message, reply;
173  message.addInt(ocra::TASK_MESSAGE::GET_STIFFNESS);
174  this->taskRpcClient.write(message, reply);
175  if(reply.get(0).asInt() != 0) {
176  int dummy;
177  return ocra::util::pourBottleIntoEigenMatrix(reply, dummy);
178  } else {
179  return Eigen::MatrixXd::Zero(0,0);
180  }
181 }
182 
184 {
185  yarp::os::Bottle message, reply;
186  message.addInt(ocra::TASK_MESSAGE::SET_DAMPING);
187  message.addDouble(B);
188  this->taskRpcClient.write(message, reply);
189  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_FAILURE) {
190  yLog.error() << "setDamping() did not work.";
191  }
192 }
193 
194 void TaskConnection::setDamping(const Eigen::VectorXd& B)
195 {
196  yarp::os::Bottle message, reply;
199  this->taskRpcClient.write(message, reply);
200  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_FAILURE) {
201  yLog.error() << "setDamping() did not work.";
202  }
203 }
204 
205 void TaskConnection::setDamping(const Eigen::MatrixXd& B)
206 {
207  yarp::os::Bottle message, reply;
210  this->taskRpcClient.write(message, reply);
211  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_FAILURE) {
212  yLog.error() << "setDamping() did not work.";
213  }
214 }
215 
217 {
218  return this->getDampingMatrix()(0,0);
219 }
220 
222 {
223  yarp::os::Bottle message, reply;
224  message.addInt(ocra::TASK_MESSAGE::GET_DAMPING);
225  this->taskRpcClient.write(message, reply);
226  if(reply.get(0).asInt() != 0) {
227  int dummy;
228  return ocra::util::pourBottleIntoEigenMatrix(reply, dummy);
229  } else {
230  return Eigen::MatrixXd::Zero(0,0);
231  }
232 }
233 
234 void TaskConnection::setWeight(double weight)
235 {
236  yarp::os::Bottle message, reply;
237  message.addInt(ocra::TASK_MESSAGE::SET_WEIGHT);
238  message.addDouble(weight);
239  this->taskRpcClient.write(message, reply);
240  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_FAILURE) {
241  yLog.error() << "setDamping() did not work.";
242  }
243 }
244 
245 void TaskConnection::setWeight(Eigen::VectorXd& weights)
246 {
247  yarp::os::Bottle message, reply;
249  ocra::util::pourEigenVectorIntoBottle(weights, message);
250  this->taskRpcClient.write(message, reply);
251  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_FAILURE) {
252  yLog.error() << "setDamping() did not work.";
253  }
254 }
255 
256 Eigen::VectorXd TaskConnection::getWeight()
257 {
258  yarp::os::Bottle message, reply;
259  message.addInt(ocra::TASK_MESSAGE::GET_WEIGHTS);
260  this->taskRpcClient.write(message, reply);
261  if(reply.get(0).asInt() != 0) {
262  int dummy;
263  return ocra::util::pourBottleIntoEigenVector(reply, dummy);
264  } else {
265  return Eigen::VectorXd::Zero(0);
266  }
267 }
268 
270 {
271  yarp::os::Bottle message, reply;
272  message.addInt(ocra::TASK_MESSAGE::GET_DIMENSION);
273  this->taskRpcClient.write(message, reply);
274  return reply.get(0).asInt();
275 }
276 
278 {
279  yarp::os::Bottle message, reply;
280  message.addInt(ocra::TASK_MESSAGE::GET_TYPE);
281  this->taskRpcClient.write(message, reply);
282  return ocra::Task::META_TASK_TYPE(reply.get(0).asInt());
283 }
284 
286 {
287  yarp::os::Bottle message, reply;
289  this->taskRpcClient.write(message, reply);
290  return reply.get(0).asString();
291 }
292 
294 {
295  if (this->controlPortsAreOpen && firstUpdateOfTaskStateHasOccured) {
296  return this->currentState;
297  } else {
298  yarp::os::Bottle message, reply;
299  ocra::TaskState state;
300  message.addInt(ocra::TASK_MESSAGE::GET_TASK_STATE);
301  this->taskRpcClient.write(message, reply);
302  int dummy;
303  state.extractFromBottle(reply, dummy);
304  return state;
305  }
306 }
307 
309 {
310 
311  if (this->controlPortsAreOpen && firstUpdateOfTaskDesiredStateHasOccured) {
312  return currentDesiredState;
313  } else {
314  ocra::TaskState state;
315  int dummy;
316  yarp::os::Bottle message, reply;
318  this->taskRpcClient.write(message, reply);
319  state.extractFromBottle(reply, dummy);
320  return state;
321  }
322 
323 }
324 
325 void TaskConnection::setDesiredTaskState(const ocra::TaskState& newDesiredTaskState)
326 {
327  yarp::os::Bottle message, reply;
329  newDesiredTaskState.putIntoBottle(message);
330  this->taskRpcClient.write(message, reply);
331 }
332 
334 {
335  if(controlPortsAreOpen){
336  yarp::os::Bottle bottle;
337  newDesiredTaskState.putIntoBottle(bottle);
338  outputPort.write(bottle);
339  } else {
340  std::cout << "Can't use this method until the control ports have been opened." << std::endl;
341  }
342 }
343 
345 {
346 
347 }
348 
350 {
351 
352 }
353 
355 {
356 
357 }
358 
360 {
361 
362 }
363 
365 {
366 
367 }
368 
370 {
371 
372 }
373 
375 {
376 
377 }
378 
380 {
381 
382 }
383 
385 {
386 
387 }
388 
390 {
391  bool portsConnected = true;
392 
393  yarp::os::Bottle message, reply;
395  this->taskRpcClient.write(message, reply);
396  if ( (reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_SUCCESS) && connect) {
397  message.clear();
398  reply.clear();
400  this->taskRpcClient.write(message, reply);
401  this->taskInputPortName = reply.get(0).asString();
402  this->taskOutputPortName = reply.get(1).asString();
403  this->taskDesiredStateOutputPortName = reply.get(2).asString();
404 
405  this->inputPortName = "/TaskConnection/"+std::to_string(taskConnectionNumber)+"/"+this->taskName+":i";
406  this->desiredStateInputPortName = "/TaskConnection/"+std::to_string(taskConnectionNumber)+"/"+this->taskName+"/desired_state:i";
407  this->outputPortName = "/TaskConnection/"+std::to_string(taskConnectionNumber)+"/"+this->taskName+":o";
408 
409  portsConnected &= inputPort.open(this->inputPortName);
410  portsConnected &= desiredStateInputPort.open(this->desiredStateInputPortName);
411  portsConnected &= outputPort.open(this->outputPortName);
412 
413  this->inpCallback = std::make_shared<InputCallback>(*this);
414  this->desiredStateInputCallback = std::make_shared<DesiredStateInputCallback>(*this);
415  inputPort.setReader(*(this->inpCallback));
416  desiredStateInputPort.setReader(*(this->desiredStateInputCallback));
417 
418 
419  if (portsConnected) {
420  portsConnected &= yarp.connect(this->taskOutputPortName, this->inputPortName);
421  portsConnected &= yarp.connect(this->taskDesiredStateOutputPortName, this->desiredStateInputPortName);
422  portsConnected &= yarp.connect(this->outputPortName, this->taskInputPortName);
423 
424  this->controlPortsAreOpen = portsConnected;
425  if (!this->controlPortsAreOpen) {
426  OCRA_ERROR("Could not open the control ports!")
427  }
428  } else {
429  return false;
430  }
431  } else {
432  return false;
433  }
434 
435  return portsConnected;
436 }
437 
439 {
440  if (controlPortsAreOpen) {
441  return taskOutputPortName;
442  } else {
443  return "";
444  }
445 }
447 {
448  if (controlPortsAreOpen) {
449  return taskInputPortName;
450  } else {
451  return "";
452  }
453 }
454 
456 {
457  if (this->controlPortsAreOpen) {
458  yarp::os::Bottle message, reply;
460  this->taskRpcClient.write(message, reply);
461  if(reply.get(0).asInt() == ocra::TASK_MESSAGE::OCRA_SUCCESS) {
462  this->inputPort.close();
463  this->outputPort.close();
464  this->controlPortsAreOpen = false;
465  return true;
466  } else {
467  return false;
468  }
469  } else {
470  return false;
471  }
472 
473 }
474 
475 void TaskConnection::queryTask(ocra::TASK_MESSAGE tag, yarp::os::Bottle& reply)
476 {
477  yarp::os::Bottle message;
478  message.addInt(tag);
479  this->taskRpcClient.write(message, reply);
480 }
481 
482 
483 /**************************************************************************************************
484  Nested PortReader Class
485 **************************************************************************************************/
487 : tcRef(_tcRef)
488 {
489  //do nothing
490 }
491 
492 bool TaskConnection::InputCallback::read(yarp::os::ConnectionReader& connection)
493 {
494  yarp::os::Bottle input;
495  if (input.read(connection)){
496  tcRef.parseInput(input);
497  return true;
498  }
499  else{
500  return false;
501  }
502 }
503 /**************************************************************************************************
504 **************************************************************************************************/
505 
506 /**************************************************************************************************
507  Nested PortReader Class
508 **************************************************************************************************/
510 : tcRef(_tcRef)
511 {
512  //do nothing
513 }
514 
515 bool TaskConnection::DesiredStateInputCallback::read(yarp::os::ConnectionReader& connection)
516 {
517  yarp::os::Bottle input;
518  if (input.read(connection)){
519  tcRef.parseDesiredStateInput(input);
520  return true;
521  }
522  else{
523  return false;
524  }
525 }
526 /**************************************************************************************************
527 **************************************************************************************************/
528 
529 void TaskConnection::parseInput(yarp::os::Bottle& input)
530 {
531  int dummy;
532  if(!firstUpdateOfTaskStateHasOccured) {
533  firstUpdateOfTaskStateHasOccured = true;
534  }
535  this->currentState.extractFromBottle(input, dummy);
536 }
537 
538 void TaskConnection::parseDesiredStateInput(yarp::os::Bottle& input)
539 {
540  int dummy;
541  if(!firstUpdateOfTaskDesiredStateHasOccured) {
542  firstUpdateOfTaskDesiredStateHasOccured = true;
543  }
544  this->currentDesiredState.extractFromBottle(input, dummy);
545 }
#define OCRA_WARNING(msg)
Definition: ErrorsHelper.h:35
bool openControlPorts(bool connect=true)
Eigen::Twistd getTaskFrameVelocity()
ocra::Task::META_TASK_TYPE getTaskType()
#define OCRA_ERROR(msg)
Definition: ErrorsHelper.h:32
Eigen::Twistd getTaskFrameAcceleration()
Eigen::Rotation3d getTaskFrameOrientation()
void putIntoBottle(yarp::os::Bottle &bottle) const
Definition: TaskState.cpp:230
ocra::TaskState getDesiredTaskState()
bool extractFromBottle(const yarp::os::Bottle &bottle, int &sizeOfOptions)
Definition: TaskState.cpp:174
Eigen::MatrixXd pourBottleIntoEigenMatrix(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:92
#define OCRA_INFO(msg)
Definition: ErrorsHelper.h:38
Eigen::Vector3d getTaskFrameAngularVelocity()
void setDesiredTaskState(const ocra::TaskState &newDesiredTaskState)
void pourEigenMatrixIntoBottle(const Eigen::MatrixXd &mat, yarp::os::Bottle &bottle)
META_TASK_TYPE
Definition: Task.h:44
virtual bool read(yarp::os::ConnectionReader &connection)
Eigen::Vector3d getTaskFrameAngularAcceleration()
Eigen::MatrixXd getStiffnessMatrix()
void setDesiredTaskStateDirect(const ocra::TaskState &newDesiredTaskState)
Eigen::Vector3d getTaskFramePosition()
Eigen::Vector3d getTaskFrameLinearAcceleration()
virtual bool read(yarp::os::ConnectionReader &connection)
void pourEigenVectorIntoBottle(const Eigen::VectorXd &vec, yarp::os::Bottle &bottle)
void setWeight(double weight)
Eigen::Vector3d getTaskFrameLinearVelocity()
Eigen::MatrixXd getDampingMatrix()
Eigen::VectorXd pourBottleIntoEigenVector(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:27
void queryTask(ocra::TASK_MESSAGE tag, yarp::os::Bottle &bottle)
Eigen::Displacementd getTaskFrameDisplacement()