Class DcMotor
The DC motor class provides a uniform interface for using regular DC motors with no fancy controls or feedback. This includes LEGO MINDSTORMS RCX motors and LEGO Power Functions motors.
Declaration
source linkDocumentation
Methods
▶ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, ...) overrideinherited doc Spin through the Linux sysfs class for the device type and find a device that matches the provided name pattern and attributes (if any).
def __init__(
self,
address=None,
name_pattern=SYSTEM_DEVICE_NAME_CONVENTION,
name_exact=False,
**kwargs,
)
This method overrides ev3dev2.Device.__init__.
- Parameters:
- class_name: class name of the device, a subdirectory of /sys/class.
For example, 'tacho-motor'.
- name_pattern: pattern that device name should match.
For example, 'sensor*' or 'motor*'. Default value: '*'.
- name_exact: when True, assume that the name_pattern provided is the
exact device name and use it directly.
- keyword arguments: used for matching the corresponding device
attributes. For example, address='outA', or driver_name=['lego-ev3-us', 'lego-nxt-us']. When argument value is a list, then a match against any entry of the list is enough.
Example:
d = ev3dev.Device('tacho-motor', address='outA')
s = ev3dev.Device('lego-sensor', driver_name=['lego-ev3-us', 'lego-nxt-us'])If there was no valid connected device, an error is thrown.
▷ def address(self) @property Returns the name of the port that this motor is connected to.
@property
def address(self)
▷ def command(self) @property Sets the command for the motor. Possible values are ``run-forever``, ``run-timed`` and ``stop``. Not all commands may be supported, so be sure to check the contents of the ``commands`` attribute.
@property
def command(self)
▷ def command(self, value) @command.setter @command.setter
def command(
self,
value,
)
▷ def commands(self) @property Returns a list of commands supported by the motor controller.
@property
def commands(self)
▷ def driver_name(self) @property Returns the name of the motor driver that loaded this device. See the list of [supported devices] for a list of drivers.
@property
def driver_name(self)
▷ def duty_cycle(self) @property Shows the current duty cycle of the PWM signal sent to the motor. Values are -100 to 100 (-100% to 100%).
@property
def duty_cycle(self)
▷ def duty_cycle_sp(self) @property Writing sets the duty cycle setpoint of the PWM signal sent to the motor. Valid values are -100 to 100 (-100% to 100%). Reading returns the current setpoint.
@property
def duty_cycle_sp(self)
@duty_cycle_sp.setter
def duty_cycle_sp(
self,
value,
)
▷ def polarity(self) @property Sets the polarity of the motor. Valid values are ``normal`` and ``inversed``.
@property
def polarity(self)
▷ def polarity(self, value) @polarity.setter @polarity.setter
def polarity(
self,
value,
)
▷ def ramp_down_sp(self) @property Sets the time in milliseconds that it take the motor to ramp down from 100% to 0%. Valid values are 0 to 10000 (10 seconds). Default is 0.
@property
def ramp_down_sp(self)
@ramp_down_sp.setter
def ramp_down_sp(
self,
value,
)
▷ def ramp_up_sp(self) @property Sets the time in milliseconds that it take the motor to up ramp from 0% to 100%. Valid values are 0 to 10000 (10 seconds). Default is 0.
@property
def ramp_up_sp(self)
@ramp_up_sp.setter
def ramp_up_sp(
self,
value,
)
▷ def run_direct(self, **kwargs) 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.
▷ def run_forever(self, **kwargs) Run the motor until another command is sent.
▷ def run_timed(self, **kwargs) Run the motor for the amount of time specified in ``time_sp`` and then stop the motor using the action specified by ``stop_action``.
▶ def state(self) @property Gets a list of flags indicating the motor status. Possible
flags are running and ramping. running indicates that the motor is powered. ramping indicates that the motor has not yet reached the duty_cycle_sp.
▷ def stop(self, **kwargs) Stop any of the run commands before they are complete using the action specified by ``stop_action``.
▷ def stop_action(self) @property Sets the stop action that will be used when the motor stops. Read ``stop_actions`` to get the list of valid values.
@property
def stop_action(self)
@stop_action.setter
def stop_action(
self,
value,
)
▷ def stop_actions(self) @property Gets a list of stop actions. Valid values are ``coast`` and ``brake``.
@property
def stop_actions(self)
▷ def time_sp(self) @property Writing specifies the amount of time the motor will run when using the ``run-timed`` command. Reading returns the current value. Units are in milliseconds.
@property
def time_sp(self)
▷ def time_sp(self, value) @time_sp.setter @time_sp.setter
def time_sp(
self,
value,
)