vrpn
07.35
Virtual Reality Peripheral Network
Toggle main menu visibility
Loading...
Searching...
No Matches
vrpn_Analog_USDigital_A2.C
Go to the documentation of this file.
1
// vrpn_Analog_USDigital_A2.C
2
//
3
// This is a driver for USDigital A2 Absolute Encoders.
4
// They can be daisy changed together, and utlimately, one or
5
// more plug into a serial port and communicate using RS-232.
6
// You can find out more at www.usdigital.com.
7
//
8
// To use this class, install the US Digital software, specifying
9
// the "SEI Explorer Demo Software" to install.
10
//
11
// Then uncomment the following line in vrpn_configure.h:
12
// #define VRPN_USE_USDIGITAL
13
//
14
// Note that because the 3rd party library is used, this class
15
// will only work under Windows.
16
//
17
// You must also include the following in your compilers include
18
// path for the 'vrpn' project:
19
// $(SYSTEMDRIVE)\Program Files\SEI Explorer
20
//
21
// Finally, the following must be included in vrpn.cfg to use
22
// the generic server:
23
//
24
// ################################################################################
25
// # US Digital A2 Absolute Encoder Analog Input server. This will open the COM
26
// # port specified, configure the number of channels specified, and report
27
// # Absolute Encoder values in tenths of a degree from 0 to 3599.
28
// #
29
// # Arguments:
30
// # char name_of_this_device[]
31
// # int COM_port. If 0, search for correct COM port.
32
// # int number_of_channels
33
// # int 0 to report always, 1 to report on change only (optional, default=0)
34
//
35
// vrpn_Analog_USDigital_A2 Analog0 0 2
36
//
37
// This code was written in October 2006 by Bill West, who
38
// used the vrpn_Analog_Server sample code written by
39
// Tom Hudson in March 1999 as a starting point. Bill also
40
// used some ideas from vrpn_Radamec_SPI.[Ch] written by
41
// Russ Taylor in August 2000.
42
43
#include "
vrpn_Analog_USDigital_A2.h
"
44
45
class
VRPN_API
vrpn_Connection
;
46
#ifdef VRPN_USE_USDIGITAL
47
extern
"C"
{
48
#include <SEIDrv32.H>
49
}
50
#endif
51
#include <stdio.h>
// for fprintf, stderr
52
53
// Constants used by this class
54
const
vrpn_uint32
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_CHANNEL_MAX
=
55
// deallocate the list of device addresses.
56
(15<
vrpn_CHANNEL_MAX
) ? 15 :
vrpn_CHANNEL_MAX
;
// pick the least
57
const
vrpn_uint32
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_FIND_PORT
= 0 ;
58
59
// Constructor initializes the USDigital's SEI communication, and prepares to read the A2
60
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2
(
const
char
* name,
61
vrpn_Connection
* c,
62
vrpn_uint32 portNum,
63
vrpn_uint32 numChannels,
64
vrpn_int32 reportOnChangeOnly) :
65
vrpn_Analog
(name, c),
66
_SEIopened(vrpn_false),
67
_devAddr(NULL),
68
_reportChange(reportOnChangeOnly!=0),
69
_numDevices(0)
70
{
71
#ifdef VRPN_USE_USDIGITAL
72
this->_devAddr =
new
long
[
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_CHANNEL_MAX
];
73
this->setNumChannels( numChannels );
74
75
// Check if we got a connection.
76
if
(
d_connection
== NULL) {
77
fprintf(stderr,
"vrpn_Analog_USDigital_A2: Can't get connection!\n"
);
78
return
;
79
}
80
81
// Prepare to get data from the SEI bus
82
long
err;
83
#ifdef VRPN_USE_USDIGITAL
84
err = InitializeSEI(portNum, AUTOASSIGN) ;
85
#else
86
fprintf(stderr,
"vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2(): Not yet implemented for this architecture\n"
);
87
err = -1;
88
#endif
89
if
(err) {
90
fprintf(stderr,
"vrpn_Analog_USDigital_A2: Can't initialize SEI bus for port %d.\n"
,
91
#ifdef VRPN_USE_USDIGITAL
92
GetCommPort()
93
#
else
94
0
95
#endif
96
);
97
98
return ;
99
}
else
{
100
_SEIopened = vrpn_true ;
101
}
102
103
// Check if the number of devices matches that expected
104
#ifdef VRPN_USE_USDIGITAL
105
_numDevices = GetNumberOfDevices() ;
106
#endif
107
if
(_numDevices<0 || _numDevices>
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_CHANNEL_MAX
) {
108
fprintf(stderr,
109
"vrpn_Analog_USDigital_A2: Error (%d) returned from GetNumberOfDevices call on SEI bus"
,
110
_numDevices) ;
111
_numDevices = 0 ;
112
}
113
if
(_numDevices != numChannels)
114
fprintf(stderr,
115
"vrpn_Analog_USDigital_A2: Warning, number of requested devices (%d) is not the same as found (%d)\n"
,
116
numChannels, _numDevices) ;
117
118
// Initialize the addresses
119
for
(
int
c=0 ; c<
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_CHANNEL_MAX
; c++)
120
_devAddr[c] = -1 ;
121
122
// Get the device addresses.
123
for
(vrpn_uint32 d=0 ; d<_numDevices ; d++) {
124
long
deviceInfoErr, model, serialnum, version, addr ;
125
#ifdef VRPN_USE_USDIGITAL
126
deviceInfoErr = GetDeviceInfo(d, &model, &serialnum, &version, &addr) ;
127
if
(!deviceInfoErr)
128
_devAddr[d] = addr ;
129
#endif
130
131
#ifdef VERBOSE
132
// Dump out the device data
133
if
(deviceInfoErr)
134
fprintf(stderr,
"vrpn_Analog_USDigital_A2: could not get information on Device #%d!\n"
, d) ;
135
else
136
fprintf(stderr,
"vrpn_Analog_USDigital_A2: Device #%d: model=%d, serialnum=%d, version=%d, addr=%d\n"
,
137
d, model, serialnum, version, addr) ;
138
#endif
// VERBOSE
139
}
140
#else
141
fprintf(stderr,
"vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2(): Not compiled in; define VRPN_USE_USDIGITAL in vrpn_Configure.h and recompile VRPN\n"
);
142
portNum = portNum + 1;
// Remove unused parameter warning.
143
numChannels = numChannels + 1;
// Remove unused parameter warning.
144
_SEIopened = !_SEIopened;
// Removed unused variable warning.
145
_devAddr = _devAddr + 1;
// Removed unused variable warning.
146
_numDevices = _numDevices + 1;
// Removed unused variable warning.
147
#endif
148
}
// constructor
149
150
// This destructor closes out the SEI bus, and deallocates memory
151
vrpn_Analog_USDigital_A2::~vrpn_Analog_USDigital_A2
()
152
{
153
#ifdef VRPN_USE_USDIGITAL
154
// close out the SEI bus
155
if
(_SEIopened==vrpn_true) {
156
(void) CloseSEI() ;
157
}
158
159
// deallocate the list of device addresses.
160
try
{
161
delete
_devAddr;
162
}
catch
(...) {
163
fprintf(stderr,
"vrpn_Analog_USDigital_A2::~vrpn_Analog_USDigital_A2(): delete failed\n"
);
164
return
;
165
}
166
_devAddr = 0 ;
167
#endif
168
}
// destructor
169
176
177
void
vrpn_Analog_USDigital_A2::mainloop
()
178
{
179
server_mainloop
();
// let the server do its stuff
180
#ifdef VRPN_USE_USDIGITAL
181
long
readErr, readVal ;
182
183
// Read the data from the available channels
184
for
(vrpn_uint32 c=0 ; c<(vrpn_uint32)
num_channel
; c++)
185
{
186
// see if there's really a readable device there.
187
if
(c<_numDevices && _devAddr[c]>=0)
188
{
189
readErr = A2GetPosition(_devAddr[c], &readVal) ;
190
if
(readErr)
191
{
192
fprintf(stderr,
193
"vrpn_Analog_USDigital_A2: Error code %d received while reading channel %d.\n"
,
194
readErr, c) ;
195
fprintf(stderr,
"vrpn_Analog_USDigital_A2: Attempting to reinitialize SEI bus..."
) ;
196
readErr = ResetSEI() ;
197
if
(readErr)
198
fprintf(stderr,
"failed."
) ;
199
fprintf(stderr,
"failed."
) ;
200
// don't flood the log, and give the reset time to work
201
vrpn_SleepMsecs
(1000) ;
202
}
203
else
204
channel
[c] = (vrpn_float64) readVal ;
205
}
206
else
207
channel
[c] = 0 ;
// default to 0 for unreadable/unavailable.
208
}
// for
209
#endif
210
211
// Finally, the point of all this, deliver the data
212
if
(_reportChange)
213
report_changes
() ;
214
else
215
report
() ;
216
217
}
// mainloop
218
219
vrpn_int32 vrpn_Analog_USDigital_A2::setNumChannels (vrpn_int32 sizeRequested)
220
{
221
if
(sizeRequested < 0)
222
num_channel
= 0;
223
else
if
(
static_cast<
unsigned
>
(sizeRequested) >
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_CHANNEL_MAX
)
224
num_channel
= (vrpn_int32)
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_CHANNEL_MAX
;
225
else
226
num_channel
= (vrpn_int32) sizeRequested;
227
228
return
num_channel
;
229
230
}
// setNumChannels
231
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2
vrpn_Analog_USDigital_A2(const char *name, vrpn_Connection *c, vrpn_uint32 portNum=vrpn_Analog_USDigital_A2_FIND_PORT, vrpn_uint32 numChannels=vrpn_Analog_USDigital_A2_CHANNEL_MAX, vrpn_int32 reportOnChangeOnly=0)
Definition
vrpn_Analog_USDigital_A2.C:60
vrpn_Analog_USDigital_A2::~vrpn_Analog_USDigital_A2
virtual ~vrpn_Analog_USDigital_A2()
Definition
vrpn_Analog_USDigital_A2.C:151
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_FIND_PORT
static const vrpn_uint32 vrpn_Analog_USDigital_A2_FIND_PORT
Definition
vrpn_Analog_USDigital_A2.h:57
vrpn_Analog_USDigital_A2::mainloop
virtual void mainloop()
This routine is called each time through the server's main loop.
Definition
vrpn_Analog_USDigital_A2.C:177
vrpn_Analog_USDigital_A2::vrpn_Analog_USDigital_A2_CHANNEL_MAX
static const vrpn_uint32 vrpn_Analog_USDigital_A2_CHANNEL_MAX
Definition
vrpn_Analog_USDigital_A2.h:56
vrpn_Analog::channel
vrpn_float64 channel[vrpn_CHANNEL_MAX]
Definition
vrpn_Analog.h:38
vrpn_Analog::vrpn_Analog
vrpn_Analog(const char *name, vrpn_Connection *c=NULL)
Definition
vrpn_Analog.C:14
vrpn_Analog::num_channel
vrpn_int32 num_channel
Definition
vrpn_Analog.h:40
vrpn_Analog::report
virtual void report(vrpn_uint32 class_of_service=vrpn_CONNECTION_LOW_LATENCY, const struct timeval time=vrpn_ANALOG_NOW)
Send a report whether something has changed or not (for servers) Optionally, tell what time to stamp ...
Definition
vrpn_Analog.C:94
vrpn_Analog::report_changes
virtual void report_changes(vrpn_uint32 class_of_service=vrpn_CONNECTION_LOW_LATENCY, const struct timeval time=vrpn_ANALOG_NOW)
Send a report only if something has changed (for servers) Optionally, tell what time to stamp the val...
Definition
vrpn_Analog.C:71
vrpn_BaseClassUnique::d_connection
vrpn_Connection * d_connection
Connection that this object talks to.
Definition
vrpn_BaseClass.h:227
vrpn_BaseClassUnique::server_mainloop
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
Definition
vrpn_BaseClass.C:604
vrpn_Connection
Generic connection class not specific to the transport mechanism.
Definition
vrpn_Connection.h:561
vrpn_CHANNEL_MAX
#define vrpn_CHANNEL_MAX
Definition
vrpn_Analog.h:16
vrpn_Analog_USDigital_A2.h
vrpn_Connection
class VRPN_API vrpn_Connection
Definition
vrpn_Analog_USDigital_A2.h:49
VRPN_API
#define VRPN_API
Definition
vrpn_Configure.h:648
vrpn_SleepMsecs
void vrpn_SleepMsecs(double dMilliSecs)
Definition
vrpn_Shared.C:166
vrpn_Analog_USDigital_A2.C
Generated by
1.17.0