Put not requested tracking on standby

This commit is contained in:
Patrick Nicolas
2024-09-03 23:34:23 +02:00
parent 843a83761a
commit 96bbfa1a23
17 changed files with 178 additions and 51 deletions
+3 -2
View File
@@ -118,12 +118,13 @@ static xrt_hand_joint_set convert_joints(const std::optional<std::array<from_hea
return output_joints;
}
void hand_joints_list::update_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
bool hand_joints_list::update_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
{
if (tracking.hand == hand_id)
add_sample(
return add_sample(
tracking.production_timestamp,
tracking.timestamp,
convert_joints(tracking.joints),
offset);
return true;
}
+3 -3
View File
@@ -24,14 +24,14 @@
class hand_joints_list : public history<hand_joints_list, xrt_hand_joint_set>
{
int hand_id;
public:
const int hand_id;
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);
bool update_tracking(const xrt::drivers::wivrn::from_headset::hand_tracking & tracking, const clock_offset & offset);
};
+15 -3
View File
@@ -20,6 +20,7 @@
#pragma once
#include "clock_offset.h"
#include "os/os_time.h"
#include "util/u_logging.h"
#include <algorithm>
#include <cstddef>
@@ -37,14 +38,21 @@ class history
std::mutex mutex;
std::list<TimedData> data;
XrTime last_request;
protected:
void add_sample(XrTime produced_timestamp, XrTime timestamp, const Data & sample, const clock_offset & offset)
history() :
last_request(os_monotonic_get_ns()) {}
// return true if object is active (last request is not too old)
bool add_sample(XrTime produced_timestamp, XrTime timestamp, const Data & sample, const clock_offset & offset)
{
XrTime produced = offset.from_headset(produced_timestamp);
XrTime t = offset.from_headset(timestamp);
std::lock_guard lock(mutex);
bool active = produced - last_request < 1'000'000'000;
// Discard outdated data, packets could be reordered
if (not data.empty())
{
@@ -54,11 +62,11 @@ protected:
U_LOG_D("not using history: clock_offset not stable");
data.clear();
data.emplace_back(sample, produced, t);
return;
return active;
}
if (data.back().produced_timestamp > produced)
return;
return active;
}
// Discard outdated predictions
@@ -86,6 +94,8 @@ protected:
while (data.size() > MaxSamples)
data.pop_front();
return active;
}
public:
@@ -94,6 +104,8 @@ public:
std::lock_guard lock(mutex);
std::chrono::nanoseconds ex(0);
last_request = at_timestamp_ns;
if (data.empty())
{
return {};
+3 -3
View File
@@ -60,16 +60,16 @@ xrt_space_relation pose_list::extrapolate(const xrt_space_relation & a, const xr
return res;
}
void pose_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
bool pose_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{
for (const auto & pose: tracking.device_poses)
{
if (pose.device != device)
continue;
add_sample(tracking.production_timestamp, tracking.timestamp, convert_pose(pose), offset);
return;
return add_sample(tracking.production_timestamp, tracking.timestamp, convert_pose(pose), offset);
}
return true;
}
xrt_space_relation pose_list::convert_pose(const from_headset::tracking::pose & pose)
+3 -3
View File
@@ -26,16 +26,16 @@
class pose_list : public history<pose_list, xrt_space_relation>
{
xrt::drivers::wivrn::device_id device;
public:
const xrt::drivers::wivrn::device_id device;
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) {}
void update_tracking(const xrt::drivers::wivrn::from_headset::tracking &, const clock_offset & offset);
bool update_tracking(const xrt::drivers::wivrn::from_headset::tracking &, const clock_offset & offset);
static xrt_space_relation convert_pose(const xrt::drivers::wivrn::from_headset::tracking::pose &);
};
+3 -3
View File
@@ -35,7 +35,7 @@ tracked_views view_list::extrapolate(const tracked_views & a, const tracked_view
return result;
}
void view_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
bool view_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{
for (const auto & pose: tracking.device_poses)
{
@@ -53,7 +53,7 @@ void view_list::update_tracking(const from_headset::tracking & tracking, const c
view.fovs[eye] = xrt_cast(tracking.views[eye].fov);
}
add_sample(tracking.production_timestamp, tracking.timestamp, view, offset);
return;
return add_sample(tracking.production_timestamp, tracking.timestamp, view, offset);
}
return true;
}
+1 -1
View File
@@ -36,5 +36,5 @@ 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);
bool update_tracking(const from_headset::tracking & tracking, const clock_offset & offset);
};
+9 -3
View File
@@ -315,9 +315,11 @@ xrt_space_relation wivrn_controller::get_tracked_pose(xrt_input_name name, uint6
{
case XRT_INPUT_TOUCH_AIM_POSE:
std::tie(extrapolation_time, res) = aim.get_at(at_timestamp_ns);
cnx->set_enabled(aim.device, true);
break;
case XRT_INPUT_TOUCH_GRIP_POSE:
std::tie(extrapolation_time, res) = grip.get_at(at_timestamp_ns);
cnx->set_enabled(grip.device, true);
break;
default:
U_LOG_W("Unknown input name requested");
@@ -335,6 +337,7 @@ std::pair<xrt_hand_joint_set, uint64_t> wivrn_controller::get_hand_tracking(xrt_
case XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT: {
auto [extrapolation_time, data] = joints.get_at(desired_timestamp_ns);
cnx->add_predict_offset(extrapolation_time);
cnx->set_enabled(joints.hand_id == 0 ? to_headset::tracking_control::id::left_hand : to_headset::tracking_control::id::right_hand, true);
return {data, desired_timestamp_ns};
}
@@ -346,13 +349,16 @@ std::pair<xrt_hand_joint_set, uint64_t> wivrn_controller::get_hand_tracking(xrt_
void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{
aim.update_tracking(tracking, offset);
grip.update_tracking(tracking, offset);
if (not aim.update_tracking(tracking, offset))
cnx->set_enabled(aim.device, false);
if (not grip.update_tracking(tracking, offset))
cnx->set_enabled(grip.device, false);
}
void wivrn_controller::update_hand_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
{
joints.update_tracking(tracking, offset);
if (not joints.update_tracking(tracking, offset))
cnx->set_enabled(joints.hand_id == 0 ? to_headset::tracking_control::id::left_hand : to_headset::tracking_control::id::right_hand, false);
}
void wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * value)
+3 -1
View File
@@ -79,13 +79,15 @@ void wivrn_fb_face2_tracker::update_tracking(const from_headset::fb_face2 & face
data.weights = face.weights;
data.confidences = face.confidences;
face_list.update_tracking(face.production_timestamp, face.timestamp, data, offset);
if (not face_list.update_tracking(face.production_timestamp, face.timestamp, data, offset))
cnx->set_enabled(to_headset::tracking_control::id::face, false);
}
xrt_result_t wivrn_fb_face2_tracker::get_face_tracking(enum xrt_input_name facial_expression_type, struct xrt_facial_expression_set * inout_value)
{
if (facial_expression_type == XRT_INPUT_FB_FACE_TRACKING2_VISUAL)
{
cnx->set_enabled(to_headset::tracking_control::id::face, true);
auto [_, data] = face_list.get_at(inout_value->face_expression_set2_fb.sample_time_ns);
inout_value->face_expression_set2_fb.is_valid = data.is_valid;
+2 -2
View File
@@ -75,9 +75,9 @@ public:
return result;
}
void update_tracking(const XrTime & production_timestamp, const XrTime & timestamp, const wivrn_fb_face2_data & data, const clock_offset & offset)
bool update_tracking(const XrTime & production_timestamp, const XrTime & timestamp, const wivrn_fb_face2_data & data, const clock_offset & offset)
{
this->add_sample(production_timestamp, timestamp, data, offset);
return this->add_sample(production_timestamp, timestamp, data, offset);
}
};
+4
View File
@@ -268,6 +268,8 @@ void wivrn_hmd::update_tracking(const from_headset::tracking & tracking, const c
void wivrn_hmd::update_battery(const from_headset::battery & new_battery)
{
// We will only request a new sample if the current one is consumed
cnx->set_enabled(to_headset::tracking_control::id::battery, false);
std::lock_guard lock(mutex);
battery = new_battery;
}
@@ -312,6 +314,8 @@ xrt_result_t wivrn_hmd::get_battery_status(struct xrt_device * xdev,
bool * out_charging,
float * out_charge)
{
cnx->set_enabled(to_headset::tracking_control::id::battery, true);
std::lock_guard lock(mutex);
*out_present = battery.present;
*out_charging = battery.charging;
+37 -3
View File
@@ -39,6 +39,7 @@
#include "xrt/xrt_session.h"
#include <cmath>
#include <magic_enum.hpp>
#include <vulkan/vulkan.h>
struct wivrn_comp_target_factory : public comp_target_factory
@@ -78,18 +79,27 @@ struct wivrn_comp_target_factory : public comp_target_factory
}
};
void xrt::drivers::wivrn::max_accumulator::send(wivrn_connection & connection)
void xrt::drivers::wivrn::tracking_control_t::send(wivrn_connection & connection)
{
if (std::chrono::steady_clock::now() < next_sample)
return;
if (auto offset = max.exchange(0))
connection.send_stream(to_headset::prediction_offset{
connection.send_stream(to_headset::tracking_control{
.offset = std::chrono::nanoseconds(offset),
.enabled = enabled,
});
next_sample += std::chrono::seconds(1);
}
void xrt::drivers::wivrn::tracking_control_t::set_enabled(to_headset::tracking_control::id id, bool enabled)
{
std::lock_guard lock(mutex);
if (enabled != this->enabled[size_t(id)])
U_LOG_I("%s tracking: %s", std::string(magic_enum::enum_name(id)).c_str(), enabled ? "enabled" : "disabled");
this->enabled[size_t(id)] = enabled;
}
xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp, u_system & system) :
connection(std::move(tcp)), xrt_system(system)
{
@@ -300,6 +310,30 @@ void wivrn_session::operator()(from_headset::timesync_response && timesync)
offset_est.add_sample(timesync);
}
static auto to_tracking_control(device_id id)
{
using tid = to_headset::tracking_control::id;
switch (id)
{
case device_id::LEFT_AIM:
return tid::left_aim;
case device_id::LEFT_GRIP:
return tid::left_grip;
case device_id::RIGHT_AIM:
return tid::right_aim;
case device_id::RIGHT_GRIP:
return tid::right_grip;
default:
break;
}
__builtin_unreachable();
}
void wivrn_session::set_enabled(device_id id, bool enabled)
{
tracking_control.set_enabled(to_tracking_control(id), enabled);
}
void wivrn_session::operator()(from_headset::feedback && feedback)
{
assert(comp_target);
@@ -343,7 +377,7 @@ void wivrn_session::run(std::weak_ptr<wivrn_session> weak_self)
if (self and not self->quit)
{
self->offset_est.request_sample(self->connection);
self->predict_offset.send(self->connection);
self->tracking_control.send(self->connection);
self->connection.poll(*self, 20);
}
else
+21 -6
View File
@@ -48,15 +48,20 @@ namespace xrt::drivers::wivrn
{
struct wivrn_comp_target;
class max_accumulator
class tracking_control_t
{
using T = std::chrono::nanoseconds::rep;
std::atomic<T> max;
std::chrono::steady_clock::time_point next_sample;
std::mutex mutex;
decltype(to_headset::tracking_control::enabled) enabled;
public:
max_accumulator() :
next_sample(std::chrono::steady_clock::now()) {}
tracking_control_t() :
next_sample(std::chrono::steady_clock::now())
{
enabled.fill(true);
}
void add(std::chrono::nanoseconds s)
{
auto sample = s.count();
@@ -66,6 +71,8 @@ public:
}
}
void send(wivrn_connection & connection);
void set_enabled(to_headset::tracking_control::id id, bool enabled);
};
class wivrn_session : public std::enable_shared_from_this<wivrn_session>
@@ -91,8 +98,9 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
clock_offset_estimator offset_est;
// offsets in ns of each requested frame from its generation time
max_accumulator predict_offset;
// prediction offset and enabled tracking to configure client
tracking_control_t tracking_control;
std::mutex tracking_control_mutex;
std::mutex csv_mutex;
std::ofstream feedback_csv;
@@ -117,9 +125,16 @@ public:
void add_predict_offset(std::chrono::nanoseconds off)
{
predict_offset.add(off);
tracking_control.add(off);
}
void set_enabled(to_headset::tracking_control::id id, bool enabled)
{
tracking_control.set_enabled(id, enabled);
}
void set_enabled(device_id id, bool enabled);
void operator()(from_headset::handshake &&) {}
void operator()(from_headset::headset_info_packet &&);
void operator()(from_headset::tracking &&);