我正在研究一个用于构建机器人的小组项目,我们正在尝试实现一个加速度计,如果在途中捡起/按下机器人,它将触发警报。目前,我们的加速度计非常嘈杂,而且不太可靠。有人可以为我指出正确的解决方法吗? 当前代码:
def accel_alarm(self):
cur_accel = self.read_smooth_accel()
accel_x, accel_y, accel_z = cur_accel
base_x, base_y, base_z = (-32, 31, 1075) # values when robot is static
if abs(accel_x - base_x) > 350 or abs(
accel_y - base_y) > 350 or abs(accel_z - base_z) > 350:
if not (self.alarm):
self.send_alarm()
self.alarm = True
print(
"[accel_alarm] ALARM STATE " + str(accel_x) + " X " + str(
accel_y) + " Y " + str(accel_z) + " Z")
# Smooth accelerometer output by taking the average of the last n values
# where n = len(self.accel_data)
def read_smooth_accel(self):
cur_accel, _ = self.accel.read()
self.accel_data.pop()
self.accel_data.appendleft(cur_accel)
# For the first len(accel_data) values the average is not
# representative - just return current value
if [0, 0, 0] in self.accel_data:
return cur_accel
av_accel = [sum(i) / float(len(i)) for i in zip(*self.accel_data)]
return av_accel
第二种方法正试图解决嘈杂的问题,但它仍然不是很好...