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  */
50 mark(VrpnTrackerDevice *device) {
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  */
62 unmark(VrpnTrackerDevice *device) {
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  * Polls the connected device. Normally you should not call this directly;
77  * this will be called by the VrpnClient.
78  */
80 poll() {
81  _tracker->mainloop();
82 }
83 
84 /**
85  *
86  */
87 void VrpnTracker::
88 output(std::ostream &out) const {
89  out << _tracker_name;
90 }
91 
92 /**
93  *
94  */
95 void VrpnTracker::
96 write(std::ostream &out, int indent_level) const {
97  indent(out, indent_level)
98  << get_tracker_name() << " ("
99  << _devices.size() << " devices)\n";
100 }
101 
102 /**
103  * Receives the tracker positional data from the VRPN code and sends it to any
104  * interested VrpnTrackerDevices.
105  */
106 void VRPN_CALLBACK VrpnTracker::
107 vrpn_position_callback(void *userdata, const vrpn_TRACKERCB info) {
108  VrpnTracker *self = (VrpnTracker *)userdata;
109  if (vrpn_cat.is_spam()) {
110  vrpn_cat.spam()
111  << *self << " position_callback\n";
112  }
113 
114  Devices::iterator di;
115  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
116  VrpnTrackerDevice *device = (*di);
117  if (device->get_sensor() == info.sensor &&
118  device->get_data_type() == VrpnTrackerDevice::DT_position) {
119  device->tracker_changed(LPoint3(info.pos[0], info.pos[1], info.pos[2]),
120  LOrientation(info.quat[3], info.quat[0],
121  info.quat[1], info.quat[2]),
122  VrpnClient::convert_to_secs(info.msg_time));
123  }
124  }
125 }
126 
127 /**
128  * Receives the tracker velocity data from the VRPN code and sends it to any
129  * interested VrpnTrackerDevices.
130  */
131 void VRPN_CALLBACK VrpnTracker::
132 vrpn_velocity_callback(void *userdata, const vrpn_TRACKERVELCB info) {
133  VrpnTracker *self = (VrpnTracker *)userdata;
134  if (vrpn_cat.is_spam()) {
135  vrpn_cat.spam()
136  << *self << " velocity_callback\n";
137  }
138 
139  Devices::iterator di;
140  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
141  VrpnTrackerDevice *device = (*di);
142  if (device->get_sensor() == info.sensor &&
143  device->get_data_type() == VrpnTrackerDevice::DT_velocity) {
144  device->tracker_changed(LPoint3(info.vel[0], info.vel[1], info.vel[2]),
145  LOrientation(info.vel_quat[3], info.vel_quat[0],
146  info.vel_quat[1], info.vel_quat[2]),
147  VrpnClient::convert_to_secs(info.msg_time));
148  }
149  }
150 }
151 
152 /**
153  * Receives the tracker acceleration data from the VRPN code and sends it to
154  * any interested VrpnTrackerDevices.
155  */
156 void VRPN_CALLBACK VrpnTracker::
157 vrpn_acceleration_callback(void *userdata, const vrpn_TRACKERACCCB info) {
158  VrpnTracker *self = (VrpnTracker *)userdata;
159  if (vrpn_cat.is_spam()) {
160  vrpn_cat.spam()
161  << *self << " acceleration_callback\n";
162  }
163 
164  Devices::iterator di;
165  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
166  VrpnTrackerDevice *device = (*di);
167  if (device->get_sensor() == info.sensor &&
168  device->get_data_type() == VrpnTrackerDevice::DT_acceleration) {
169  device->tracker_changed(LPoint3(info.acc[0], info.acc[1], info.acc[2]),
170  LOrientation(info.acc_quat[3], info.acc_quat[0],
171  info.acc_quat[1], info.acc_quat[2]),
172  VrpnClient::convert_to_secs(info.msg_time));
173  }
174  }
175 }
indent
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
vrpnTracker.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
config_vrpn.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
VrpnTracker::get_tracker_name
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
VrpnTracker::unmark
void unmark(VrpnTrackerDevice *device)
Removes the indicated VrpnTrackerDevice from the list of devices that are sharing this VrpnTracker.
Definition: vrpnTracker.cxx:62
VrpnTrackerDevice::get_sensor
int get_sensor() const
Returns the particular sensor index that this device wants to hear about from the VrpnTracker.
Definition: vrpnTrackerDevice.I:19
vrpnClient.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
VrpnClient::convert_to_secs
static double convert_to_secs(struct timeval msg_time)
Little inline function to convert a struct timeval to only seconds.
Definition: vrpnClient.I:26
VrpnTracker::poll
void poll()
Polls the connected device.
Definition: vrpnTracker.cxx:80
VrpnTracker::mark
void mark(VrpnTrackerDevice *device)
Adds the indicated VrpnTrackerDevice to the list of devices that are sharing this VrpnTracker.
Definition: vrpnTracker.cxx:50
vrpnTrackerDevice.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
VrpnTrackerDevice
The Panda interface to a VRPN tracker.
Definition: vrpnTrackerDevice.h:34
indent.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
VrpnTracker
This is the actual interface to a particular VRPN tracker object, and all of its sensors.
Definition: vrpnTracker.h:37
VrpnTrackerDevice::get_data_type
DataType get_data_type() const
Returns the type of data this device represents from the VrpnTracker.
Definition: vrpnTrackerDevice.I:28