Skip to content

Commit

Permalink
Add refactored version of setInteractionMode/setControlMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicogene committed Dec 12, 2024
1 parent e45d99a commit 4ffd1ad
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 104 deletions.
1 change: 1 addition & 0 deletions plugins/controlboard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(HEADER_FILES

set(SRC_FILES
src/ControlBoard.cpp
src/ControlBoardData.cpp
src/ControlBoardDriver.cpp
src/ControlBoardTrajectory.cpp
)
Expand Down
3 changes: 3 additions & 0 deletions plugins/controlboard/include/ControlBoardData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public:
yarp::dev::IJointCoupling* ijointcoupling{nullptr};
// TODO (xela95): read this value from configuration file
std::chrono::milliseconds controlUpdatePeriod = std::chrono::milliseconds(1);

bool setInteractionMode((int axis, yarp::dev::InteractionModeEnum mode);
bool setControlMode(int j, int mode);
};

class IControlBoardData
Expand Down
113 changes: 113 additions & 0 deletions plugins/controlboard/src/ControlBoardData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

#include "ControlBoardData.h"

namespace yarp::dev::gzyarp
{
bool ControlBoardData::setInteractionMode(int axis, InteractionModeEnum mode)
{
yarp::sig::VectorOf<size_t> coupledActuatedAxes, coupledPhysicalJoints;
try
{
// If there is coupling let's check if we have to change the interaction mode for all the coupled joints
if (this->ijointcoupling)
{
this->ijointcoupling->getCoupledActuatedAxes(coupledActuatedAxes);
this->ijointcoupling->getCoupledPhysicalJoints(coupledPhysicalJoints);
// If the joint is coupled, we have to change the interaction mode for all the coupled joints
if(std::find(coupledActuatedAxes.begin(), coupledActuatedAxes.end(), axis) != coupledActuatedAxes.end())
{
for (auto& actuatedAxis : coupledActuatedAxes)
{
this->actuatedAxes.at(actuatedAxis).commonJointProperties.interactionMode = mode;
}
for (auto& physicalJoint : coupledPhysicalJoints)
{
this->physicalJoints.at(physicalJoint).commonJointProperties.interactionMode = mode;
}
} // if the joint is not coupled, we change the interaction mode only for the selected joint
else
{
this->actuatedAxes.at(axis).commonJointProperties.interactionMode = mode;
this->physicalJoints.at(axis).commonJointProperties.interactionMode = mode;
}
} // No coupling, we change the interaction mode only for the selected joint
else {
this->physicalJoints.at(axis).commonJointProperties.interactionMode = mode;
this->actuatedAxes.at(axis).commonJointProperties.interactionMode = mode;
}
} catch (const std::exception& e)
{
yError() << "Error while setting interaction mode for axis " + std::to_string(axis) + ": \n"
+ e.what();
return false;
}
}

bool ControlBoardData::setControlMode(int j, int mode) {
int desired_mode = mode;

if (j < 0 || j >= this->actuatedAxes.size())
{
yError() << "Error while setting control mode: joint index out of range";
return false;
}

// Only accept supported control modes
// The only not supported control mode is
// (for now) VOCAB_CM_MIXED
if (!(mode == VOCAB_CM_POSITION || mode == VOCAB_CM_POSITION_DIRECT || mode == VOCAB_CM_VELOCITY
|| mode == VOCAB_CM_TORQUE || mode == VOCAB_CM_MIXED || mode == VOCAB_CM_PWM
|| mode == VOCAB_CM_CURRENT || mode == VOCAB_CM_IDLE || mode == VOCAB_CM_FORCE_IDLE))
{
yWarning() << "request control mode " << yarp::os::Vocab32::decode(mode)
<< " that is not supported by "
<< " gz-sim-yarp-controlboard-system plugin.";
return false;
}

// If joint is in hw fault, only a force idle command can recover it
if (this->actuatedAxes.at(j).controlMode == VOCAB_CM_HW_FAULT
&& mode != VOCAB_CM_FORCE_IDLE)
{
return true;
}

if (mode == VOCAB_CM_FORCE_IDLE)
{
// Clean the fault status and set control mode to idle
desired_mode = VOCAB_CM_IDLE;
}

yarp::sig::VectorOf<size_t> coupledActuatedAxes, coupledPhysicalJoints;
// If there is coupling let's check if we have to change the interaction mode for all the coupled joints
if (this->ijointcoupling)
{
this->ijointcoupling->getCoupledActuatedAxes(coupledActuatedAxes);
this->ijointcoupling->getCoupledPhysicalJoints(coupledPhysicalJoints);
// If the joint is coupled, we have to change the interaction mode for all the coupled joints
if(std::find(coupledActuatedAxes.begin(), coupledActuatedAxes.end(), axis) != coupledActuatedAxes.end())
{
for (auto& actuatedAxis : coupledActuatedAxes)
{
this->actuatedAxes.at(actuatedAxis).commonJointProperties.controlMode = desired_mode;
}
for (auto& physicalJoint : coupledPhysicalJoints)
{
this->physicalJoints.at(physicalJoint).commonJointProperties.controlMode = desired_mode;
}
} // if the joint is not coupled, we change the interaction mode only for the selected joint
else
{
this->actuatedAxes.at(axis).commonJointProperties.controlMode = desired_mode;
this->physicalJoints.at(axis).commonJointProperties.controlMode = desired_mode;
}
} // No coupling, we change the interaction mode only for the selected joint
else {
this->physicalJoints.at(axis).commonJointProperties.controlMode = desired_mode;
this->actuatedAxes.at(axis).commonJointProperties.controlMode = desired_mode;
}

}


} // namespace yarp::dev::gzyarp
106 changes: 2 additions & 104 deletions plugins/controlboard/src/ControlBoardDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,44 +103,7 @@ bool ControlBoardDriver::getInteractionModes(yarp::dev::InteractionModeEnum* mod
bool ControlBoardDriver::setInteractionMode(int axis, yarp::dev::InteractionModeEnum mode)
{
std::lock_guard<std::mutex> lock(m_controlBoardData->mutex);
yarp::sig::VectorOf<size_t> coupledActuatedAxes, coupledPhysicalJoints;
try
{
// If there is coupling let's check if we have to change the interaction mode for all the coupled joints
if (m_controlBoardData->ijointcoupling)
{
m_controlBoardData->ijointcoupling->getCoupledActuatedAxes(coupledActuatedAxes);
m_controlBoardData->ijointcoupling->getCoupledPhysicalJoints(coupledPhysicalJoints);
// If the joint is coupled, we have to change the interaction mode for all the coupled joints
if(std::find(coupledActuatedAxes.begin(), coupledActuatedAxes.end(), axis) != coupledActuatedAxes.end())
{
for (auto& actuatedAxis : coupledActuatedAxes)
{
m_controlBoardData->actuatedAxes.at(actuatedAxis).commonJointProperties.interactionMode = mode;
}
for (auto& physicalJoint : coupledPhysicalJoints)
{
m_controlBoardData->physicalJoints.at(physicalJoint).commonJointProperties.interactionMode = mode;
}
} // if the joint is not coupled, we change the interaction mode only for the selected joint
else
{
m_controlBoardData->actuatedAxes.at(axis).commonJointProperties.interactionMode = mode;
m_controlBoardData->physicalJoints.at(axis).commonJointProperties.interactionMode = mode;
}
} // No coupling, we change the interaction mode only for the selected joint
else {
m_controlBoardData->physicalJoints.at(axis).commonJointProperties.interactionMode = mode;
m_controlBoardData->actuatedAxes.at(axis).commonJointProperties.interactionMode = mode;
}
} catch (const std::exception& e)
{
yError() << "Error while setting interaction mode for axis " + std::to_string(axis) + ": \n"
+ e.what();
return false;
}

return true;
return m_controlBoardData->setInteractionMode(axis, mode);
}

bool ControlBoardDriver::setInteractionModes(int n_joints,
Expand Down Expand Up @@ -258,72 +221,7 @@ bool ControlBoardDriver::getControlModes(const int n_joint, const int* joints, i
bool ControlBoardDriver::setControlMode(const int j, const int mode)
{
std::lock_guard<std::mutex> lock(m_controlBoardData->mutex);

int desired_mode = mode;

if (j < 0 || j >= m_controlBoardData->actuatedAxes.size())
{
yError() << "Error while setting control mode: joint index out of range";
return false;
}

// Only accept supported control modes
// The only not supported control mode is
// (for now) VOCAB_CM_MIXED
if (!(mode == VOCAB_CM_POSITION || mode == VOCAB_CM_POSITION_DIRECT || mode == VOCAB_CM_VELOCITY
|| mode == VOCAB_CM_TORQUE || mode == VOCAB_CM_MIXED || mode == VOCAB_CM_PWM
|| mode == VOCAB_CM_CURRENT || mode == VOCAB_CM_IDLE || mode == VOCAB_CM_FORCE_IDLE))
{
yWarning() << "request control mode " << yarp::os::Vocab32::decode(mode)
<< " that is not supported by "
<< " gz-sim-yarp-controlboard-system plugin.";
return false;
}

// If joint is in hw fault, only a force idle command can recover it
if (m_controlBoardData->actuatedAxes.at(j).controlMode == VOCAB_CM_HW_FAULT
&& mode != VOCAB_CM_FORCE_IDLE)
{
return true;
}

if (mode == VOCAB_CM_FORCE_IDLE)
{
// Clean the fault status and set control mode to idle
desired_mode = VOCAB_CM_IDLE;
}

yarp::sig::VectorOf<size_t> coupledActuatedAxes, coupledPhysicalJoints;
// If there is coupling let's check if we have to change the interaction mode for all the coupled joints
if (m_controlBoardData->ijointcoupling)
{
m_controlBoardData->ijointcoupling->getCoupledActuatedAxes(coupledActuatedAxes);
m_controlBoardData->ijointcoupling->getCoupledPhysicalJoints(coupledPhysicalJoints);
// If the joint is coupled, we have to change the interaction mode for all the coupled joints
if(std::find(coupledActuatedAxes.begin(), coupledActuatedAxes.end(), axis) != coupledActuatedAxes.end())
{
for (auto& actuatedAxis : coupledActuatedAxes)
{
m_controlBoardData->actuatedAxes.at(actuatedAxis).commonJointProperties.controlMode = desired_mode;
}
for (auto& physicalJoint : coupledPhysicalJoints)
{
m_controlBoardData->physicalJoints.at(physicalJoint).commonJointProperties.controlMode = desired_mode;
}
} // if the joint is not coupled, we change the interaction mode only for the selected joint
else
{
m_controlBoardData->actuatedAxes.at(axis).commonJointProperties.controlMode = desired_mode;
m_controlBoardData->physicalJoints.at(axis).commonJointProperties.controlMode = desired_mode;
}
} // No coupling, we change the interaction mode only for the selected joint
else {
m_controlBoardData->physicalJoints.at(axis).commonJointProperties.controlMode = desired_mode;
m_controlBoardData->actuatedAxes.at(axis).commonJointProperties.controlMode = desired_mode;
}


return true;
return m_controlBoardData->setControlMode(j, mode);
}

bool ControlBoardDriver::setControlModes(const int n_joint, const int* joints, int* modes)
Expand Down

0 comments on commit 4ffd1ad

Please sign in to comment.