Class: Commands::RotationSensor

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/rotation_sensor.rb

Overview

Implements the “Rotation Sensor” block in NXT-G (using an nxt motor as rotation sensor)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nxt) ⇒ RotationSensor

Returns a new instance of RotationSensor.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/commands/rotation_sensor.rb', line 22

def initialize(nxt)
  @nxt      = nxt
  
  # defaults the same as NXT-G
  @port               = :a
  @trigger_direction  = :forward
  @trigger_point      = 360
  @comparison         = ">"
  reset
  @internal_counter   = degrees
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



69
70
71
# File 'lib/commands/rotation_sensor.rb', line 69

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

Instance Attribute Details

#comparisonObject

Returns the value of attribute comparison.



20
21
22
# File 'lib/commands/rotation_sensor.rb', line 20

def comparison
  @comparison
end

#portObject

Returns the value of attribute port.



20
21
22
# File 'lib/commands/rotation_sensor.rb', line 20

def port
  @port
end

#trigger_directionObject

Returns the value of attribute trigger_direction.



20
21
22
# File 'lib/commands/rotation_sensor.rb', line 20

def trigger_direction
  @trigger_direction
end

#trigger_pointObject

Returns the value of attribute trigger_point.



20
21
22
# File 'lib/commands/rotation_sensor.rb', line 20

def trigger_point
  @trigger_point
end

Instance Method Details

#degreesObject

returns the number of degrees moved since last reset point



51
52
53
54
# File 'lib/commands/rotation_sensor.rb', line 51

def degrees
  @internal_counter = rotation_count
  @internal_counter
end

#directionObject

attempts to determine direction based on last time position was requested (may not be accurate)



57
58
59
60
61
62
63
64
65
66
# File 'lib/commands/rotation_sensor.rb', line 57

def direction
  case true
    when rotation_count > @internal_counter
      "forwards"
    when rotation_count < @internal_counter
      "backwards"
    when rotation_count == @internal_counter
      "stopped"
  end
end

#logicObject

returns true or false based on comparison type and difference between last time reset point



35
36
37
38
39
40
41
42
43
# File 'lib/commands/rotation_sensor.rb', line 35

def logic
  @trigger_direction == :forward ? trigger = @trigger_point : trigger = -@trigger_point
  case @comparison
    when ">"
      degrees >= trigger ? true : false
    when "<"
      degrees <= trigger ? true : false
  end
end

#resetObject

resets the value_scaled property, use this to reset the degrees counter



46
47
48
# File 'lib/commands/rotation_sensor.rb', line 46

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