Class: NXT::Connector::Output::Motor
- Inherits:
-
Object
- Object
- NXT::Connector::Output::Motor
- 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
-
#interface ⇒ Object
Returns the value of attribute interface.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #backwards ⇒ Object
- #duration=(duration, options = {}) ⇒ Object
- #forwards ⇒ Object
-
#initialize(port, interface) ⇒ Motor
constructor
A new instance of Motor.
-
#move ⇒ Object
takes block for response, or can return the response instead.
- #reset ⇒ Object
- #stop(mode = :coast) ⇒ Object
Methods included from Utils::Accessors
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
#interface ⇒ Object
Returns the value of attribute interface.
17 18 19 |
# File 'lib/nxt/connector/output/motor.rb', line 17 def interface @interface end |
#port ⇒ Object
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
#backwards ⇒ Object
52 53 54 55 |
# File 'lib/nxt/connector/output/motor.rb', line 52 def backwards self.direction = :backwards self end |
#duration=(duration, options = {}) ⇒ Object
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, = {}) raise(TypeError, 'Expected duration to be a number') unless duration.is_a?(Integer) @duration = duration self.duration_type = [:type] self.duration_after = [:after] case @duration_type when :rotations self.tacho_limit = @duration * 360 when :degrees self.tacho_limit = @duration end end |
#forwards ⇒ Object
47 48 49 50 |
# File 'lib/nxt/connector/output/motor.rb', line 47 def forwards self.direction = :forwards self end |
#move ⇒ Object
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 |
#reset ⇒ Object
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 |