# Motor

Controlling the motors directly is not very precise because the NXT firmware does not expose the needed method for precise control. If you need more than basic controls, `nxt.motcont.MotCont` provides finer controls thanks to a program running on the NXT brick.

Work in progress… The `nxt.motor.Motor` class will probably be reworked in a future version.

class nxt.motor.Port(*values)

  Bases: `Enum`

  Output port identifier.

  A = 0

    Output port A.

  B = 1

    Output port B.

  C = 2

    Output port C.

class nxt.motor.Mode(*values)

  Bases: `Flag`

  Motor mode.

  These are flags that can be combined together using the | operator.

  IDLE = 0

    Keep the motor unpowered.

  ON = 1

    Enable motor power.

  BRAKE = 2

    Enable brake, motor input voltage is not left floating, must also be `ON`.

  REGULATED = 4

    Enable regulation, must also be `ON`.

class nxt.motor.RegulationMode(*values)

  Bases: `Enum`

  Motor regulation mode.

  IDLE = 0

    No regulation.

  SPEED = 1

    Speed regulation, `Mode.REGULATED` must be enabled.

  SYNC = 2

    Synchronous regulation of two motors, `Mode.REGULATED` must be enabled.

class nxt.motor.RunState(*values)

  Bases: `Enum`

  Motor run state.

  IDLE = 0

    Not running.

  RAMP_UP = 16

    Ramping speed up.

  RUNNING = 32

    Running at constant speed.

  RAMP_DOWN = 64

    Ramping speed down.

nxt.motor.LIMIT_RUN_FOREVER = 0

  No angle limit.

exception nxt.motor.BlockedException

  Bases: `Exception`

  Raised when a motor is not moving as expected.

class nxt.motor.OutputState(values)

  Bases: `object`

  An object holding the internal state of a motor, not including rotation counters.

  to_list()

    Returns a list of properties that can be used with set_output_state.

class nxt.motor.TachoInfo(values)

  Bases: `object`

  An object containing the information about the rotation of a motor.

  get_target(tacho_limit, direction)

    Returns a TachoInfo object which corresponds to tacho state after moving for tacho_limit ticks in the given direction.

    Parameters:

      - tacho_limit (int) – Move angle.

      - direction (int) – 1 (add) or -1 (subtract).

    Returns:

      Updated state.

    Return type:

      TachoInfo

  is_greater(target, direction)
  is_near(target, threshold)

class nxt.motor.SynchronizedTacho(leader_tacho, follower_tacho)

  Bases: `object`

  get_target(tacho_limit, direction)

    This method will leave follower’s target as None

  is_greater(other, direction)
  is_near(other, threshold)

nxt.motor.get_tacho_and_state(values)

  A convenience function. values is the list of values from get_output_state. Returns both OutputState and TachoInfo.

class nxt.motor.BaseMotor

  Bases: `object`

  Base class for motors

  turn(power, tacho_units, brake=True, timeout=1, emulate=True, stop_turn=<function BaseMotor.<lambda>>)

    Use this to turn a motor.

    Parameters:

      - power (int) – Value between -127 and 128 (an absolute value greater than 64 is recommended)

      - tacho_units (int) – Number of degrees to turn the motor. Values smaller than 50 are not recommended and may have strange results.

      - brake (bool) – Whether or not to hold the motor after the function exits (either by reaching the distance or throwing an exception).

      - timeout (int) – Number of seconds after which a BlockedException is raised if the motor doesn’t turn.

      - emulate (bool) – If set to `False`, the motor is aware of the tacho limit. If `True`, a run() function equivalent is used. Warning: motors remember their positions and not using emulate may lead to strange behavior, especially with synced motors

      - lambda – bool stop_turn: If stop_turn returns `True` the motor stops turning. Depending on `brake` it stops by holding or not holding the motor.

    The motor will not stop until it turns the desired distance or stop_turn returns True. Accuracy is much better over a USB connection than with bluetooth…

class nxt.motor.Motor(brick, port)

  Bases: `BaseMotor`

  get_tacho()
  reset_position(relative)

    Resets the counters. Relative can be True or False

  run(power=100, regulated=False)

    Tells the motor to run continuously.

    Parameters:

      - power (int) – Motor power or speed if regulated.

      - regulated (bool) – If `True`, use speed regulation.

  brake()

    Holds the motor in place

  idle()

    Tells the motor to stop whatever it’s doing. It also desyncs it.

  weak_turn(power, tacho_units)

    Tries to turn a motor for the specified distance. This function returns immediately, and it’s not guaranteed that the motor turns that distance. This is an interface to use tacho_limit without RegulationMode.SPEED

class nxt.motor.SynchronizedMotors(leader, follower, turn_ratio)

  Bases: `BaseMotor`

  The object used to make two motors run in sync. Many objects may be present at the same time but they can’t be used at the same time. Warning! Movement methods reset tacho counter. THIS CODE IS EXPERIMENTAL!!!

  get_tacho()
  reset_position(relative)

    Resets the counters. Relative can be True or False

  run(power=100)

    Warning! After calling this method, make sure to call idle. The motors are reported to behave wildly otherwise.

  brake()
  idle()
  turn(power, tacho_units, brake=True, timeout=1)

    Use this to turn a motor.

    Parameters:

      - power (int) – Value between -127 and 128 (an absolute value greater than 64 is recommended)

      - tacho_units (int) – Number of degrees to turn the motor. Values smaller than 50 are not recommended and may have strange results.

      - brake (bool) – Whether or not to hold the motor after the function exits (either by reaching the distance or throwing an exception).

      - timeout (int) – Number of seconds after which a BlockedException is raised if the motor doesn’t turn.

      - emulate (bool) – If set to `False`, the motor is aware of the tacho limit. If `True`, a run() function equivalent is used. Warning: motors remember their positions and not using emulate may lead to strange behavior, especially with synced motors

      - lambda – bool stop_turn: If stop_turn returns `True` the motor stops turning. Depending on `brake` it stops by holding or not holding the motor.

    The motor will not stop until it turns the desired distance or stop_turn returns True. Accuracy is much better over a USB connection than with bluetooth…