Skip to content

Commit

Permalink
Send HMD battery at 10Hz
Browse files Browse the repository at this point in the history
  • Loading branch information
0forks committed Nov 1, 2023
1 parent d4349fb commit fdd6e9a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
28 changes: 12 additions & 16 deletions src/VRDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<messages::Battery>(&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<std::chrono::milliseconds>(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<messages::Battery>(&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");
Expand Down
1 change: 1 addition & 0 deletions src/VRDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace SlimeVRDriver {
std::map<std::string, std::shared_ptr<IVRDevice>> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/bridge/BridgeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void BridgeClient::CreateConnection() {
});
connection_handle_->on<uvw::error_event>([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();
Expand Down
4 changes: 2 additions & 2 deletions src/bridge/BridgeTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -93,7 +93,7 @@ void BridgeTransport::OnRecv(const uvw::data_event& event) {

auto message_buf = std::make_unique<char[]>(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;
}
Expand Down
4 changes: 2 additions & 2 deletions test/BridgeServerMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void BridgeServerMock::CreateConnection() {
OnRecv(event);
});
connection_handle_->on<uvw::error_event>([this](const uvw::error_event &event, uvw::pipe_handle &) {
logger_->Log("Pipe error: %s", event.what());
logger_->Log("pipe error: %s", event.what());
StopAsync();
});

Expand All @@ -53,7 +53,7 @@ void BridgeServerMock::CreateConnection() {
connected_ = true;
});
server_handle_->on<uvw::error_event>([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();
});

Expand Down
4 changes: 2 additions & 2 deletions test/common/TestBridgeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void TestBridgeClient() {

bool last_logged_position = false;

auto logger = std::static_pointer_cast<Logger>(std::make_shared<ConsoleLogger>("Bridge"));
auto logger = std::static_pointer_cast<Logger>(std::make_shared<ConsoleLogger>("Test"));
auto bridge = std::make_shared<BridgeClient>(
logger,
[&](const messages::ProtobufMessage& message) {
Expand Down Expand Up @@ -125,7 +125,7 @@ void TestBridgeClient() {
for (const auto& [id, sum] : latency_nanos_sum) {
auto avg_latency_nanos = static_cast<int>(latency_nanos_count[id] ? sum / latency_nanos_count[id] : -1);
auto avg_latency_ms = duration_cast<duration<double, std::milli>>(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");
Expand Down

0 comments on commit fdd6e9a

Please sign in to comment.