Class: NXT::Connector::Output::Motor

Inherits:
Object
  • Object
show all
Extended by:
Utils::Accessors
Includes:
NXT::Command::Output, Utils::Assertions
Defined in:
lib/nxt/connector/output/motor.rb

Overview

Implements the “motor” output for the NXT 2.0 module.

Constant Summary collapse

DURATION_TYPE =
%i[seconds degrees rotations].freeze
DURATION_AFTER =
%i[coast brake].freeze
DIRECTION =
%i[forwards backwards].freeze

Constants included from NXT::Command::Output

NXT::Command::Output::COMMAND_IDENTIFIER, NXT::Command::Output::MODE, NXT::Command::Output::REGULATION_MODE, NXT::Command::Output::RUN_STATE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Accessors

attr_setter

Methods included from Utils::Assertions

#assert_in, #assert_responds_to, #assert_type

Methods included from NXT::Command::Output

#command_type, #output_state, #update_output_state

Constructor Details

#initialize(port, interface) ⇒ Motor

Returns a new instance of Motor.



26
27
28
29
# File 'lib/nxt/connector/output/motor.rb', line 26

def initialize(port, interface)
  @port = port
  @interface = interface
end

Instance Attribute Details

#interfaceObject

Returns the value of attribute interface.



17
18
19
# File 'lib/nxt/connector/output/motor.rb', line 17

def interface
  @interface
end

#portObject

Returns the value of attribute port.



17
18
19
# File 'lib/nxt/connector/output/motor.rb', line 17

def port
  @port
end

Instance Method Details

#backwardsObject



52
53
54
55
# File 'lib/nxt/connector/output/motor.rb', line 52

def backwards
  self.direction = :backwards
  self
end

#duration=(duration, options = {}) ⇒ Object

Raises:

  • (TypeError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nxt/connector/output/motor.rb', line 31

def duration=(duration, options = {})
  raise(TypeError, 'Expected duration to be a number') unless duration.is_a?(Integer)

  @duration = duration

  self.duration_type = options[:type]
  self.duration_after = options[:after]

  case @duration_type
  when :rotations
    self.tacho_limit = @duration * 360
  when :degrees
    self.tacho_limit = @duration
  end
end

#forwardsObject



47
48
49
50
# File 'lib/nxt/connector/output/motor.rb', line 47

def forwards
  self.direction = :forwards
  self
end

#moveObject

takes block for response, or can return the response instead.



65
66
67
68
69
70
71
72
73
# File 'lib/nxt/connector/output/motor.rb', line 65

def move
  update_output_state(duration.positive? && duration_type != :seconds)

  if duration.positive? && duration_type == :seconds
    wait_after_move
  else
    reset
  end
end

#resetObject



75
76
77
78
79
80
81
82
83
# File 'lib/nxt/connector/output/motor.rb', line 75

def reset
  self.duration = 0
  self.direction = :forwards
  self.power = 75
  self.mode = :motor_on
  self.regulation_mode = :idle
  self.run_state = :running
  self.tacho_limit = 0
end

#stop(mode = :coast) ⇒ Object



57
58
59
60
61
62
# File 'lib/nxt/connector/output/motor.rb', line 57

def stop(mode = :coast)
  self.power = 0
  self.mode = mode

  move
end