-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_data.py
175 lines (131 loc) · 5.24 KB
/
send_data.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
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
"""Small example OSC client
This program sends 10 random values between 0.0 and 1.0 to the /filter address,
waiting for 1 seconds between each value.
/dev/tty.SLAB_USBtoUART
"""
import argparse
import random
import time
import math
import statistics
from pythonosc import osc_message_builder
from pythonosc import udp_client
import socket
# /*
# check angle ranges, merge closer ones
# build up an array rawData
# loop through the array and detect gap:
# if prev is 3 degree smaller than cur, then there is a gap, then
# update variable 3; variable 3 is group number
# if x is larger than 6,
# then divide the gap again with a larger degree gap
# build up a new array -> finalData[x][3]
# for each group:
# [0] average the distance
# [1] average the angle
# [2] get group size (lastAngle - firstAngle)
# from angle calculate x,y distance -> x = abs(distance*cosA); y = abs(distance*sinA)
# */
def groupData(arr):
angle = []
distance = []
newArr = [] # newArr = [[angle1, distance1, group0],[angle2, distance2, group0]]
groupIndex = 0
groupSize = []
finalArr = [[],[],[]] # final array = [[x, y, groupSize], [x, y groupSize]]
# print("length of rawData is " + str(len(arr)))
maxgap = 4 # 4 degree maximum gap
detected = False
# print("maximum gap is " + str(maxgap))
if(len(arr)>0):
#loop through rawData and seperate angle and distance
for i in range(len(arr)):
# angle
if(i%2 == 0):
if((i+1)<len(arr)):
try:
x = float(arr[i])
dist = float(arr[i+1])
detected = True
except Exception as e:
print ('index {%d} is corrupt!', i)
break
# print("angle, distance: %f %f " %(x, dist))
#first angle, create new group
if(i==0):
angleGroups = [[x]]
distanceGroups = [[dist]]
else:
if(dist!=0):
if abs(x - float(angleGroups[-1][-1]) )<= maxgap:
angleGroups[-1].append(x)
distanceGroups[-1].append(dist)
else:
# print("creating new group")
angleGroups.append([x])
distanceGroups.append([dist])
if(detected):
#get avg of each group
for i in range(len(angleGroups)):
eachAngleGroup = angleGroups[i]
eachDistanceGroup = distanceGroups[i]
avgAngle = statistics.median(eachAngleGroup) #turn degree into radius
avgDistance = statistics.median(eachDistanceGroup)
x = round( avgDistance * math.cos(math.radians(avgAngle)), 1)
y = round( avgDistance * math.sin(math.radians(avgAngle)), 1)
size = len(eachAngleGroup)
if(size>7):
print("avgAngle, avgDistance: %f %f %f" % (avgAngle, avgDistance, size))
# print("each angle group")
# print(*eachAngleGroup)
# print("each distance group")
# print(*eachDistanceGroup)
finalArr[0].append(x)
finalArr[1].append(y)
finalArr[2].append(size)
# finalArr.append([x, y ,size])
# finalArr.append([x, y ,size])
print("found people!!!!")
return finalArr
# Python program to get average of a list
def Average(lst):
return sum(lst) / len(lst)
client = udp_client.SimpleUDPClient("192.168.8.231", 9996)# Music Mac port
# client = udp_client.SimpleUDPClient("192.168.8.231", 9996)#heidi's macbook
# client2 = udp_client.SimpleUDPClient("192.168.8.106", 9999)
s = socket.socket()
host = socket.gethostname()
port = 7777
s.bind(('192.168.8.231',port)) # Music Mac
# s.bind(('137.146.126.135',port))
s.listen(5)
while True:
# print("sending osc loop 1")
c, addr = s.accept()
print("Connection accepted from " + repr(addr[1]))
while True:
# print("rendering osc loop 2")
data = c.recv(1026).decode("utf-8")
# print("debug 1")
if(data):
# print("Data sent: " + data)
newData = data.split(",")
newData.pop()
strData = ' '.join(newData)
# print("newData 1 is " + newData[0])
# print("newData -1 is " + newData[-1])
# print("newData is " + strData)
#group data and return an 2d array with poeple's position: x, y, size
finalData = groupData(newData)
print("final data is" )
print(*finalData)
client.send_message("/x", finalData[0])
client.send_message("/y", finalData[1])
client.send_message("/size", finalData[2])
# up to six people
# 2D array people[6][2]
# client.send_message("/open", newData[2])
# client2.send_message("/Austin", newData[3])
# print("Data sent: " + data)
#print(repr(addr[1]) + ": " + data)
#192,168,8,231