PSMoveSteamVRBridge
virtual_controller.h
1 #pragma once
2 #include "trackable_device.h"
3 #include "constants.h"
4 #include "controller.h"
5 #include "PSMoveClient_CAPI.h"
6 #include <openvr_driver.h>
7 
8 namespace steamvrbridge {
10  {
11  public:
12  static const int CONFIG_VERSION;
13 
14  VirtualControllerConfig(const std::string &fnamebase = "VirtualControllerConfig")
15  : ControllerConfig(fnamebase)
16  , extend_Y_meters(0.f)
17  , extend_Z_meters(0.f)
18  , z_rotate_90_degrees(false)
19  , delay_after_touchpad_press(false)
20  , meters_per_touchpad_axis_units()
21  , calibration_offset_meters(0.f)
22  , disable_alignment_gesture(false)
23  , use_orientation_in_hmd_alignment(true)
24  , steamvr_trigger_axis_index(1)
25  , virtual_touchpad_XAxis_index(-1)
26  , virtual_touchpad_YAxis_index(-1)
27  , thumbstick_deadzone(k_defaultThumbstickDeadZoneRadius)
28  , thumbstick_touch_as_press(true)
29  , linear_velocity_multiplier(1.f)
30  , linear_velocity_exponent(0.f)
31  , system_button_id(k_PSMButtonID_Virtual_4) // "Start" button on a xbox 360 controller
32  , hmd_align_button_id(k_PSMButtonID_Virtual_5) // "Back" button on a xbox 360 controller
33  {
34  };
35 
36  configuru::Config WriteToJSON() override;
37  bool ReadFromJSON(const configuru::Config &pt) override;
38 
39  // Virtual extend controller in meters.
40  float extend_Y_meters;
41  float extend_Z_meters;
42 
43  // Rotate controllers orientation 90 degrees about the z-axis (for gun style games).
44  bool z_rotate_90_degrees;
45 
46  // Delay in resetting touchpad position after touchpad press.
47  bool delay_after_touchpad_press;
48 
49  // Settings values. Used to determine whether we'll map controller movement after touchpad
50  // presses to touchpad axis values.
51  float meters_per_touchpad_axis_units;
52 
53  // Settings value: used to determine how many meters in front of the HMD the controller
54  // is held when it's being calibrated.
55  float calibration_offset_meters;
56 
57  // Flag used to completely disable the alignment gesture.
58  bool disable_alignment_gesture;
59 
60  // Flag to tell if we should use the controller orientation as part of the controller alignment.
61  bool use_orientation_in_hmd_alignment;
62 
63  // The axis to use for trigger input
64  int steamvr_trigger_axis_index;
65 
66  // The axes to use for touchpad input (virtual controller only)
67  int virtual_touchpad_XAxis_index;
68  int virtual_touchpad_YAxis_index;
69 
70  // The size of the deadzone for the controller's thumbstick
71  float thumbstick_deadzone;
72 
73  // Treat a thumbstick touch also as a press
74  bool thumbstick_touch_as_press;
75 
76  // Settings values. Used to adjust throwing power using linear velocity and acceleration.
77  float linear_velocity_multiplier;
78  float linear_velocity_exponent;
79 
80  // The button to use as the system button
81  ePSMButtonID system_button_id;
82 
83  // The button to use for controller hmd alignment
84  ePSMButtonID hmd_align_button_id;
85  };
86 
87  /* A trackable Virtual controller (tracking bulb + game pad).
88  The controller class bridges the PSMoveService controller to OpenVR's tracked device.*/
89  class VirtualController : public Controller {
90 
91  public:
92  // Constructor/Destructor
93  VirtualController(PSMControllerID psmControllerID, vr::ETrackedControllerRole trackedControllerRole, const char *psmSerialNo);
94  virtual ~VirtualController();
95 
96  // Overridden Implementation of vr::ITrackedDeviceServerDriver
97  vr::EVRInitError Activate(vr::TrackedDeviceIndex_t unObjectId) override;
98  void Deactivate() override;
99 
100  // TrackableDevice interface implementation
101  vr::ETrackedDeviceClass GetTrackedDeviceClass() const override { return vr::TrackedDeviceClass_Controller; }
102  void Update() override;
103  void RefreshWorldFromDriverPose() override;
104 
105  // IController interface implementation
106  const char *GetControllerSettingsPrefix() const override { return "virtual_controller"; }
107  bool HasPSMControllerId(int ControllerID) const override { return ControllerID == m_nPSMControllerId; }
108  const PSMController * GetPSMControllerView() const override { return m_PSMServiceController; }
109  std::string GetPSMControllerSerialNo() const override { return m_strPSMControllerSerialNo; }
110  PSMControllerType GetPSMControllerType() const override { return PSMController_Virtual; }
111 
112  protected:
113  const VirtualControllerConfig *getConfig() const { return static_cast<const VirtualControllerConfig *>(m_config); }
114  ControllerConfig *AllocateControllerConfig() override {
115  std::string fnamebase= std::string("virtual_controller_") + m_strPSMControllerSerialNo;
116  return new VirtualControllerConfig(fnamebase);
117  }
118 
119  private:
120  void UpdateEmulatedTrackpad();
121  void UpdateControllerState();
122  void UpdateTrackingState();
123 
124  // Controller State
125  int m_nPSMControllerId;
126  PSMController *m_PSMServiceController;
127  std::string m_strPSMControllerSerialNo;
128 
129  // Used to report the controllers calibration status
130  vr::ETrackingResult m_trackingStatus;
131 
132  // Used to ignore old state from PSM Service
133  int m_nPoseSequenceNumber;
134 
135  // True while the touchpad is considered active (touched or pressed)
136  // after the initial touchpad delay, if any.
137  bool m_bTouchpadWasActive;
138 
139  std::chrono::time_point<std::chrono::high_resolution_clock> m_lastTouchpadPressTime;
140  bool m_touchpadDirectionsUsed;
141 
142  std::chrono::time_point<std::chrono::high_resolution_clock> m_resetPoseButtonPressTime;
143  bool m_bResetPoseRequestSent;
144  std::chrono::time_point<std::chrono::high_resolution_clock> m_resetAlignButtonPressTime;
145  bool m_bResetAlignRequestSent;
146 
147  // The position of the controller in meters in driver space relative to its own rotation
148  // at the time when the touchpad was most recently pressed (after being up).
149  PSMVector3f m_posMetersAtTouchpadPressTime;
150 
151  // The orientation of the controller in driver space at the time when
152  // the touchpad was most recently pressed (after being up).
153  PSMQuatf m_driverSpaceRotationAtTouchpadPressTime;
154 
155  // Optional solver used to determine hand orientation.
156  class IHandOrientationSolver *m_orientationSolver;
157 
158  // Callbacks
159  static void start_controller_response_callback(const PSMResponseMessage *response, void *userdata);
160  };
161 }
Definition: controller.h:39
Provides printf-style line logging via the vr::IVRDriverLog interface provided by SteamVR during init...
Definition: config.cpp:18
Definition: virtual_controller.h:89
Definition: virtual_controller.h:9
Definition: controller.h:14
Definition: facing_handsolver.h:5