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
1 change: 1 addition & 0 deletions src/plugins/sharpa_avatar/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_library(avatar_plugin_core STATIC
avatar_hand_tracking_plugin.cpp
inc/avatar/avatar_health.hpp
inc/avatar/avatar_hand_tracking_plugin.hpp
inc/avatar/avatar_stream_health.hpp
)

add_library(isaac::avatar_plugin ALIAS avatar_plugin_core)
Expand Down
207 changes: 91 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 @@ -39,6 +39,7 @@ namespace

constexpr size_t kJointFlatbufferSize = 4096;
constexpr auto kGloveRetryInterval = std::chrono::seconds(2);
constexpr auto kGloveWaitReminderInterval = std::chrono::minutes(1);
constexpr auto kOutputSetupRetryInterval = std::chrono::seconds(2);
constexpr auto kHapticCommandTimeout = std::chrono::milliseconds(200);
constexpr auto kHapticFailureRetryInterval = std::chrono::seconds(1);
Expand Down Expand Up @@ -369,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 @@ -386,8 +389,9 @@ void GloveState::clear_current_data() noexcept
robot_frame = {};
for (auto& dataset : datasets)
{
dataset.last_stamp.reset();
dataset.last_fresh_data.reset();
// 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 @@ -633,14 +637,18 @@ void AvatarTracker::try_connect_missing_gloves()
start_glove_if_present(state, side);
}
}
}

const auto has_device = [](const GloveState& state) { return state.device != nullptr; };
const bool waiting = std::none_of(m_gloves.begin(), m_gloves.end(), has_device);
if (waiting && !m_glove_wait_logged)
void AvatarTracker::log_waiting_gloves()
{
const auto now = std::chrono::steady_clock::now();
for (const ::avatar::DeviceSide side : kDeviceSides)
{
logger()->info("Waiting for an online glove...");
if (m_glove_wait_reminders[side_index(side)].observe(glove(side).ready, now, kGloveWaitReminderInterval))
{
logger()->info("Waiting for {} glove to connect and stream.", to_string(side));
}
}
m_glove_wait_logged = waiting;
}

void AvatarTracker::refresh_glove_activity()
Expand Down Expand Up @@ -668,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 @@ -699,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 @@ -777,19 +729,19 @@ void AvatarTracker::refresh_data()
return;
}
dataset.fetch_error.reset();
if (stamp != nullptr && (!dataset.last_stamp || stamp->sec != dataset.last_stamp->sec ||
stamp->nanosec != dataset.last_stamp->nanosec))
if (stamp != nullptr)
{
dataset.last_stamp = *stamp;
dataset.last_fresh_data = now;
dataset.freshness.observe(stamp->sec, stamp->nanosec, now);
dataset.ever_received_data = true;
}
};
if (m_config.human)
{
::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 @@ -809,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 @@ -860,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 @@ -876,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.last_fresh_data && now - *dataset.last_fresh_data < kAvatarDataTimeout,
.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 @@ -912,7 +865,16 @@ void AvatarTracker::update()
refresh_glove_activity();
m_deviceio_session->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 @@ -934,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 @@ -1022,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 @@ -1042,16 +1025,7 @@ void AvatarTracker::push_joint_frame(const GloveState& glove,
::avatar::DeviceDataCategory category,
core::SchemaPusher& pusher)
{
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 @@ -1232,25 +1206,26 @@ void AvatarTracker::inject_hand_data()

const auto process_hand = [this, time](::avatar::DeviceSide side)
{
const std::vector<::avatar::Pose>& landmarks = glove(side).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
Loading
Loading