Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error upon construction of linear motor objects #734

Open
wants to merge 2 commits into
base: ev3dev-stretch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we set these four to None in an else so they are always defined


@property
def address(self):
Expand Down