-
Notifications
You must be signed in to change notification settings - Fork 2
/
viconArduinoOSC.cpp
435 lines (395 loc) · 11 KB
/
viconArduinoOSC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// viconArduinoOSC.cpp : Send marker data to Max for comparison w/ Arduino data
//
#include "osc/OscOutboundPacketStream.h"
#include "ip/UdpSocket.h"
#define OUTPUT_BUFFER_SIZE 1024
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) OMG Plc 2009.
// All rights reserved. This software is protected by copyright
// law and international treaties. No part of this software / document
// may be reproduced or distributed in any form or by any means,
// whether transiently or incidentally to some other use of this software,
// without the written permission of the copyright owner.
//
///////////////////////////////////////////////////////////////////////////////
#include "Client.h"
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <fstream>
#include <cassert>
#include <ctime>
#include <vector>
#include <string.h>
#ifdef WIN32
#include <conio.h> // For _kbhit()
#include <cstdio> // For getchar()
#include <windows.h> // For Sleep()
#else
#include <unistd.h> // For sleep()
#endif // WIN32
#include <time.h>
using namespace ViconDataStreamSDK::CPP;
#define output_stream if(!LogFile.empty()) ; else std::cout
namespace
{
std::string Adapt(const bool i_Value)
{
return i_Value ? "True" : "False";
}
std::string Adapt(const Direction::Enum i_Direction)
{
switch (i_Direction)
{
case Direction::Forward:
return "Forward";
case Direction::Backward:
return "Backward";
case Direction::Left:
return "Left";
case Direction::Right:
return "Right";
case Direction::Up:
return "Up";
case Direction::Down:
return "Down";
default:
return "Unknown";
}
}
std::string Adapt(const DeviceType::Enum i_DeviceType)
{
switch (i_DeviceType)
{
case DeviceType::ForcePlate:
return "ForcePlate";
case DeviceType::Unknown:
default:
return "Unknown";
}
}
std::string Adapt(const Unit::Enum i_Unit)
{
switch (i_Unit)
{
case Unit::Meter:
return "Meter";
case Unit::Volt:
return "Volt";
case Unit::NewtonMeter:
return "NewtonMeter";
case Unit::Newton:
return "Newton";
case Unit::Kilogram:
return "Kilogram";
case Unit::Second:
return "Second";
case Unit::Ampere:
return "Ampere";
case Unit::Kelvin:
return "Kelvin";
case Unit::Mole:
return "Mole";
case Unit::Candela:
return "Candela";
case Unit::Radian:
return "Radian";
case Unit::Steradian:
return "Steradian";
case Unit::MeterSquared:
return "MeterSquared";
case Unit::MeterCubed:
return "MeterCubed";
case Unit::MeterPerSecond:
return "MeterPerSecond";
case Unit::MeterPerSecondSquared:
return "MeterPerSecondSquared";
case Unit::RadianPerSecond:
return "RadianPerSecond";
case Unit::RadianPerSecondSquared:
return "RadianPerSecondSquared";
case Unit::Hertz:
return "Hertz";
case Unit::Joule:
return "Joule";
case Unit::Watt:
return "Watt";
case Unit::Pascal:
return "Pascal";
case Unit::Lumen:
return "Lumen";
case Unit::Lux:
return "Lux";
case Unit::Coulomb:
return "Coulomb";
case Unit::Ohm:
return "Ohm";
case Unit::Farad:
return "Farad";
case Unit::Weber:
return "Weber";
case Unit::Tesla:
return "Tesla";
case Unit::Henry:
return "Henry";
case Unit::Siemens:
return "Siemens";
case Unit::Becquerel:
return "Becquerel";
case Unit::Gray:
return "Gray";
case Unit::Sievert:
return "Sievert";
case Unit::Katal:
return "Katal";
case Unit::Unknown:
default:
return "Unknown";
}
}
#ifdef WIN32
bool Hit()
{
bool hit = false;
while (_kbhit())
{
getchar();
hit = true;
}
return hit;
}
#endif
}
int main(int argc, char* argv[])
{
// define OSC send target
UdpTransmitSocket transmitSocket(IpEndpointName("localhost", 444));
// Program options
std::string HostName = "localhost:801";
if (argc > 1)
{
HostName = argv[1];
}
// log contains:
// version number
// log of framerate over time
// --multicast
// kill off internal app
std::string LogFile = "";
std::string MulticastAddress = "244.0.0.0:44801";
bool ConnectToMultiCast = false;
bool EnableMultiCast = false;
bool EnableHapticTest = false;
bool bReadCentroids = false;
std::vector<std::string> HapticOnList(0);
for (int a = 2; a < argc; ++a)
{
std::string arg = argv[a];
if (arg == "--help")
{
std::cout << argv[0] << " <HostName>: allowed options include:\n --log_file <LogFile> --enable_multicast <MulticastAddress:Port> --connect_to_multicast <MulticastAddress:Port> --help --enable_haptic_test <DeviceName> --centroids" << std::endl;
return 0;
}
else if (arg == "--log_file")
{
if (a < argc)
{
LogFile = argv[a + 1];
std::cout << "Using log file <" << LogFile << "> ..." << std::endl;
++a;
}
}
else if (arg == "--enable_multicast")
{
EnableMultiCast = true;
if (a < argc)
{
MulticastAddress = argv[a + 1];
std::cout << "Enabling multicast address <" << MulticastAddress << "> ..." << std::endl;
++a;
}
}
else if (arg == "--connect_to_multicast")
{
ConnectToMultiCast = true;
if (a < argc)
{
MulticastAddress = argv[a + 1];
std::cout << "connecting to multicast address <" << MulticastAddress << "> ..." << std::endl;
++a;
}
}
else if (arg == "--enable_haptic_test")
{
EnableHapticTest = true;
++a;
if (a < argc)
{
//assuming no haptic device name starts with "--"
while (a < argc && strncmp(argv[a], "--", 2) != 0)
{
HapticOnList.push_back(argv[a]);
++a;
}
}
}
else if (arg == "--centroids")
{
bReadCentroids = true;
}
else
{
std::cout << "Failed to understand argument <" << argv[a] << ">...exiting" << std::endl;
return 1;
}
}
std::ofstream ofs;
if (!LogFile.empty())
{
ofs.open(LogFile.c_str());
if (!ofs.is_open())
{
std::cout << "Could not open log file <" << LogFile << ">...exiting" << std::endl;
return 1;
}
}
// Make a new client
Client MyClient;
for (int i = 0; i != 3; ++i) // repeat to check disconnecting doesn't wreck next connect
{
// Connect to a server
std::cout << "Connecting to " << HostName << " ..." << std::flush;
while (!MyClient.IsConnected().Connected)
{
// Direct connection
bool ok = false;
if (ConnectToMultiCast)
{
// Multicast connection
ok = (MyClient.ConnectToMulticast(HostName, MulticastAddress).Result == Result::Success);
}
else
{
ok = (MyClient.Connect(HostName).Result == Result::Success);
}
if (!ok)
{
std::cout << "Warning - connect failed..." << std::endl;
}
std::cout << ".";
#ifdef WIN32
Sleep(1000);
#else
sleep(1);
#endif
}
std::cout << std::endl;
// Enable some different data types
MyClient.EnableSegmentData();
MyClient.EnableMarkerData();
MyClient.EnableUnlabeledMarkerData();
MyClient.EnableDeviceData();
if (bReadCentroids)
{
MyClient.EnableCentroidData();
}
std::cout << "Segment Data Enabled: " << Adapt(MyClient.IsSegmentDataEnabled().Enabled) << std::endl;
std::cout << "Marker Data Enabled: " << Adapt(MyClient.IsMarkerDataEnabled().Enabled) << std::endl;
std::cout << "Unlabeled Marker Data Enabled: " << Adapt(MyClient.IsUnlabeledMarkerDataEnabled().Enabled) << std::endl;
std::cout << "Device Data Enabled: " << Adapt(MyClient.IsDeviceDataEnabled().Enabled) << std::endl;
std::cout << "Centroid Data Enabled: " << Adapt(MyClient.IsCentroidDataEnabled().Enabled) << std::endl;
// Set the streaming mode
//MyClient.SetStreamMode( ViconDataStreamSDK::CPP::StreamMode::ClientPull );
// MyClient.SetStreamMode( ViconDataStreamSDK::CPP::StreamMode::ClientPullPreFetch );
MyClient.SetStreamMode(ViconDataStreamSDK::CPP::StreamMode::ServerPush);
// Set the global up axis
MyClient.SetAxisMapping(Direction::Forward,
Direction::Left,
Direction::Up); // Z-up
// MyClient.SetGlobalUpAxis( Direction::Forward,
// Direction::Up,
// Direction::Right ); // Y-up
Output_GetAxisMapping _Output_GetAxisMapping = MyClient.GetAxisMapping();
std::cout << "Axis Mapping: X-" << Adapt(_Output_GetAxisMapping.XAxis)
<< " Y-" << Adapt(_Output_GetAxisMapping.YAxis)
<< " Z-" << Adapt(_Output_GetAxisMapping.ZAxis) << std::endl;
// Discover the version number
Output_GetVersion _Output_GetVersion = MyClient.GetVersion();
std::cout << "Version: " << _Output_GetVersion.Major << "."
<< _Output_GetVersion.Minor << "."
<< _Output_GetVersion.Point << std::endl;
if (EnableMultiCast)
{
assert(MyClient.IsConnected().Connected);
MyClient.StartTransmittingMulticast(HostName, MulticastAddress);
}
size_t FrameRateWindow = 1000; // frames
size_t Counter = 0;
clock_t LastTime = clock();
// Loop until a key is pressed
#ifdef WIN32
while (!Hit())
#else
while (true)
#endif
{
// Get a frame
// output_stream << "Waiting for new frame...";
while (MyClient.GetFrame().Result != Result::Success)
{
// Sleep a little so that we don't lumber the CPU with a busy poll
#ifdef WIN32
Sleep(200);
#else
sleep(1);
#endif
output_stream << ".";
}
int m_x[10], m_y[10], m_z[10];
// Get the unlabeled markers
unsigned int UnlabeledMarkerCount = MyClient.GetUnlabeledMarkerCount().MarkerCount;
//output_stream << " Unlabeled Markers (" << UnlabeledMarkerCount << "):" << std::endl;
for (unsigned int UnlabeledMarkerIndex = 0; UnlabeledMarkerIndex < UnlabeledMarkerCount; ++UnlabeledMarkerIndex)
{
// Get the global marker translation
Output_GetUnlabeledMarkerGlobalTranslation _Output_GetUnlabeledMarkerGlobalTranslation =
MyClient.GetUnlabeledMarkerGlobalTranslation(UnlabeledMarkerIndex);
m_x[UnlabeledMarkerIndex] = (osc::int32)_Output_GetUnlabeledMarkerGlobalTranslation.Translation[0];
m_y[UnlabeledMarkerIndex] = (osc::int32)_Output_GetUnlabeledMarkerGlobalTranslation.Translation[1];
m_z[UnlabeledMarkerIndex] = (osc::int32)_Output_GetUnlabeledMarkerGlobalTranslation.Translation[2];
char buffer[OUTPUT_BUFFER_SIZE];
osc::OutboundPacketStream p(buffer, OUTPUT_BUFFER_SIZE);
p << osc::BeginBundleImmediate
<< osc::BeginMessage("/3d/marker")
<< m_x[0] << m_y[0]<< m_z[0] << osc::EndMessage;
p << osc::EndBundle;
transmitSocket.Send(p.Data(), p.Size());
/*output_stream << " Marker #" << UnlabeledMarkerIndex << ": ("
<< _Output_GetUnlabeledMarkerGlobalTranslation.Translation[0] << ", "
<< _Output_GetUnlabeledMarkerGlobalTranslation.Translation[1] << ", "
<< _Output_GetUnlabeledMarkerGlobalTranslation.Translation[2] << ")" << std::endl;*/
}
}
if (EnableMultiCast)
{
MyClient.StopTransmittingMulticast();
}
MyClient.DisableSegmentData();
MyClient.DisableMarkerData();
MyClient.DisableUnlabeledMarkerData();
MyClient.DisableDeviceData();
if (bReadCentroids)
{
MyClient.DisableCentroidData();
}
// Disconnect and dispose
int t = clock();
std::cout << " Disconnecting..." << std::endl;
MyClient.Disconnect();
int dt = clock() - t;
double secs = (double)(dt) / (double)CLOCKS_PER_SEC;
std::cout << " Disconnect time = " << secs << " secs" << std::endl;
}
}