# Linus Atorf’s MotorControl Support

class nxt.motcont.MotCont(brick)

  Interface to MotorControl.

  This class provides an interface to Linus Atorf’s MotorControl NXC program. It is a wrapper which follows the MotorControl documentation and provides command strings and timing intervals as dictated there.

  To use this module, you will need to put `MotorControl22.rxe` on your NXT brick. It can be built using NXC and the corresponding source can be found at https://github.com/schodet/MotorControl.

  Use `MotCont.start()` to start the program. You can also start it manually my using the menu on the brick. When your script exits, it would be a good idea to do `MotCont.stop()`.

  Original MotorControl site is no longer available, but you can still find a mirror on web archive.

  Parameters:

    brick (Brick)

  cmd(ports, power, tacholimit, speedreg=True, smoothstart=False, brake=False)

    Send a controlled motor command to MotorControl.

    Parameters:

      - ports (Port | Iterable[Port]) – Port or ports to control, use one of the port identifiers, or an iterable returning one or two of them.

      - power (int) – Speed or power, -100 to 100.

      - tacholimit (int) – Position to drive to, 1 to 999999.

      - speedreg (bool) – `True` to enable regulation.

      - smoothstart (bool) – `True` to enable smooth start.

      - brake (bool) – `True` to hold brake at end of movement.

    Return type:

      None

    The motors on the selected ports must be idle, i.e. they can not be carrying out any other movement commands. If this should happen, the NXT will indicate this error by a signal (high and low beep) and will drop the message. To find out if a motor is ready to accept new commands, use the `is_ready()` method. It is also possible to stop the motor before using `set_output_state()` method.

    In certain situations, `set_output_state()` method should be used instead. See `set_output_state()` for more details.

  reset_tacho(ports)

    Reset NXT tacho count.

    Parameters:

      ports (Port | Iterable[Port]) – Port or ports to control, use one of the port identifiers, or an iterable returning one to three of them.

    Return type:

      None

  is_ready(port)

    Determine the state of a single motor.

    Parameters:

      port (Port | Iterable[Port]) – Port to control, use one of the port identifiers, or an iterable returning one of them.

    Returns:

      `True` if the motor is ready to accept new commands.

    Return type:

      bool

  set_output_state(ports, power, tacholimit, speedreg=True)

    Send a classic motor command to MotorControl.

    Parameters:

      - ports (Port | Iterable[Port]) – Port or ports to control, use one of the port identifiers, or an iterable returning one or two of them.

      - power (int) – Speed or power, -100 to 100.

      - tacholimit (int) – Position to drive to, 1 to 999999, or 0 for unlimited.

      - speedreg (bool) – `True` to enable regulation.

    Return type:

      None

    This is similar to the regular `nxt.brick.Brick.set_output_state()` method, but informs the MotorControl program to avoid any unwanted side effect.

    When to use `set_output_state()` instead of `cmd()`:

    - when tacholimit is 0 for unlimited movement,

    - or when the motor must coast (spin freely) after tacholimit has been reached (it will overshoot then),

    - or when you want to be able to change the power of a running motor at runtime.

    Also use this method to stop a currently running motor:

    - To stop and let a motor spin freely (coasting), use power 0 and no regulation.

    - To stop and hold the current position (brake), use power 0 with regulation.

  start(version=22)

    Start the MotorControl program on the brick.

    Parameters:

      version (int) – Version to start, default to 22 (version 2.2).

    Return type:

      None

    It needs to already be present on the brick’s flash and named `MotorControlXX.rxe`, where XX is the version number passed as the version argument.

    Warning

    When starting or stopping a program, the NXT firmware resets every sensors and motors.

  stop()

    Stop the MotorControl program.

    All this actually does is to stop the currently running program.

    Warning

    When starting or stopping a program, the NXT firmware resets every sensors and motors.

    Return type:

      None