cmbruns / pyopenvr

Unofficial python bindings for Valve's OpenVR virtual reality SDK
BSD 3-Clause "New" or "Revised" License
245 stars 39 forks source link

Establishing VSYNC timing of HMD - GetTimeSinceLastVsync #84

Closed mungewell closed 3 years ago

mungewell commented 3 years ago

I am tinkering on a DIY project and want to establish the timing of HMD's VSYNC, so that I can calculate a predicted Rotation/Position to send through the system.

It looks like OpenVR has a call "GetTimeSinceLastVsync" https://github.com/ValveSoftware/openvr/blob/0eb70325a4ea3edca2a0ff8aefa631f2529e6eb7/headers/openvr.h#L2046

But this is not implement in pyOpenVR. :-(

Is this the correct method, or is there a better way to sync the timing?

There do seem to be properties for this: https://github.com/cmbruns/pyopenvr/blob/129e38228cbf61d129ed9757a177ea6cf4aa7fe7/src/translate/openvr_capi.h#L532 https://github.com/ValveSoftware/openvr/blob/0eb70325a4ea3edca2a0ff8aefa631f2529e6eb7/headers/openvr.h#L366

risa2000 commented 3 years ago

I am tinkering on a DIY project and want to establish the timing of HMD's VSYNC, so that I can calculate a predicted Rotation/Position to send through the system.

You should look into IVRCompositor::WaitGetPoses documentation (I know it is not easy to find), because this will get you (I believe) exactly what you need.

For what concerns IVRSystem::GetTimeSinceLastVsync not being implemented in pyOpenVR, I will look into it.

cmbruns commented 3 years ago

But this is not implement in pyOpenVR. :-(

How hard did you look?

https://github.com/cmbruns/pyopenvr/blob/129e38228cbf61d129ed9757a177ea6cf4aa7fe7/src/openvr/__init__.py#L2921

Did you try VRSystem().getTimeSinceLastVsync() ? If so, what exactly went wrong?

mungewell commented 3 years ago

I think probably about '2 out of 10'. It was late and I think I was using GitHub search, which still flags 0 hits... so perhaps that's broken at the moment.

Thanks for the follow up. I'll try both methods and see what works for me. Simon.

mungewell commented 3 years ago

So again, with minimum effort expended, a slight mod to the example 'track_hmd.py' script.

    print(hmd_pose.mDeviceToAbsoluteTracking)

    print(openvr.VRSystem().getTimeSinceLastVsync())
    sys.stdout.flush()
    time.sleep(0.2)

I got plausible results. 60Hz screen/HMD rate, 0.2s sleep...

[[0.9185850024223328, 0.0, -0.3952234089374542, -0.3952234089374542], [0.0, 1.0, 0.0, -24.69011116027832], [0.3952234089374542, 0.0, 0.9185850024223328, 0.9185850024223328]]
(1, 0.007271000184118748, 44916)
[[0.9185850024223328, 0.0, -0.3952234089374542, -0.3952234089374542], [0.0, 1.0, 0.0, -24.69011116027832], [0.3952234089374542, 0.0, 0.9185850024223328, 0.9185850024223328]]
(1, 0.009304800070822239, 44928)
[[0.9185850024223328, 0.0, -0.3952234089374542, -0.3952234089374542], [0.0, 1.0, 0.0, -24.69011116027832], [0.3952234089374542, 0.0, 0.9185850024223328, 0.9185850024223328]]
(1, 0.012063900008797646, 44940)
[[0.9185850024223328, 0.0, -0.3952234089374542, -0.3952234089374542], [0.0, 1.0, 0.0, -24.69011116027832], [0.3952234089374542, 0.0, 0.9185850024223328, 0.9185850024223328]]
(1, -0.0022718000691384077, 44953)
[[0.9185850024223328, 0.0, -0.3952234089374542, -0.3952234089374542], [0.0, 1.0, 0.0, -24.69011116027832], [0.3952234089374542, 0.0, 0.9185850024223328, 0.9185850024223328]]
(1, -0.000631399976555258, 44965)

Though I'm not sure how a 'time since' something can be a negative value.

I think that this is enough to get a real script going. Thanks for the help, and I'll re-open if have more issues.

mungewell commented 3 years ago

Not tested with PSVR yet (as that's not available ATM), but seems to function as expected. https://github.com/mungewell/pyPSVR/blob/master/example4_vridge_sensors/reposition.py#L123

mungewell commented 3 years ago

Minor detail. need to start OpenVR with "VRApplication_Overlay" or it stops (or is stopped by) true application.