-
Notifications
You must be signed in to change notification settings - Fork 1
/
GiftSizeCalculator.py
157 lines (143 loc) · 5.57 KB
/
GiftSizeCalculator.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
from threading import Thread, Timer
import time
import operator
width_sensor_dist = 35
height_sensor_dist = 58
depth_sensor_dist = 51
gift_present_sensor_dist = 85
noise = 2
class GiftSizeCalculator:
def __init__(self, on_size_calculated, gift_placed, led):
self.on_size_calculated = on_size_calculated
self.gift_placed = gift_placed
self.gift_width = -1
self.gift_height = -1
self.gift_depth = -1
self.gift_distance = -1
self.paper_width = 50
self.paper_height = -1
self.paper_overlap = 3 # how many cms the wrapping paper needs to overlap for the gift
self.finished = False
self.measuring = False
self.active = False
self.dist_active = False
self.led = led
def generate_mock_values(self):
print("Generating mocks")
self.gift_width = 30
self.gift_height = 20
self.gift_depth = 10
self.paper_height = 64
"""self.active = True
self.set_width(9)
self.set_height(39)
self.set_depth(53)"""
def set_width(self, w):
if self.active:
if width_sensor_dist - noise <= w <= width_sensor_dist + noise:
# this is the default distance
self.gift_width = -1
else:
width = width_sensor_dist - w
print("set width to " + str(width))
self.gift_width = width
self.check_dimensions()
def set_height(self, h):
if self.active:
if height_sensor_dist - noise <= h <= height_sensor_dist + noise:
self.gift_height = -1
# initial value
else:
height = height_sensor_dist - h
print("set height to " + str(height))
self.gift_height = height
self.check_dimensions()
def set_depth(self, d):
if self.active:
if depth_sensor_dist - noise <= d <= depth_sensor_dist + noise:
self.gift_depth = -1
# initial value
else:
depth = depth_sensor_dist - d
print("set depth to " + str(depth))
self.gift_depth = depth
self.check_dimensions()
def set_distance_to_gift(self, dist):
if self.dist_active:
if gift_present_sensor_dist - noise <= dist <= gift_present_sensor_dist + noise:
self.gift_distance = -1
# initial value
else:
dist = gift_present_sensor_dist - dist
print("gift distance: " + str(dist))
self.gift_distance = dist
t1 = Timer(1.0, self.timer_dist, (dist,0))
t1.start()
def timer(self, w, h, d, led_id):
if w != self.gift_width or h != self.gift_height or d != self. gift_depth:
self.led.set_rgb("0,0,0")
# set LEDs to black
else:
if self.active:
self.led.set_rgb("0,255,0",led_id)
if led_id != 2:
t_next = Timer(1.0, self.timer, (w, h, d,led_id+1))
t_next.start()
else:
print("Woop woop, size measured!")
self.active = False
self.calc_dimensions(w,h,d)
return
def timer_dist(self, dist, led_id):
if dist != self.gift_distance:
self.led.set_rgb("0,0,0")
else:
if self.dist_active:
self.led.set_rgb("0,255,0",led_id)
if led_id != 2:
t_next = Timer(1.0, self.timer_dist, (dist,led_id+1))
t_next.start()
else:
print("Woop woop, gift placed!")
self.dist_active = False
self.gift_placed()
return
def check_dimensions(self):
if not (self.gift_width == -1 or self.gift_height == -1 or self.gift_depth == -1):
print("Started new timer")
t1 = Timer(1.0, self.timer, (self.gift_width, self.gift_height, self.gift_depth,0))
t1.start()
def calc_dimensions(self, w,h,d):
print("______________________________________________________")
print("Calculating size...")
time.sleep(1)
print("______________________________________________________")
dimensions = {
"width": w,
"height": h,
"depth": d,
}
dimensions = sorted(dimensions.items(), key=operator.itemgetter(1))
print(dimensions)
self.gift_width = dimensions[2][1]
self.gift_height = dimensions[1][1]
self.gift_depth = dimensions[0][1]
print("width " + str(self.gift_width))
print("height " + str(self.gift_height))
print("depth " + str(self.gift_depth))
if self.gift_width + (2 * self.gift_depth) <= self.paper_width:
self.paper_height = self.gift_height * 2 + self.gift_depth * 2 + 2 * self.paper_overlap
else:
# gift is too wide
# check if it is also too high --> if so, we cannot pack this gift
if self.gift_height + (2 * self.gift_depth) > self.paper_width:
print("Gift is too big")
exit()
else:
# swap gift width and height
tmp = self.gift_width
self.gift_width = self.gift_height
self.gift_height = tmp
self.paper_height = self.gift_height * 2 + self.gift_depth * 2 + 2 * self.paper_overlap
self.on_size_calculated()
return