-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotionSDK.py
More file actions
1031 lines (840 loc) · 32.1 KB
/
MotionSDK.py
File metadata and controls
1031 lines (840 loc) · 32.1 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# @file sdk/python/MotionSDK.py
# @version 2.5
#
# Copyright (c) 2017, Motion Workshop
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import functools
import select
import socket
import struct
import sys
#
# Only load functools in Python version 2.5 or newer.
#
if sys.version_info >= (2, 5):
import functools
class Client:
"""
Implements socket connection and basic binary message protocol for
client application access to all Motion Service streams. Use the static
Format methods to convert a binary message into the associated object.
"""
def __init__(self, host, port):
"""
Create client socket connection to the Motion Service data stream
on host:port.
"""
self.__socket = None
self.__recv_flags = 0
self.__send_flags = 0
self.__description = None
self.__time_out_second = None
self.__time_out_second_send = None
# Set the default host name to the local host.
if (None == host) or (0 == len(host)):
host = "127.0.0.1"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
self.__socket = s
# Read the first message from the service. It is a
# string description of the remote service.
self.__description = self.__receive()
def __del__(self):
"""
Destructor. Close the socket connection.
"""
self.close()
def close(self):
"""
Close the socket connection if it exists.
"""
if None != self.__socket:
self.__socket.shutdown(2)
self.__socket.close()
self.__socket = None
def isConnected(self):
"""
Return true if the current connection is active.
"""
if None != self.__socket:
return True
else:
return False
def waitForData(self, time_out_second=None):
"""
Wait until there is incoming data on this client
connection and then returns True.
"""
# Default time out is 5 seconds.
if None == time_out_second:
time_out_second = 5
if time_out_second != self.__time_out_second:
self.__socket.settimeout(time_out_second)
self.__time_out_second = self.__socket.gettimeout()
data = self.__receive()
if None != data:
return True
else:
return False
def readData(self, time_out_second=None):
"""
Read a single sample of data from the open connection.
Returns a single sample of data, or None if the incoming
data is invalid.
"""
if None == self.__socket:
return None
# Default time out is 1 second.
if None == time_out_second:
time_out_second = 1
if time_out_second != self.__time_out_second:
self.__socket.settimeout(time_out_second)
self.__time_out_second = self.__socket.gettimeout()
return self.__receive()
def writeData(self, data, time_out_second=None):
"""
Write a single sample of data to the open connection.
Returns True iff the message was successfully written
to the socket. Otherwise returns False.
"""
if None == self.__socket:
return False
if len(data) <= 0:
return False
# Default time out is 1 second.
if None == time_out_second:
time_out_second = 1
if time_out_second != self.__time_out_second_send:
self.__socket.settimeout(time_out_second)
self.__time_out_second_send = self.__socket.gettimeout()
return self.__send(data)
def __receive(self):
"""
Read a single binary message defined by a length header.
"""
if None == self.__socket:
return None
if False == self.__select_receive():
return None
try:
header_size = struct.calcsize("!I")
# Single integer network order (=big-endian) message length header.
header = self.__socket.recv(header_size, self.__recv_flags)
if header_size != len(header):
return None
# Parse the length field, read the raw data field.
length = struct.unpack("!I", header)[0]
# Use one or more socket.recv calls to read the data payload.
data = b""
while True:
message = self.__socket.recv(
length - len(data),
self.__recv_flags)
if not message or 0 == len(message):
return None
data += message
if len(data) == length:
break
elif len(data) > length:
return None
return data
except socket.timeout:
pass
return None
def __send(self, data):
"""
Write a single binary message defined by a length header.
Returns true iff the message was successfully written
to the socket.
"""
if None == self.__socket:
return False
if False == self.__select_send():
return None
try:
# Convert Python 3 strings to byte string.
if not isinstance(data, bytes):
data = data.encode("utf-8")
length = len(data)
message = struct.pack("!I" + str(length) + "s", length, data)
send_result = self.__socket.sendall(message, self.__send_flags)
if None == send_result:
return True
except socket.timeout:
pass
return False
def __select_receive(self):
"""
Use the select function to wait until there is data available to read
on the internal socket. Returns True iff there is at least one byte
ready to be read.
"""
fd = self.__socket.fileno()
try:
list, _, _ = select.select(
[fd], [], [], self.__time_out_second)
for s in list:
if fd == s:
return True
except socket.timeout:
pass
return False
def __select_send(self):
"""
Use the select function to wait until there data can be written to the
internal socket object.
"""
fd = self.__socket.fileno()
try:
_, list, _ = select.select(
[], [fd], [], self.__time_out_second_send)
for s in list:
if fd == s:
return True
except socket.timeout:
pass
return False
#
# END class Client
#
class File:
"""
Implements a file input stream interface for reading Motion Service
binary take data files. Provide a simple interface to develop
external applications that can read Motion take data from disk.
This class only handles the reading of binary data and conversion
to arrays of native data types. The Format class implements
interfaces to the service specific data formats.
"""
def __init__(self, pathname):
"""
Open a Motion take data file for reading.
Set parameter pathname to the file to open as the input stream.
"""
self.__input = None
self.__input = open(pathname, "rb")
def __del__(self):
"""
Destrucutor. Close the input file stream.
"""
try:
self.close()
except RuntimeError:
pass
def close(self):
"""
Close the input file stream.
Throws a RuntimeError if the file stream is not open.
"""
if None != self.__input:
self.__input.close()
self.__input = None
else:
raise RuntimeError("failed to close input file stream, not open")
def readData(self, length, real_valued):
"""
Read a single block of binary data from the current position
in the input file stream. Convert the block of data into an
array of length typed elements.
Integer parameter length defines the required number of typed elements.
Set boolean parameter real_valued to True if the typed elements are
real valued, i.e. floats. Set real_valued to false for short integers.
"""
if None == self.__input:
return None
data = None
if length > 0:
# Choose the binary format of the array values,
# "f" == float and "h" == short.
value_format = "f"
if False == real_valued:
value_format = "h"
element_size = length * struct.calcsize("<" + str(value_format))
input_buffer = self.__input.read(element_size)
if element_size == len(input_buffer):
data = struct.unpack(
"<" + str(length) + str(value_format), input_buffer)
else:
self.close()
return data
#
# END class File
#
class Format:
"""
Motion Service streams send a list of data elements. The static Format
methods create a map from integer id to array packed data for each
service specific format.
"""
class Element:
"""
Motion Service streams send a list of data elements. The {@link Format}
functions create a map from integer id to array packed data for each
service specific format.
This is an abstract base class to implement a single format specific
data element. The idea is that a child class implements a format
specific interface (API) to access individual components of an array of
packed data.
For example, the PreviewElement class extends this class
and provides a PreviewElement.getEuler() method to access
an array of {x, y, z} Euler angles.
"""
def __init__(self, data, length, real_valued):
"""
Initialize element data.
"""
self.__data = None
self.__real_valued = None
if (len(data) == length) or (0 == length):
self.__data = data
self.__real_valued = real_valued
else:
raise RuntimeError("invalid input data for format element")
def getData(self, base, length):
"""
Utility function to copy portions of the packed data array into its
component elements.
Parameter base defines starting index to copy data from the
internal data array.
Parameter element_length defines the number of data values in this
component element.
Returns an array of element_length values, assigned to
[m_data[i] ... m_data[i+element_length]] if there are valid values
available or zeros otherwise
"""
if (None != self.__data) and (base + length <= len(self.__data)):
return self.__data[base:(base + length)]
else:
value = float(0)
if False == real_valued:
value = int(0)
result = list()
for i in range(0, length):
result.append(value)
def access(self):
"""
Direct access to the internal buffer.
"""
return self.__data
#
# END class Element
#
class ConfigurableElement(Element):
"""
The Configurable data services provides access to all data streams in
a single message. The client selects channels and ordering at the
start of the connection. The Configurable service sends a map of
N data elements. Each data element is an array of M single precision
floating point numbers.
"""
def __init__(self, data):
"""
Defines the parameters of this element.
"""
Format.Element.__init__(self, data, 0, True)
def value(self, index):
"""
Get a single channel entry at specified index.
"""
return self.access()[index]
def size(self):
"""
Convenience method. Size accessor.
"""
return len(self.access())
#
# END class ConfigurableElement
#
class PreviewElement(Element):
"""
The Preview service sends a map of N Preview data elements. Use this
class to wrap a single Preview data element such that we can access
individual components through a simple API.
Preview element format:
id => [global quaternion, local quaternion, local euler, acceleration]
id => [
Gqw, Gqx, Gqy, Gqz, Lqw, Lqx, Lqy, Lqz, rx, ry, rz, lax, lay, laz
]
"""
def __init__(self, data):
"""
Defines the parameters of this element.
"""
Format.Element.__init__(self, data, 14, True)
def getEuler(self):
"""
Get a set of x, y, and z Euler angles that define the
current orientation. Specified in radians assuming x-y-z
rotation order. Not necessarily continuous over time, each
angle lies on the domain [-pi, pi].
Euler angles are computed on the server side based on the
current local quaternion orientation.
Returns a three element array [x, y, z] of Euler angles
in radians or None if there is no available data
"""
return self.getData(8, 3)
def getMatrix(self, local):
"""
Get a 4-by-4 rotation matrix from the current global or local
quaternion orientation. Specified as a 16 element array in
row-major order.
Set parameter local to true get the local orientation, set local
to false to get the global orientation.
"""
return Format.quaternion_to_R3_rotation(self.getQuaternion(local))
def getQuaternion(self, local):
"""
Get the global or local unit quaternion that defines the current
orientation.
@param local set local to true get the local orientation, set local
to false to get the global orientation
Returns a four element array [w, x, y, z] that defines
a unit length quaternion q = w + x*i + y*j + z*k or None
if there is no available data
"""
if (local):
return self.getData(4, 4)
else:
return self.getData(0, 4)
def getAccelerate(self):
"""
Get x, y, and z of the current estimate of linear acceleration.
Specified in g.
Returns a three element array [x, y, z] of of linear acceleration
channels specified in g or zeros if there is no available data
"""
return self.getData(11, 3)
#
# END class PreviewElement
#
class SensorElement(Element):
"""
The Sensor service provides access to the current un-filtered sensor
signals in real units. The Sensor service sends a map of N data
elements. Use this class to wrap a single Sensor data element such
that we can access individual components through a simple API.
Sensor element format:
id => [accelerometer, magnetometer, gyroscope]
id => [ax, ay, az, mx, my, mz, gx, gy, gz]
"""
def __init__(self, data):
"""
Initialize this container identifier with a packed data
array in the Sensor format.
Parameter data is a packed array of accelerometer, magnetometer,
and gyroscope un-filtered signal data.
"""
Format.Element.__init__(self, data, 9, True)
def getAccelerometer(self):
"""
Get a set of x, y, and z values of the current un-filtered
accelerometer signal. Specified in g where 1 g =
-9.8 meters/sec^2.
Domain varies with configuration. Maximum is [-6, 6] g.
Returns a three element array [x, y, z] of acceleration
in gs or zeros if there is no available data
"""
return self.getData(0, 3)
def getGyroscope(self):
"""
Get a set of x, y, and z values of the current un-filtered
gyroscope signal. Specified in degrees/second.
Valid domain is [-500, 500] degrees/second.
Returns a three element array [x, y, z] of angular velocity
in degrees/second or zeros if there is no available data.
"""
return self.getData(6, 3)
def getMagnetometer(self):
"""
Get a set of x, y, and z values of the current un-filtered
magnetometer signal. Specified in uT (microtesla).
Domain varies with local magnetic field strength. Expect values
on [-60, 60] uT (microtesla).
Returns a three element array [x, y, z] of magnetic field
strength in uT (microtesla) or zeros if there is no
available data.
"""
return self.getData(3, 3)
#
# class SensorElement
#
class RawElement(Element):
"""
The Raw service provides access to the current uncalibrated,
unprocessed sensor signals in signed integer format. The Raw service
sends a map of N data elements. Use this class to wrap a single Raw
data element such that we can access individual components through a
simple API.
Raw element format:
id => [accelerometer, magnetometer, gyroscope]
id => [ax, ay, az, mx, my, mz, gx, gy, gz]
All sensors output 12-bit integers. Process as 16-bit short integers on
the server side.
"""
def __init__(self, data):
"""
Initialize this container identifier with a packed data
array in the Raw format.
Parameter data is a packed array of accelerometer, magnetometer,
and gyroscope un-filtered signal data.
"""
Format.Element.__init__(self, data, 9, False)
def getAccelerometer(self):
"""
Get a set of x, y, and z values of the current unprocessed
accelerometer signal.
Valid domain is [0, 4095].
Returns a three element array [x, y, z] of raw accelerometer
output or zeros if there is no available data.
"""
return self.getData(0, 3)
def getGyroscope(self):
"""
Get a set of x, y, and z values of the current unprocessed
gyroscope signal.
Valid domain is [0, 4095].
Returns a three element array [x, y, z] of raw gyroscope
output or zeros if there is no available data.
"""
return self.getData(6, 3)
def getMagnetometer(self):
"""
Get a set of x, y, and z values of the current unprocessed
magnetometer signal.
Valid domain is [0, 4095].
Returns a three element array [x, y, z] of raw magnetometer
output or zeros if there is no available data.
"""
return self.getData(3, 3)
#
# class RawElement
#
def __Configurable(data):
"""
Convert a container of binary data into an associative
container of ConfigurableElement entries.
"""
return Format.__IdToValueArray(
data, 0, Format.ConfigurableElement, True)
Configurable = staticmethod(__Configurable)
def __Preview(data):
"""
Convert a container of binary data into an associative
container of PreviewElement entries.
"""
return Format.__IdToValueArray(data, 14, Format.PreviewElement, True)
Preview = staticmethod(__Preview)
def __Sensor(data):
"""
Convert a container of binary data into an associative
container of SensorElement entries.
"""
return Format.__IdToValueArray(data, 9, Format.SensorElement, True)
Sensor = staticmethod(__Sensor)
def __Raw(data):
"""
Convert a container of binary data into an associative
container of RawElement entries.
"""
return Format.__IdToValueArray(data, 9, Format.RawElement, False)
Raw = staticmethod(__Raw)
def __IdToValueArray(data, length, factory, real_valued):
"""
Utility method to convert a packed binary representation of
an associative container into that container.
"""
result = {}
if None == data:
return result
# Choose the binary format of the array values,
# "f" == float and "h" == short.
value_format = "f"
if False == real_valued:
value_format = "h"
# Prefix "<" for little-endian byte ordering.
sizeof_key = struct.calcsize("<I")
sizeof_value = struct.calcsize("<" + str(value_format))
itr = 0
while (itr < len(data)) and ((len(data) - itr) > sizeof_key):
# Read the integer id for this element.
key = struct.unpack("<I", data[itr:itr + sizeof_key])[0]
itr += sizeof_key
# Optionally read the integer length field of the
# data array.
element_length = length
if (0 == element_length) and ((len(data) - itr) > sizeof_key):
element_length = struct.unpack(
"<I", data[itr:itr + sizeof_key])[0]
itr += sizeof_key
# Read the array of values for this element.
sizeof_array = sizeof_value * element_length
if (element_length > 0) and ((len(data) - itr) >= sizeof_array):
value = struct.unpack(
str(element_length) + value_format,
data[itr:itr + sizeof_array])
itr += sizeof_array
result[key] = factory(value)
# If we did not consume all of the input bytes this is an
# invalid message.
if len(data) != itr:
result = {}
return result
__IdToValueArray = staticmethod(__IdToValueArray)
def quaternion_to_R3_rotation(q):
"""
Ported from the Boost.Quaternion library at:
http://www.boost.org/libs/math/quaternion/HSO3.hpp
Parameter q defines a quaternion in the format [w x y z] where
q = w + x*i + y*j + z*k.
Returns an array of 16 elements that defines a 4-by-4 rotation
matrix computed from the input quaternion or the identity matrix
if the input quaternion has zero length. Matrix is in row-major
order.
"""
if 4 != len(q):
return None
a = q[0]
b = q[1]
c = q[2]
d = q[3]
aa = a * a
ab = a * b
ac = a * c
ad = a * d
bb = b * b
bc = b * c
bd = b * d
cc = c * c
cd = c * d
dd = d * d
norme_carre = aa + bb + cc + dd
result = list()
for i in range(0, 4):
for j in range(0, 4):
if i == j:
result.append(1)
else:
result.append(0)
if (norme_carre > 1e-6):
result[0] = (aa + bb - cc - dd) / norme_carre
result[1] = 2 * (-ad + bc) / norme_carre
result[2] = 2 * (ac + bd) / norme_carre
result[4] = 2 * (ad + bc) / norme_carre
result[5] = (aa - bb + cc - dd) / norme_carre
result[6] = 2 * (-ab + cd) / norme_carre
result[8] = 2 * (-ac + bd) / norme_carre
result[9] = 2 * (ab + cd) / norme_carre
result[10] = (aa - bb - cc + dd) / norme_carre
return result
quaternion_to_R3_rotation = staticmethod(quaternion_to_R3_rotation)
#
# END class Format
#
class LuaConsole:
"""
Implements the communication protocol with the Motion Service console.
Send general Lua scripting commands to the Motion Service and receive
a result code and printed results.
"""
# The Lua chunk was successfully parsed and executed. The
# printed results are in the result string.
Success = 0
# The Lua chunk failed due to a compile time or execution
# time error. An error description is in the result string.
Failure = 1
# The Lua chunk was incomplete. The Console service is waiting
# for a complete chunk before it executes.
# For example, "if x > 1 then" is incomplete since it requires
# and "end" token to close the "if" control statement.
Continue = 2
def __init__(self, client):
"""
Constructor. Supply the already open client socket connection
to the Motion Service console..
"""
self.__client = client
def send_chunk(self, chunk, time_out_second=None):
"""
Write a general Lua chunk to the open Console service
socket and read back the results.
"""
result_code = self.Failure
result_string = None
# Write the Lua chunk.
if self.__client.writeData(chunk, time_out_second):
# Read back the response. This is how the Lua Console
# service works. It will always respond with at least
# an error code.
data = self.__client.readData(time_out_second)
if None != data and len(data) > 0:
if not isinstance(data, str):
data = str(data, "utf-8")
code = ord(data[0])
if code >= self.Success and code <= self.Continue:
result_code = code
if len(data) > 1:
result_string = data[1:]
return result_code, result_string
def __SendChunk(client, chunk, time_out_second=None):
"""
A more Python friendly version of the SendChunk method.
This will throw an exception if there is an error in the
scripting command. Otherwise, this will only return the
printed results.
"""
lua_console = LuaConsole(client)
result_code, result_string = lua_console.send_chunk(
chunk, time_out_second)
if lua_console.Success == result_code:
return result_string
elif lua_console.Continue == result_code:
raise RuntimeError(
"Lua chunk incomplete: " + str(result_string))
else:
raise RuntimeError(
"Lua command chunk failed: " + str(result_string))
SendChunk = staticmethod(__SendChunk)
class Node:
"""
Utility class to implement a generic scripting interface
from the Motion Service console (Lua) to Python and vice versa.
Dispatch named methods with variable length argument
lists.
Implements all Lua node.* methods that return a boolean
and string value pair. Also supports simple string result
but the client script must handle this correctly.
Example usage:
node = LuaConsole.Node(Client("", 32075))
result, message = node.start()
if result:
# Success. We are reading from at least one device.
pass
else:
# Failure. There are no configured devices or the
# hardware is not available.
print message
"""
def __init__(self, client):
self.__client = client
def __getattr__(self, name):
if sys.version_info >= (2, 5):
return functools.partial(self.__dispatch, name)
else:
return None
def __dispatch(self, name, *arg_list):
result = self.__string_call(name, *arg_list)
if result.startswith("true"):
return True, result[4:]
elif result.startswith("false"):
return False, result[5:]
else:
return str(result)
def __string_call(self, name, *arg_list):
lua_call = "=node.%s(" % name
# Create a string valued argument list from a variable
# length list of arguments. Note that this only supports
# String and Float valued arguments.
sep = ""
for item in arg_list:
if isinstance(item, str):
lua_call += "%s%s" % (sep, "".join(
["'", ("\\'").join(i for i in item.split("'")), "'"]))
else:
lua_call += "%s%s" % (sep, float(item))
sep = ", "
lua_call += ")"
return LuaConsole.SendChunk(self.__client, lua_call, 5)
#
# END class Node
#
#
#
# END class LuaConsole
#
def main():
"""
Example usage and test function for the Client, File, Format, and
LuaConsole classes.
"""
# Set the default host and port parameters. The SDK is
# socket bases so any networked Motion Service is available.
Host = ""
PortPreview = 32079
PortConsole = 32075
#
# General Lua scripting interface.
#
lua_client = Client(Host, PortConsole)
lua_chunk = \
"if not node.is_reading() then" \
" node.close()" \
" node.scan()" \
" node.start()" \
" end" \
" if node.is_reading() then" \
" print('Reading from ' .. node.num_reading() .. ' device(s)')" \
" else" \
" print('Failed to start reading')" \
" end"
print(LuaConsole.SendChunk(lua_client, lua_chunk, 5))
# Scripting language compatibility class. Translate
# Python calls into Lua calls and send them to the
# console service.
if sys.version_info >= (2, 5):
node = LuaConsole.Node(lua_client)
print(node.is_reading())
# Connect to the Preview data service.
# Print out the Euler angle orientation output.
client = Client(Host, PortPreview)
print("Connected to {}:{}".format(Host, PortPreview))
if client.waitForData():
sample_count = 0
while sample_count < 100:
data = client.readData()