Class: Commands::Motor

Inherits:
Object
  • Object
show all
Includes:
Commands::Mixins::Motor
Defined in:
lib/commands/motor.rb

Overview

Implements the “Motor” block in NXT-G

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands::Mixins::Motor

#duration, #duration=

Constructor Details

#initialize(nxt = NXTComm.new($DEV)) ⇒ Motor

Returns a new instance of Motor.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/commands/motor.rb', line 34

def initialize(nxt = NXTComm.new($DEV))
  @nxt          = nxt
  
  # defaults the same as NXT-G
  @port           = :a
  @direction      = :forward
  @action         = :constant
  @power          = 75
  @control_power  = false
  @duration       = nil # Same as :unlimited
  @wait           = false
  @next_action    = :brake
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



132
133
134
# File 'lib/commands/motor.rb', line 132

def method_missing(cmd)
  @nxt.get_output_state(NXTComm.const_get("MOTOR_#{@port.to_s.upcase}"))[cmd]
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



28
29
30
# File 'lib/commands/motor.rb', line 28

def action
  @action
end

#control_powerObject

Returns the value of attribute control_power.



30
31
32
# File 'lib/commands/motor.rb', line 30

def control_power
  @control_power
end

#directionObject

Returns the value of attribute direction.



26
27
28
# File 'lib/commands/motor.rb', line 26

def direction
  @direction
end

#next_actionObject

Returns the value of attribute next_action.



32
33
34
# File 'lib/commands/motor.rb', line 32

def next_action
  @next_action
end

#portObject

Returns the value of attribute port.



25
26
27
# File 'lib/commands/motor.rb', line 25

def port
  @port
end

#powerObject

Returns the value of attribute power.



29
30
31
# File 'lib/commands/motor.rb', line 29

def power
  @power
end

#steeringObject

Returns the value of attribute steering.



27
28
29
# File 'lib/commands/motor.rb', line 27

def steering
  @steering
end

#waitObject

Returns the value of attribute wait.



31
32
33
# File 'lib/commands/motor.rb', line 31

def wait
  @wait
end

Instance Method Details

#startObject

execute the Motor command based on the properties specified



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/commands/motor.rb', line 49

def start
  @nxt.reset_motor_position(NXTComm.const_get("MOTOR_#{@port.to_s.upcase}"))

  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

  turn_ratio = 0

  if @control_power
    mode |= NXTComm::REGULATED
    reg_mode = NXTComm::REGULATION_MODE_MOTOR_SPEED
  else
    reg_mode = NXTComm::REGULATION_MODE_IDLE
  end
  
  if @duration
    if @duration[:degrees] or @duration[:seconds]
      case @action
        when :constant
          run_state = NXTComm::MOTOR_RUN_STATE_RUNNING
        when :ramp_up
          run_state = NXTComm::MOTOR_RUN_STATE_RAMPUP
        when :ramp_down
          run_state = NXTComm::MOTOR_RUN_STATE_RAMPDOWN
      end
    end
  end

  @nxt.set_output_state(
    NXTComm.const_get("MOTOR_#{@port.to_s.upcase}"),
    motor_power,
    mode,
    reg_mode,
    turn_ratio,
    run_state,
    tacho_limit
  )
  
  if (@duration and @duration[:seconds]) or @wait
    if @duration and @duration[:seconds]
      sleep(@duration[:seconds])
    else
      until self.run_state == NXTComm::MOTOR_RUN_STATE_IDLE
        sleep(0.25)
      end
    end
    self.stop
  end
end

#stopObject

stop the Motor command based on the next_action property



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/commands/motor.rb', line 107

def stop
  if @next_action == :brake
    @nxt.set_output_state(
      NXTComm.const_get("MOTOR_#{@port.to_s.upcase}"),
      0,
      NXTComm::MOTORON | NXTComm::BRAKE | NXTComm::REGULATED,
      NXTComm::REGULATION_MODE_MOTOR_SPEED,
      0,
      NXTComm::MOTOR_RUN_STATE_RUNNING,
      0
    )
  else
    @nxt.set_output_state(
      NXTComm.const_get("MOTOR_#{@port.to_s.upcase}"),
      0,
      NXTComm::COAST,
      NXTComm::REGULATION_MODE_IDLE,
      0,
      NXTComm::MOTOR_RUN_STATE_IDLE,
      0
    )
  end
end