Class: Commands::Move
- Inherits:
-
Object
- Object
- Commands::Move
- Includes:
- Commands::Mixins::Motor
- Defined in:
- lib/commands/move.rb
Overview
Implements the “Move” block in NXT-G
Instance Attribute Summary collapse
-
#direction ⇒ Object
Returns the value of attribute direction.
-
#left_motor ⇒ Object
Returns the value of attribute left_motor.
-
#next_action ⇒ Object
Returns the value of attribute next_action.
-
#ports ⇒ Object
Returns the value of attribute ports.
-
#power ⇒ Object
Returns the value of attribute power.
-
#right_motor ⇒ Object
Returns the value of attribute right_motor.
Instance Method Summary collapse
-
#initialize(nxt = NXTComm.new($DEV)) ⇒ Move
constructor
A new instance of Move.
-
#method_missing(cmd) ⇒ Object
attempt to return the output_state requested.
-
#start ⇒ Object
execute the Move command based on the properties specified.
-
#stop ⇒ Object
stop the Move command based on the next_action property.
- #turn_ratio ⇒ Object (also: #steering)
- #turn_ratio=(turn_ratio) ⇒ Object (also: #steering=)
Methods included from Commands::Mixins::Motor
Constructor Details
#initialize(nxt = NXTComm.new($DEV)) ⇒ Move
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/commands/move.rb', line 31 def initialize(nxt = NXTComm.new($DEV)) @nxt = nxt # defaults the same as NXT-G @ports = [:b, :c] @direction = :forward @power = 75 @duration = {:rotations => 1} @next_action = :brake self.turn_ratio = :straight end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(cmd) ⇒ Object
attempt to return the output_state requested
202 203 204 205 206 207 208 |
# File 'lib/commands/move.rb', line 202 def method_missing(cmd) states = {} @ports.each do |p| states[p] = @nxt.get_output_state(NXTComm.const_get("MOTOR_#{p.to_s.upcase}"))[cmd] end states end |
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
26 27 28 |
# File 'lib/commands/move.rb', line 26 def direction @direction end |
#left_motor ⇒ Object
Returns the value of attribute left_motor.
27 28 29 |
# File 'lib/commands/move.rb', line 27 def left_motor @left_motor end |
#next_action ⇒ Object
Returns the value of attribute next_action.
29 30 31 |
# File 'lib/commands/move.rb', line 29 def next_action @next_action end |
#ports ⇒ Object
Returns the value of attribute ports.
25 26 27 |
# File 'lib/commands/move.rb', line 25 def ports @ports end |
#power ⇒ Object
Returns the value of attribute power.
28 29 30 |
# File 'lib/commands/move.rb', line 28 def power @power end |
#right_motor ⇒ Object
Returns the value of attribute right_motor.
27 28 29 |
# File 'lib/commands/move.rb', line 27 def right_motor @right_motor end |
Instance Method Details
#start ⇒ Object
execute the Move command based on the properties specified
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/commands/move.rb', line 90 def start @ports.each do |p| @nxt.reset_motor_position(NXTComm.const_get("MOTOR_#{p.to_s.upcase}")) end if @direction == :stop motor_power = 0 mode = NXTComm::COAST run_state = NXTComm::MOTOR_RUN_STATE_IDLE else @direction == :forward ? motor_power = @power : motor_power = -@power mode = NXTComm::MOTORON | NXTComm::BRAKE run_state = NXTComm::MOTOR_RUN_STATE_RUNNING end if @ports.size == 2 mode |= NXTComm::REGULATED reg_mode = NXTComm::REGULATION_MODE_MOTOR_SYNC else reg_mode = NXTComm::REGULATION_MODE_IDLE end if @ports.include?(:a) and @ports.include?(:b) and @ports.include?(:c) @nxt.set_output_state( NXTComm::MOTOR_ALL, motor_power, mode, reg_mode, turn_ratio, run_state, tacho_limit ) else @ports.each do |p| @nxt.set_output_state( NXTComm.const_get("MOTOR_#{p.to_s.upcase}"), motor_power, mode, reg_mode, turn_ratio, run_state, tacho_limit ) end end unless @duration.nil? if @duration[:seconds] sleep(@duration[:seconds]) else until self.run_state[@ports[0]] == NXTComm::MOTOR_RUN_STATE_IDLE sleep(0.25) end end self.stop end end |
#stop ⇒ Object
stop the Move command based on the next_action property
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/commands/move.rb', line 149 def stop if @next_action == :brake if @ports.include?(:a) and @ports.include?(:b) and @ports.include?(:c) @nxt.set_output_state( NXTComm::MOTOR_ALL, 0, NXTComm::MOTORON | NXTComm::BRAKE | NXTComm::REGULATED, NXTComm::REGULATION_MODE_MOTOR_SPEED, 0, NXTComm::MOTOR_RUN_STATE_RUNNING, 0 ) else @ports.each do |p| @nxt.set_output_state( NXTComm.const_get("MOTOR_#{p.to_s.upcase}"), 0, NXTComm::MOTORON | NXTComm::BRAKE | NXTComm::REGULATED, NXTComm::REGULATION_MODE_MOTOR_SPEED, 0, NXTComm::MOTOR_RUN_STATE_RUNNING, 0 ) end end else if @ports.include?(:a) and @ports.include?(:b) and @ports.include?(:c) @nxt.set_output_state( NXTComm::MOTOR_ALL, 0, NXTComm::COAST, NXTComm::REGULATION_MODE_IDLE, 0, NXTComm::MOTOR_RUN_STATE_IDLE, 0 ) else @ports.each do |p| @nxt.set_output_state( NXTComm.const_get("MOTOR_#{p.to_s.upcase}"), 0, NXTComm::COAST, NXTComm::REGULATION_MODE_IDLE, 0, NXTComm::MOTOR_RUN_STATE_IDLE, 0 ) end end end end |
#turn_ratio ⇒ Object Also known as: steering
79 80 81 82 83 84 85 |
# File 'lib/commands/move.rb', line 79 def turn_ratio if @ports.size > 1 @turn_ratio else 0 end end |
#turn_ratio=(turn_ratio) ⇒ Object Also known as: steering=
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/commands/move.rb', line 54 def turn_ratio=(turn_ratio) # simplified steering... if the user wants fine control, they should just specify -100 to 100 case turn_ratio when :straight then @turn_ratio = 0 when :spin_left then @turn_ratio = -100 when :spin_right then @turn_ratio = 100 when :left then @turn_ratio = -50 when :right then @turn_ration = 50 else @turn_ratio = turn_ratio end # DEPRECATED: for backwards compatibility we parse the argument as a hash... I think though that this should be deprecated if turn_ratio.kind_of? Hash old_steering = turn_ratio self.left_motor = old_steering[:left_motor] if old_steering.has_key? :left_motor self.right_motor = old_steering[:right_motor] if old_steering.has_key? :right_motor if old_steering[:power] self.turn_ratio = old_steering[:power] * (old_steering[:direction] == :left ? -1 : 1) else self.turn_ratio = old_steering[:direction] end end end |