From fdd6e9a19babb2e26748df11d91171dbcdcb9190 Mon Sep 17 00:00:00 2001 From: 0forks <114709761+0forks@users.noreply.github.com> Date: Wed, 1 Nov 2023 06:54:23 +0300 Subject: [PATCH] Send HMD battery at 10Hz --- src/VRDriver.cpp | 28 ++++++++++++---------------- src/VRDriver.hpp | 1 + src/bridge/BridgeClient.cpp | 2 +- src/bridge/BridgeTransport.cpp | 4 ++-- test/BridgeServerMock.cpp | 4 ++-- test/common/TestBridgeClient.cpp | 4 ++-- 6 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/VRDriver.cpp b/src/VRDriver.cpp index 21db360..6d34bf7 100644 --- a/src/VRDriver.cpp +++ b/src/VRDriver.cpp @@ -148,26 +148,22 @@ void SlimeVRDriver::VRDriver::RunPoseRequestThread() { hmd_position->set_qw((float) q.w); bridge_->SendBridgeMessage(*message); - vr::ETrackedPropertyError err; - if (vr::VRProperties()->GetBoolProperty(vr::VRProperties()->TrackedDeviceToPropertyContainer(0), vr::Prop_DeviceProvidesBatteryStatus_Bool, &err) == true) { - messages::Battery* hmdBattery = google::protobuf::Arena::CreateMessage(&arena_); - message->set_allocated_battery(hmdBattery); - hmdBattery->set_tracker_id(0); - hmdBattery->set_battery_level(vr::VRProperties()->GetFloatProperty(vr::VRProperties()->TrackedDeviceToPropertyContainer(0), vr::Prop_DeviceBatteryPercentage_Float, &err) * 100); - hmdBattery->set_is_charging(vr::VRProperties()->GetBoolProperty(vr::VRProperties()->TrackedDeviceToPropertyContainer(0), vr::Prop_DeviceIsCharging_Bool, &err)); - bridge_->SendBridgeMessage(*message); + auto now = std::chrono::steady_clock::now(); + if (std::chrono::duration_cast(now - battery_sent_at_).count() > 100) { + vr::ETrackedPropertyError err; + if (vr::VRProperties()->GetBoolProperty(vr::VRProperties()->TrackedDeviceToPropertyContainer(0), vr::Prop_DeviceProvidesBatteryStatus_Bool, &err)) { + messages::Battery* hmdBattery = google::protobuf::Arena::CreateMessage(&arena_); + message->set_allocated_battery(hmdBattery); + hmdBattery->set_tracker_id(0); + hmdBattery->set_battery_level(vr::VRProperties()->GetFloatProperty(vr::VRProperties()->TrackedDeviceToPropertyContainer(0), vr::Prop_DeviceBatteryPercentage_Float, &err) * 100); + hmdBattery->set_is_charging(vr::VRProperties()->GetBoolProperty(vr::VRProperties()->TrackedDeviceToPropertyContainer(0), vr::Prop_DeviceIsCharging_Bool, &err)); + bridge_->SendBridgeMessage(*message); + } + battery_sent_at_ = now; } arena_.Reset(); - // Windows: - // p1: 2.191 ms 456.413 tps - // avg: 2.492 ms 401.225 tps - // p99: 2.526 ms 395.883 tps - // Linux: - // p1: 2.063 ms 484.731 tps - // avg: 2.079 ms 480.992 tps - // p99: 2.115 ms 472.813 tps std::this_thread::sleep_for(std::chrono::milliseconds(2)); } logger_->Log("Pose request thread exited"); diff --git a/src/VRDriver.hpp b/src/VRDriver.hpp index 84a9735..fced819 100644 --- a/src/VRDriver.hpp +++ b/src/VRDriver.hpp @@ -60,6 +60,7 @@ namespace SlimeVRDriver { std::map> devices_by_serial_; std::chrono::milliseconds frame_timing_ = std::chrono::milliseconds(16); std::chrono::steady_clock::time_point last_frame_time_ = std::chrono::steady_clock::now(); + std::chrono::steady_clock::time_point battery_sent_at_ = std::chrono::steady_clock::now(); std::string settings_key_ = "driver_slimevr"; vr::HmdQuaternion_t GetRotation(vr::HmdMatrix34_t &matrix); diff --git a/src/bridge/BridgeClient.cpp b/src/bridge/BridgeClient.cpp index 246fb00..e0a77cb 100644 --- a/src/bridge/BridgeClient.cpp +++ b/src/bridge/BridgeClient.cpp @@ -48,7 +48,7 @@ void BridgeClient::CreateConnection() { }); connection_handle_->on([this](const uvw::error_event& event, uvw::pipe_handle&) { if (!last_error_.has_value() || last_error_ != event.what()) { - logger_->Log("Pipe error: %s", event.what()); + logger_->Log("pipe error: %s", event.what()); last_error_ = event.what(); } Reconnect(); diff --git a/src/bridge/BridgeTransport.cpp b/src/bridge/BridgeTransport.cpp index d88ee18..2f9a94b 100644 --- a/src/bridge/BridgeTransport.cpp +++ b/src/bridge/BridgeTransport.cpp @@ -69,7 +69,7 @@ void BridgeTransport::ResetBuffers() { void BridgeTransport::OnRecv(const uvw::data_event& event) { if (!recv_buf_.Push(event.data.get(), event.length)) { - logger_->Log("recv_buf_.Push %i failed", event.length); + logger_->Log("recv_buf_.Push(%i) failed", event.length); ResetConnection(); return; } @@ -93,7 +93,7 @@ void BridgeTransport::OnRecv(const uvw::data_event& event) { auto message_buf = std::make_unique(size); if (!recv_buf_.Skip(4) || !recv_buf_.Pop(message_buf.get(), unwrapped_size)) { - logger_->Log("recv_buf_.Pop %i failed", size); + logger_->Log("recv_buf_.Pop(%i) failed", size); ResetConnection(); return; } diff --git a/test/BridgeServerMock.cpp b/test/BridgeServerMock.cpp index 0e89f47..dd591e7 100644 --- a/test/BridgeServerMock.cpp +++ b/test/BridgeServerMock.cpp @@ -43,7 +43,7 @@ void BridgeServerMock::CreateConnection() { OnRecv(event); }); connection_handle_->on([this](const uvw::error_event &event, uvw::pipe_handle &) { - logger_->Log("Pipe error: %s", event.what()); + logger_->Log("pipe error: %s", event.what()); StopAsync(); }); @@ -53,7 +53,7 @@ void BridgeServerMock::CreateConnection() { connected_ = true; }); server_handle_->on([this](const uvw::error_event &event, uvw::pipe_handle &) { - logger_->Log("Bind %s error: %s", path_.c_str(), event.what()); + logger_->Log("bind %s error: %s", path_.c_str(), event.what()); StopAsync(); }); diff --git a/test/common/TestBridgeClient.cpp b/test/common/TestBridgeClient.cpp index d281883..fecbc48 100644 --- a/test/common/TestBridgeClient.cpp +++ b/test/common/TestBridgeClient.cpp @@ -39,7 +39,7 @@ void TestBridgeClient() { bool last_logged_position = false; - auto logger = std::static_pointer_cast(std::make_shared("Bridge")); + auto logger = std::static_pointer_cast(std::make_shared("Test")); auto bridge = std::make_shared( logger, [&](const messages::ProtobufMessage& message) { @@ -125,7 +125,7 @@ void TestBridgeClient() { for (const auto& [id, sum] : latency_nanos_sum) { auto avg_latency_nanos = static_cast(latency_nanos_count[id] ? sum / latency_nanos_count[id] : -1); auto avg_latency_ms = duration_cast>(nanoseconds(avg_latency_nanos)); - logger->Log("Avg latency for tracker %i: %.3fms", id, avg_latency_ms.count()); + logger->Log("avg latency for tracker %i: %.3fms", id, avg_latency_ms.count()); } if (invalid_messages) FAIL("Invalid messages received");