diff --git a/examples/plugin/CMakeLists.txt b/examples/plugin/CMakeLists.txt index 1d11959a3..a1f9a4d79 100644 --- a/examples/plugin/CMakeLists.txt +++ b/examples/plugin/CMakeLists.txt @@ -13,6 +13,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) @@ -74,6 +75,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 + 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}) diff --git a/examples/plugin/com_osvr_example_TrackerSpin.cpp b/examples/plugin/com_osvr_example_TrackerSpin.cpp new file mode 100644 index 000000000..9a0a7e8e7 --- /dev/null +++ b/examples/plugin/com_osvr_example_TrackerSpin.cpp @@ -0,0 +1,182 @@ +/** @file + @brief Implementation + + @date 2016 + + @author + Sensics, Inc. + +*/ + +// 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 +#include +#include + +// Generated JSON header file +#include "com_osvr_example_TrackerSpin_json.h" + +// Library/third-party includes +#include +#include + +// Standard includes +#include +#include +#include + +// 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(×tamp); + 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(¤tTime); + /// time to report + if (osvrTimeValueDurationSeconds(¤tTime, ×tamp) >= + 1.0 / reportRate) { + timestamp.seconds = currentTime.seconds; + timestamp.microseconds = currentTime.microseconds; + double duration = + osvrTimeValueDurationSeconds(¤tTime, &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, ×tamp); + } + + 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; +} diff --git a/examples/plugin/com_osvr_example_TrackerSpin.json b/examples/plugin/com_osvr_example_TrackerSpin.json new file mode 100644 index 000000000..65ab7e2a2 --- /dev/null +++ b/examples/plugin/com_osvr_example_TrackerSpin.json @@ -0,0 +1,22 @@ +{ + "deviceVendor": "OSVR", + "deviceName": "Tracker Spin", + "author": "Georgiy Frolov ", + "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" + } +} \ No newline at end of file diff --git a/examples/plugin/org_osvr_example_Tracker.json b/examples/plugin/org_osvr_example_Tracker.json index 281ff9e13..739431099 100644 --- a/examples/plugin/org_osvr_example_Tracker.json +++ b/examples/plugin/org_osvr_example_Tracker.json @@ -8,7 +8,7 @@ "tracker": { "count": 1, "bounded": true, - "position": false, + "position": true, "orientation": true } }, diff --git a/examples/plugin/osvr_server_config.TrackerSpin.sample.json b/examples/plugin/osvr_server_config.TrackerSpin.sample.json new file mode 100644 index 000000000..4c23e45af --- /dev/null +++ b/examples/plugin/osvr_server_config.TrackerSpin.sample.json @@ -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) */ + } + }] +} \ No newline at end of file