Panda3D
vrpnTracker.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file vrpnTracker.cxx
10  * @author drose
11  * @date 2001-01-25
12  */
13 
14 #include "vrpnTracker.h"
15 #include "vrpnTrackerDevice.h"
16 #include "vrpnClient.h"
17 #include "config_vrpn.h"
18 
19 #include "indent.h"
20 
21 #include <algorithm>
22 
23 /**
24  *
25  */
26 VrpnTracker::
27 VrpnTracker(const std::string &tracker_name, vrpn_Connection *connection) :
28  _tracker_name(tracker_name)
29 {
30  _tracker = new vrpn_Tracker_Remote(_tracker_name.c_str(), connection);
31 
32  _tracker->register_change_handler((void*)this, &vrpn_position_callback);
33  _tracker->register_change_handler((void*)this, &vrpn_velocity_callback);
34  _tracker->register_change_handler((void*)this, &vrpn_acceleration_callback);
35 }
36 
37 /**
38  *
39  */
40 VrpnTracker::
41 ~VrpnTracker() {
42  delete _tracker;
43 }
44 
45 /**
46  * Adds the indicated VrpnTrackerDevice to the list of devices that are
47  * sharing this VrpnTracker.
48  */
49 void VrpnTracker::
51  if (vrpn_cat.is_debug()) {
52  vrpn_cat.debug() << *this << " marking " << *device << "\n";
53  }
54  _devices.push_back(device);
55 }
56 
57 /**
58  * Removes the indicated VrpnTrackerDevice from the list of devices that are
59  * sharing this VrpnTracker.
60  */
61 void VrpnTracker::
63  if (vrpn_cat.is_debug()) {
64  vrpn_cat.debug() << *this << " unmarking " << *device << "\n";
65  }
66 
67  Devices::iterator di =
68  find(_devices.begin(), _devices.end(), device);
69 
70  if (di != _devices.end()) {
71  _devices.erase(di);
72  }
73 }
74 
75 /**
76  *
77  */
78 void VrpnTracker::
79 output(std::ostream &out) const {
80  out << _tracker_name;
81 }
82 
83 /**
84  *
85  */
86 void VrpnTracker::
87 write(std::ostream &out, int indent_level) const {
88  indent(out, indent_level)
89  << get_tracker_name() << " ("
90  << _devices.size() << " devices)\n";
91 }
92 
93 /**
94  * Receives the tracker positional data from the VRPN code and sends it to any
95  * interested VrpnTrackerDevices.
96  */
97 void VRPN_CALLBACK VrpnTracker::
98 vrpn_position_callback(void *userdata, const vrpn_TRACKERCB info) {
99  VrpnTracker *self = (VrpnTracker *)userdata;
100  if (vrpn_cat.is_spam()) {
101  vrpn_cat.spam()
102  << *self << " position_callback\n";
103  }
104 
105  Devices::iterator di;
106  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
107  VrpnTrackerDevice *device = (*di);
108  if (device->get_sensor() == info.sensor &&
109  device->get_data_type() == VrpnTrackerDevice::DT_position) {
110  device->tracker_changed(LPoint3(info.pos[0], info.pos[1], info.pos[2]),
111  LOrientation(info.quat[3], info.quat[0],
112  info.quat[1], info.quat[2]),
113  VrpnClient::convert_to_secs(info.msg_time));
114  }
115  }
116 }
117 
118 /**
119  * Receives the tracker velocity data from the VRPN code and sends it to any
120  * interested VrpnTrackerDevices.
121  */
122 void VRPN_CALLBACK VrpnTracker::
123 vrpn_velocity_callback(void *userdata, const vrpn_TRACKERVELCB info) {
124  VrpnTracker *self = (VrpnTracker *)userdata;
125  if (vrpn_cat.is_spam()) {
126  vrpn_cat.spam()
127  << *self << " velocity_callback\n";
128  }
129 
130  Devices::iterator di;
131  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
132  VrpnTrackerDevice *device = (*di);
133  if (device->get_sensor() == info.sensor &&
134  device->get_data_type() == VrpnTrackerDevice::DT_velocity) {
135  device->tracker_changed(LPoint3(info.vel[0], info.vel[1], info.vel[2]),
136  LOrientation(info.vel_quat[3], info.vel_quat[0],
137  info.vel_quat[1], info.vel_quat[2]),
138  VrpnClient::convert_to_secs(info.msg_time));
139  }
140  }
141 }
142 
143 /**
144  * Receives the tracker acceleration data from the VRPN code and sends it to
145  * any interested VrpnTrackerDevices.
146  */
147 void VRPN_CALLBACK VrpnTracker::
148 vrpn_acceleration_callback(void *userdata, const vrpn_TRACKERACCCB info) {
149  VrpnTracker *self = (VrpnTracker *)userdata;
150  if (vrpn_cat.is_spam()) {
151  vrpn_cat.spam()
152  << *self << " acceleration_callback\n";
153  }
154 
155  Devices::iterator di;
156  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
157  VrpnTrackerDevice *device = (*di);
158  if (device->get_sensor() == info.sensor &&
159  device->get_data_type() == VrpnTrackerDevice::DT_acceleration) {
160  device->tracker_changed(LPoint3(info.acc[0], info.acc[1], info.acc[2]),
161  LOrientation(info.acc_quat[3], info.acc_quat[0],
162  info.acc_quat[1], info.acc_quat[2]),
163  VrpnClient::convert_to_secs(info.msg_time));
164  }
165  }
166 }
DataType get_data_type() const
Returns the type of data this device represents from the VrpnTracker.
int get_sensor() const
Returns the particular sensor index that this device wants to hear about from the VrpnTracker.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void unmark(VrpnTrackerDevice *device)
Removes the indicated VrpnTrackerDevice from the list of devices that are sharing this VrpnTracker.
Definition: vrpnTracker.cxx:62
This is the actual interface to a particular VRPN tracker object, and all of its sensors.
Definition: vrpnTracker.h:37
const std::string & get_tracker_name() const
Returns the name of the tracker device that was used to create this VrpnTracker.
Definition: vrpnTracker.I:19
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The Panda interface to a VRPN tracker.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void mark(VrpnTrackerDevice *device)
Adds the indicated VrpnTrackerDevice to the list of devices that are sharing this VrpnTracker.
Definition: vrpnTracker.cxx:50
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static double convert_to_secs(struct timeval msg_time)
Little inline function to convert a struct timeval to only seconds.
Definition: vrpnClient.I:45