From d557e5457224fc95a1338314a95b1bc63ad513bb Mon Sep 17 00:00:00 2001 From: Kaelin Laundry Date: Tue, 14 Apr 2020 15:11:24 -0700 Subject: [PATCH] Fix error upon construction of linear motor objects Fixes #733 --- ev3dev2/__init__.py | 3 +++ ev3dev2/motor.py | 26 ++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/ev3dev2/__init__.py b/ev3dev2/__init__.py index 5e776fb..19fab52 100644 --- a/ev3dev2/__init__.py +++ b/ev3dev2/__init__.py @@ -163,6 +163,7 @@ class Device(object): __slots__ = [ '_path', + '_name', '_device_index', '_attr_cache', 'kwargs', @@ -214,9 +215,11 @@ def get_index(file): else: try: name = next(list_device_names(classpath, name_pattern, **kwargs)) + self._name = name self._path = classpath + '/' + name self._device_index = get_index(name) except StopIteration: + self._name = None self._path = None self._device_index = None diff --git a/ev3dev2/motor.py b/ev3dev2/motor.py index 6e61ecf..bf89235 100644 --- a/ev3dev2/motor.py +++ b/ev3dev2/motor.py @@ -177,6 +177,8 @@ def to_native_units(self, motor): if abs(self.rotations_per_second) > motor.max_rps: raise SpeedInvalid("invalid rotations-per-second: {} max RPS is {}, {} was requested".format( motor, motor.max_rps, self.rotations_per_second)) + if motor._motor_type != 'rotational': + raise SpeedInvalid("{} units can only be used for rotational motors".format(type(self).__name__)) return self.rotations_per_second / motor.max_rps * motor.max_speed @@ -202,6 +204,8 @@ def to_native_units(self, motor): if abs(self.rotations_per_minute) > motor.max_rpm: raise SpeedInvalid("invalid rotations-per-minute: {} max RPM is {}, {} was requested".format( motor, motor.max_rpm, self.rotations_per_minute)) + if motor._motor_type != 'rotational': + raise SpeedInvalid("{} units can only be used for rotational motors".format(type(self).__name__)) return self.rotations_per_minute / motor.max_rpm * motor.max_speed @@ -227,6 +231,8 @@ def to_native_units(self, motor): if abs(self.degrees_per_second) > motor.max_dps: raise SpeedInvalid("invalid degrees-per-second: {} max DPS is {}, {} was requested".format( motor, motor.max_dps, self.degrees_per_second)) + if motor._motor_type != 'rotational': + raise SpeedInvalid("{} units can only be used for rotational motors".format(type(self).__name__)) return self.degrees_per_second / motor.max_dps * motor.max_speed @@ -252,6 +258,8 @@ def to_native_units(self, motor): if abs(self.degrees_per_minute) > motor.max_dpm: raise SpeedInvalid("invalid degrees-per-minute: {} max DPM is {}, {} was requested".format( motor, motor.max_dpm, self.degrees_per_minute)) + if motor._motor_type != 'rotational': + raise SpeedInvalid("{} units can only be used for rotational motors".format(type(self).__name__)) return self.degrees_per_minute / motor.max_dpm * motor.max_speed @@ -310,6 +318,7 @@ class Motor(Device): 'max_rpm', 'max_dps', 'max_dpm', + '_motor_type', ] #: Run the motor until another command is sent. @@ -394,6 +403,13 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam kwargs['address'] = address super(Motor, self).__init__(self.SYSTEM_CLASS_NAME, name_pattern, name_exact, **kwargs) + if self._name and self._name.startswith('motor'): + self._motor_type = 'rotational' + elif self._name and self._name.startswith('linear'): + self._motor_type = 'linear' + else: + self._motor_type = None + self._address = None self._command = None self._commands = None @@ -422,10 +438,12 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam self._stop_actions = None self._time_sp = None self._poll = None - self.max_rps = float(self.max_speed / self.count_per_rot) - self.max_rpm = self.max_rps * 60 - self.max_dps = self.max_rps * 360 - self.max_dpm = self.max_rpm * 360 + + if self._motor_type == 'rotational': + self.max_rps = float(self.max_speed / self.count_per_rot) + self.max_rpm = self.max_rps * 60 + self.max_dps = self.max_rps * 360 + self.max_dpm = self.max_rpm * 360 @property def address(self):