mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-29 21:03:17 +02:00
moved vive pulses to correct thread, works with duration
This commit is contained in:
parent
b01a00584a
commit
afca0ec2c9
2 changed files with 45 additions and 49 deletions
|
@ -125,12 +125,6 @@ bool ViveControllerManager::activate() {
|
||||||
auto userInputMapper = DependencyManager::get<controller::UserInputMapper>();
|
auto userInputMapper = DependencyManager::get<controller::UserInputMapper>();
|
||||||
userInputMapper->registerDevice(_inputDevice);
|
userInputMapper->registerDevice(_inputDevice);
|
||||||
_registeredWithInputMapper = true;
|
_registeredWithInputMapper = true;
|
||||||
|
|
||||||
_leftHapticTimer.setSingleShot(true);
|
|
||||||
_rightHapticTimer.setSingleShot(true);
|
|
||||||
connect(&_leftHapticTimer, SIGNAL(timeout()), this, SLOT(hapticPulseHelper(true)));
|
|
||||||
connect(&_rightHapticTimer, SIGNAL(timeout()), this, SLOT(hapticPulseHelper(false)));
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,6 +244,17 @@ void ViveControllerManager::InputDevice::update(float deltaTime, const controlle
|
||||||
handleHandController(deltaTime, leftHandDeviceIndex, inputCalibrationData, true);
|
handleHandController(deltaTime, leftHandDeviceIndex, inputCalibrationData, true);
|
||||||
handleHandController(deltaTime, rightHandDeviceIndex, inputCalibrationData, false);
|
handleHandController(deltaTime, rightHandDeviceIndex, inputCalibrationData, false);
|
||||||
|
|
||||||
|
// handle haptics
|
||||||
|
{
|
||||||
|
Locker locker(_lock);
|
||||||
|
if (_leftHapticDuration > 0.0f) {
|
||||||
|
hapticsHelper(deltaTime, true);
|
||||||
|
}
|
||||||
|
if (_rightHapticDuration > 0.0f) {
|
||||||
|
hapticsHelper(deltaTime, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int numTrackedControllers = 0;
|
int numTrackedControllers = 0;
|
||||||
if (leftHandDeviceIndex != vr::k_unTrackedDeviceIndexInvalid) {
|
if (leftHandDeviceIndex != vr::k_unTrackedDeviceIndexInvalid) {
|
||||||
numTrackedControllers++;
|
numTrackedControllers++;
|
||||||
|
@ -447,27 +452,28 @@ void ViveControllerManager::InputDevice::handlePoseEvent(float deltaTime, const
|
||||||
_poseStateMap[isLeftHand ? controller::LEFT_HAND : controller::RIGHT_HAND] = avatarPose.transform(controllerToAvatar);
|
_poseStateMap[isLeftHand ? controller::LEFT_HAND : controller::RIGHT_HAND] = avatarPose.transform(controllerToAvatar);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ViveControllerManager::hapticPulseHelper(bool leftHand) {
|
|
||||||
if (_inputDevice) {
|
|
||||||
_inputDevice->hapticPulseHelper(leftHand);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ViveControllerManager::InputDevice::hapticPulseHelper(bool leftHand) {
|
|
||||||
if (leftHand) {
|
|
||||||
triggerHapticPulse(prevLeftHapticStrength, prevLeftHapticDuration, leftHand);
|
|
||||||
} else {
|
|
||||||
triggerHapticPulse(prevRightHapticStrength, prevRightHapticDuration, leftHand);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ViveControllerManager::InputDevice::triggerHapticPulse(float strength, float duration, bool leftHand) {
|
bool ViveControllerManager::InputDevice::triggerHapticPulse(float strength, float duration, bool leftHand) {
|
||||||
|
Locker locker(_lock);
|
||||||
|
if (leftHand) {
|
||||||
|
_leftHapticStrength = strength;
|
||||||
|
_leftHapticDuration = duration;
|
||||||
|
} else {
|
||||||
|
_rightHapticStrength = strength;
|
||||||
|
_rightHapticDuration = duration;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ViveControllerManager::InputDevice::hapticsHelper(float deltaTime, bool leftHand) {
|
||||||
auto handRole = leftHand ? vr::TrackedControllerRole_LeftHand : vr::TrackedControllerRole_RightHand;
|
auto handRole = leftHand ? vr::TrackedControllerRole_LeftHand : vr::TrackedControllerRole_RightHand;
|
||||||
auto deviceIndex = _system->GetTrackedDeviceIndexForControllerRole(handRole);
|
auto deviceIndex = _system->GetTrackedDeviceIndexForControllerRole(handRole);
|
||||||
|
|
||||||
if (_system->IsTrackedDeviceConnected(deviceIndex) &&
|
if (_system->IsTrackedDeviceConnected(deviceIndex) &&
|
||||||
_system->GetTrackedDeviceClass(deviceIndex) == vr::TrackedDeviceClass_Controller &&
|
_system->GetTrackedDeviceClass(deviceIndex) == vr::TrackedDeviceClass_Controller &&
|
||||||
_trackedDevicePose[deviceIndex].bPoseIsValid) {
|
_trackedDevicePose[deviceIndex].bPoseIsValid) {
|
||||||
|
float strength = leftHand ? _leftHapticStrength : _rightHapticStrength;
|
||||||
|
float duration = leftHand ? _leftHapticDuration : _rightHapticDuration;
|
||||||
|
|
||||||
// Vive Controllers only support duration up to 4 ms, which is short enough that any variation feels more like strength
|
// Vive Controllers only support duration up to 4 ms, which is short enough that any variation feels more like strength
|
||||||
const float MAX_HAPTIC_TIME = 3999.0f; // in microseconds
|
const float MAX_HAPTIC_TIME = 3999.0f; // in microseconds
|
||||||
float hapticTime = strength*MAX_HAPTIC_TIME;
|
float hapticTime = strength*MAX_HAPTIC_TIME;
|
||||||
|
@ -478,22 +484,13 @@ bool ViveControllerManager::InputDevice::triggerHapticPulse(float strength, floa
|
||||||
// Must wait 5 ms before triggering another pulse on this controller
|
// Must wait 5 ms before triggering another pulse on this controller
|
||||||
// https://github.com/ValveSoftware/openvr/wiki/IVRSystem::TriggerHapticPulse
|
// https://github.com/ValveSoftware/openvr/wiki/IVRSystem::TriggerHapticPulse
|
||||||
const float HAPTIC_RESET_TIME = 5.0f;
|
const float HAPTIC_RESET_TIME = 5.0f;
|
||||||
float remainingHapticTime = duration - (hapticTime / 1000.0f + HAPTIC_RESET_TIME); // in milliseconds
|
float remainingHapticTime = duration - (hapticTime / 1000.0f + deltaTime * 1000.0f); // in milliseconds
|
||||||
if (remainingHapticTime > 0.0f) {
|
if (leftHand) {
|
||||||
if (leftHand) {
|
_leftHapticDuration = remainingHapticTime;
|
||||||
prevLeftHapticStrength = strength;
|
} else {
|
||||||
prevLeftHapticDuration = remainingHapticTime;
|
_rightHapticDuration = remainingHapticTime;
|
||||||
_parent._leftHapticTimer.start(remainingHapticTime);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
prevRightHapticStrength = strength;
|
|
||||||
prevRightHapticDuration = remainingHapticTime;
|
|
||||||
_parent._rightHapticTimer.start(remainingHapticTime);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
controller::Input::NamedVector ViveControllerManager::InputDevice::getAvailableInputs() const {
|
controller::Input::NamedVector ViveControllerManager::InputDevice::getAvailableInputs() const {
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
#define hifi__ViveControllerManager
|
#define hifi__ViveControllerManager
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QTimer>
|
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
|
||||||
#include <GLMHelpers.h>
|
#include <GLMHelpers.h>
|
||||||
|
@ -46,13 +45,10 @@ public:
|
||||||
|
|
||||||
void setRenderControllers(bool renderControllers) { _renderControllers = renderControllers; }
|
void setRenderControllers(bool renderControllers) { _renderControllers = renderControllers; }
|
||||||
|
|
||||||
private slots:
|
|
||||||
void hapticPulseHelper(bool leftHand);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class InputDevice : public controller::InputDevice {
|
class InputDevice : public controller::InputDevice {
|
||||||
public:
|
public:
|
||||||
InputDevice(ViveControllerManager& parent, vr::IVRSystem*& system) : controller::InputDevice("Vive"), _parent(parent), _system(system) {}
|
InputDevice(vr::IVRSystem*& system) : controller::InputDevice("Vive"), _system(system), _leftHapticDuration(0.0f), _rightHapticDuration(0.0f) {}
|
||||||
private:
|
private:
|
||||||
// Device functions
|
// Device functions
|
||||||
controller::Input::NamedVector getAvailableInputs() const override;
|
controller::Input::NamedVector getAvailableInputs() const override;
|
||||||
|
@ -60,8 +56,8 @@ private:
|
||||||
void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
|
void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
|
||||||
void focusOutEvent() override;
|
void focusOutEvent() override;
|
||||||
|
|
||||||
void hapticPulseHelper(bool leftHand);
|
|
||||||
bool triggerHapticPulse(float strength, float duration, bool leftHand) override;
|
bool triggerHapticPulse(float strength, float duration, bool leftHand) override;
|
||||||
|
void hapticsHelper(float deltaTime, bool leftHand);
|
||||||
|
|
||||||
void handleHandController(float deltaTime, uint32_t deviceIndex, const controller::InputCalibrationData& inputCalibrationData, bool isLeftHand);
|
void handleHandController(float deltaTime, uint32_t deviceIndex, const controller::InputCalibrationData& inputCalibrationData, bool isLeftHand);
|
||||||
void handleButtonEvent(float deltaTime, uint32_t button, bool pressed, bool touched, bool isLeftHand);
|
void handleButtonEvent(float deltaTime, uint32_t button, bool pressed, bool touched, bool isLeftHand);
|
||||||
|
@ -97,13 +93,19 @@ private:
|
||||||
FilteredStick _filteredLeftStick;
|
FilteredStick _filteredLeftStick;
|
||||||
FilteredStick _filteredRightStick;
|
FilteredStick _filteredRightStick;
|
||||||
|
|
||||||
|
// perform an action when the InputDevice mutex is acquired.
|
||||||
|
using Locker = std::unique_lock<std::recursive_mutex>;
|
||||||
|
template <typename F>
|
||||||
|
void withLock(F&& f) { Locker locker(_lock); f(); }
|
||||||
|
|
||||||
int _trackedControllers { 0 };
|
int _trackedControllers { 0 };
|
||||||
vr::IVRSystem*& _system;
|
vr::IVRSystem*& _system;
|
||||||
ViveControllerManager& _parent;
|
float _leftHapticStrength;
|
||||||
float prevLeftHapticStrength;
|
float _leftHapticDuration;
|
||||||
float prevLeftHapticDuration;
|
float _rightHapticStrength;
|
||||||
float prevRightHapticStrength;
|
float _rightHapticDuration;
|
||||||
float prevRightHapticDuration;
|
mutable std::recursive_mutex _lock;
|
||||||
|
|
||||||
friend class ViveControllerManager;
|
friend class ViveControllerManager;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -119,10 +121,7 @@ private:
|
||||||
|
|
||||||
bool _renderControllers { false };
|
bool _renderControllers { false };
|
||||||
vr::IVRSystem* _system { nullptr };
|
vr::IVRSystem* _system { nullptr };
|
||||||
std::shared_ptr<InputDevice> _inputDevice { std::make_shared<InputDevice>(*this, _system) };
|
std::shared_ptr<InputDevice> _inputDevice { std::make_shared<InputDevice>(_system) };
|
||||||
|
|
||||||
QTimer _leftHapticTimer;
|
|
||||||
QTimer _rightHapticTimer;
|
|
||||||
|
|
||||||
static const QString NAME;
|
static const QString NAME;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue