Module: NXT::Command::Output

Extended by:
Utils::Accessors
Includes:
Base
Included in:
NXT::Connector::Output::Motor
Defined in:
lib/nxt/commands/output.rb

Overview

An implementation of all the output related NXT commands:

  • SETOUTPUTSTATE

  • GETOUTPUTSTATE

This is used predominantly to interface with the servo-motor connectors that come prepackaged with NXT kits.

This class can also be used to talk to other third-party accessories connected in the output ports on the NXT brick.

Constant Summary collapse

COMMAND_IDENTIFIER =
{
  set_output_state: 0x04,
  get_output_state: 0x06
}.freeze
MODE =

The mode enum. This is a list of possible values when setting the mode byte.

Reference: Appendix 2, Page 6

{
  # Motor will rotate freely.
  # NOTE: This is not documented in the Appendixes.
  coast: 0x00,
  # Turn on the specified motor.
  motor_on: 0x01,
  # Use run/brake instead of run/float in PWM. This means the voltage is
  # not allowed to float between PWM pulses, improving accuracy at the
  # expense of greater power usage.
  brake: 0x02,
  # Turns on the regulation. This is required when setting a regulation
  # mode setting.
  regulated: 0x04
}.freeze
REGULATION_MODE =

The regulation mode enum. This is a list of possible values when setting the regulation mode byte.

Reference: Appendix 2, Page 6

{
  # No regulation will be enabled.
  idle: 0x00,
  # Power control will be enabled on specific output.
  motor_speed: 0x01,
  # Synchronisation will be enabled. This requires two output ports to
  # have this enabled before it will work.
  motor_sync: 0x02
}.freeze
RUN_STATE =

The run state enum. This is a list of possible values when setting the run state byte.

Reference: Appendix 2, Page 6

{
  # Output will be idle.
  idle: 0x00,
  # Output will ramp-up to the desired speed.
  ramp_up: 0x10,
  # Output will be running.
  running: 0x20,
  # Output will ramp-down to the desired speed.
  ramp_down: 0x40
}.freeze

Instance Method Summary collapse

Methods included from Utils::Accessors

attr_setter

Methods included from Utils::Assertions

#assert_in, #assert_responds_to, #assert_type

Instance Method Details

#command_typeObject



86
87
88
# File 'lib/nxt/commands/output.rb', line 86

def command_type
  COMMAND_TYPES[:direct]
end

#output_stateObject



106
107
108
109
# File 'lib/nxt/commands/output.rb', line 106

def output_state
  # TODO: Parse this response and return hash or something similar.
  send_and_receive(COMMAND_IDENTIFIER[:get_output_state])
end

#update_output_state(response_required: false) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/nxt/commands/output.rb', line 90

def update_output_state(response_required: false)
  # Pack this value into a 32-bit unsigned little-endian binary string,
  # then unpack it into 4 8 bit unsigned integer chunks. We are
  # converting the passed in value to a little endian, unsigned long
  # value.
  tacho_limit_as_bytes = [tacho_limit].pack('V').unpack('C4')

  send_and_receive(COMMAND_IDENTIFIER[:set_output_state], [
    power,
    MODE[mode],
    REGULATION_MODE[regulation_mode],
    0, # turn ratio
    RUN_STATE[run_state]
  ] + tacho_limit_as_bytes, response_required)
end