Skip to content
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ __pycache__
dist
*.egg-info
RELEASE-VERSION
.idea/

2 changes: 1 addition & 1 deletion demo/auto-drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def backup():
# `run-timed` command will return immediately, so we will have to wait
# until both motors are stopped before continuing.
for m in motors:
m.stop(stop_command='brake')
m.stop(stop_action='brake')
m.run_timed(duty_cycle_sp=-50, time_sp=1500)

# When motor is stopped, its `state` attribute returns empty list.
Expand Down
4 changes: 2 additions & 2 deletions demo/remote-control.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def on_press(state):
Leds.set_color(led_group, direction > 0 and Leds.GREEN or Leds.RED)
else:
# Stop otherwise
motor.stop(stop_command='brake')
motor.stop(stop_action='brake')
Leds.set(led_group, brightness_pct=0)

return on_press
Expand All @@ -87,7 +87,7 @@ def on_press(state):
Sound.speak('Oops, excuse me!')

for motor in (lmotor, rmotor):
motor.stop(stop_command='brake')
motor.stop(stop_action='brake')

# Turn red lights on
for led in (Leds.LEFT, Leds.RIGHT):
Expand Down
64 changes: 32 additions & 32 deletions ev3dev/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,17 @@ def commands(self):

- `run-forever` will cause the motor to run until another command is sent.
- `run-to-abs-pos` will run to an absolute position specified by `position_sp`
and then stop using the command specified in `stop_command`.
and then stop using the command specified in `stop_action`.
- `run-to-rel-pos` will run to a position relative to the current `position` value.
The new position will be current `position` + `position_sp`. When the new
position is reached, the motor will stop using the command specified by `stop_command`.
position is reached, the motor will stop using the command specified by `stop_action`.
- `run-timed` will run the motor for the amount of time specified in `time_sp`
and then stop the motor using the command specified by `stop_command`.
and then stop the motor using the command specified by `stop_action`.
- `run-direct` will run the motor at the duty cycle specified by `duty_cycle_sp`.
Unlike other run commands, changing `duty_cycle_sp` while running *will*
take effect immediately.
- `stop` will stop any of the run commands before they are complete using the
command specified by `stop_command`.
command specified by `stop_action`.
- `reset` will reset all of the motor parameter attributes to their default value.
This will also have the effect of stopping the motor.
"""
Expand Down Expand Up @@ -594,21 +594,21 @@ def state(self):
return self.get_attr_set('state')

@property
def stop_command(self):
def stop_action(self):
"""
Reading returns the current stop command. Writing sets the stop command.
The value determines the motors behavior when `command` is set to `stop`.
Also, it determines the motors behavior when a run command completes. See
`stop_commands` for a list of possible values.
`stop_actions` for a list of possible values.
"""
return self.get_attr_string('stop_command')
return self.get_attr_string('stop_action')

@stop_command.setter
def stop_command(self, value):
self.set_attr_string('stop_command', value)
@stop_action.setter
def stop_action(self, value):
self.set_attr_string('stop_action', value)

@property
def stop_commands(self):
def stop_actions(self):
"""
Returns a list of stop modes supported by the motor controller.
Possible values are `coast`, `brake` and `hold`. `coast` means that power will
Expand All @@ -621,7 +621,7 @@ def stop_commands(self):
position. If an external force tries to turn the motor, the motor will 'push
back' to maintain its position.
"""
return self.get_attr_set('stop_commands')
return self.get_attr_set('stop_actions')

@property
def time_sp(self):
Expand All @@ -644,17 +644,17 @@ def time_sp(self, value):
COMMAND_RUN_FOREVER = 'run-forever'

# Run to an absolute position specified by `position_sp` and then
# stop using the command specified in `stop_command`.
# stop using the command specified in `stop_action`.
COMMAND_RUN_TO_ABS_POS = 'run-to-abs-pos'

# Run to a position relative to the current `position` value.
# The new position will be current `position` + `position_sp`.
# When the new position is reached, the motor will stop using
# the command specified by `stop_command`.
# the command specified by `stop_action`.
COMMAND_RUN_TO_REL_POS = 'run-to-rel-pos'

# Run the motor for the amount of time specified in `time_sp`
# and then stop the motor using the command specified by `stop_command`.
# and then stop the motor using the command specified by `stop_action`.
COMMAND_RUN_TIMED = 'run-timed'

# Run the motor at the duty cycle specified by `duty_cycle_sp`.
Expand All @@ -663,7 +663,7 @@ def time_sp(self, value):
COMMAND_RUN_DIRECT = 'run-direct'

# Stop any of the run commands before they are complete using the
# command specified by `stop_command`.
# command specified by `stop_action`.
COMMAND_STOP = 'stop'

# Reset all of the motor parameter attributes to their default value.
Expand Down Expand Up @@ -711,7 +711,7 @@ def run_forever(self, **kwargs):

def run_to_abs_pos(self, **kwargs):
"""Run to an absolute position specified by `position_sp` and then
stop using the command specified in `stop_command`.
stop using the command specified in `stop_action`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
Expand All @@ -721,15 +721,15 @@ def run_to_rel_pos(self, **kwargs):
"""Run to a position relative to the current `position` value.
The new position will be current `position` + `position_sp`.
When the new position is reached, the motor will stop using
the command specified by `stop_command`.
the command specified by `stop_action`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-to-rel-pos'

def run_timed(self, **kwargs):
"""Run the motor for the amount of time specified in `time_sp`
and then stop the motor using the command specified by `stop_command`.
and then stop the motor using the command specified by `stop_action`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
Expand All @@ -746,7 +746,7 @@ def run_direct(self, **kwargs):

def stop(self, **kwargs):
"""Stop any of the run commands before they are complete using the
command specified by `stop_command`.
command specified by `stop_action`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
Expand Down Expand Up @@ -999,24 +999,24 @@ def state(self):
return self.get_attr_set('state')

@property
def stop_command(self):
def stop_action(self):
"""
Sets the stop command that will be used when the motor stops. Read
`stop_commands` to get the list of valid values.
`stop_actions` to get the list of valid values.
"""
raise Exception("stop_command is a write-only property!")
raise Exception("stop_action is a write-only property!")

@stop_command.setter
def stop_command(self, value):
self.set_attr_string('stop_command', value)
@stop_action.setter
def stop_action(self, value):
self.set_attr_string('stop_action', value)

@property
def stop_commands(self):
def stop_actions(self):
"""
Gets a list of stop commands. Valid values are `coast`
and `brake`.
"""
return self.get_attr_set('stop_commands')
return self.get_attr_set('stop_actions')

@property
def time_sp(self):
Expand All @@ -1039,7 +1039,7 @@ def time_sp(self, value):
COMMAND_RUN_FOREVER = 'run-forever'

# Run the motor for the amount of time specified in `time_sp`
# and then stop the motor using the command specified by `stop_command`.
# and then stop the motor using the command specified by `stop_action`.
COMMAND_RUN_TIMED = 'run-timed'

# Run the motor at the duty cycle specified by `duty_cycle_sp`.
Expand All @@ -1048,7 +1048,7 @@ def time_sp(self, value):
COMMAND_RUN_DIRECT = 'run-direct'

# Stop any of the run commands before they are complete using the
# command specified by `stop_command`.
# command specified by `stop_action`.
COMMAND_STOP = 'stop'

# With `normal` polarity, a positive duty cycle will
Expand Down Expand Up @@ -1081,7 +1081,7 @@ def run_forever(self, **kwargs):

def run_timed(self, **kwargs):
"""Run the motor for the amount of time specified in `time_sp`
and then stop the motor using the command specified by `stop_command`.
and then stop the motor using the command specified by `stop_action`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
Expand All @@ -1098,7 +1098,7 @@ def run_direct(self, **kwargs):

def stop(self, **kwargs):
"""Stop any of the run commands before they are complete using the
command specified by `stop_command`.
command specified by `stop_action`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
Expand Down
2 changes: 1 addition & 1 deletion tests/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def dummy(self):
self.assertEqual(m.speed, 0)
self.assertEqual(m.speed_sp, 0)
self.assertEqual(m.state, ['running'])
self.assertEqual(m.stop_command, 'coast')
self.assertEqual(m.stop_action, 'coast')
self.assertEqual(m.time_sp, 1000)

with self.assertRaises(Exception):
Expand Down
2 changes: 1 addition & 1 deletion tests/fake-sys
39 changes: 34 additions & 5 deletions tests/motor/motor_motion_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def tearDownClass(cls):
def initialize_motor(self):
self._param['motor'].command = 'reset'

def run_to_positions(self,stop_command,command,speed_sp,positions,tolerance):
self._param['motor'].stop_command = stop_command
def run_to_positions(self,stop_action,command,speed_sp,positions,tolerance):
self._param['motor'].stop_action = stop_action
self._param['motor'].speed_sp = speed_sp

target = self._param['motor'].position
Expand All @@ -48,6 +48,8 @@ def run_to_positions(self,stop_command,command,speed_sp,positions,tolerance):
self._param['motor'].command = 'stop'

def test_stop_brake_no_ramp_med_speed_relative(self):
if not self._param['has_brake']:
self.skipTest('brake not supported by this motor controller')
self.initialize_motor()
self.run_to_positions('brake','run-to-rel-pos',400,[0,90,180,360,720,-720,-360,-180,-90,0],20)

Expand All @@ -56,6 +58,8 @@ def test_stop_hold_no_ramp_med_speed_relative(self):
self.run_to_positions('hold','run-to-rel-pos',400,[0,90,180,360,720,-720,-360,-180,-90,0],5)

def test_stop_brake_no_ramp_low_speed_relative(self):
if not self._param['has_brake']:
self.skipTest('brake not supported by this motor controller')
self.initialize_motor()
self.run_to_positions('brake','run-to-rel-pos',100,[0,90,180,360,720,-720,-360,-180,-90,0],20)

Expand All @@ -64,6 +68,8 @@ def test_stop_hold_no_ramp_low_speed_relative(self):
self.run_to_positions('hold','run-to-rel-pos',100,[0,90,180,360,720,-720,-360,-180,-90,0],5)

def test_stop_brake_no_ramp_high_speed_relative(self):
if not self._param['has_brake']:
self.skipTest('brake not supported by this motor controller')
self.initialize_motor()
self.run_to_positions('brake','run-to-rel-pos',900,[0,90,180,360,720,-720,-360,-180,-90,0],50)

Expand All @@ -72,6 +78,8 @@ def test_stop_hold_no_ramp_high_speed_relative(self):
self.run_to_positions('hold','run-to-rel-pos',100,[0,90,180,360,720,-720,-360,-180,-90,0],5)

def test_stop_brake_no_ramp_med_speed_absolute(self):
if not self._param['has_brake']:
self.skipTest('brake not supported by this motor controller')
self.initialize_motor()
self.run_to_positions('brake','run-to-abs-pos',400,[0,90,180,360,180,90,0,-90,-180,-360,-180,-90,0],20)

Expand All @@ -80,6 +88,8 @@ def test_stop_hold_no_ramp_med_speed_absolute(self):
self.run_to_positions('hold','run-to-abs-pos',400,[0,90,180,360,180,90,0,-90,-180,-360,-180,-90,0],5)

def test_stop_brake_no_ramp_low_speed_absolute(self):
if not self._param['has_brake']:
self.skipTest('brake not supported by this motor controller')
self.initialize_motor()
self.run_to_positions('brake','run-to-abs-pos',100,[0,90,180,360,180,90,0,-90,-180,-360,-180,-90,0],20)

Expand All @@ -88,6 +98,8 @@ def test_stop_hold_no_ramp_low_speed_absolute(self):
self.run_to_positions('hold','run-to-abs-pos',100,[0,90,180,360,180,90,0,-90,-180,-360,-180,-90,0],5)

def test_stop_brake_no_ramp_high_speed_absolute(self):
if not self._param['has_brake']:
self.skipTest('brake not supported by this motor controller')
self.initialize_motor()
self.run_to_positions('brake','run-to-abs-pos',900,[0,90,180,360,180,90,0,-90,-180,-360,-180,-90,0],50)

Expand All @@ -97,14 +109,31 @@ def test_stop_hold_no_ramp_high_speed_absolute(self):

# Add all the tests to the suite - some tests apply only to certain drivers!

def AddTachoMotorMotionTestsToSuite( suite, driver_name, params ):
def AddTachoMotorMotionTestsToSuite(suite, params):
suite.addTest(ptc.ParameterizedTestCase.parameterize(TestMotorMotion, param=params))

if __name__ == '__main__':
params = { 'motor': ev3.Motor('outA'), 'port': 'outA', 'driver_name': 'lego-ev3-l-motor' }
ev3_params = {
'motor': ev3.Motor('outA'),
'port': 'outA',
'driver_name': 'lego-ev3-l-motor',
'has_brake': True,
}
brickpi_params = {
'motor': ev3.Motor('ttyAMA0:MA'),
'port': 'ttyAMA0:MA',
'driver_name': 'lego-nxt-motor',
'has_brake': False,
}
pistorms_params = {
'motor': ev3.Motor('pistorms:BAM1'),
'port': 'pistorms:BAM1',
'driver_name': 'lego-nxt-motor',
'has_brake': True,
}

suite = unittest.TestSuite()

AddTachoMotorMotionTestsToSuite( suite, 'lego-ev3-l-motor', params )
AddTachoMotorMotionTestsToSuite(suite, ev3_params)

unittest.TextTestRunner(verbosity=1,buffer=True ).run(suite)
34 changes: 17 additions & 17 deletions tests/motor/motor_param_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,37 +461,37 @@ def test_state_value_after_reset(self):

class TestTachoMotorStopCommandValue(ptc.ParameterizedTestCase):

def test_stop_command_illegal(self):
def test_stop_action_illegal(self):
with self.assertRaises(IOError):
self._param['motor'].stop_command = 'ThisShouldNotWork'
self._param['motor'].stop_action = 'ThisShouldNotWork'

def test_stop_command_coast(self):
self._param['motor'].stop_command = 'coast'
self.assertEqual(self._param['motor'].stop_command, 'coast')
def test_stop_action_coast(self):
self._param['motor'].stop_action = 'coast'
self.assertEqual(self._param['motor'].stop_action, 'coast')

def test_stop_command_brake(self):
self._param['motor'].stop_command = 'brake'
self.assertEqual(self._param['motor'].stop_command, 'brake')
def test_stop_action_brake(self):
self._param['motor'].stop_action = 'brake'
self.assertEqual(self._param['motor'].stop_action, 'brake')

def test_stop_command_hold(self):
self._param['motor'].stop_command = 'hold'
self.assertEqual(self._param['motor'].stop_command, 'hold')
def test_stop_action_hold(self):
self._param['motor'].stop_action = 'hold'
self.assertEqual(self._param['motor'].stop_action, 'hold')

def test_time_sp_after_reset(self):
self._param['motor'].stop_command = 'hold'
self._param['motor'].stop_action = 'hold'
self._param['motor'].command = 'reset'
self.assertEqual(self._param['motor'].stop_command, 'coast')
self.assertEqual(self._param['motor'].stop_action, 'coast')

class TestTachoMotorStopCommandsValue(ptc.ParameterizedTestCase):

def test_stop_commands_value(self):
self.assertTrue(set(self._param['motor'].stop_commands) == {'coast'
def test_stop_actions_value(self):
self.assertTrue(set(self._param['motor'].stop_actions) == {'coast'
,'brake'
,'hold'})

def test_stop_commands_value_is_read_only(self):
def test_stop_actions_value_is_read_only(self):
with self.assertRaises(AttributeError):
self._param['motor'].stop_commands = "ThisShouldNotWork"
self._param['motor'].stop_actions = "ThisShouldNotWork"

class TestTachoMotorTimeSpValue(ptc.ParameterizedTestCase):

Expand Down
Loading