-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathoptsimulate.py
69 lines (53 loc) · 2.4 KB
/
optsimulate.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
# OpenPTrack Sender Simulator
# Sept 13, 2015
import socket, time, json, time, random
UDP_IP = "127.0.0.1"
UDP_PORT = 21234
PERIOD = .100 # how often to publish in time
# For the random walk
MAXSTEP_X = 10
MAXSTEP_Y = 10
WOBBLE_Z = 1
Z_NOMINAL = 40
# Increasing packet seq number
_SEQ = 0
# Current message format
# https://github.com/OpenPTrack/open_ptrack/wiki/Using%20The%20Data
#
#MESSAGE = '{"header":{"seq":336988,"stamp":{"sec":1441244414,"nsec":266356327},"frame_id":"world"},"tracks":[{"id":170,"x":0.740519,"y":-3.21577,"height":1.01898,"age":79.4518,"confidence":0.491777},{"id":172,"x":0.843167,"y":-3.29433,"height":1.10497,"age":29.471,"confidence":0.500193}]}'
def track( id, x, y, height, age, confidence ) :
return {"id":id, "x":x, "y":y, "height":height, "age": age, "confidence":confidence}
def packet( tracks ) :
global _SEQ
_SEQ+=1
now = float(time.time())
sec = int(now)
nsec = int((now-sec) * 1e9)
header = { "seq":_SEQ, "stamp": {"sec":sec, "nsec":nsec}, "frame_id":"world" }
return { "header":header, "tracks":tracks }
# Provide two random walkers
# More is exercise for reader ...
def walk(W):
for w in W:
w[0] += MAXSTEP_X * 2*(random.random() - 0.5)
w[1] += MAXSTEP_Y * 2*(random.random() - 0.5)
w[2] = Z_NOMINAL + WOBBLE_Z*2*(random.random()-0.5)
walkers = [ [random.randrange(200)-100, random.randrange(200)-100, Z_NOMINAL],
[random.randrange(200)-100, random.randrange(200)-100, Z_NOMINAL] ]
print("^C to stop")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
try:
while True:
walk(walkers)
MESSAGE = json.dumps( packet( [ track(42, walkers[0][0], walkers[0][1], walkers[0][2], _SEQ+100+random.random(), random.random()),
track(43, walkers[1][0], walkers[1][1], walkers[1][2], _SEQ+100+random.random(), random.random())] ) )
# We throw some zeroes at the end to simulate OpenPTrack's current zero padding,
# so parsers make sure to handle it. This padding should be removed soon.
# https://github.com/OpenPTrack/open_ptrack/issues/52
payload = bytes(MESSAGE.encode('utf-8')) + bytes(bytearray(100))
sock.sendto(payload, (UDP_IP, UDP_PORT))
print(payload)
time.sleep(PERIOD)
except KeyboardInterrupt:
pass # do cleanup here