Class: UltrasonicSensor

Inherits:
Sensor show all
Defined in:
lib/sensors/ultrasonic_sensor.rb

Defined Under Namespace

Classes: UnmeasurableDistance

Constant Summary collapse

INCHES_PER_CM =
0.3937008

Constants inherited from Sensor

Sensor::POLL_INTERVAL

Instance Attribute Summary

Attributes inherited from Brick

#log, #port

Instance Method Summary collapse

Methods inherited from Sensor

#name, #off, #read_data, #set_input_mode, #wait_for_event

Constructor Details

#initialize(nxt, port = NXTComm::SENSOR_4) ⇒ UltrasonicSensor

Returns a new instance of UltrasonicSensor.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sensors/ultrasonic_sensor.rb', line 24

def initialize(nxt, port = NXTComm::SENSOR_4)
  super(nxt, port)
  
  # The Ultrasonic sensor is digital and unlike the other sensors it
  # uses the lowspeed communication protocol.
  set_input_mode(NXTComm::LOWSPEED_9V, NXTComm::RAWMODE)
  
  # Read the sensor in case there was some garbage data in the buffer waiting to be read
  @nxt.ls_read(@port)
  
  # Set the sensor to continuously send pings
  @nxt.ls_write(@port, UltrasonicComm.continuous_measurement_command)
end

Instance Method Details

#get_distanceObject Also known as: get_distance_in_cm

Return the measured distance in the default units (the default units being centimeters). A value of 255 is returned when the sensor cannot get an accurate reading (because the object is out of range, or there is too much interference, etc.) Note that the sensor’s real-world range is at best 175 cm. At larger distances it will almost certainly just return 255.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sensors/ultrasonic_sensor.rb', line 43

def get_distance
  @nxt.ls_write(@port, UltrasonicComm.read_measurement_byte(0))
  
  # Keep checking until we have 1 byte of data to read
  while @nxt.ls_get_status(@port)[0] < 1
    sleep(0.1)
    @nxt.ls_write(@port, UltrasonicComm.read_measurement_byte(0))
    # TODO: implement timeout so we don't get stuck if the expected data never comes
  end
  
  resp = @nxt.ls_read(@port)
  # TODO: probably need a better error message here...
  raise "ls_read returned more than one byte!" if resp[:bytes_read] > 1
  raise "ls_read did not return any data!" if resp[:bytes_read] < 1
 
  # If the sensor cannot determine the distance, it will return
  # 0xff (255)... this usually means that the object is out of
  # sensor range, but it can also mean that there is too much
  # interference or that the object is too close to the sensor.
  # I considered returning nil or false under such cases, but
  # this makes numeric comparison (i.e. greather than/less than)
  # more difficult
  d = resp[:data][0]
end

#get_distance!Object Also known as: get_distance_in_cm!

Same as get_distance, but raises an UnmeasurableDistance exception when the sensor cannot accurately determine the distance.



77
78
79
80
81
82
83
84
# File 'lib/sensors/ultrasonic_sensor.rb', line 77

def get_distance!
d = get_distance
if d == 255
  raise UnmeasurableDistance
else
  return d
end
end

#get_distance_in_inchesObject

Return the measured distance in inches.



70
71
72
# File 'lib/sensors/ultrasonic_sensor.rb', line 70

def get_distance_in_inches    
  get_distance.to_f * INCHES_PER_CM
end

#get_distance_in_inches!Object



87
88
89
# File 'lib/sensors/ultrasonic_sensor.rb', line 87

def get_distance_in_inches!
  get_distance!.to_f * INCHES_PER_CM
end