4 int TaskConnection::TASK_CONNECTION_COUNT = 0;
12 : taskName(destinationTaskName)
13 , controlPortsAreOpen(false)
14 , firstUpdateOfTaskStateHasOccured(false)
15 , firstUpdateOfTaskDesiredStateHasOccured(false)
17 taskConnectionNumber = ++TaskConnection::TASK_CONNECTION_COUNT;
24 taskRpcServerName =
"/Task/"+taskName+
"/rpc:i";
27 while(yarp.exists((
"/TaskConnection/"+std::to_string(taskConnectionNumber)+
"/"+taskName+
"/rpc:o")))
29 ++taskConnectionNumber;
33 this->taskRpcClientName =
"/TaskConnection/"+std::to_string(taskConnectionNumber)+
"/"+taskName+
"/rpc:o";
34 this->taskRpcClient.open(taskRpcClientName);
36 this->yarp.connect(taskRpcClientName, taskRpcServerName);
41 this->taskRpcClient.close();
43 --TaskConnection::TASK_CONNECTION_COUNT;
48 yarp.disconnect(taskRpcClientName, taskRpcServerName);
49 if (controlPortsAreOpen) {
50 yarp.disconnect(taskOutputPortName, inputPortName);
51 yarp.disconnect(outputPortName, taskInputPortName);
53 if (yarp.isConnected(taskRpcClientName, taskRpcServerName)) {
54 OCRA_WARNING(
"The ports "<< taskRpcClientName <<
" and " << taskRpcServerName <<
" are still connected.")
56 OCRA_INFO(
"Disconnected "<< taskRpcClientName <<
" from " << taskRpcServerName)
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);}
66 OCRA_WARNING(
"The ports "<< taskRpcClientName <<
" and " << taskRpcServerName <<
" are already connected.")
72 yarp::os::Bottle message, reply;
74 this->taskRpcClient.write(message, reply);
76 yLog.error() <<
"Could not activate " << taskName;
84 yarp::os::Bottle message, reply;
86 this->taskRpcClient.write(message, reply);
88 yLog.error() <<
"Could not deactivate " << taskName;
96 yarp::os::Bottle message, reply;
98 this->taskRpcClient.write(message, reply);
99 return reply.get(0).asString();
104 yarp::os::Bottle message, reply;
106 this->taskRpcClient.write(message, reply);
116 yarp::os::Bottle message, reply;
118 this->taskRpcClient.write(message, reply);
119 if(reply.get(0).asInt() != 0) {
123 return Eigen::VectorXd::Zero(0);
134 yarp::os::Bottle message, reply;
136 message.addDouble(K);
137 this->taskRpcClient.write(message, reply);
139 yLog.error() <<
"setStiffness() did not work.";
145 yarp::os::Bottle message, reply;
148 this->taskRpcClient.write(message, reply);
150 yLog.error() <<
"setStiffness() did not work.";
156 yarp::os::Bottle message, reply;
159 this->taskRpcClient.write(message, reply);
161 yLog.error() <<
"setStiffness() did not work.";
172 yarp::os::Bottle message, reply;
174 this->taskRpcClient.write(message, reply);
175 if(reply.get(0).asInt() != 0) {
179 return Eigen::MatrixXd::Zero(0,0);
185 yarp::os::Bottle message, reply;
187 message.addDouble(B);
188 this->taskRpcClient.write(message, reply);
190 yLog.error() <<
"setDamping() did not work.";
196 yarp::os::Bottle message, reply;
199 this->taskRpcClient.write(message, reply);
201 yLog.error() <<
"setDamping() did not work.";
207 yarp::os::Bottle message, reply;
210 this->taskRpcClient.write(message, reply);
212 yLog.error() <<
"setDamping() did not work.";
223 yarp::os::Bottle message, reply;
225 this->taskRpcClient.write(message, reply);
226 if(reply.get(0).asInt() != 0) {
230 return Eigen::MatrixXd::Zero(0,0);
236 yarp::os::Bottle message, reply;
238 message.addDouble(weight);
239 this->taskRpcClient.write(message, reply);
241 yLog.error() <<
"setDamping() did not work.";
247 yarp::os::Bottle message, reply;
250 this->taskRpcClient.write(message, reply);
252 yLog.error() <<
"setDamping() did not work.";
258 yarp::os::Bottle message, reply;
260 this->taskRpcClient.write(message, reply);
261 if(reply.get(0).asInt() != 0) {
265 return Eigen::VectorXd::Zero(0);
271 yarp::os::Bottle message, reply;
273 this->taskRpcClient.write(message, reply);
274 return reply.get(0).asInt();
279 yarp::os::Bottle message, reply;
281 this->taskRpcClient.write(message, reply);
287 yarp::os::Bottle message, reply;
289 this->taskRpcClient.write(message, reply);
290 return reply.get(0).asString();
295 if (this->controlPortsAreOpen && firstUpdateOfTaskStateHasOccured) {
296 return this->currentState;
298 yarp::os::Bottle message, reply;
301 this->taskRpcClient.write(message, reply);
311 if (this->controlPortsAreOpen && firstUpdateOfTaskDesiredStateHasOccured) {
312 return currentDesiredState;
316 yarp::os::Bottle message, reply;
318 this->taskRpcClient.write(message, reply);
327 yarp::os::Bottle message, reply;
330 this->taskRpcClient.write(message, reply);
335 if(controlPortsAreOpen){
336 yarp::os::Bottle bottle;
338 outputPort.write(bottle);
340 std::cout <<
"Can't use this method until the control ports have been opened." << std::endl;
391 bool portsConnected =
true;
393 yarp::os::Bottle message, reply;
395 this->taskRpcClient.write(message, reply);
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();
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";
409 portsConnected &= inputPort.open(this->inputPortName);
410 portsConnected &= desiredStateInputPort.open(this->desiredStateInputPortName);
411 portsConnected &= outputPort.open(this->outputPortName);
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));
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);
424 this->controlPortsAreOpen = portsConnected;
425 if (!this->controlPortsAreOpen) {
426 OCRA_ERROR(
"Could not open the control ports!")
435 return portsConnected;
440 if (controlPortsAreOpen) {
441 return taskOutputPortName;
448 if (controlPortsAreOpen) {
449 return taskInputPortName;
457 if (this->controlPortsAreOpen) {
458 yarp::os::Bottle message, reply;
460 this->taskRpcClient.write(message, reply);
462 this->inputPort.close();
463 this->outputPort.close();
464 this->controlPortsAreOpen =
false;
477 yarp::os::Bottle message;
479 this->taskRpcClient.write(message, reply);
494 yarp::os::Bottle input;
495 if (input.read(connection)){
496 tcRef.parseInput(input);
517 yarp::os::Bottle input;
518 if (input.read(connection)){
519 tcRef.parseDesiredStateInput(input);
529 void TaskConnection::parseInput(yarp::os::Bottle& input)
532 if(!firstUpdateOfTaskStateHasOccured) {
533 firstUpdateOfTaskStateHasOccured =
true;
535 this->currentState.extractFromBottle(input, dummy);
538 void TaskConnection::parseDesiredStateInput(yarp::os::Bottle& input)
541 if(!firstUpdateOfTaskDesiredStateHasOccured) {
542 firstUpdateOfTaskDesiredStateHasOccured =
true;
544 this->currentDesiredState.extractFromBottle(input, dummy);
ocra::TaskState getTaskState()
std::string getTaskTypeAsString()
#define OCRA_WARNING(msg)
bool openControlPorts(bool connect=true)
Eigen::Twistd getTaskFrameVelocity()
ocra::Task::META_TASK_TYPE getTaskType()
std::string getTaskOutputPortName()
Eigen::Twistd getTaskFrameAcceleration()
Eigen::Rotation3d getTaskFrameOrientation()
void putIntoBottle(yarp::os::Bottle &bottle) const
ocra::TaskState getDesiredTaskState()
bool extractFromBottle(const yarp::os::Bottle &bottle, int &sizeOfOptions)
Eigen::VectorXd getWeight()
void setStiffness(double K)
Eigen::MatrixXd pourBottleIntoEigenMatrix(yarp::os::Bottle bottle, int &indexesToSkip)
Eigen::Vector3d getTaskFrameAngularVelocity()
void setDesiredTaskState(const ocra::TaskState &newDesiredTaskState)
void pourEigenMatrixIntoBottle(const Eigen::MatrixXd &mat, yarp::os::Bottle &bottle)
Eigen::Vector3d getTaskFrameAngularAcceleration()
Eigen::MatrixXd getStiffnessMatrix()
std::string getPortName()
void setDesiredTaskStateDirect(const ocra::TaskState &newDesiredTaskState)
Eigen::Vector3d getTaskFramePosition()
Eigen::Vector3d getTaskFrameLinearAcceleration()
void pourEigenVectorIntoBottle(const Eigen::VectorXd &vec, yarp::os::Bottle &bottle)
void setWeight(double weight)
Eigen::Vector3d getTaskFrameLinearVelocity()
virtual ~TaskConnection()
Eigen::MatrixXd getDampingMatrix()
Eigen::VectorXd pourBottleIntoEigenVector(yarp::os::Bottle bottle, int &indexesToSkip)
std::string getTaskInputPortName()
void setDamping(double B)
void queryTask(ocra::TASK_MESSAGE tag, yarp::os::Bottle &bottle)
Eigen::Displacementd getTaskFrameDisplacement()
double getTaskErrorNorm()
Eigen::VectorXd getTaskError()