Skip to content

Commit

Permalink
Merge pull request #46 from ami-iit/static_poses
Browse files Browse the repository at this point in the history
Added possibility to publish custom static poses
  • Loading branch information
S-Dafarra authored Oct 25, 2024
2 parents ad4ec15 + d93c8c9 commit 9e23ace
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/conda-forge-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: conda-incubator/setup-miniconda@v2
- uses: conda-incubator/setup-miniconda@v3
with:
miniforge-variant: Mambaforge
miniforge-version: latest
Expand Down
22 changes: 21 additions & 1 deletion src/devices/openxrheadset/CustomPosePublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ bool CustomPosePublisherSettings::parseFromConfigurationFile(const yarp::os::Bot
return false;
}

staticPose = inputGroup.check("static_pose") && (inputGroup.find("static_pose").isNull() || inputGroup.find("static_pose").asBool());

std::string parametrization = inputGroup.check("euler_angles", yarp::os::Value("")).toString();

if (parametrization.size() != 3)
Expand Down Expand Up @@ -217,7 +219,7 @@ bool CustomPosePublisherSettings::parseFromConfigurationFile(const yarp::os::Bot
{
yCError(OPENXRHEADSET) << "Failed to parse" << maskName
<< "at element" << i
<< ". Only float numbers and the special carachter \"*\" are allowed";
<< ". Only float numbers and the special character \"*\" are allowed";
return false;
}
}
Expand All @@ -235,5 +237,23 @@ bool CustomPosePublisherSettings::parseFromConfigurationFile(const yarp::os::Bot
return false;
}

if (staticPose)
{
for (size_t i = 0; i < 3; ++i)
{
if (!positionMask[i])
{
yCError(OPENXRHEADSET) << "The relative_position mask is not valid for a static pose.";
return false;
}

if (!rotationMask[i])
{
yCError(OPENXRHEADSET) << "The relative_rotation mask is not valid for a static pose.";
return false;
}
}
}

return true;
}
8 changes: 8 additions & 0 deletions src/devices/openxrheadset/OpenXrHeadset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,3 +1021,11 @@ bool yarp::dev::OpenXrHeadset::setCustomPoseRelativeOrientation(const std::strin
static_cast<float>(angle2),
static_cast<float>(angle3)});
}

bool yarp::dev::OpenXrHeadset::resetTransforms()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_tfPublisher->clear();
m_posesManager.reset();
return true;
}
8 changes: 8 additions & 0 deletions src/devices/openxrheadset/OpenXrHeadset.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ class yarp::dev::OpenXrHeadset : public yarp::dev::DeviceDriver,
*/
virtual bool setCustomPoseRelativeOrientation(const std::string& customFrameName, const double angle1, const double angle2, const double angle3) override;

/**
* Reset the transforms all the published tranforms.
* This will also delete all the transforms currently stored in the transform server,
* so also the static poses will be published again. This must be used with caution,
* as it will reset all the transforms, including the ones that are not published by this module.
*/
virtual bool resetTransforms() override;

private:

struct GuiParam
Expand Down
24 changes: 23 additions & 1 deletion src/devices/openxrheadset/OpenXrInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,29 @@ bool OpenXrInterface::prepareXrInstance()
};
instanceCreateInfo.next = &debugCallbackSettings;

XrResult result = xrCreateInstance(&instanceCreateInfo, &(m_pimpl->instance));
XrResult result = XR_ERROR_API_VERSION_UNSUPPORTED;

std::vector<std::pair<XrVersion, std::string>> api_versions = {
#ifdef XR_API_VERSION_1_0
{XR_API_VERSION_1_0, "XR_API_VERSION_1_0"},
#endif
{XR_CURRENT_API_VERSION, "XR_CURRENT_API_VERSION"},
#ifdef XR_API_VERSION_1_1
{XR_API_VERSION_1_1, "XR_API_VERSION_1_1"},
#endif // XR_API_VERSION_1_1
};
size_t version_index = 0;

while ((result == XR_ERROR_API_VERSION_UNSUPPORTED || result == XR_ERROR_INITIALIZATION_FAILED) && version_index < api_versions.size())
{
instanceCreateInfo.applicationInfo.apiVersion = api_versions[version_index].first;
result = xrCreateInstance(&instanceCreateInfo, &(m_pimpl->instance));
if (!XR_SUCCEEDED(result))
{
yCWarning(OPENXRHEADSET) << "The runtime does not support the API version" << api_versions[version_index].second;
}
version_index++;
}

if (!m_pimpl->checkXrOutput(result, "Failed to create XR instance."))
return false;
Expand Down
33 changes: 33 additions & 0 deletions src/devices/openxrheadset/PosePublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void PosePublisher::configurePublisher(std::shared_ptr<PosePublisherSettings> se

m_tfBaseFrame = settings->tfBaseFrame;
m_tfPublisher = settings->tfPublisher;
m_staticPose = settings->staticPose;
}

void PosePublisher::setLabel(const std::string &label)
Expand All @@ -63,6 +64,11 @@ const std::string &PosePublisher::tfBaseFrame() const
return m_tfBaseFrame;
}

const bool PosePublisher::staticPose() const
{
return m_staticPose;
}

bool PosePublisher::configured() const
{
return m_tfPublisher != nullptr;
Expand Down Expand Up @@ -103,6 +109,11 @@ void PosePublisher::publish()
return;
}

if (m_staticPose && m_publishedOnce)
{
return;
}

if (!m_publishedOnce)
{
if (m_label.empty())
Expand Down Expand Up @@ -136,8 +147,30 @@ void PosePublisher::publish()
}
}

if (m_staticPose)
{
if (!m_tfPublisher->setTransformStatic(m_label, m_tfBaseFrame, m_localPose))
{
yCWarning(OPENXRHEADSET) << "Failed to publish" << m_label << "frame (static). It will not be published again.";
}
else
{
yCInfo(OPENXRHEADSET) << "Published transformation from" << m_tfBaseFrame << "to" << m_label << " (static).";
}
return;
}

if (!m_tfPublisher->setTransform(m_label, m_tfBaseFrame, m_localPose))
{
yCWarning(OPENXRHEADSET) << "Failed to publish" << m_label << "frame.";
}
}

void PosePublisher::reset()
{
m_publishedOnce = false;
m_data = OpenXrInterface::NamedPoseVelocity();
m_previouslyPublishedData = OpenXrInterface::NamedPoseVelocity();
m_localPose.eye();
deactivate();
}
6 changes: 6 additions & 0 deletions src/devices/openxrheadset/PosePublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct PosePublisherSettings
{
yarp::dev::IFrameTransform* tfPublisher{nullptr};
std::string tfBaseFrame;
bool staticPose{ false };
};

class PosePublisher
Expand All @@ -30,6 +31,7 @@ class PosePublisher
bool m_publishedOnce{false};
double m_lastWarningTime{0.0};
size_t m_warningCount{0};
bool m_staticPose{ false };

void resetWarnings();

Expand All @@ -53,6 +55,8 @@ class PosePublisher

const std::string& tfBaseFrame() const;

const bool staticPose() const;

virtual bool configured() const;

virtual void updateInputPose(const OpenXrInterface::NamedPoseVelocity& input);
Expand All @@ -61,6 +65,8 @@ class PosePublisher

void publish();

void reset();

};

#endif // YARP_DEV_POSEPUBLISHER_H
31 changes: 28 additions & 3 deletions src/devices/openxrheadset/PosesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ void PosesManager::initialize(const std::string &editedRootFrame, const std::vec
m_customPoses.emplace_back();
auto customOptions = std::make_shared<CustomPosePublisherSettings>(customPose);
customOptions->tfPublisher = m_settings->tfPublisher;
customOptions->tfBaseFrame = editedRootFrame;
if (customOptions->staticPose)
{
customOptions->tfBaseFrame = customOptions->parentFrame; //for static poses we publish wrt the parent frame
}
else
{
customOptions->tfBaseFrame = editedRootFrame; //for dynamic poses we publish wrt the root frame
}
m_customPoses.back().configure(customOptions);
m_customPosesMap[customPose.name] = m_customPoses.size() - 1;
}
Expand Down Expand Up @@ -71,8 +78,11 @@ void PosesManager::publishFrames()
auto& customPose = m_customPoses[i];
const std::string& relativeFrame = customPose.relativeFrame();
OpenXrInterface::NamedPoseVelocity parentFramePose;

if (relativeFrame == m_rootFramePublisher.name())
if (customPose.staticPose())
{
parentFramePose = OpenXrInterface::NamedPoseVelocity::Identity(relativeFrame);
}
else if (relativeFrame == m_rootFramePublisher.name())
{
parentFramePose = m_rootPose;
}
Expand Down Expand Up @@ -195,3 +205,18 @@ bool PosesManager::setCustomPoseRelativeOrientation(const std::string &customFra
m_customPoses[customFrameIt->second].setRelativeOrientation(relativeOrientationEulerAngles);
return true;
}

void PosesManager::reset()
{
m_rootFramePublisher.reset();

for (auto& poseIt : m_poses)
{
poseIt.second.reset();
}

for (auto& customPose : m_customPoses)
{
customPose.reset();
}
}
2 changes: 2 additions & 0 deletions src/devices/openxrheadset/PosesManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class PosesManager

bool setCustomPoseRelativeOrientation(const std::string& customFrameName, const Eigen::Vector3f& relativeOrientationEulerAngles);

void reset();

};


Expand Down
2 changes: 1 addition & 1 deletion src/devices/openxrheadset/impl/OpenXrInterfaceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class OpenXrInterface::Implementation
}
else
{
yCError(OPENXRHEADSET) << format;
yCError(OPENXRHEADSET) << format << "(error:" << result << ")";
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,12 @@ service OpenXrHeadsetCommands
* @return True if successfull, false if the pose is not found
*/
bool setCustomPoseRelativeOrientation(1:string customFrameName, 2:double angle1, 3:double angle2, 4:double angle3);

/**
* Reset the transforms all the published tranforms.
* This will also delete all the transforms currently stored in the transform server,
* so also the static poses will be published again. This must be used with caution,
* as it will reset all the transforms, including the ones that are not published by this module.
*/
bool resetTransforms();
}

0 comments on commit 9e23ace

Please sign in to comment.