Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TrackerSpin virtual plugin #519

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(OSVR_EXAMPLE_DEVICE_PLUGINS_SIMPLE
set(OSVR_EXAMPLE_DEVICE_PLUGINS
com_osvr_example_selfcontained
org_osvr_example_SampleCPlugin
com_osvr_example_TrackerSpin
${OSVR_EXAMPLE_DEVICE_PLUGINS_SIMPLE}
CACHE INTERNAL "" FORCE)

Expand Down Expand Up @@ -72,6 +73,22 @@ osvr_add_plugin(NAME org_osvr_example_SampleCPlugin
set_target_properties(org_osvr_example_SampleCPlugin PROPERTIES
FOLDER "OSVR Example Plugins")

## Build the TrackerSpin example
osvr_convert_json(com_osvr_example_TrackerSpin_json
com_osvr_example_TrackerSpin.json
"${CMAKE_CURRENT_BINARY_DIR}/com_osvr_example_TrackerSpin_json.h")
osvr_add_plugin(com_osvr_example_TrackerSpin
NO_INSTALL MANUAL_LOAD CPP
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we actually get this installed, since it's useful for testing? I don't think we need manual_load here either, since it has a constructor argument.

SOURCES
com_osvr_example_TrackerSpin.cpp
"${CMAKE_CURRENT_BINARY_DIR}/com_osvr_example_TrackerSpin_json.h")
target_link_libraries(com_osvr_example_TrackerSpin
JsonCpp::JsonCpp
vendored-vrpn
osvr_cxx11_flags)
set_target_properties(com_osvr_example_TrackerSpin PROPERTIES
FOLDER "OSVR Example Plugins")


# Put all the example plugins in a suitable solution folder.
foreach(pluginname ${OSVR_EXAMPLE_DEVICE_PLUGINS})
Expand Down
182 changes: 182 additions & 0 deletions examples/plugin/com_osvr_example_TrackerSpin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/** @file
@brief Implementation

@date 2016

@author
Sensics, Inc.
<http://sensics.com/osvr>
*/

// Copyright 2016 Sensics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Internal Includes
#include <osvr/PluginKit/PluginKit.h>
#include <osvr/PluginKit/TrackerInterfaceC.h>
#include <quat/quat.h>

// Generated JSON header file
#include "com_osvr_example_TrackerSpin_json.h"

// Library/third-party includes
#include <json/reader.h>
#include <json/value.h>

// Standard includes
#include <iostream>
#include <memory>
#include <vector>

// Anonymous namespace to avoid symbol collision
namespace {

static const auto DRIVER_NAME = "TrackerSpin";
static double inline getDefaultReportRate() { return 200.0; }
static double inline getDefaultXSpin() { return 0.0; }
static double inline getDefaultYSpin() { return 1.0; }
static double inline getDefaultZSpin() { return 0.0; }
static double inline getDefaultRotationRate() { return 0.1; }

class TrackerSpinDevice {
public:
TrackerSpinDevice(OSVR_PluginRegContext ctx, std::string const &name,
double &axisX, double &axisY, double &axisZ,
double &updateRate, double &rotationRate)
: x(axisX), y(axisY), z(axisZ), reportRate(updateRate),
spinRate(rotationRate) {

if (spinRate < 0.) {
x *= -1.;
y *= -1.;
z *= -1.;
}

double dt;
if (spinRate == 0.) {
dt = 1.0;
} else {
dt = 0.9 * (0.5 / spinRate);
}
q_from_axis_angle(vel_quat, x, y, z, dt * spinRate * Q_PI);
vel_quat_dt = dt;
osvrTimeValueGetNow(&timestamp);
osvrTimeValueGetNow(&start);

/// Create the initialization options
OSVR_DeviceInitOptions opts = osvrDeviceCreateInitOptions(ctx);

osvrDeviceTrackerConfigure(opts, &m_tracker);

/// Create the device token with the options
m_dev.initAsync(ctx, name, opts);

/// Send JSON descriptor
m_dev.sendJsonDescriptor(com_osvr_example_TrackerSpin_json);

/// Register update callback
m_dev.registerUpdateCallback(this);
}

OSVR_ReturnCode update() {

osvrTimeValueGetNow(&currentTime);
/// time to report
if (osvrTimeValueDurationSeconds(&currentTime, &timestamp) >=
1.0 / reportRate) {
timestamp.seconds = currentTime.seconds;
timestamp.microseconds = currentTime.microseconds;
double duration =
osvrTimeValueDurationSeconds(&currentTime, &start);
q_from_axis_angle(d_quat, x, y, z, duration * spinRate * 2 * Q_PI);
OSVR_OrientationState state;
osvrQuatSetW(&state, d_quat[3]);
osvrQuatSetX(&state, d_quat[0]);
osvrQuatSetY(&state, d_quat[1]);
osvrQuatSetZ(&state, d_quat[2]);
osvrDeviceTrackerSendOrientationTimestamped(m_dev, m_tracker,
&state, 0, &timestamp);
}

OSVR_OrientationState orientState;

return OSVR_RETURN_SUCCESS;
}

private:
OSVR_TrackerDeviceInterface m_tracker;
osvr::pluginkit::DeviceToken m_dev;
double x, y, z, spinRate, reportRate, vel_quat_dt;
q_type vel_quat, d_quat;
OSVR_TimeValue timestamp, currentTime, start;
};

class TrackerSpinCreate {
public:
TrackerSpinCreate() : m_found(false) {}
OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx, const char *params) {

if (m_found) {
return OSVR_RETURN_SUCCESS;
}

m_found = true;

Json::Value root;
{
Json::Reader reader;
if (!reader.parse(params, root)) {
std::cerr << "Couldn't parse JSON for tracker spin!"
<< std::endl;
return OSVR_RETURN_FAILURE;
}
}

auto updateRate =
root.get("report_rate", getDefaultReportRate()).asDouble();
auto xAxisSpin =
root.get("x_of_axis_to_spin_around", getDefaultXSpin()).asDouble();
auto yAxisSpin =
root.get("y_of_axis_to_spin_around", getDefaultYSpin()).asDouble();
auto zAxisSpin =
root.get("z_of_axis_to_spin_around", getDefaultZSpin()).asDouble();
auto spinRate = root.get("rotation_rate_around_axis_in_Hz",
getDefaultRotationRate())
.asDouble();
// optional
auto deviceName = root.get("name", DRIVER_NAME).asString();

osvr::pluginkit::PluginContext context(ctx);

/// @todo make the token own this instead once there is API for that.
context.registerObjectForDeletion(
new TrackerSpinDevice(ctx, deviceName, xAxisSpin, yAxisSpin,
zAxisSpin, updateRate, spinRate));
return OSVR_RETURN_SUCCESS;
}

private:
bool m_found;
};
} // namespace

OSVR_PLUGIN(com_osvr_example_TrackerSpin) {
osvr::pluginkit::PluginContext context(ctx);

/// Register a detection callback function object.
context.registerDriverInstantiationCallback(DRIVER_NAME,
TrackerSpinCreate());

return OSVR_RETURN_SUCCESS;
}
22 changes: 22 additions & 0 deletions examples/plugin/com_osvr_example_TrackerSpin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"deviceVendor": "OSVR",
"deviceName": "Tracker Spin",
"author": "Georgiy Frolov <georgiy@sensics.com>",
"version": 1,
"lastModified": "2017-01-09T21:13:07.585Z",
"interfaces": {
"tracker": {
"position": false,
"orientation": true,
"cout": 1
}
},
"semantic": {
"head": {
"$target": "tracker/0"
}
},
"automaticAliases": {
"/me/head": "semantic/head"
}
}
2 changes: 1 addition & 1 deletion examples/plugin/org_osvr_example_Tracker.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"tracker": {
"count": 1,
"bounded": true,
"position": false,
"position": true,
"orientation": true
}
},
Expand Down
15 changes: 15 additions & 0 deletions examples/plugin/osvr_server_config.TrackerSpin.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"desciprtion" : "This plugin is adapter from VRPN Tracker Spin, which is a virtual plugin for a tracker that stays around the origin and spin around the specified axis at the specified rate of rotation, reporting orientation at specified rate. It can be used to test the smoothness of rendering for VR system."
"drivers": [{
"plugin": "com_osvr_example_TrackerSpin",
"driver": "TrackerSpin",
"params": {
"report_rate": 400.0, /* (default 200.0) - Rate at which reports will be sent */
"x_of_axis_to_spin_around": 0.0, /* (default 0.0) - How much to spin around X axis */
"y_of_axis_to_spin_around": 1.0, /* (default 1.0) - How much to spin around Y axis */
"z_of_axis_to_spin_around": 0.0, /* (default 0.0) - How much to spin around Z axis */
"rotation_rate_around_axis_in_Hz": 0.1, /* (default 0.1) - How fast to spin the tracker */
"name": "TrackerSpin" /* (optional) */
}
}]
}