Add hand tracking

This commit is contained in:
Guillaume Meunier
2024-02-25 16:09:32 +01:00
parent 91f6dc08f0
commit a2271d486a
23 changed files with 615 additions and 93 deletions
+1
View File
@@ -78,6 +78,7 @@ add_executable(wivrn-server
driver/wivrn_controller.cpp
driver/pose_list.cpp
driver/view_list.cpp
driver/hand_joints_list.cpp
driver/wivrn_session.cpp
driver/wivrn_connection.cpp
driver/xrt_cast.cpp
+123
View File
@@ -0,0 +1,123 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "hand_joints_list.h"
#include "pose_list.h"
#include "xrt_cast.h"
using namespace xrt::drivers::wivrn;
static_assert(XRT_HAND_JOINT_COUNT == XR_HAND_JOINT_COUNT_EXT);
namespace
{
xrt_hand_joint_value interpolate(const xrt_hand_joint_value & a, const xrt_hand_joint_value & b, float t)
{
return {
.relation = pose_list::interpolate(a.relation, b.relation, t),
.radius = a.radius * (1-t) + b.radius * t
};
}
xrt_hand_joint_value extrapolate(const xrt_hand_joint_value & a, const xrt_hand_joint_value & b, uint64_t ta, uint64_t tb, uint64_t t)
{
float λ = std::clamp((t - ta) / float(tb - ta), 0.f, 1.f);
return {
.relation = pose_list::extrapolate(a.relation, b.relation, ta, tb, t),
.radius = a.radius * (1-λ) + b.radius * λ
};
}
}
xrt_hand_joint_set hand_joints_list::interpolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, float t)
{
xrt_hand_joint_set j = a;
for(int i = 0; i < XRT_HAND_JOINT_COUNT; i++)
{
j.values.hand_joint_set_default[i] = ::interpolate(a.values.hand_joint_set_default[i], b.values.hand_joint_set_default[i], t);
}
return j;
}
xrt_hand_joint_set hand_joints_list::extrapolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, uint64_t ta, uint64_t tb, uint64_t t)
{
xrt_hand_joint_set j = a;
for(int i = 0; i < XRT_HAND_JOINT_COUNT; i++)
{
j.values.hand_joint_set_default[i] = ::extrapolate(a.values.hand_joint_set_default[i], b.values.hand_joint_set_default[i], ta, tb, t);
}
return j;
}
static xrt_hand_joint_set convert_joints(const std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT_COUNT_EXT>& input_joints)
{
xrt_hand_joint_set output_joints;
output_joints.is_active = true;
output_joints.hand_pose = xrt_space_relation{
.relation_flags = XRT_SPACE_RELATION_BITMASK_ALL,
.pose = {
.orientation = {0, 0, 0, 1},
.position = {0, 0, 0}
},
.linear_velocity = {0, 0, 0},
.angular_velocity = {0, 0, 0},
}; // TODO
for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++)
{
xrt_hand_joint_value& res = output_joints.values.hand_joint_set_default[i];
res.radius = input_joints[i].radius;
res.relation.pose = xrt_cast(input_joints[i].pose);
res.relation.angular_velocity = xrt_cast(input_joints[i].angular_velocity);
res.relation.linear_velocity = xrt_cast(input_joints[i].linear_velocity);
int flags = 0;
if (input_joints[i].flags & from_headset::hand_tracking::position_valid)
flags |= XRT_SPACE_RELATION_POSITION_VALID_BIT;
if (input_joints[i].flags & from_headset::hand_tracking::orientation_valid)
flags |= XRT_SPACE_RELATION_ORIENTATION_VALID_BIT;
if (input_joints[i].flags & from_headset::hand_tracking::linear_velocity_valid)
flags |= XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT;
if (input_joints[i].flags & from_headset::hand_tracking::angular_velocity_valid)
flags |= XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT;
if (input_joints[i].flags & from_headset::hand_tracking::position_tracked)
flags |= XRT_SPACE_RELATION_POSITION_TRACKED_BIT;
if (input_joints[i].flags & from_headset::hand_tracking::orientation_tracked)
flags |= XRT_SPACE_RELATION_ORIENTATION_TRACKED_BIT;
res.relation.relation_flags = (xrt_space_relation_flags)flags;
}
return output_joints;
}
void hand_joints_list::update_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
{
if (hand_id == 0)
add_sample(tracking.timestamp, convert_joints(tracking.left), offset);
else
add_sample(tracking.timestamp, convert_joints(tracking.right), offset);
}
+37
View File
@@ -0,0 +1,37 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "history.h"
#include "xrt/xrt_defines.h"
class hand_joints_list : public history<hand_joints_list, xrt_hand_joint_set>
{
int hand_id;
public:
static xrt_hand_joint_set interpolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, float t);
static xrt_hand_joint_set extrapolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, uint64_t ta, uint64_t tb, uint64_t t);
hand_joints_list(int hand_id) : hand_id(hand_id) {}
void update_tracking(const xrt::drivers::wivrn::from_headset::hand_tracking & tracking, const clock_offset & offset);
};
+96
View File
@@ -0,0 +1,96 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022-2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022-2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <vector>
#include <algorithm>
#include "clock_offset.h"
template <typename Derived, typename Data, size_t MaxSamples = 10>
class history
{
struct TimedData : public Data
{
TimedData(const Data & d, uint64_t t) :
Data(d), at_timestamp_ns(t) {}
uint64_t at_timestamp_ns;
};
std::mutex mutex;
std::vector<TimedData> data;
protected:
void add_sample(uint64_t timestamp, const Data & sample, const clock_offset & offset)
{
std::lock_guard lock(mutex);
uint64_t t = offset.from_headset(timestamp);
auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; });
if (it == data.end())
data.emplace_back(sample, t);
else if (it->at_timestamp_ns == t)
*it = TimedData(sample, t);
else
data.emplace(it, sample, t);
if (data.size() > MaxSamples)
data.erase(data.begin());
}
public:
Data get_at(uint64_t at_timestamp_ns)
{
std::lock_guard lock(mutex);
if (data.empty())
{
return {};
}
if (data.size() == 1)
{
return data[0];
}
if (data.front().at_timestamp_ns > at_timestamp_ns)
{
return Derived::extrapolate(data[0], data[1], data[0].at_timestamp_ns, data[1].at_timestamp_ns, at_timestamp_ns);
}
for (size_t i = 1; i < data.size(); ++i)
{
if (data[i].at_timestamp_ns > at_timestamp_ns)
{
float t = float(data[i].at_timestamp_ns - at_timestamp_ns) /
(data[i].at_timestamp_ns - data[i - 1].at_timestamp_ns);
return Derived::interpolate(data[i - 1], data[i], t);
}
}
const auto & d0 = data[data.size() - 2];
const auto & d1 = data.back();
return Derived::extrapolate(d0, d1, d0.at_timestamp_ns, d1.at_timestamp_ns, at_timestamp_ns);
}
};
+2 -2
View File
@@ -27,7 +27,7 @@
using namespace xrt::auxiliary::math;
using namespace xrt::drivers::wivrn;
xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t)
xrt_space_relation pose_list::interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t)
{
xrt_space_relation result;
xrt_space_relation_flags flags = xrt_space_relation_flags(a.relation_flags & b.relation_flags);
@@ -35,7 +35,7 @@ xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_rel
return result;
}
xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t)
xrt_space_relation pose_list::extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t)
{
if (t < ta)
return a;
+5 -76
View File
@@ -21,89 +21,18 @@
#include "wivrn_packets.h"
#include "clock_offset.h"
#include "history.h"
#include "xrt/xrt_defines.h"
#include <algorithm>
#include <mutex>
#include <vector>
template <typename Data, size_t MaxSamples = 10>
class history
{
struct TimedData : public Data
{
TimedData(const Data & d, uint64_t t) :
Data(d), at_timestamp_ns(t) {}
uint64_t at_timestamp_ns;
};
std::mutex mutex;
std::vector<TimedData> data;
protected:
void add_sample(uint64_t timestamp, const Data & sample, const clock_offset & offset)
{
std::lock_guard lock(mutex);
uint64_t t = offset.from_headset(timestamp);
auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; });
if (it == data.end())
data.emplace_back(sample, t);
else if (it->at_timestamp_ns == t)
*it = TimedData(sample, t);
else
data.emplace(it, sample, t);
if (data.size() > MaxSamples)
data.erase(data.begin());
}
public:
Data get_at(uint64_t at_timestamp_ns)
{
std::lock_guard lock(mutex);
if (data.empty())
{
return {};
}
if (data.size() == 1)
{
return data[0];
}
if (data.front().at_timestamp_ns > at_timestamp_ns)
{
return extrapolate(data[0], data[1], data[0].at_timestamp_ns, data[1].at_timestamp_ns, at_timestamp_ns);
}
for (size_t i = 1; i < data.size(); ++i)
{
if (data[i].at_timestamp_ns > at_timestamp_ns)
{
float t = float(data[i].at_timestamp_ns - at_timestamp_ns) /
(data[i].at_timestamp_ns - data[i - 1].at_timestamp_ns);
return interpolate(data[i - 1], data[i], t);
}
}
const auto & d0 = data[data.size() - 2];
const auto & d1 = data.back();
return extrapolate(d0, d1, d0.at_timestamp_ns, d1.at_timestamp_ns, at_timestamp_ns);
}
};
xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t);
xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t);
class pose_list : public history<xrt_space_relation>
class pose_list : public history<pose_list, xrt_space_relation>
{
xrt::drivers::wivrn::device_id device;
public:
static xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t);
static xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t);
pose_list(xrt::drivers::wivrn::device_id id) :
device(id) {}
+4 -4
View File
@@ -21,17 +21,17 @@
#include "pose_list.h"
#include "xrt_cast.h"
tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t)
tracked_views view_list::interpolate(const tracked_views & a, const tracked_views & b, float t)
{
tracked_views result = a;
result.relation = interpolate(a.relation, b.relation, t);
result.relation = pose_list::interpolate(a.relation, b.relation, t);
return result;
}
tracked_views extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t)
tracked_views view_list::extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t)
{
tracked_views result = a;
result.relation = extrapolate(a.relation, b.relation, ta, tb, t);
result.relation = pose_list::extrapolate(a.relation, b.relation, ta, tb, t);
return result;
}
+4 -4
View File
@@ -30,12 +30,12 @@ struct tracked_views
std::array<xrt_fov, 2> fovs;
};
tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t);
tracked_views extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t);
class view_list : public history<tracked_views>
class view_list : public history<view_list, tracked_views>
{
public:
static tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t);
static tracked_views extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t);
void update_tracking(const from_headset::tracking & tracking, const clock_offset & offset);
};
+46 -3
View File
@@ -34,6 +34,8 @@ enum wivrn_controller_input_index
{
WIVRN_CONTROLLER_AIM_POSE,
WIVRN_CONTROLLER_GRIP_POSE,
WIVRN_CONTROLLER_HAND_TRACKER,
WIVRN_CONTROLLER_MENU_CLICK, // /user/hand/left/input/menu/click
WIVRN_CONTROLLER_A_CLICK, // /user/hand/right/input/a/click
WIVRN_CONTROLLER_A_TOUCH, // /user/hand/right/input/a/touch
@@ -136,6 +138,12 @@ static void wivrn_controller_get_tracked_pose(xrt_device * xdev,
uint64_t at_timestamp_ns,
xrt_space_relation * out_relation);
static void wivrn_controller_get_hand_tracking(xrt_device * xdev,
xrt_input_name name,
uint64_t desired_timestamp_ns,
xrt_hand_joint_set * out_value,
uint64_t *out_timestamp_ns);
static void wivrn_controller_set_output(struct xrt_device * xdev, enum xrt_output_name name, const union xrt_output_value * value);
static void wivrn_controller_update_inputs(xrt_device * xdev);
@@ -143,19 +151,23 @@ static void wivrn_controller_update_inputs(xrt_device * xdev);
wivrn_controller::wivrn_controller(int hand_id,
xrt_device * hmd,
std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx) :
xrt_device{}, grip(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP), aim(hand_id == 0 ? device_id::LEFT_AIM : device_id::RIGHT_AIM), cnx(cnx)
xrt_device{},
grip(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP),
aim(hand_id == 0 ? device_id::LEFT_AIM : device_id::RIGHT_AIM),
joints(hand_id),
cnx(cnx)
{
xrt_device * base = this;
base->destroy = wivrn_controller_destroy;
base->get_tracked_pose = wivrn_controller_get_tracked_pose;
base->get_hand_tracking = NULL; // TODO
base->get_hand_tracking = wivrn_controller_get_hand_tracking;
base->set_output = wivrn_controller_set_output;
base->update_inputs = wivrn_controller_update_inputs;
base->name = XRT_DEVICE_TOUCH_CONTROLLER;
base->orientation_tracking_supported = true;
base->hand_tracking_supported = false;
base->hand_tracking_supported = true;
base->position_tracking_supported = true;
base->tracking_origin = hmd->tracking_origin;
@@ -197,6 +209,9 @@ wivrn_controller::wivrn_controller(int hand_id,
SET_INPUT(THUMBSTICK_TOUCH);
SET_INPUT(THUMBREST_TOUCH);
inputs[WIVRN_CONTROLLER_HAND_TRACKER].name = hand_id == 0 ? XRT_INPUT_GENERIC_HAND_TRACKING_LEFT : XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT;
inputs[WIVRN_CONTROLLER_HAND_TRACKER].active = true;
output_count = 1;
outputs = &haptic_output;
haptic_output.name = XRT_OUTPUT_NAME_TOUCH_HAPTIC;
@@ -308,12 +323,31 @@ xrt_space_relation wivrn_controller::get_tracked_pose(xrt_input_name name, uint6
}
}
std::pair<xrt_hand_joint_set, uint64_t> wivrn_controller::get_hand_tracking(xrt_input_name name, uint64_t desired_timestamp_ns)
{
switch(name)
{
case XRT_INPUT_GENERIC_HAND_TRACKING_LEFT:
case XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT:
return { joints.get_at(desired_timestamp_ns), desired_timestamp_ns };
default:
U_LOG_W("Unknown input name requested");
return {};
}
}
void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{
aim.update_tracking(tracking, offset);
grip.update_tracking(tracking, offset);
}
void wivrn_controller::update_hand_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
{
joints.update_tracking(tracking, offset);
}
void wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * value)
{
auto id = device_type == XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER ? device_id::LEFT_CONTROLLER_HAPTIC
@@ -357,6 +391,15 @@ static void wivrn_controller_get_tracked_pose(xrt_device * xdev,
*out_relation = static_cast<wivrn_controller *>(xdev)->get_tracked_pose(name, at_timestamp_ns);
}
static void wivrn_controller_get_hand_tracking(xrt_device * xdev,
xrt_input_name name,
uint64_t desired_timestamp_ns,
xrt_hand_joint_set * out_value,
uint64_t *out_timestamp_ns)
{
std::tie(*out_value, *out_timestamp_ns) = static_cast<wivrn_controller *>(xdev)->get_hand_tracking(name, desired_timestamp_ns);
}
static void wivrn_controller_set_output(struct xrt_device * xdev, enum xrt_output_name name, const union xrt_output_value * value)
{
static_cast<wivrn_controller *>(xdev)->set_output(name, value);
+5 -2
View File
@@ -22,6 +22,7 @@
#include "xrt/xrt_device.h"
#include "pose_list.h"
#include "hand_joints_list.h"
#include "wivrn_session.h"
#include <memory>
@@ -34,6 +35,7 @@ class wivrn_controller : public xrt_device
pose_list grip;
pose_list aim;
hand_joints_list joints;
std::vector<xrt_input> inputs_staging;
std::vector<xrt_input> inputs_array;
@@ -51,14 +53,15 @@ public:
void update_inputs();
xrt_space_relation get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns);
std::pair<xrt_hand_joint_set, uint64_t> get_hand_tracking(xrt_input_name name, uint64_t desired_timestamp_ns);
void set_output(xrt_output_name name, const xrt_output_value * value);
void set_inputs(const from_headset::inputs &, const clock_offset &);
void update_tracking(const from_headset::tracking &, const clock_offset &);
void update_hand_tracking(const from_headset::hand_tracking &, const clock_offset &);
private:
void
set_inputs(device_id input_id, float value, uint64_t last_change_time);
void set_inputs(device_id input_id, float value, uint64_t last_change_time);
};
+16
View File
@@ -120,9 +120,15 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
if (self->hmd)
usysds->base.base.static_roles.head = devices->xdevs[n++] = self->hmd.get();
if (self->left_hand)
{
devices->xdevs[n++] = self->left_hand.get();
usysds->base.base.static_roles.hand_tracking.left = self->left_hand.get();
}
if (self->right_hand)
{
devices->xdevs[n++] = self->right_hand.get();
usysds->base.base.static_roles.hand_tracking.right = self->right_hand.get();
}
devices->xdev_count = n;
u_system_devices_static_finalize(usysds, self->left_hand.get(), self->right_hand.get());
@@ -179,6 +185,16 @@ void wivrn_session::operator()(from_headset::tracking && tracking)
right_hand->update_tracking(tracking, offset);
}
void wivrn_session::operator()(from_headset::hand_tracking && hand_tracking)
{
auto offset = offset_est.get_offset();
if (not offset)
return;
left_hand->update_hand_tracking(hand_tracking, offset);
right_hand->update_hand_tracking(hand_tracking, offset);
}
void wivrn_session::operator()(from_headset::inputs && inputs)
{
auto offset = get_offset();
+1
View File
@@ -77,6 +77,7 @@ public:
void operator()(from_headset::handshake&&) {}
void operator()(from_headset::headset_info_packet &&);
void operator()(from_headset::tracking &&);
void operator()(from_headset::hand_tracking &&);
void operator()(from_headset::inputs &&);
void operator()(from_headset::timesync_response &&);
void operator()(from_headset::feedback &&);