Skip to content

Commit fc3d5c6

Browse files
committed
Added input paths
1 parent bc717d9 commit fc3d5c6

File tree

3 files changed

+95
-57
lines changed

3 files changed

+95
-57
lines changed

driver_tutorial_devices/src/Driver/ControllerDevice.cpp

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "ControllerDevice.hpp"
2+
#include <string>
23
#include <Windows.h>
34

4-
TutorialDriver::ControllerDevice::ControllerDevice(std::string serial):m_serial(serial)
5+
TutorialDriver::ControllerDevice::ControllerDevice(std::string serial, bool isLeftController):m_serial(serial),m_isLeft(isLeftController)
56
{
67
}
78

@@ -20,43 +21,14 @@ void TutorialDriver::ControllerDevice::update(std::vector<vr::VREvent_t> events)
2021
double deltaTimeSeconds = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastFrameTime).count()/1000.0;
2122
this->m_lastFrameTime = now;
2223

23-
// Setup pose for this frame
24-
auto pose = this->GetPose();
2524

26-
// Get orientation
27-
this->m_yRot += (1.0 * (GetAsyncKeyState(0x66) == 0) - 1.0 * (GetAsyncKeyState(0x64) == 0)) * deltaTimeSeconds;
28-
this->m_xRot += (-1.0 * (GetAsyncKeyState(0x68) == 0) + 1.0 * (GetAsyncKeyState(0x62) == 0)) * deltaTimeSeconds;
29-
this->m_xRot = std::fmax(this->m_xRot, -3.14159/2);
30-
this->m_xRot = std::fmin(this->m_xRot, 3.14159/2);
31-
32-
linalg::vec<float, 4> yQuat{ 0, std::sinf((float)this->m_yRot / 2), 0, std::cosf((float)this->m_yRot / 2) };
33-
34-
linalg::vec<float, 4> xQuat{ std::sinf((float)this->m_xRot / 2), 0, 0, std::cosf((float)this->m_xRot / 2) };
35-
36-
linalg::vec<float, 4> pose_rot = linalg::qmul(yQuat, xQuat);
37-
38-
pose.qRotation.w = pose_rot.w;
39-
pose.qRotation.x = pose_rot.x;
40-
pose.qRotation.y = pose_rot.y;
41-
pose.qRotation.z = pose_rot.z;
42-
43-
// Update position based on rotation
44-
linalg::vec<float, 3> forward_vec{-1.0f * (GetAsyncKeyState(0x65) == 0) + 1.0f * (GetAsyncKeyState(0x60) == 0), 0, 0};
45-
linalg::vec<float, 3> right_vec{0, 0, 0};
46-
linalg::vec<float, 3> final_dir = forward_vec + right_vec;
47-
if (linalg::length(final_dir) > 0.01) {
48-
final_dir = linalg::normalize(final_dir) * (float)deltaTimeSeconds;
49-
final_dir = linalg::qrot(pose_rot, final_dir);
50-
this->m_x += final_dir.x;
51-
this->m_y += final_dir.y;
52-
this->m_z += final_dir.z;
53-
}
5425

55-
pose.vecPosition[0] = this->m_x;
56-
pose.vecPosition[1] = this->m_y;
57-
pose.vecPosition[2] = this->m_z;
26+
27+
5828

5929
// Post pose
30+
auto pose = this->GetPose();
31+
pose.vecPosition[0] = this->m_isLeft ? 1 : -1;
6032
vr::VRServerDriverHost()->TrackedDevicePoseUpdated(this->device_index(), pose, sizeof(vr::DriverPose_t));
6133
}
6234

@@ -74,27 +46,62 @@ vr::EVRInitError TutorialDriver::ControllerDevice::Activate(uint32_t unObjectId)
7446

7547
// Set some universe ID (Must be 2 or higher)
7648
vr::VRProperties()->SetUint64Property(props, vr::Prop_CurrentUniverseId_Uint64, 2);
77-
78-
// Set the IPD to be whatever steam has configured
79-
vr::VRProperties()->SetFloatProperty(props, vr::Prop_UserIpdMeters_Float, vr::VRSettings()->GetFloat(vr::k_pch_SteamVR_Section, vr::k_pch_SteamVR_IPD_Float));
80-
81-
// Set the display FPS
82-
vr::VRProperties()->SetFloatProperty(props, vr::Prop_DisplayFrequency_Float, 90.f);
8349

8450
// Set up a model "number" (not needed but good to have)
85-
vr::VRProperties()->SetStringProperty(props, vr::Prop_ModelNumber_String, "Tutorial HMD Device");
51+
vr::VRProperties()->SetStringProperty(props, vr::Prop_ModelNumber_String, "Tutorial Controller Device");
52+
53+
// Set render model which is visible in SteamVR and some SteamVR apps
54+
vr::VRProperties()->SetStringProperty(props, vr::Prop_RenderModelName_String, "locator");
55+
56+
if (this->m_isLeft) {
57+
vr::VRProperties()->SetInt32Property(props, vr::Prop_ControllerRoleHint_Int32, vr::ETrackedControllerRole::TrackedControllerRole_LeftHand);
58+
} else {
59+
vr::VRProperties()->SetInt32Property(props, vr::Prop_ControllerRoleHint_Int32, vr::ETrackedControllerRole::TrackedControllerRole_RightHand);
60+
}
8661

8762
// Set up icon paths
88-
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceReady_String, "{tutorial_hmd}/icons/hmd_ready.png");
89-
90-
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceOff_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
91-
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceSearching_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
92-
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceSearchingAlert_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
93-
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceReadyAlert_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
94-
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceNotReady_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
95-
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceStandby_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
96-
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceAlertLow_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
63+
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceReady_String, "{tutorial_devices}/icons/controller_ready.png");
64+
65+
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceOff_String, "{tutorial_devices}/icons/controller_not_ready.png");
66+
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceSearching_String, "{tutorial_devices}/icons/controller_not_ready.png");
67+
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceSearchingAlert_String, "{tutorial_devices}/icons/controller_not_ready.png");
68+
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceReadyAlert_String, "{tutorial_devices}/icons/controller_not_ready.png");
69+
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceNotReady_String, "{tutorial_devices}/icons/controller_not_ready.png");
70+
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceStandby_String, "{tutorial_devices}/icons/controller_not_ready.png");
71+
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceAlertLow_String, "{tutorial_devices}/icons/controller_not_ready.png");
72+
73+
// Create input/output paths
74+
vr::VRDriverInput()->CreateHapticComponent(props, "/output/haptic", &this->m_compHaptic);
75+
76+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/a/touch", &this->m_compATouch);
77+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/a/click", &this->m_compAClick);
9778

79+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/b/touch", &this->m_compBTouch);
80+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/b/click", &this->m_compBClick);
81+
82+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/system/touch", &this->m_compSysTouch);
83+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/system/click", &this->m_compSysClick);
84+
85+
vr::VRDriverInput()->CreateScalarComponent(props, "/input/trigger/value", &this->m_compTriggerValue, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
86+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/trigger/touch", &this->m_compTriggerTouch);
87+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/trigger/click", &this->m_compTriggerClick);
88+
89+
vr::VRDriverInput()->CreateScalarComponent(props, "/input/grip/value", &this->m_compGripValue, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
90+
vr::VRDriverInput()->CreateScalarComponent(props, "/input/grip/force", &this->m_compGripForce, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
91+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/grip/touch", &this->m_compGripTouch);
92+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/grip/click", &this->m_compGripClick);
93+
94+
vr::VRDriverInput()->CreateScalarComponent(props, "/input/trackpad/x", &this->m_compTrackpadX, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedTwoSided);
95+
vr::VRDriverInput()->CreateScalarComponent(props, "/input/trackpad/y", &this->m_compTrackpadY, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedTwoSided);
96+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/trackpad/touch", &this->m_compTrackpadTouch);
97+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/trackpad/click", &this->m_compTrackpadClick);
98+
99+
vr::VRDriverInput()->CreateScalarComponent(props, "/input/joystick/x", &this->m_compJoystickX, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedTwoSided);
100+
vr::VRDriverInput()->CreateScalarComponent(props, "/input/joystick/y", &this->m_compJoystickY, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedTwoSided);
101+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/joystick/touch", &this->m_compJoystickTouch);
102+
vr::VRDriverInput()->CreateBooleanComponent(props, "/input/joystick/click", &this->m_compJoystickClick);
103+
104+
98105
this->m_lastFrameTime = std::chrono::system_clock::now();
99106

100107
return vr::EVRInitError::VRInitError_None;

driver_tutorial_devices/src/Driver/ControllerDevice.hpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <chrono>
44
#include <cmath>
5+
#include <string>
56

67
#include <linalg.h>
78

@@ -10,7 +11,7 @@
1011
namespace TutorialDriver {
1112
class ControllerDevice : public IVRDevice {
1213
public:
13-
ControllerDevice(std::string serial);
14+
ControllerDevice(std::string serial, bool leftController);
1415
~ControllerDevice() = default;
1516

1617
// Inherited via IVRDevice
@@ -28,11 +29,41 @@ namespace TutorialDriver {
2829
vr::TrackedDeviceIndex_t m_deviceIndex = vr::k_unTrackedDeviceIndexInvalid;
2930
std::string m_serial;
3031

31-
double m_x = 0, m_y = 0, m_z = 0;
32-
double m_xRot = 0, m_yRot = 0;
32+
vr::VRInputComponentHandle_t m_compHaptic = vr::k_ulInvalidInputComponentHandle;
3333

34-
std::chrono::system_clock::time_point m_lastFrameTime;
35-
34+
vr::VRInputComponentHandle_t m_compATouch = vr::k_ulInvalidInputComponentHandle;
35+
vr::VRInputComponentHandle_t m_compAClick = vr::k_ulInvalidInputComponentHandle;
36+
37+
vr::VRInputComponentHandle_t m_compBTouch = vr::k_ulInvalidInputComponentHandle;
38+
vr::VRInputComponentHandle_t m_compBClick = vr::k_ulInvalidInputComponentHandle;
39+
40+
vr::VRInputComponentHandle_t m_compSysTouch = vr::k_ulInvalidInputComponentHandle;
41+
vr::VRInputComponentHandle_t m_compSysClick = vr::k_ulInvalidInputComponentHandle;
42+
43+
vr::VRInputComponentHandle_t m_compTriggerValue = vr::k_ulInvalidInputComponentHandle;
44+
vr::VRInputComponentHandle_t m_compTriggerTouch = vr::k_ulInvalidInputComponentHandle;
45+
vr::VRInputComponentHandle_t m_compTriggerClick = vr::k_ulInvalidInputComponentHandle;
3646

47+
vr::VRInputComponentHandle_t m_compGripForce = vr::k_ulInvalidInputComponentHandle;
48+
vr::VRInputComponentHandle_t m_compGripValue = vr::k_ulInvalidInputComponentHandle;
49+
vr::VRInputComponentHandle_t m_compGripTouch = vr::k_ulInvalidInputComponentHandle;
50+
vr::VRInputComponentHandle_t m_compGripClick = vr::k_ulInvalidInputComponentHandle;
51+
52+
vr::VRInputComponentHandle_t m_compTrackpadX = vr::k_ulInvalidInputComponentHandle;
53+
vr::VRInputComponentHandle_t m_compTrackpadY = vr::k_ulInvalidInputComponentHandle;
54+
vr::VRInputComponentHandle_t m_compTrackpadTouch = vr::k_ulInvalidInputComponentHandle;
55+
vr::VRInputComponentHandle_t m_compTrackpadClick = vr::k_ulInvalidInputComponentHandle;
56+
57+
vr::VRInputComponentHandle_t m_compJoystickX = vr::k_ulInvalidInputComponentHandle;
58+
vr::VRInputComponentHandle_t m_compJoystickY = vr::k_ulInvalidInputComponentHandle;
59+
vr::VRInputComponentHandle_t m_compJoystickTouch = vr::k_ulInvalidInputComponentHandle;
60+
vr::VRInputComponentHandle_t m_compJoystickClick = vr::k_ulInvalidInputComponentHandle;
61+
62+
double m_trackX, m_trackY;
63+
double m_joyX, m_joyY;
64+
65+
bool m_isLeft = false;
66+
67+
std::chrono::system_clock::time_point m_lastFrameTime;
3768
};
3869
};

driver_tutorial_devices/src/Driver/DeviceProvider.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ vr::EVRInitError TutorialDriver::DeviceProvider::Init(vr::IVRDriverContext* pDri
88
return init_error;
99
}
1010

11-
auto leftControllerDevice = std::make_shared<ControllerDevice>("Tutorial_LeftControllerDevice");
11+
auto leftControllerDevice = std::make_shared<ControllerDevice>("Tutorial_LeftControllerDevice", true);
1212
if (vr::VRServerDriverHost()->TrackedDeviceAdded(leftControllerDevice->serial().c_str(), vr::ETrackedDeviceClass::TrackedDeviceClass_Controller, leftControllerDevice.get()))
1313
this->m_devices.push_back(leftControllerDevice);
1414

15-
auto rightControllerDevice = std::make_shared<ControllerDevice>("Tutorial_RightControllerDevice");
15+
auto rightControllerDevice = std::make_shared<ControllerDevice>("Tutorial_RightControllerDevice", false);
1616
if (vr::VRServerDriverHost()->TrackedDeviceAdded(rightControllerDevice->serial().c_str(), vr::ETrackedDeviceClass::TrackedDeviceClass_Controller, rightControllerDevice.get()))
1717
this->m_devices.push_back(rightControllerDevice);
1818

0 commit comments

Comments
 (0)