Skip to content

Commit

Permalink
Fix error upon construction of linear motor objects
Browse files Browse the repository at this point in the history
Fixes #733
  • Loading branch information
WasabiFan committed Apr 14, 2020
1 parent af12545 commit d557e54
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions ev3dev2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class Device(object):

__slots__ = [
'_path',
'_name',
'_device_index',
'_attr_cache',
'kwargs',
Expand Down Expand Up @@ -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

Expand Down
26 changes: 22 additions & 4 deletions ev3dev2/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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


Expand All @@ -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


Expand All @@ -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


Expand Down Expand Up @@ -310,6 +318,7 @@ class Motor(Device):
'max_rpm',
'max_dps',
'max_dpm',
'_motor_type',
]

#: Run the motor until another command is sent.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit d557e54

Please sign in to comment.