Skip to content

Commit

Permalink
Only let a spot in the middle activate the light
Browse files Browse the repository at this point in the history
  • Loading branch information
iver56 committed Oct 19, 2015
1 parent 86761f9 commit 92a33b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
36 changes: 27 additions & 9 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,37 @@ def is_max_larger_than(pixel_list, number):
return max(pixel_list) > number


def is_stationary_human(celsius_data):
def convert_1d_to_2d(index_1d):
x = index_1d % 4
y = index_1d / 4
return x, y


def is_in_the_middle(x, y):
return x in (1, 2) and y in (1, 2)


def is_almost_equal(a, b, eps=0.05):
return abs(a - b) <= eps


def is_human(celsius_data):
max_temp = max(celsius_data)
lowest_values = get_six_lowest_values(celsius_data)
median_of_lowest_values = median(lowest_values)
diff_median_max = max_temp - median_of_lowest_values

result = diff_median_max >= 2.2
#print 'is stationary human detected:', result
return result
warm_enough = diff_median_max >= 2.2

should_activate = False # becomes true if warm enough in the middle
if warm_enough:
for i in xrange(len(celsius_data)):
if is_almost_equal(celsius_data[i], max_temp):
x, y = convert_1d_to_2d(i)
if is_in_the_middle(x, y):
should_activate = True
# print 'is human detected:', warm_enough
return warm_enough, should_activate


def is_moving_human(celsius_data, previous_celsius_data):
Expand All @@ -47,9 +69,5 @@ def is_moving_human(celsius_data, previous_celsius_data):
if max_abs_diff_between_frames >= 0.4:
result = True

#print 'is moving human detected:', result
# print 'is moving human detected:', result
return result


def get_frequency():
return timedelta(seconds=2)
6 changes: 3 additions & 3 deletions read_thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ def tick(self):
temperature_celsius = convert_two_bytes_to_celsius(first_byte, second_byte)
celsius_data.append(temperature_celsius)

stationary = is_stationary_human(celsius_data)
warm_enough, should_activate = is_human(celsius_data)
moving = is_moving_human(celsius_data, self.previous_celsius_data)
now = datetime.now()

if stationary and not moving:
if warm_enough and should_activate and not moving:
self.time_stationary_detected = now
self.turn_light_on()
elif stationary and self.time_stationary_detected >= now - timedelta(seconds=10):
elif warm_enough and self.time_stationary_detected >= now - timedelta(seconds=10):
if self.current_light_level > 5:
self.turn_light_on()
else:
Expand Down

0 comments on commit 92a33b1

Please sign in to comment.