OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
org_osvr_example_Tracker.cpp
1 
7 // Copyright 2016 Sensics Inc.
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 
21 // Internal Includes
24 
25 // Generated JSON header file
26 #include "org_osvr_example_Tracker_json.h"
27 
28 // Library/third-party includes
29 
30 // Standard includes
31 #include <chrono>
32 #include <thread>
33 
34 // Anonymous namespace to avoid symbol collision
35 namespace {
36 
37 class TrackerDevice {
38  public:
39  TrackerDevice(OSVR_PluginRegContext ctx) {
42 
43  // configure our tracker
44  osvrDeviceTrackerConfigure(opts, &m_tracker);
45 
47  m_dev.initAsync(ctx, "Tracker", opts);
48 
50  m_dev.sendJsonDescriptor(org_osvr_example_Tracker_json);
51 
53  m_dev.registerUpdateCallback(this);
54  }
55 
56  OSVR_ReturnCode update() {
57 
58  std::this_thread::sleep_for(std::chrono::milliseconds(
59  5)); // Simulate waiting a quarter second for data.
60 
61  OSVR_TimeValue now;
62  osvrTimeValueGetNow(&now);
63  double t = 5.0 * ((double)now.seconds + ((double)now.microseconds / 1000000.0));
64 
66  OSVR_PoseState pose;
67  osvrPose3SetIdentity(&pose);
68  pose.translation.data[0] = std::sin(t) * 0.25;
69  pose.translation.data[1] = std::cos(t + 0.5) * 0.25;
70  pose.translation.data[2] = std::sin(t + 0.25) * 0.25;
71  osvrDeviceTrackerSendPose(m_dev, m_tracker, &pose, 0);
72 
73  return OSVR_RETURN_SUCCESS;
74  }
75 
76  private:
79 };
80 
81 class HardwareDetection {
82  public:
83  HardwareDetection() : m_found(false) {}
84  OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx) {
85 
86  if (m_found) {
87  return OSVR_RETURN_SUCCESS;
88  }
89 
91  m_found = true;
92 
94  osvr::pluginkit::registerObjectForDeletion(ctx, new TrackerDevice(ctx));
95 
96  return OSVR_RETURN_SUCCESS;
97  }
98 
99  private:
100  bool m_found;
101 };
102 } // namespace
103 
104 OSVR_PLUGIN(org_osvr_example_Tracker) {
105 
106  osvr::pluginkit::PluginContext context(ctx);
107 
109  context.registerHardwareDetectCallback(new HardwareDetection());
110 
111  return OSVR_RETURN_SUCCESS;
112 }
Wrapper class for OSVR_DeviceToken.
struct OSVR_TrackerDeviceInterfaceObject * OSVR_TrackerDeviceInterface
Opaque type used in conjunction with a device token to send data on a tracker interface.
void osvrTimeValueGetNow(OSVR_TimeValue *dest)
Gets the current time in the TimeValue. Parallel to gettimeofday.
C++ wrapper class for the opaque plugin context.
Definition: PluginKit.h:59
OSVR_ReturnCode osvrDeviceTrackerSendPose(OSVR_DeviceToken dev, OSVR_TrackerDeviceInterface iface, OSVR_PoseState const *val, OSVR_ChannelCount sensor)
Report the full rigid body pose of a sensor, automatically generating a timestamp.
double data[3]
Internal array data.
Definition: Vec3C.h:50
T * registerObjectForDeletion(OSVR_PluginRegContext ctx, T *obj)
Registers an object to be destroyed with delete when the plugin is unloaded.
struct OSVR_DeviceInitObject * OSVR_DeviceInitOptions
Opaque type of a device initialization object.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
OSVR_DeviceInitOptions osvrDeviceCreateInitOptions(OSVR_PluginRegContext ctx)
Create a OSVR_DeviceInitOptions object.
Header including the full PluginKit C++ interface.
#define OSVR_PLUGIN(PLUGIN_NAME)
This macro begins the entry point function of your plugin.
OSVR_TimeValue_Microseconds microseconds
Microseconds portion of the time value.
Definition: TimeValueC.h:85
A structure defining a 3D (6DOF) rigid body pose: translation and rotation.
Definition: Pose3C.h:54
OSVR_ReturnCode osvrDeviceTrackerConfigure(OSVR_DeviceInitOptions opts, OSVR_TrackerDeviceInterface *iface)
Specify that your device will implement the Tracker interface.
OSVR_EXTERN_C_BEGIN typedef void * OSVR_PluginRegContext
A context pointer passed in to your plugin's entry point and other locations of control flow transfer...
void osvrPose3SetIdentity(OSVR_Pose3 *pose)
Set a pose to identity.
Definition: Pose3C.h:62
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
OSVR_Vec3 translation
Position vector.
Definition: Pose3C.h:56
OSVR_TimeValue_Seconds seconds
Seconds portion of the time value.
Definition: TimeValueC.h:83