Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/plugins/sharpa_avatar/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ SPDX-License-Identifier: Apache-2.0
- Keep transport presence, physical-glove ingress/faults, and HUMAN/RAW/ROBOT
output freshness/errors as separate health layers. One advancing dataset must
not keep another dataset healthy.
- Withdraw each unusable dataset's output resource; skipping pushes leaves cached
samples visible to consumers. Recreate only that dataset on recovery, with
isolated, bounded setup and delivery retries. Physical ingress readiness must
not depend on output construction, including startup and reconnect. Use the
same payload-validity rule for health and delivery; test tracker orchestration
as well as helper clocks and resource lifetimes. Retain timestamp baselines
across outages on the same SDK handle; clear them only for a new generation.
- Validate every emitted health state/reason pair through the Python status reducer; producer-only tests do not
protect the cross-language monitoring contract.
- Keep the static HUMAN-to-OpenXR index table synchronized with the pinned
Expand Down
194 changes: 78 additions & 116 deletions src/plugins/sharpa_avatar/core/avatar_hand_tracking_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,14 @@ void GloveState::reset() noexcept
}
}
clear_current_data();
for (auto& dataset : datasets)
{
dataset.freshness = {};
}
device.reset();
observed_ingress_generation = 0;
last_ingress.reset();
haptic_error.reset();
output_setup_error.reset();
last_output_setup_attempt.reset();
started = false;
ready = false;
}
Expand All @@ -387,7 +389,9 @@ void GloveState::clear_current_data() noexcept
robot_frame = {};
for (auto& dataset : datasets)
{
dataset.freshness = {};
// A retained SDK handle can still return the pre-outage cached sample.
dataset.freshness.invalidate();
dataset.output = {};
dataset.fetch_error.reset();
}
}
Expand Down Expand Up @@ -672,15 +676,6 @@ void AvatarTracker::refresh_glove_activity()
const bool ingress_fresh = state.last_ingress && now - *state.last_ingress < kAvatarDataTimeout;
if (ingress_fresh && !state.ready)
{
if (state.last_output_setup_attempt && now - *state.last_output_setup_attempt < kOutputSetupRetryInterval)
{
continue;
}
state.last_output_setup_attempt = now;
if (!try_create_output_resources(side))
{
continue;
}
state.ready = true;
if (m_config.haptic)
{
Expand All @@ -703,53 +698,6 @@ void AvatarTracker::refresh_glove_activity()
}
}

bool AvatarTracker::try_create_output_resources(::avatar::DeviceSide side)
{
const size_t index = side_index(side);
GloveState& state = glove(side);
try
{
std::unique_ptr<plugin_utils::HandInjector> injector;
std::array<std::unique_ptr<core::SchemaPusher>, 2> joint_pushers;

if (m_config.human)
{
injector = std::make_unique<plugin_utils::HandInjector>(
m_handles.instance, m_handles.session, xr_hand(side), m_handles.space);
}
for (const ::avatar::DeviceDataCategory category : kJointDataCategories)
{
if (!dataset_enabled(category))
{
continue;
}
const std::string localized_name =
"Avatar " + std::string(to_string(category)) + " " + std::string(to_string(side));
joint_pushers[joint_category_index(category)] = std::make_unique<core::SchemaPusher>(
m_handles, core::SchemaPusherConfig{ .collection_id = collection_id(side, category),
.max_flatbuffer_size = kJointFlatbufferSize,
.tensor_identifier = "joint_state",
.localized_name = localized_name });
}

m_injectors[index] = std::move(injector);
m_joint_pushers[index] = std::move(joint_pushers);
state.output_setup_error.reset();
return true;
}
catch (const std::exception& error)
{
state.output_setup_error = "output setup failed: " + std::string(error.what());
logger()->warn("{} glove output setup failed; this side will retry: {}", to_string(side), error.what());
}
catch (...)
{
state.output_setup_error = "output setup failed with an unknown error";
logger()->warn("{} glove output setup failed; this side will retry.", to_string(side));
}
return false;
}

void AvatarTracker::destroy_output_resources(::avatar::DeviceSide side) noexcept
{
const size_t index = side_index(side);
Expand Down Expand Up @@ -791,7 +739,9 @@ void AvatarTracker::refresh_data()
{
::avatar::AvatarDataFrame frame;
const auto error = state.device->fetch_data(frame, ::avatar::DeviceDataCategory::HUMAN);
if (error == ::avatar::ErrorCode::SUCCESS)
if (error == ::avatar::ErrorCode::SUCCESS &&
frame.payload_case() == payload_case(::avatar::DeviceDataCategory::HUMAN) &&
!frame.skeleton.landmark.empty())
{
note_result(::avatar::DeviceDataCategory::HUMAN, error, &frame.skeleton.header.stamp);
state.landmarks = std::move(frame.skeleton.landmark);
Expand All @@ -811,7 +761,8 @@ void AvatarTracker::refresh_data()
::avatar::AvatarDataFrame frame;
const auto error = state.device->fetch_data(frame, category);
::avatar::AvatarDataFrame& cached = cached_joint_frame(state, category);
if (error == ::avatar::ErrorCode::SUCCESS)
if (error == ::avatar::ErrorCode::SUCCESS && frame.payload_case() == payload_case(category) &&
!hand_payload(frame, category).joint.position.empty())
{
note_result(category, error, &hand_payload(frame, category).header.stamp);
cached = std::move(frame);
Expand Down Expand Up @@ -862,9 +813,9 @@ void AvatarTracker::publish_device_status()
const auto& dataset = state.datasets[category_idx];
const bool has_no_current_data = dataset.fetch_error == ::avatar::ErrorCode::NO_VALID_DATA_RETURNED;
std::optional<std::string> fetch_error;
if (state.output_setup_error)
if (dataset.output.error())
{
fetch_error = state.output_setup_error;
fetch_error = dataset.output.error();
}
else if (dataset.fetch_error && !has_no_current_data)
{
Expand All @@ -878,7 +829,7 @@ void AvatarTracker::publish_device_status()
.ever_observed = m_ever_observed[index],
.ingress_fresh = ingress_fresh,
.ever_received_ingress = state.ever_received_ingress,
.data_fresh = dataset.freshness.fresh(now),
.data_fresh = dataset_usable(state, category, now),
.ever_received_data = dataset.ever_received_data,
.fetch_has_no_current_data = has_no_current_data,
.fetch_error = std::move(fetch_error),
Expand Down Expand Up @@ -916,6 +867,14 @@ void AvatarTracker::update()
refresh_data();
log_waiting_gloves();
process_haptics();
if (m_config.raw || m_config.robot)
{
push_joint_frames();
}
if (m_config.human)
{
inject_hand_data();
}
try
{
publish_device_status();
Expand All @@ -937,14 +896,6 @@ void AvatarTracker::update()
m_status_publish_error_logged = true;
}
}
if (m_config.raw || m_config.robot)
{
push_joint_frames();
}
if (m_config.human)
{
inject_hand_data();
}
}

void AvatarTracker::process_haptics()
Expand Down Expand Up @@ -1025,17 +976,46 @@ bool AvatarTracker::dataset_enabled(::avatar::DeviceDataCategory category) const
return false;
}

bool AvatarTracker::dataset_usable(const GloveState& state,
::avatar::DeviceDataCategory category,
std::chrono::steady_clock::time_point now) const
{
if (!dataset_enabled(category) || !state.ready || !state.datasets[data_category_index(category)].freshness.fresh(now))
{
return false;
}
if (category == ::avatar::DeviceDataCategory::HUMAN)
{
return !state.landmarks.empty();
}
const auto& frame = cached_joint_frame(state, category);
return frame.payload_case() == payload_case(category) && !hand_payload(frame, category).joint.position.empty();
}

void AvatarTracker::push_joint_frames()
{
for (const ::avatar::DeviceSide side : kDeviceSides)
{
for (const ::avatar::DeviceDataCategory category : kJointDataCategories)
{
const auto& pusher = m_joint_pushers[side_index(side)][joint_category_index(category)];
if (pusher)
{
push_joint_frame(glove(side), side, category, *pusher);
}
GloveState& state = glove(side);
auto& dataset = state.datasets[data_category_index(category)];
const auto now = std::chrono::steady_clock::now();
dataset.output.publish(
m_joint_pushers[side_index(side)][joint_category_index(category)], dataset_usable(state, category, now),
now, kOutputSetupRetryInterval,
[this, side, category]
{
const std::string localized_name =
"Avatar " + std::string(to_string(category)) + " " + std::string(to_string(side));
return std::make_unique<core::SchemaPusher>(
m_handles, core::SchemaPusherConfig{ .collection_id = collection_id(side, category),
.max_flatbuffer_size = kJointFlatbufferSize,
.tensor_identifier = "joint_state",
.localized_name = localized_name });
},
[this, &state, side, category](core::SchemaPusher& pusher)
{ push_joint_frame(state, side, category, pusher); });
}
}
}
Expand All @@ -1045,20 +1025,7 @@ void AvatarTracker::push_joint_frame(const GloveState& glove,
::avatar::DeviceDataCategory category,
core::SchemaPusher& pusher)
{
if (!glove.datasets[data_category_index(category)].freshness.fresh(std::chrono::steady_clock::now()))
{
return;
}
const ::avatar::AvatarDataFrame& frame = cached_joint_frame(glove, category);
if (frame.payload_case() != payload_case(category))
{
return;
}
const ::avatar::Hand& hand = hand_payload(frame, category);
if (hand.joint.position.empty())
{
return;
}
const auto& hand = hand_payload(cached_joint_frame(glove, category), category);

core::JointStateOutputT output;
output.device_id = collection_id(side, category);
Expand Down Expand Up @@ -1239,31 +1206,26 @@ void AvatarTracker::inject_hand_data()

const auto process_hand = [this, time](::avatar::DeviceSide side)
{
const GloveState& state = glove(side);
if (!state.datasets[data_category_index(::avatar::DeviceDataCategory::HUMAN)].freshness.fresh(
std::chrono::steady_clock::now()))
{
return;
}
const std::vector<::avatar::Pose>& landmarks = state.landmarks;
if (landmarks.empty())
{
return;
}

// No optical/controller wrist: inject wrist-relative at identity with
// VALID-only flags so HandsSource still has a skeleton to draw.
const plugin_utils::WristSample wrist = m_wrist_source->query(side == ::avatar::DeviceSide::LEFT, time);
const XrPosef root = wrist.valid ? wrist.pose : oxr_utils::identity_posef();

XrHandJointLocationEXT joints[XR_HAND_JOINT_COUNT_EXT];
map_landmarks_to_openxr(landmarks, root, wrist.valid && wrist.tracked, joints);

plugin_utils::HandInjector* injector = m_injectors[side_index(side)].get();
if (injector != nullptr)
{
injector->push(joints, time);
}
GloveState& state = glove(side);
auto& dataset = state.datasets[data_category_index(::avatar::DeviceDataCategory::HUMAN)];
const auto now = std::chrono::steady_clock::now();
dataset.output.publish(
m_injectors[side_index(side)], dataset_usable(state, ::avatar::DeviceDataCategory::HUMAN, now), now,
kOutputSetupRetryInterval,
[this, side]
{
return std::make_unique<plugin_utils::HandInjector>(
m_handles.instance, m_handles.session, xr_hand(side), m_handles.space);
},
[this, &state, side, time](plugin_utils::HandInjector& injector)
{
// No optical/controller wrist: keep valid wrist-relative joints at identity.
const auto wrist = m_wrist_source->query(side == ::avatar::DeviceSide::LEFT, time);
const XrPosef root = wrist.valid ? wrist.pose : oxr_utils::identity_posef();
XrHandJointLocationEXT joints[XR_HAND_JOINT_COUNT_EXT];
map_landmarks_to_openxr(state.landmarks, root, wrist.valid && wrist.tracked, joints);
injector.push(joints, time);
});
};

for (const ::avatar::DeviceSide side : kDeviceSides)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct GloveState
struct DatasetState
{
DatasetFreshness freshness;
StreamOutputRecovery output;
std::optional<::avatar::ErrorCode> fetch_error;
bool ever_received_data = false;
};
Expand All @@ -131,8 +132,6 @@ struct GloveState
uint64_t observed_ingress_generation = 0;
std::optional<std::chrono::steady_clock::time_point> last_ingress;
std::optional<std::string> haptic_error;
std::optional<std::string> output_setup_error;
std::optional<std::chrono::steady_clock::time_point> last_output_setup_attempt;
bool ever_received_ingress = false;
bool started = false;
bool ready = false;
Expand Down Expand Up @@ -234,6 +233,8 @@ class __attribute__((visibility("default"))) AvatarTracker
void update();

private:
friend struct AvatarTrackerTestAccess;

static constexpr std::size_t kAvatarFingerCount = 5;

void initialize_openxr();
Expand All @@ -244,7 +245,6 @@ class __attribute__((visibility("default"))) AvatarTracker
void try_connect_missing_gloves();
void start_glove_if_present(GloveState& glove, ::avatar::DeviceSide side);
void refresh_glove_activity();
bool try_create_output_resources(::avatar::DeviceSide side);
void destroy_output_resources(::avatar::DeviceSide side) noexcept;
void refresh_data();
void log_waiting_gloves();
Expand All @@ -257,6 +257,9 @@ class __attribute__((visibility("default"))) AvatarTracker
::avatar::DeviceDataCategory category,
core::SchemaPusher& pusher);
bool dataset_enabled(::avatar::DeviceDataCategory category) const;
bool dataset_usable(const GloveState& state,
::avatar::DeviceDataCategory category,
std::chrono::steady_clock::time_point now) const;
bool apply_haptic_command(::avatar::DeviceSide side, const std::array<float, kAvatarFingerCount>& powers);
bool send_haptic_command(::avatar::DeviceSide side, const std::array<float, kAvatarFingerCount>& powers);
void stop_haptics(::avatar::DeviceSide side) noexcept;
Expand Down
Loading
Loading