def _compute_speed(self, values, speed):
wheel_diff = values.last_wheel_event_time - self._previous_wheel
rev_diff = values.cumulative_wheel_revolutions - self._previous_revolutions
if wheel_diff:
# Rotations per minute is 60 times the amount of revolutions since
# the last update over the time since the last update
rpm = 60*(rev_diff/(wheel_diff/1024))
# We then mutiply it by the wheel's circumference and convert it to mph
speed = round((rpm * self.circumference) * (60/63360), 1)
if speed < 0:
speed = self._previous_speed
self._previous_speed = speed
self._previous_revolutions = values.cumulative_wheel_revolutions
self._speed_failed = 0
else:
self._speed_failed += 1
if self._speed_failed >= 3:
speed = 0
self._previous_wheel = values.last_wheel_event_time
return speed