def set_units(self, distance_units=None, speed_units=None, acceleration_units=None): """ Sets the units this instance will use. These units are used in with settings and the data returned. By default SI units (m and m/s) are used. It is a good idea to set the units before any other commands are sent if using non-default units. :param distance_units: The distance units to use: "mm", "cm", "m", "km", "in", "ft", "mi" :type distance_units: str :param speed_units: The speed units to use: "mm/s", "cm/s", "m/s", "km/h", "in/s", "ft/s", "mi/h" :type speed_units: str :param acceleration_units: The acceleration units to use: "mm/s^2", "m/s^2", "in/s^2", "ft/s^2" :type acceleration_units: str """ try: # Performing a units conversion will throw an exception if the units are not valid if distance_units is not None: units.convert_distance_to_si(distance_units, 1) self.distance_units = distance_units if speed_units is not None: units.convert_speed_to_si(speed_units, 1) self.speed_units = speed_units if acceleration_units is not None: units.convert_acceleration_to_si(acceleration_units, 1) self.acceleration_units = acceleration_units except ValueError as err: raise ValueError(err)
def set_distance_filter(self, minimum, maximum): """ Sets the distance filter applied to the readings. :param minimum: The minimum distance (in units as specified by :meth:`set_units`) :type minimum: number :param maximum: The maximum distance (in units as specified by :meth:`set_units`) :type maximum: number """ minimum = int( units.convert_distance_to_si(self.distance_units, minimum) * 1000) maximum = int( units.convert_distance_to_si(self.distance_units, maximum) * 1000) if not (isinstance(minimum, int) and 0 <= minimum <= 10000): raise ValueError( "Distance filter minimum must be a number between 0 and 10000mm" ) if not (isinstance(maximum, int) and 0 <= maximum <= 10000): raise ValueError( "Distance filter maximum must be a number between 0 and 10000mm" ) if maximum < minimum: raise ValueError( "Distance filter maximum must be greater than the minimum") try: self._send(pack("<BBHH", 0x06, 0x02, minimum, maximum)) res = unpack("<BBHH", self._read()) if res[0] == 0x06 and res[1] == 0x01: if res[2] == minimum and res[3] == maximum: return True else: raise Exception("Distance filter did not set correctly") else: raise Exception("Invalid response") except Exception: raise Exception("Failed to set distance filter")
def test_convert_from_mi_to_m(self): # Test conversions of distances to SI units converted = units.convert_distance_to_si('mi', 0.135) self.assertEqual(217.3, converted)
def test_convert_from_ft_to_m(self): # Test conversions of distances to SI units converted = units.convert_distance_to_si('ft', 23) self.assertEqual(7.01, converted)
def test_convert_from_km_to_m(self): # Test conversions of distances to SI units converted = units.convert_distance_to_si('km', 0.123) self.assertEqual(123, converted)
def test_convert_from_mm_to_m(self): # Test conversions of distances to SI units converted = units.convert_distance_to_si('mm', 1010) self.assertEqual(1.01, converted)
def test_convert_from_m_to_m(self): # Test the identity conversion converted = units.convert_distance_to_si('m', 1.01) self.assertEqual(1.01, converted)