-
Notifications
You must be signed in to change notification settings - Fork 20
/
tracker.py
72 lines (53 loc) · 2.07 KB
/
tracker.py
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
import socket
import json
import sys
import pyrealsense2 as rs
pipe = rs.pipeline()
cfg = rs.config()
cfg.enable_stream(rs.stream.pose)
try:
IPs = sys.argv[1:]
except IndexError:
IPs = ["127.0.0.1"]
print("Realsense T265 sending to" , IPs)
TCP_PORT = 54321
BUFFER_SIZE = 1024
starting_height = 1 # starting height in meters
pos_scale = 100
rot_scale = 1
threshold = 0.0005
for IP in IPs:
MESSAGE = '{"MHTrack": [{"Type": "CameraSubject"}, {"FieldOfView": "true","AspectRatio": "true","FocalLength": "true","ProjectionMode": "false"}]}'
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((IP, TCP_PORT))
s.send(MESSAGE.encode('utf-8'))
s.close()
pipe.start(cfg)
frames = pipe.wait_for_frames()
# Fetch pose frame
pose = frames.get_pose_frame()
if pose:
data = pose.get_pose_data()
zeroX = data.translation.x
zeroY = data.translation.y
zeroZ = data.translation.z
try:
while True:
# Wait for the next set of frames from the camera
frames = pipe.wait_for_frames()
# Fetch pose frame
pose = frames.get_pose_frame()
if pose:
# Print some of the pose data to the terminal
data = pose.get_pose_data()
# if data.translation.z
data.translation.y += starting_height
# print ("x : ", - data.translation.z, " y: ", data.translation.x , "z: " , data.translation.y, " and delta is ", delta)
MESSAGE = '{{"MHTrack": [{{"Type":"CameraAnimation"}},{{"Location":[{},{},{}],"Rotation":[{:.4f},{:.4f},{:.4f},{:.4f}],"Scale":[1,1,1]}}]}}'.format((pos_scale*(-data.translation.z-zeroZ)),(pos_scale*(data.translation.x-zeroX)),(pos_scale*(data.translation.y-zeroY)), rot_scale*data.rotation.z, rot_scale*-data.rotation.x, rot_scale*-data.rotation.y,rot_scale*data.rotation.w)
for IP in IPs:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((IP, TCP_PORT))
s.send(MESSAGE.encode('utf-8'))
s.close()
finally:
pipe.stop()