Code: Select all
shake_accel = (0, 0, 0)
for _ in range(avg_count):
# shake_accel creates a list of tuples from acceleration data.
# zip takes multiple tuples and zips them together, as in:
# In : zip([-0.2, 0.0, 9.5], [37.9, 13.5, -72.8])
# Out: [(-0.2, 37.9), (0.0, 13.5), (9.5, -72.8)]
# map applies sum to each member of this tuple, resulting in a
# 3-member list. tuple converts this list into a tuple which is
# used as shake_accel.
shake_accel = tuple(map(sum, zip(shake_accel, self.acceleration)))
time.sleep(total_delay / avg_count)
avg = tuple(value / avg_count for value in shake_accel)
total_accel = math.sqrt(sum(map(lambda x: x * x, avg)))
return total_accel > shake_threshold
I'm pretty sure this isn't correct. Imagine if you are shaking the accelerometer backwards and forwards parallel to the x-axis. That will give both positive and negative x-acceleration values: if you total them up, the positive and negative values will cancel out and you'll end up with approximately zero. That means when you compute the average and magnitude, you'll likely end up with a value below the threshold, no matter how vigorously you shake the accelerometer. Indeed when I shake the accelerometer and look at the values, sometimes total_accel is a lower value than when standing still.
Can someone check my understanding, and hopefully tell me where I've gone wrong?
Any input gratefully received!
(PS If I'm correct and there is an issue, I believe I have a simple fix!)