Class: Commands::UltrasonicSensor
- Inherits:
-
Object
- Object
- Commands::UltrasonicSensor
- Includes:
- Mixins::Sensor
- Defined in:
- lib/commands/ultrasonic_sensor.rb
Overview
Implements the “Ultrasonic Sensor” block in NXT-G
Defined Under Namespace
Classes: UnmeasurableDistance
Instance Attribute Summary collapse
-
#comparison ⇒ Object
Returns the value of attribute comparison.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#trigger_point ⇒ Object
Returns the value of attribute trigger_point.
Instance Method Summary collapse
-
#distance ⇒ Object
(also: #value_scaled)
returns distance in requested mode (:inches or :centimeters).
-
#distance! ⇒ Object
returns the distance in requested mode; raises an UnmeasurableDistance exception when the distance cannot be measured (i.e. when the distance == 255, which is the code the sensor returns when it cannot get a distance reading).
-
#initialize(nxt) ⇒ UltrasonicSensor
constructor
A new instance of UltrasonicSensor.
-
#set_mode ⇒ Object
sets up the sensor port.
Methods included from Mixins::Sensor
Constructor Details
#initialize(nxt) ⇒ UltrasonicSensor
Returns a new instance of UltrasonicSensor.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/commands/ultrasonic_sensor.rb', line 32 def initialize(nxt) @nxt = nxt # defaults the same as NXT-G @port = 4 @trigger_point = 50 @comparison = "<" @mode = :inches set_mode end |
Instance Attribute Details
#comparison ⇒ Object
Returns the value of attribute comparison.
30 31 32 |
# File 'lib/commands/ultrasonic_sensor.rb', line 30 def comparison @comparison end |
#mode ⇒ Object
Returns the value of attribute mode.
30 31 32 |
# File 'lib/commands/ultrasonic_sensor.rb', line 30 def mode @mode end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
29 30 31 |
# File 'lib/commands/ultrasonic_sensor.rb', line 29 def port @port end |
#trigger_point ⇒ Object
Returns the value of attribute trigger_point.
30 31 32 |
# File 'lib/commands/ultrasonic_sensor.rb', line 30 def trigger_point @trigger_point end |
Instance Method Details
#distance ⇒ Object Also known as: value_scaled
returns distance in requested mode (:inches or :centimeters)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/commands/ultrasonic_sensor.rb', line 44 def distance @nxt.ls_write(NXTComm.const_get("SENSOR_#{@port}"), UltrasonicComm.read_measurement_byte(0)) # Keep checking until we have data to read while @nxt.ls_get_status(NXTComm.const_get("SENSOR_#{@port}")) < 1 sleep(0.1) # TODO: implement timeout so we don't get stuck if the expected data never comes end distance = @nxt.ls_read(NXTComm.const_get("SENSOR_#{@port}"))[:data][0] if @mode == :centimeters distance.to_i else (distance * 0.3937008).to_i end end |
#distance! ⇒ Object
returns the distance in requested mode; raises an UnmeasurableDistance exception when the distance cannot be measured (i.e. when the distance == 255, which is the code the sensor returns when it cannot get a distance reading)
66 67 68 69 70 |
# File 'lib/commands/ultrasonic_sensor.rb', line 66 def distance! d = distance raise UnmeasurableDistance if d == 255 return d end |
#set_mode ⇒ Object
sets up the sensor port
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/commands/ultrasonic_sensor.rb', line 73 def set_mode @nxt.set_input_mode( NXTComm.const_get("SENSOR_#{@port}"), NXTComm::LOWSPEED_9V, NXTComm::RAWMODE ) # clear buffer @nxt.ls_read(NXTComm.const_get("SENSOR_#{@port}")) # set sensor to continuously send pings @nxt.ls_write(NXTComm.const_get("SENSOR_#{@port}"), UltrasonicComm.continuous_measurement_command) end |