From a89e44b0a473d3040a7170902047df88acb02658 Mon Sep 17 00:00:00 2001 From: Stefano Date: Wed, 21 Sep 2022 13:46:05 +0200 Subject: [PATCH 01/14] Unifying publication to transform server. Avoid publishing the ports for the head, left and right arm. Avoid publishing the eye frames Adding the hands poses manually. --- src/devices/openxrheadset/CMakeLists.txt | 6 +- src/devices/openxrheadset/EyesManager.cpp | 14 +- src/devices/openxrheadset/EyesManager.h | 6 +- src/devices/openxrheadset/FramePorts.cpp | 172 ------------------ src/devices/openxrheadset/FramePorts.h | 50 ----- src/devices/openxrheadset/OpenXrHeadset.cpp | 44 +---- src/devices/openxrheadset/OpenXrHeadset.h | 14 +- src/devices/openxrheadset/OpenXrInterface.cpp | 21 ++- src/devices/openxrheadset/OpenXrInterface.h | 2 +- ...nalPosesPublisher.cpp => PosesManager.cpp} | 18 +- ...itionalPosesPublisher.h => PosesManager.h} | 12 +- src/devices/openxrheadset/SingleEyePort.cpp | 31 +--- src/devices/openxrheadset/SingleEyePort.h | 12 +- 13 files changed, 52 insertions(+), 350 deletions(-) delete mode 100644 src/devices/openxrheadset/FramePorts.cpp delete mode 100644 src/devices/openxrheadset/FramePorts.h rename src/devices/openxrheadset/{AdditionalPosesPublisher.cpp => PosesManager.cpp} (54%) rename src/devices/openxrheadset/{AdditionalPosesPublisher.h => PosesManager.h} (69%) diff --git a/src/devices/openxrheadset/CMakeLists.txt b/src/devices/openxrheadset/CMakeLists.txt index e1fd5f6..1074791 100644 --- a/src/devices/openxrheadset/CMakeLists.txt +++ b/src/devices/openxrheadset/CMakeLists.txt @@ -50,8 +50,7 @@ set(yarp_openxrheadset_driver_SRCS OpenXrYarpUtilities.cpp SingleEyePort.cpp EyesManager.cpp - AdditionalPosesPublisher.cpp - FramePorts.cpp + PosesManager.cpp PosePublisher.cpp ) @@ -60,8 +59,7 @@ set(yarp_openxrheadset_driver_HDRS OpenXrYarpUtilities.h SingleEyePort.h EyesManager.h - AdditionalPosesPublisher.h - FramePorts.h + PosesManager.h PosePublisher.h ) diff --git a/src/devices/openxrheadset/EyesManager.cpp b/src/devices/openxrheadset/EyesManager.cpp index d6e4221..99585d5 100644 --- a/src/devices/openxrheadset/EyesManager.cpp +++ b/src/devices/openxrheadset/EyesManager.cpp @@ -20,11 +20,10 @@ EyesManager::Options &EyesManager::options() return m_options; } -bool EyesManager::initialize(yarp::dev::IFrameTransform *tfPublisher, const std::string &headFrame) +bool EyesManager::initialize() { if (!m_leftEye.open(m_options.leftEyeQuadLayer, - m_options.portPrefix + "/eyeAngles/left:i", - tfPublisher, m_options.leftEyeFrame, headFrame)) { + m_options.portPrefix + "/eyeAngles/left:i")) { yCError(OPENXRHEADSET) << "Cannot initialize left display texture."; return false; } @@ -34,8 +33,7 @@ bool EyesManager::initialize(yarp::dev::IFrameTransform *tfPublisher, const std: m_leftEye.setEyeRelativeImagePosition(Eigen::Vector3f(0.0, 0.0, m_options.eyeZPosition)); if (!m_rightEye.open(m_options.rightEyeQuadLayer, - m_options.portPrefix + "/eyeAngles/right:i", - tfPublisher, m_options.rightEyeFrame, headFrame)) { + m_options.portPrefix + "/eyeAngles/right:i")) { yCError(OPENXRHEADSET) << "Cannot initialize right display texture."; return false; } @@ -118,12 +116,6 @@ bool EyesManager::update() return true; } -void EyesManager::publishEyesTransforms() -{ - m_leftEye.publishEyeTransform(); - m_rightEye.publishEyeTransform(); -} - std::vector EyesManager::getLeftImageDimensions() { std::vector output(2); diff --git a/src/devices/openxrheadset/EyesManager.h b/src/devices/openxrheadset/EyesManager.h index 8585e0a..b791582 100644 --- a/src/devices/openxrheadset/EyesManager.h +++ b/src/devices/openxrheadset/EyesManager.h @@ -24,8 +24,6 @@ class EyesManager std::shared_ptr leftEyeQuadLayer; std::shared_ptr rightEyeQuadLayer; std::string portPrefix; - std::string leftEyeFrame; - std::string rightEyeFrame; double leftAzimuthOffset{0.0}; double leftElevationOffset{0.0}; double rightAzimuthOffset{0.0}; @@ -39,14 +37,12 @@ class EyesManager Options& options(); - bool initialize(yarp::dev::IFrameTransform *tfPublisher, const std::string& headFrame); + bool initialize(); void close(); bool update(); - void publishEyesTransforms(); - std::vector getLeftImageDimensions(); std::vector getRightImageDimensions(); diff --git a/src/devices/openxrheadset/FramePorts.cpp b/src/devices/openxrheadset/FramePorts.cpp deleted file mode 100644 index 60c3b69..0000000 --- a/src/devices/openxrheadset/FramePorts.cpp +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT) - * All rights reserved. - * - * This software may be modified and distributed under the terms of the - * BSD-2-Clause license. See the accompanying LICENSE file for details. - */ - -#include -#include -#include -#include - -bool FramePorts::open(const std::string &name, const std::string &portPrefix, yarp::dev::IFrameTransform *tfPublisher, const std::string &tfFrame, const std::string &rootFrame) -{ - //opening ports - std::initializer_list**, - std::string>> ports = - { - { &m_orientationPort, "quaternion" }, - { &m_positionPort, "position" }, - { &m_angularVelocityPort, "angularVelocity" }, - { &m_linearVelocityPort, "linearVelocity" } - }; - - for (auto port : ports) - { - if (*port.first) - { - yCError(OPENXRHEADSET) << port.second << "is already open."; - continue; - } - - std::string portName; - - *port.first = new yarp::os::BufferedPort; - portName = portPrefix + "/" + port.second + ":o"; - - if (!(*port.first)->open(portName)) - { - yCError(OPENXRHEADSET) << "Cannot open" << portName << "port"; - close(); - return false; - } - - (*port.first)->setWriteOnly(); - } - - double timeNow = yarp::os::Time::now(); - m_lastWarning["quaternion"] = timeNow - 10.0; - m_lastWarning["position"] = timeNow - 10.0; - m_lastWarning["angularVelocity"] = timeNow - 10.0; - m_lastWarning["linearVelocity"] = timeNow - 10.0; - m_lastWarning["republish"] = timeNow - 10.0; - - m_name = name; - m_tfPublisher = tfPublisher; - m_tfFrame = tfFrame; - m_rootFrame = rootFrame; - - m_localPose.resize(4,4); - m_localPose.eye(); - - return true; -} - -void FramePorts::close() -{ - m_localPoseValid = false; - - //Closing and deleting ports - std::initializer_list**> ports = - { - &m_orientationPort, - &m_positionPort, - &m_angularVelocityPort, - &m_linearVelocityPort, - }; - - for (auto port : ports) - { - if (*port) - { - (*port)->close(); - - delete *port; - - *port = nullptr; - } - } -} - -void FramePorts::publishFrame(const OpenXrInterface::Pose &pose, const OpenXrInterface::Velocity &velocity, yarp::os::Stamp &stamp) -{ - if (pose.positionValid && pose.rotationValid) - { - poseToYarpMatrix(pose, m_localPose); - m_localPoseValid = true; - if (!m_tfPublisher->setTransform(m_tfFrame, m_rootFrame, m_localPose)) - { - yCWarning(OPENXRHEADSET) << "Failed to publish" << m_tfFrame << "frame."; - } - } - else - { - if (m_localPoseValid) - { - if (!m_tfPublisher->setTransform(m_tfFrame, m_rootFrame, m_localPose)) - { - yCWarning(OPENXRHEADSET) << "Failed to publish" << m_tfFrame << "frame."; - } - - if (yarp::os::Time::now() - m_lastWarning["republish"] > 1.0) - { - yCWarning(OPENXRHEADSET) << "Publishing last" << m_name << "known pose."; - m_lastWarning["republish"] = yarp::os::Time::now(); - } - } - } - - if (pose.positionValid) - { - writeVec3OnPort(m_positionPort, pose.position, stamp); - } - else - { - if (yarp::os::Time::now() - m_lastWarning["position"] > 5.0) - { - yCWarning(OPENXRHEADSET) << m_name << "position not valid."; - m_lastWarning["position"] = yarp::os::Time::now(); - } - } - - if (pose.rotationValid) - { - writeQuaternionOnPort(m_orientationPort, pose.rotation, stamp); - } - else - { - if (yarp::os::Time::now() - m_lastWarning["quaternion"] > 5.0) - { - yCWarning(OPENXRHEADSET) << m_name << "rotation not valid."; - m_lastWarning["quaternion"] = yarp::os::Time::now(); - } - } - - if (velocity.linearValid) - { - writeVec3OnPort(m_linearVelocityPort, velocity.linear, stamp); - } - else - { - if (yarp::os::Time::now() - m_lastWarning["linearVelocity"] > 5.0) - { - yCWarning(OPENXRHEADSET) << m_name << "linear velocity not valid."; - m_lastWarning["linearVelocity"] = yarp::os::Time::now(); - } - } - - if (velocity.angularValid) - { - writeVec3OnPort(m_angularVelocityPort, velocity.angular, stamp); - } - else - { - if (yarp::os::Time::now() - m_lastWarning["angularVelocity"] > 5.0) - { - yCWarning(OPENXRHEADSET) << m_name << "angular velocity not valid."; - m_lastWarning["angularVelocity"] = yarp::os::Time::now(); - } - } -} diff --git a/src/devices/openxrheadset/FramePorts.h b/src/devices/openxrheadset/FramePorts.h deleted file mode 100644 index 506513a..0000000 --- a/src/devices/openxrheadset/FramePorts.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT) - * All rights reserved. - * - * This software may be modified and distributed under the terms of the - * BSD-2-Clause license. See the accompanying LICENSE file for details. - */ -#ifndef YARP_DEV_FRAMEPORTS_H -#define YARP_DEV_FRAMEPORTS_H - -#include -#include -#include -#include -#include -#include -#include - -class FramePorts -{ - yarp::os::BufferedPort* m_orientationPort{nullptr}; - yarp::os::BufferedPort* m_positionPort{nullptr}; - yarp::os::BufferedPort* m_angularVelocityPort{nullptr}; - yarp::os::BufferedPort* m_linearVelocityPort{nullptr}; - - yarp::dev::IFrameTransform* m_tfPublisher; - - std::string m_tfFrame; - std::string m_rootFrame; - std::string m_name; - - std::unordered_map m_lastWarning; - - yarp::sig::Matrix m_localPose; - bool m_localPoseValid{false}; - - -public: - - bool open(const std::string& name, const std::string& portPrefix, - yarp::dev::IFrameTransform* tfPublisher, const std::string& tfFrame, const std::string& rootFrame); - - void close(); - - void publishFrame(const OpenXrInterface::Pose& pose, - const OpenXrInterface::Velocity& velocity, - yarp::os::Stamp &stamp); -}; - -#endif // YARP_DEV_FRAMEPORTS_H diff --git a/src/devices/openxrheadset/OpenXrHeadset.cpp b/src/devices/openxrheadset/OpenXrHeadset.cpp index 1053a96..26d34dc 100644 --- a/src/devices/openxrheadset/OpenXrHeadset.cpp +++ b/src/devices/openxrheadset/OpenXrHeadset.cpp @@ -19,8 +19,7 @@ typedef bool(yarp::os::Value::*valueIsType)(void) const; yarp::dev::OpenXrHeadset::OpenXrHeadset() : yarp::dev::DeviceDriver(), - yarp::os::PeriodicThread(0.011, yarp::os::ShouldUseSystemClock::Yes), // ~90 fps - m_stamp(0,0.0) + yarp::os::PeriodicThread(0.011, yarp::os::ShouldUseSystemClock::Yes) // ~90 fps {} yarp::dev::OpenXrHeadset::~OpenXrHeadset() @@ -218,11 +217,6 @@ bool yarp::dev::OpenXrHeadset::open(yarp::os::Searchable &cfg) m_openXrInterfaceSettings.posesPredictionInMs = cfg.check("vr_poses_prediction_in_ms", yarp::os::Value(0.0)).asFloat64(); m_getStickAsAxis = cfg.check("stick_as_axis", yarp::os::Value(false)).asBool(); - m_leftFrame = cfg.check("tf_left_hand_frame", yarp::os::Value("openxr_left_hand")).asString(); - m_rightFrame = cfg.check("tf_right_hand_frame", yarp::os::Value("openxr_right_hand")).asString(); - m_headFrame = cfg.check("tf_head_frame", yarp::os::Value("openxr_head")).asString(); - m_eyesManager.options().leftEyeFrame = cfg.check("tf_left_eye_frame", yarp::os::Value("openxr_left_eye")).asString(); - m_eyesManager.options().rightEyeFrame = cfg.check("tf_right_eye_frame", yarp::os::Value("openxr_right_eye")).asString(); m_rootFrame = cfg.check("tf_root_frame", yarp::os::Value("openxr_origin")).asString(); m_rootFrameRaw = m_rootFrame + "_raw"; m_eyesManager.options().leftAzimuthOffset = cfg.check("left_azimuth_offset", yarp::os::Value(0.0)).asFloat64(); @@ -237,7 +231,7 @@ bool yarp::dev::OpenXrHeadset::open(yarp::os::Searchable &cfg) m_rawRootFrameTransform.resize(4,4); m_rawRootFrameTransform.eye(); - std::vector labels; + std::vector labels; yarp::os::Bottle& labelsGroup = cfg.findGroup("POSES_LABELS"); for (size_t i = 1; i < labelsGroup.size(); ++i) //The first element is the name of the group itself { @@ -271,21 +265,6 @@ bool yarp::dev::OpenXrHeadset::open(yarp::os::Searchable &cfg) } yCInfo(OPENXRHEADSET) << "TransformCLient successfully opened at port: " << cfg.find("tfLocal").asString(); - if (!m_headFramePorts.open("Head", m_prefix + "/headpose", m_tfPublisher, m_headFrame, m_rootFrameRaw)) - { - return false; - } - - if (!m_leftHandFramePorts.open("Left Hand", m_prefix + "/left_hand", m_tfPublisher, m_leftFrame, m_rootFrameRaw)) - { - return false; - } - - if (!m_rightHandFramePorts.open("Right Hand", m_prefix + "/right_hand", m_tfPublisher, m_rightFrame, m_rootFrameRaw)) - { - return false; - } - PosePublisherSettings posePublisherSettings; posePublisherSettings.tfPublisher = m_tfPublisher; posePublisherSettings.rootFrame = m_rootFrameRaw; @@ -302,7 +281,7 @@ bool yarp::dev::OpenXrHeadset::open(yarp::os::Searchable &cfg) yCWarning(OPENXRHEADSET) << "pose_check_convergence_ratio is supposed to be in the range [0, 1]. Clamping to" << posePublisherSettings.checks.convergenceRatio << "."; } - m_additionalPosesPublisher.initialize(labels, posePublisherSettings); + m_posesManager.initialize(labels, posePublisherSettings); // Start the thread if (!this->start()) { @@ -334,7 +313,7 @@ bool yarp::dev::OpenXrHeadset::threadInit() m_eyesManager.options().leftEyeQuadLayer = m_openXrInterface.addHeadFixedQuadLayer(); m_eyesManager.options().rightEyeQuadLayer = m_openXrInterface.addHeadFixedQuadLayer(); - if (!m_eyesManager.initialize(m_tfPublisher, m_headFrame)) + if (!m_eyesManager.initialize()) { yCError(OPENXRHEADSET) << "Failed to initialize eyes."; } @@ -427,9 +406,6 @@ void yarp::dev::OpenXrHeadset::threadRelease() m_tfPublisher = nullptr; } - m_headFramePorts.close(); - m_leftHandFramePorts.close(); - m_rightHandFramePorts.close(); m_eyesManager.close(); m_rpcPort.close(); @@ -474,9 +450,7 @@ void yarp::dev::OpenXrHeadset::run() m_openXrInterface.getButtons(m_buttons); m_openXrInterface.getAxes(m_axes); m_openXrInterface.getThumbsticks(m_thumbsticks); - m_openXrInterface.getAdditionalPoses(m_additionalPosesPublisher.inputs()); - - m_stamp.update(m_openXrInterface.currentNanosecondsSinceEpoch() * 1e-9); + m_openXrInterface.getAllPoses(m_posesManager.inputs()); if (m_openXrInterface.shouldResetLocalReferenceSpace()) { @@ -490,13 +464,7 @@ void yarp::dev::OpenXrHeadset::run() yCWarning(OPENXRHEADSET) << "Failed to update the transformation of the raw root frame."; } - m_headFramePorts.publishFrame(m_openXrInterface.headPose(), m_openXrInterface.headVelocity(), m_stamp); - m_leftHandFramePorts.publishFrame(m_openXrInterface.leftHandPose(), m_openXrInterface.leftHandVelocity(), m_stamp); - m_rightHandFramePorts.publishFrame(m_openXrInterface.rightHandPose(), m_openXrInterface.rightHandVelocity(), m_stamp); - - m_eyesManager.publishEyesTransforms(); - - m_additionalPosesPublisher.publishFrames(); + m_posesManager.publishFrames(); } else { diff --git a/src/devices/openxrheadset/OpenXrHeadset.h b/src/devices/openxrheadset/OpenXrHeadset.h index b7b95be..a7a1801 100644 --- a/src/devices/openxrheadset/OpenXrHeadset.h +++ b/src/devices/openxrheadset/OpenXrHeadset.h @@ -29,8 +29,7 @@ #include #include #include -#include -#include +#include #include #include @@ -236,13 +235,7 @@ class yarp::dev::OpenXrHeadset : public yarp::dev::DeviceDriver, SlideManager layer; }; - FramePorts m_headFramePorts; - FramePorts m_leftHandFramePorts; - FramePorts m_rightHandFramePorts; - - AdditionalPosesPublisher m_additionalPosesPublisher; - - yarp::os::Stamp m_stamp; + PosesManager m_posesManager; std::string m_prefix; @@ -255,9 +248,6 @@ class yarp::dev::OpenXrHeadset : public yarp::dev::DeviceDriver, bool m_getStickAsAxis; IFrameTransform* m_tfPublisher; - std::string m_leftFrame; - std::string m_rightFrame; - std::string m_headFrame; std::string m_rootFrameRaw; std::string m_rootFrame; PolyDriver m_driver; diff --git a/src/devices/openxrheadset/OpenXrInterface.cpp b/src/devices/openxrheadset/OpenXrInterface.cpp index 7c9e338..96fbb56 100644 --- a/src/devices/openxrheadset/OpenXrInterface.cpp +++ b/src/devices/openxrheadset/OpenXrInterface.cpp @@ -1558,9 +1558,10 @@ void OpenXrInterface::getThumbsticks(std::vector &thumbsticks) } } -void OpenXrInterface::getAdditionalPoses(std::vector &additionalPoses) const +void OpenXrInterface::getAllPoses(std::vector &additionalPoses) const { size_t numberOfPoses = 0; + numberOfPoses += 3; //This is because the head and the hands are added manually for (size_t topLevelIndex = 0; topLevelIndex < m_pimpl->top_level_paths.size(); ++topLevelIndex) { if (topLevelIndex < 2) //Left and right hand are already considered with leftHandPose() and rightHandPose() @@ -1580,6 +1581,24 @@ void OpenXrInterface::getAdditionalPoses(std::vector &additio size_t poseIndex = 0; + auto& head = additionalPoses[poseIndex]; + head.name = "openxr_head"; + head.pose = headPose(); + head.velocity = headVelocity(); + poseIndex++; + + auto& left_arm = additionalPoses[poseIndex]; + left_arm.name = "openxr_left_hand"; + left_arm.pose = leftHandPose(); + left_arm.velocity = leftHandVelocity(); + poseIndex++; + + auto& right_arm = additionalPoses[poseIndex]; + right_arm.name = "openxr_right_hand"; + right_arm.pose = rightHandPose(); + right_arm.velocity = rightHandVelocity(); + poseIndex++; + for (size_t topLevelIndex = 0; topLevelIndex < m_pimpl->top_level_paths.size(); ++topLevelIndex) { std::vector& posesList = m_pimpl->top_level_paths[topLevelIndex].currentActions().poses; diff --git a/src/devices/openxrheadset/OpenXrInterface.h b/src/devices/openxrheadset/OpenXrInterface.h index 92880b7..36ec920 100644 --- a/src/devices/openxrheadset/OpenXrInterface.h +++ b/src/devices/openxrheadset/OpenXrInterface.h @@ -185,7 +185,7 @@ class OpenXrInterface void getThumbsticks(std::vector& thumbsticks) const; - void getAdditionalPoses(std::vector &additionalPoses) const; + void getAllPoses(std::vector &additionalPoses) const; int64_t currentNanosecondsSinceEpoch() const; diff --git a/src/devices/openxrheadset/AdditionalPosesPublisher.cpp b/src/devices/openxrheadset/PosesManager.cpp similarity index 54% rename from src/devices/openxrheadset/AdditionalPosesPublisher.cpp rename to src/devices/openxrheadset/PosesManager.cpp index 17fe229..bf02463 100644 --- a/src/devices/openxrheadset/AdditionalPosesPublisher.cpp +++ b/src/devices/openxrheadset/PosesManager.cpp @@ -6,31 +6,31 @@ * BSD-2-Clause license. See the accompanying LICENSE file for details. */ -#include +#include #include #include #include -void AdditionalPosesPublisher::initialize(const std::vector