Class: Commands::RotationSensor
- Inherits:
-
Object
- Object
- Commands::RotationSensor
- 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
-
#comparison ⇒ Object
Returns the value of attribute comparison.
-
#port ⇒ Object
Returns the value of attribute port.
-
#trigger_direction ⇒ Object
Returns the value of attribute trigger_direction.
-
#trigger_point ⇒ Object
Returns the value of attribute trigger_point.
Instance Method Summary collapse
-
#degrees ⇒ Object
returns the number of degrees moved since last reset point.
-
#direction ⇒ Object
attempts to determine direction based on last time position was requested (may not be accurate).
-
#initialize(nxt) ⇒ RotationSensor
constructor
A new instance of RotationSensor.
-
#logic ⇒ Object
returns true or false based on comparison type and difference between last time reset point.
-
#method_missing(cmd) ⇒ Object
attempt to return the output_state requested.
-
#reset ⇒ Object
resets the value_scaled property, use this to reset the degrees counter.
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
#comparison ⇒ Object
Returns the value of attribute comparison.
20 21 22 |
# File 'lib/commands/rotation_sensor.rb', line 20 def comparison @comparison end |
#port ⇒ Object
Returns the value of attribute port.
20 21 22 |
# File 'lib/commands/rotation_sensor.rb', line 20 def port @port end |
#trigger_direction ⇒ Object
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_point ⇒ Object
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
#degrees ⇒ Object
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 |
#direction ⇒ Object
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 |
#logic ⇒ Object
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 |
#reset ⇒ Object
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 |