Skip to content

Commit d557e54

Browse files
committed
Fix error upon construction of linear motor objects
Fixes #733
1 parent af12545 commit d557e54

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

ev3dev2/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class Device(object):
163163

164164
__slots__ = [
165165
'_path',
166+
'_name',
166167
'_device_index',
167168
'_attr_cache',
168169
'kwargs',
@@ -214,9 +215,11 @@ def get_index(file):
214215
else:
215216
try:
216217
name = next(list_device_names(classpath, name_pattern, **kwargs))
218+
self._name = name
217219
self._path = classpath + '/' + name
218220
self._device_index = get_index(name)
219221
except StopIteration:
222+
self._name = None
220223
self._path = None
221224
self._device_index = None
222225

ev3dev2/motor.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ def to_native_units(self, motor):
177177
if abs(self.rotations_per_second) > motor.max_rps:
178178
raise SpeedInvalid("invalid rotations-per-second: {} max RPS is {}, {} was requested".format(
179179
motor, motor.max_rps, self.rotations_per_second))
180+
if motor._motor_type != 'rotational':
181+
raise SpeedInvalid("{} units can only be used for rotational motors".format(type(self).__name__))
180182
return self.rotations_per_second / motor.max_rps * motor.max_speed
181183

182184

@@ -202,6 +204,8 @@ def to_native_units(self, motor):
202204
if abs(self.rotations_per_minute) > motor.max_rpm:
203205
raise SpeedInvalid("invalid rotations-per-minute: {} max RPM is {}, {} was requested".format(
204206
motor, motor.max_rpm, self.rotations_per_minute))
207+
if motor._motor_type != 'rotational':
208+
raise SpeedInvalid("{} units can only be used for rotational motors".format(type(self).__name__))
205209
return self.rotations_per_minute / motor.max_rpm * motor.max_speed
206210

207211

@@ -227,6 +231,8 @@ def to_native_units(self, motor):
227231
if abs(self.degrees_per_second) > motor.max_dps:
228232
raise SpeedInvalid("invalid degrees-per-second: {} max DPS is {}, {} was requested".format(
229233
motor, motor.max_dps, self.degrees_per_second))
234+
if motor._motor_type != 'rotational':
235+
raise SpeedInvalid("{} units can only be used for rotational motors".format(type(self).__name__))
230236
return self.degrees_per_second / motor.max_dps * motor.max_speed
231237

232238

@@ -252,6 +258,8 @@ def to_native_units(self, motor):
252258
if abs(self.degrees_per_minute) > motor.max_dpm:
253259
raise SpeedInvalid("invalid degrees-per-minute: {} max DPM is {}, {} was requested".format(
254260
motor, motor.max_dpm, self.degrees_per_minute))
261+
if motor._motor_type != 'rotational':
262+
raise SpeedInvalid("{} units can only be used for rotational motors".format(type(self).__name__))
255263
return self.degrees_per_minute / motor.max_dpm * motor.max_speed
256264

257265

@@ -310,6 +318,7 @@ class Motor(Device):
310318
'max_rpm',
311319
'max_dps',
312320
'max_dpm',
321+
'_motor_type',
313322
]
314323

315324
#: Run the motor until another command is sent.
@@ -394,6 +403,13 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
394403
kwargs['address'] = address
395404
super(Motor, self).__init__(self.SYSTEM_CLASS_NAME, name_pattern, name_exact, **kwargs)
396405

406+
if self._name and self._name.startswith('motor'):
407+
self._motor_type = 'rotational'
408+
elif self._name and self._name.startswith('linear'):
409+
self._motor_type = 'linear'
410+
else:
411+
self._motor_type = None
412+
397413
self._address = None
398414
self._command = None
399415
self._commands = None
@@ -422,10 +438,12 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
422438
self._stop_actions = None
423439
self._time_sp = None
424440
self._poll = None
425-
self.max_rps = float(self.max_speed / self.count_per_rot)
426-
self.max_rpm = self.max_rps * 60
427-
self.max_dps = self.max_rps * 360
428-
self.max_dpm = self.max_rpm * 360
441+
442+
if self._motor_type == 'rotational':
443+
self.max_rps = float(self.max_speed / self.count_per_rot)
444+
self.max_rpm = self.max_rps * 60
445+
self.max_dps = self.max_rps * 360
446+
self.max_dpm = self.max_rpm * 360
429447

430448
@property
431449
def address(self):

0 commit comments

Comments
 (0)