OSVR-Core
VrpnTrackerServer.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_VrpnTrackerServer_h_GUID_15AD76EE_C97F_4FBC_1EC5_A73576D5BAAC
26 #define INCLUDED_VrpnTrackerServer_h_GUID_15AD76EE_C97F_4FBC_1EC5_A73576D5BAAC
27 
28 // Internal Includes
29 #include "DeviceConstructionData.h"
32 
33 // Library/third-party includes
34 #include <quat.h>
35 #include <vrpn_Tracker.h>
36 
37 // Standard includes
38 // - none
39 
40 namespace osvr {
41 namespace connection {
42  class VrpnTrackerServer : public vrpn_Tracker,
43  public TrackerServerInterface {
44  public:
45  typedef vrpn_Tracker Base;
47  : vrpn_Tracker(init.getQualifiedName().c_str(), init.conn) {
48  // Initialize data
49  m_resetPos();
50  m_resetQuat();
51 
52  m_resetVel();
53  m_resetAccel();
54 
55  // Report interface out.
56  init.obj.returnTrackerInterface(*this);
57  }
58  static const vrpn_uint32 CLASS_OF_SERVICE = vrpn_CONNECTION_LOW_LATENCY;
59 
60  void sendReport(OSVR_PositionState const &val,
61  OSVR_ChannelCount sensor,
62  util::time::TimeValue const &tv) override {
63  m_resetQuat();
64  osvrVec3ToQuatlib(Base::pos, &val);
65  m_sendPose(sensor, tv);
66  }
67 
68  void sendReport(OSVR_OrientationState const &val,
69  OSVR_ChannelCount sensor,
70  util::time::TimeValue const &tv) override {
71  m_resetPos();
72  osvrQuatToQuatlib(Base::d_quat, &val);
73  m_sendPose(sensor, tv);
74  }
75 
76  void sendReport(OSVR_PoseState const &val,
77  OSVR_ChannelCount sensor,
78  util::time::TimeValue const &tv) override {
79  osvrVec3ToQuatlib(Base::pos, &(val.translation));
80  osvrQuatToQuatlib(Base::d_quat, &(val.rotation));
81  m_sendPose(sensor, tv);
82  }
83 
84  void sendVelReport(OSVR_VelocityState const &val,
85  OSVR_ChannelCount sensor,
86  util::time::TimeValue const &tv) override {
87  osvrVec3ToQuatlib(Base::vel, &(val.linearVelocity));
88  osvrQuatToQuatlib(Base::vel_quat,
89  &(val.angularVelocity.incrementalRotation));
90  Base::vel_quat_dt = val.angularVelocity.dt;
91  m_sendVelocity(sensor, tv);
92  }
93  void sendVelReport(OSVR_LinearVelocityState const &val,
94  OSVR_ChannelCount sensor,
95  util::time::TimeValue const &tv) override {
96  m_resetVel();
97 
98  osvrVec3ToQuatlib(Base::vel, &val);
99  m_sendVelocity(sensor, tv);
100  }
101  void sendVelReport(OSVR_AngularVelocityState const &val,
102  OSVR_ChannelCount sensor,
103  util::time::TimeValue const &tv) override {
104  m_resetVel();
105 
106  osvrQuatToQuatlib(Base::vel_quat, &(val.incrementalRotation));
107  Base::vel_quat_dt = val.dt;
108  m_sendVelocity(sensor, tv);
109  }
110 
111  void sendAccelReport(OSVR_AccelerationState const &val,
112  OSVR_ChannelCount sensor,
113  util::time::TimeValue const &tv) override {
114  osvrVec3ToQuatlib(Base::acc, &(val.linearAcceleration));
115  osvrQuatToQuatlib(Base::acc_quat,
116  &(val.angularAcceleration.incrementalRotation));
117  Base::acc_quat_dt = val.angularAcceleration.dt;
118  m_sendAccel(sensor, tv);
119  }
120  void sendAccelReport(OSVR_LinearAccelerationState const &val,
121  OSVR_ChannelCount sensor,
122  util::time::TimeValue const &tv) override {
123  m_resetAccel();
124 
125  osvrVec3ToQuatlib(Base::acc, &val);
126  m_sendAccel(sensor, tv);
127  }
128  void sendAccelReport(OSVR_AngularAccelerationState const &val,
129  OSVR_ChannelCount sensor,
130  util::time::TimeValue const &tv) override {
131  m_resetVel();
132 
133  osvrQuatToQuatlib(Base::acc_quat, &(val.incrementalRotation));
134  Base::acc_quat_dt = val.dt;
135  m_sendAccel(sensor, tv);
136  }
137 
138  private:
139  void m_resetVec3(vrpn_float64 vec[3]) {
140  vec[0] = 0;
141  vec[1] = 0;
142  vec[2] = 0;
143  }
144  void m_resetPos() { m_resetVec3(pos); }
145  void m_resetQuat(vrpn_float64 quat[4]) {
146  quat[Q_W] = 1;
147  quat[Q_X] = 0;
148  quat[Q_Y] = 0;
149  quat[Q_Z] = 0;
150  }
151  void m_resetQuat() { m_resetQuat(d_quat); }
152 
153  void m_resetVel() {
154  m_resetVec3(Base::vel);
155  m_resetQuat(Base::vel_quat);
156  Base::vel_quat_dt = 0;
157  }
158 
159  void m_resetAccel() {
160  m_resetVec3(Base::acc);
161  m_resetQuat(Base::acc_quat);
162  Base::acc_quat_dt = 0;
163  }
164 
165  void m_sendPose(OSVR_ChannelCount sensor,
166  util::time::TimeValue const &ts) {
167 
168  Base::d_sensor = sensor;
169  util::time::toStructTimeval(Base::timestamp, ts);
170  char msgbuf[1000];
171  vrpn_int32 len = Base::encode_to(msgbuf);
172  d_connection->pack_message(len, Base::timestamp,
173  Base::position_m_id, Base::d_sender_id,
174  msgbuf, CLASS_OF_SERVICE);
175  }
176 
177  void m_sendVelocity(OSVR_ChannelCount sensor,
178  util::time::TimeValue const &ts) {
179 
180  Base::d_sensor = sensor;
181  util::time::toStructTimeval(Base::timestamp, ts);
182  char msgbuf[1000];
183  vrpn_int32 len = Base::encode_vel_to(msgbuf);
184  d_connection->pack_message(len, Base::timestamp,
185  Base::velocity_m_id, Base::d_sender_id,
186  msgbuf, CLASS_OF_SERVICE);
187  }
188 
189  void m_sendAccel(OSVR_ChannelCount sensor,
190  util::time::TimeValue const &ts) {
191 
192  Base::d_sensor = sensor;
193  util::time::toStructTimeval(Base::timestamp, ts);
194  char msgbuf[1000];
195  vrpn_int32 len = Base::encode_acc_to(msgbuf);
196  d_connection->pack_message(len, Base::timestamp, Base::accel_m_id,
197  Base::d_sender_id, msgbuf,
198  CLASS_OF_SERVICE);
199  }
200  };
201 
202 } // namespace connection
203 } // namespace osvr
204 
205 #endif // INCLUDED_VrpnTrackerServer_h_GUID_15AD76EE_C97F_4FBC_1EC5_A73576D5BAAC
Struct for combined acceleration state.
Definition: ClientReportTypesC.h:107
Definition: VrpnTrackerServer.h:42
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Definition: ChannelCountC.h:51
Interface for external access to generating tracker reports.
Definition: TrackerServerInterface.h:46
void returnTrackerInterface(osvr::connection::TrackerServerInterface &iface)
Returns a tracker interface through the pointer-pointer.
Definition: DeviceInitObject.cpp:113
A structure defining a 3D vector, often a position/translation.
Definition: Vec3C.h:48
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
OSVR_Quaternion rotation
Orientation as a unit quaternion.
Definition: Pose3C.h:58
A structure defining a quaternion, often a unit quaternion representing 3D rotation.
Definition: QuaternionC.h:49
The quaternion represents the incremental rotation taking place over a period of dt seconds...
Definition: ClientReportTypesC.h:75
Definition: DeviceConstructionData.h:41
A structure defining a 3D (6DOF) rigid body pose: translation and rotation.
Definition: Pose3C.h:54
Struct for combined velocity state.
Definition: ClientReportTypesC.h:87
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