14 , controlPortsOpen(false)
17 , numberOfOpenRequests(0)
20 portName =
"/Task/"+task->getName()+
"/rpc:i";
21 rpcCallback = std::make_shared<RpcMessageCallback>(*this);
22 rpcPort.open(portName.c_str());
23 rpcPort.setReader(*rpcCallback);
30 std::cout <<
"\t--> Closing ports" << std::endl;
39 return task->getHierarchyLevel();
44 task->setHierarchyLevel(level);
55 yLog.error() <<
"No valid task(s) to activate.";
69 task->activateAsObjective();
75 task->activateAsConstraint();
81 yLog.error() <<
"Task, " << task->getName() <<
", has not been defined as an objective or constraint therefore I can't activate it.";
103 yLog.error() <<
"No valid task(s) to activate.";
110 if (task->isActiveAsObjective()) {
112 }
else if (task->isActiveAsConstraint()) {
122 return task->isActiveAsObjective() || task->isActiveAsConstraint();
132 return task->getError();
134 return Eigen::VectorXd::Zero(0);
146 task->setStiffness(K);
148 std::cout <<
"\n\n\n FUUUUUUCK" << std::endl;
155 task->setStiffness(K);
162 task->setStiffness(K);
172 yLog.error() <<
"Unable to get the stiffness of the task because it does not exist. Returning 0.0";
180 return task->getStiffness();
182 yLog.error() <<
"Unable to get the stiffness of the task because it does not exist. Returning empty matrix";
183 return Eigen::MatrixXd::Zero(0,0);
214 yLog.error() <<
"Unable to get the damping coeff of the task because it does not exist. Returning 0.0";
222 return task->getDamping();
224 yLog.error() <<
"Unable to get the damping coeffs of the task because it does not exist. Returning empty matrix";
225 return Eigen::MatrixXd::Zero(0,0);
232 return task->setWeight(weight);
239 return task->setWeight(weights);
246 return task->getWeight();
248 yLog.error() <<
"Unable to get the weights of the task because it does not exist. Returning empty vector";
249 return Eigen::VectorXd::Zero(0);
258 TaskState tmp = this->task->getTaskState();
265 return this->task->getDesiredTaskState();
270 return this->task->setDesiredTaskState(newDesiredTaskState);
275 return this->task->setDesiredTaskStateDirect(newDesiredTaskState);
280 stateOutBottle.clear();
282 outputControlPort.write(stateOutBottle);
284 desiredStateOutBottle.clear();
286 desiredStateOutputPort.write(desiredStateOutBottle);
289 bool TaskYarpInterface::openControlPorts()
291 ++numberOfOpenRequests;
293 if (!controlPortsOpen)
295 inputControlPortName =
"/Task/"+task->getName()+
"/state:i";
296 outputControlPortName =
"/Task/"+task->getName()+
"/state:o";
297 desiredStateOutputPortName =
"/Task/"+task->getName()+
"/desired_state:o";
299 res = res && inputControlPort.open(inputControlPortName.c_str());
300 res = res && outputControlPort.open(outputControlPortName.c_str());
301 res = res && desiredStateOutputPort.open(desiredStateOutputPortName.c_str());
303 controlPortsOpen = res;
306 OCRA_WARNING(
"You already opened the control ports. No need to do so again.")
309 if (!controlCallback) {
310 controlCallback = std::make_shared<ControlInputCallback>(*this);
311 inputControlPort.setReader(*controlCallback);
314 stateThread = std::make_shared<StateUpdateThread>(10, *
this);
316 if (!stateThread->isRunning()) {
317 stateThread->start();
323 bool TaskYarpInterface::closeControlPorts()
325 --numberOfOpenRequests;
326 if (numberOfOpenRequests==0) {
327 std::cout<<
"TaskYarpInterface::closeControlPorts() - " << std::endl;
329 std::cout<<
"Check if StateThreadis running"<<std::endl;
330 if (stateThread->isRunning()) {
331 std::cout<<
"Telling stateThread stop! motherfucker!!! "<< std::endl;
335 if (controlPortsOpen) {
336 inputControlPort.close();
337 outputControlPort.close();
338 desiredStateOutputPort.close();
341 bool controlPortsClosed = !inputControlPort.isOpen() && !outputControlPort.isOpen() && !desiredStateOutputPort.isOpen();
343 controlPortsOpen = !controlPortsClosed;
345 return controlPortsClosed;
347 OCRA_WARNING(
"There are still other TaskConnection occurences using these ports so I am keeping them open... Muthafucka.")
353 bool TaskYarpInterface::parseControlInput(yarp::os::Bottle& input)
433 yarp::os::Bottle input;
434 yarp::os::Bottle reply;
436 if (!input.read(connection)){
440 tmBase.parseIncomingMessage(input, reply);
441 yarp::os::ConnectionWriter* returnToSender = connection.getWriter();
442 if (returnToSender!=NULL) {
443 if (!reply.write(*returnToSender)) {
444 OCRA_ERROR(
"Reply was not successfully written");
468 if (input.read(connection)) {
469 return tmBase.parseControlInput(input);
488 std::cout <<
"StateUpdateThread: Opening.\n";
497 std::cout <<
"StateUpdateThread: Closing.\n";
502 void TaskYarpInterface::parseIncomingMessage(yarp::os::Bottle& input, yarp::os::Bottle& reply)
504 int btlSize = input.size();
505 for (
auto i=0; i<btlSize; ++i) {
507 int testInt = input.get(0).asInt();
508 std::string testStr = input.get(0).asString();
509 if ((testStr==
"") && (testInt!=0)) {
519 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_TASK_STATE";
521 TaskState state = this->task->getTaskState();
528 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_STIFFNESS";
536 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_DAMPING";
544 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_WEIGHTS";
552 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_DESIRED_TASK_STATE";
554 TaskState state = this->task->getDesiredTaskState();
561 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_TASK_POSITION";
563 if ((this->task->getMetaTaskType()==Task::META_TASK_TYPE::POSITION) || (this->task->getMetaTaskType()==Task::META_TASK_TYPE::COM)) {
564 Eigen::Vector3d pos = this->task->getTaskState().getPosition().getTranslation();
572 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_DESIRED_TASK_POSITION";
574 if ((this->task->getMetaTaskType()==Task::META_TASK_TYPE::POSITION) || (this->task->getMetaTaskType()==Task::META_TASK_TYPE::COM)) {
575 Eigen::Vector3d pos = this->task->getDesiredTaskState().getPosition().getTranslation();
583 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_ACTIVITY_STATUS";
596 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_DIMENSION";
598 OCRA_WARNING(
"this->task->getWeight() " << this->task->getWeight().size())
599 reply.addInt(this->task->getWeight().size());
605 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_TYPE";
607 reply.addInt(this->task->getMetaTaskType());
613 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_TYPE";
615 reply.addString(this->task->getMetaTaskTypeAsString());
621 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_NAME";
623 reply.addString(this->task->getName());
629 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_CONTROL_PORT_NAMES";
631 reply.addString(this->inputControlPortName);
632 reply.addString(this->outputControlPortName);
633 reply.addString(this->desiredStateOutputPortName);
639 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_TASK_PORT_NAME";
647 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: GET_TASK_ERROR";
655 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_STIFFNESS";
665 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_STIFFNESS_VECTOR";
676 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_STIFFNESS_MATRIX";
687 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_DAMPING";
697 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_DAMPING_VECTOR";
708 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_DAMPING_MATRIX";
719 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_WEIGHT";
722 this->
setWeight(input.get(i).asDouble());
729 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_WEIGHT_VECTOR";
740 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_DESIRED_TASK_STATE";
747 if (extractSuccess) {
760 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: SET_DESIRED_TASK_POSITION";
762 if (((this->task->getMetaTaskType()==Task::META_TASK_TYPE::POSITION) || (this->task->getMetaTaskType()==Task::META_TASK_TYPE::COM)) && (input.size()>=4)){
763 Eigen::Vector3d pos(input.get(i+1).asDouble(), input.get(i+2).asDouble(), input.get(i+3).asDouble());
765 state.
setPosition(Eigen::Displacementd(pos, Eigen::Rotation3d::Identity()));
778 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: ACTIVATE";
790 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: DEACTIVATE";
802 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: OPEN_CONTROL_PORTS";
804 if(openControlPorts()) {
814 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: CLOSE_CONTROL_PORTS";
816 if(closeControlPorts()) {
826 yLog.info() <<
" ["<< this->task->getName() <<
"]: " <<
"Processing request: HELP";
833 yLog.warning() <<
"Unrecognized request tag.";
843 std::string TaskYarpInterface::printValidMessageTags()
845 std::string helpString =
"\n=== Valid message tags are: ===\n";
846 helpString +=
"setStiffness: Allows you to set the Kp gain. Expects 1 double value.\n";
847 helpString +=
"setDamping: Allows you to set the Kd gain. Expects 1 double value.\n";
848 helpString +=
"setWeight: Allows you to set the task weight. Expects 1 double value.\n";
849 helpString +=
"setDesired: Allows you to set the desired task reference. Expects nDoF double values where nDoF is the task dimension.\n";
850 helpString +=
"getCurrentState: Allows you to get the current state of the task. No arguments expected.\n";
851 helpString +=
"getStiffness: Allows you to get the Kp gain. No arguments expected.\n";
852 helpString +=
"getDamping: Allows you to get the Kd gain. No arguments expected.\n";
853 helpString +=
"getWeight: Allows you to get the task weight. No arguments expected.\n";
854 helpString +=
"getDesired: Allows you to get the desired task reference. No arguments expected.\n";
855 helpString +=
"getActivityStatus: Queries the activity status of the task. No arguments expected.\n";
856 helpString +=
"activate: Allows you to activate the task. No arguments expected.\n";
857 helpString +=
"deactivate: Allows you to deactivate the task. No arguments expected.\n";
858 helpString +=
"getDimension: Prints the state dimension. No arguments expected.\n";
859 helpString +=
"getType: Retrieve the Type of the task. No arguments expected.\n";
860 helpString +=
"getName: Retrieve the Name of the task. No arguments expected.\n";
861 helpString +=
"openControlPorts: Open up high speed yarp control ports.\n";
862 helpString +=
"closeControlPorts: Close high speed yarp control ports.\n";
863 helpString +=
"getControlPortNames: Get the names of the high speed yarp control ports.\n";
864 helpString +=
"help: Prints all the valid commands. No arguments expected.\n";
866 helpString +=
"\nTypical usage: [message tag] [message value(s)]\ne.g. >> stiffness 20 damping 10 desired_state 1.0 2.0 2.0\n";
Eigen::Vector3d getTaskFramePosition()
StateUpdateThread(int period, TaskYarpInterface &tmBaseRef)
Eigen::VectorXd getWeight()
void setPosition(const Eigen::Displacementd &newPosition)
Eigen::Vector3d getTaskFrameAngularVelocity()
int getTaskHierarchyLevel()
Eigen::Vector3d getTaskFrameLinearVelocity()
void setWeight(double weight)
yarp::os::Bottle trimBottle(const yarp::os::Bottle &bottle, int startIndex, int endIndex=-1)
Eigen::Twistd getTaskFrameAcceleration()
Eigen::Vector3d getTaskFrameLinearAcceleration()
virtual ~TaskYarpInterface()
void setTaskHierarchyLevel(int level)
void putIntoBottle(yarp::os::Bottle &bottle) const
bool extractFromBottle(const yarp::os::Bottle &bottle, int &sizeOfOptions)
void setDesiredTaskState(const TaskState &newDesiredTaskState)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
TaskState getDesiredTaskState()
Eigen::MatrixXd pourBottleIntoEigenMatrix(yarp::os::Bottle bottle, int &indexesToSkip)
void setStiffness(double K)
RpcMessageCallback(TaskYarpInterface &tmBaseRef)
double getTaskErrorNorm()
static TASK_MESSAGE stringToTaskManagerMessageTag(std::string testString)
void pourEigenMatrixIntoBottle(const Eigen::MatrixXd &mat, yarp::os::Bottle &bottle)
Eigen::Rotation3d getTaskFrameOrientation()
Eigen::MatrixXd getDampingMatrix()
Eigen::VectorXd getTaskError()
Eigen::Twistd getTaskFrameVelocity()
Eigen::MatrixXd getStiffnessMatrix()
TASK_MODE
A basic enumeration for the different types of tasks we can have.
Eigen::Vector3d getTaskFrameAngularAcceleration()
void pourEigenVectorIntoBottle(const Eigen::VectorXd &vec, yarp::os::Bottle &bottle)
void setDamping(double B)
Eigen::Twistd getAcceleration() const
Eigen::Displacementd getPosition() const
virtual bool read(yarp::os::ConnectionReader &connection)
Eigen::Twistd getVelocity() const
Eigen::VectorXd pourBottleIntoEigenVector(yarp::os::Bottle bottle, int &indexesToSkip)
Eigen::Displacementd getTaskFrameDisplacement()
void setDesiredTaskStateDirect(const TaskState &newDesiredTaskState)
std::string getPortName()
TaskYarpInterface(Task::Ptr taskPtr)