class Tinkerforge::BrickletDistanceIR
Measures distance up to 150cm with infrared light
Constants
- CALLBACK_ANALOG_VALUE
This callback is triggered periodically with the period that is set by
BrickletDistanceIR#set_analog_value_callback_period
. The parameter is the analog value of the sensor.The
CALLBACK_ANALOG_VALUE
callback is only triggered if the analog value has changed since the last triggering.- CALLBACK_ANALOG_VALUE_REACHED
This callback is triggered when the threshold as set by
BrickletDistanceIR#set_analog_value_callback_threshold
is reached. The parameter is the analog value of the sensor.If the threshold keeps being reached, the callback is triggered periodically with the period as set by
BrickletDistanceIR#set_debounce_period
.- CALLBACK_DISTANCE
This callback is triggered periodically with the period that is set by
BrickletDistanceIR#set_distance_callback_period
. The parameter is the distance of the sensor.The
CALLBACK_DISTANCE
callback is only triggered if the distance has changed since the last triggering.- CALLBACK_DISTANCE_REACHED
This callback is triggered when the threshold as set by
BrickletDistanceIR#set_distance_callback_threshold
is reached. The parameter is the distance of the sensor.If the threshold keeps being reached, the callback is triggered periodically with the period as set by
BrickletDistanceIR#set_debounce_period
.
Public Class Methods
Creates an object with the unique device ID uid
and adds it to the IP Connection ipcon
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 75 def initialize(uid, ipcon) super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME @api_version = [2, 0, 1] @response_expected[FUNCTION_GET_DISTANCE] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_ANALOG_VALUE] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_SAMPLING_POINT] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_GET_SAMPLING_POINT] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_DISTANCE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_DISTANCE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_ANALOG_VALUE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_DISTANCE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_ANALOG_VALUE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_DEBOUNCE_PERIOD] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_DEBOUNCE_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE @callback_formats[CALLBACK_DISTANCE] = [10, 'S'] @callback_formats[CALLBACK_ANALOG_VALUE] = [10, 'S'] @callback_formats[CALLBACK_DISTANCE_REACHED] = [10, 'S'] @callback_formats[CALLBACK_ANALOG_VALUE_REACHED] = [10, 'S'] @ipcon.add_device self end
Public Instance Methods
Returns the value as read by a 12-bit analog-to-digital converter.
- .. note
-
The value returned by
BrickletDistanceIR#get_distance
is averaged over several samples to yield less noise, whileBrickletDistanceIR#get_analog_value
gives back raw unfiltered analog values. The only reason to useBrickletDistanceIR#get_analog_value
is, if you need the full resolution of the analog-to-digital converter.
If you want the analog value periodically, it is recommended to use the CALLBACK_ANALOG_VALUE
callback and set the period with BrickletDistanceIR#set_analog_value_callback_period
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 128 def get_analog_value check_validity send_request FUNCTION_GET_ANALOG_VALUE, [], '', 10, 'S' end
Returns the period as set by BrickletDistanceIR#set_analog_value_callback_period
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 196 def get_analog_value_callback_period check_validity send_request FUNCTION_GET_ANALOG_VALUE_CALLBACK_PERIOD, [], '', 12, 'L' end
Returns the threshold as set by BrickletDistanceIR#set_analog_value_callback_threshold
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 244 def get_analog_value_callback_threshold check_validity send_request FUNCTION_GET_ANALOG_VALUE_CALLBACK_THRESHOLD, [], '', 13, 'k S S' end
Returns the debounce period as set by BrickletDistanceIR#set_debounce_period
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 268 def get_debounce_period check_validity send_request FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 12, 'L' end
Returns the distance measured by the sensor. Possible distance ranges are 40 to 300, 100 to 800 and 200 to 1500, depending on the selected IR sensor.
If you want to get the distance periodically, it is recommended to use the CALLBACK_DISTANCE
callback and set the period with BrickletDistanceIR#set_distance_callback_period
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 111 def get_distance check_validity send_request FUNCTION_GET_DISTANCE, [], '', 10, 'S' end
Returns the period as set by BrickletDistanceIR#set_distance_callback_period
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 178 def get_distance_callback_period check_validity send_request FUNCTION_GET_DISTANCE_CALLBACK_PERIOD, [], '', 12, 'L' end
Returns the threshold as set by BrickletDistanceIR#set_distance_callback_threshold
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 220 def get_distance_callback_threshold check_validity send_request FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD, [], '', 13, 'k S S' end
Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.
The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an :ref:`Isolator Bricklet <isolator_bricklet>` is always at position 'z'.
The device identifier numbers can be found :ref:`here <device_identifier>`. |device_identifier_constant|
# File lib/tinkerforge/bricklet_distance_ir.rb, line 284 def get_identity send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S' end
Returns the distance to a sampling point position as set by BrickletDistanceIR#set_sampling_point
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 160 def get_sampling_point(position) check_validity send_request FUNCTION_GET_SAMPLING_POINT, [position], 'C', 10, 'S' end
Registers a callback with ID id
to the block block
.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 289 def register_callback(id, &block) callback = block @registered_callbacks[id] = callback end
Sets the period with which the CALLBACK_ANALOG_VALUE
callback is triggered periodically. A value of 0 turns the callback off.
The CALLBACK_ANALOG_VALUE
callback is only triggered if the analog value has changed since the last triggering.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 189 def set_analog_value_callback_period(period) check_validity send_request FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD, [period], 'L', 8, '' end
Sets the thresholds for the CALLBACK_ANALOG_VALUE_REACHED
callback.
The following options are possible:
"Option", "Description" "'x'", "Callback is turned off" "'o'", "Callback is triggered when the analog value is *outside* the min and max values" "'i'", "Callback is triggered when the analog value is *inside* the min and max values" "'<'", "Callback is triggered when the analog value is smaller than the min value (max is ignored)" "'>'", "Callback is triggered when the analog value is greater than the min value (max is ignored)"
# File lib/tinkerforge/bricklet_distance_ir.rb, line 237 def set_analog_value_callback_threshold(option, min, max) check_validity send_request FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD, [option, min, max], 'k S S', 8, '' end
Sets the period with which the threshold callbacks
are triggered, if the thresholds
keep being reached.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 261 def set_debounce_period(debounce) check_validity send_request FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 8, '' end
Sets the period with which the CALLBACK_DISTANCE
callback is triggered periodically. A value of 0 turns the callback off.
The CALLBACK_DISTANCE
callback is only triggered if the distance has changed since the last triggering.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 171 def set_distance_callback_period(period) check_validity send_request FUNCTION_SET_DISTANCE_CALLBACK_PERIOD, [period], 'L', 8, '' end
Sets the thresholds for the CALLBACK_DISTANCE_REACHED
callback.
The following options are possible:
"Option", "Description" "'x'", "Callback is turned off" "'o'", "Callback is triggered when the distance is *outside* the min and max values" "'i'", "Callback is triggered when the distance is *inside* the min and max values" "'<'", "Callback is triggered when the distance is smaller than the min value (max is ignored)" "'>'", "Callback is triggered when the distance is greater than the min value (max is ignored)"
# File lib/tinkerforge/bricklet_distance_ir.rb, line 213 def set_distance_callback_threshold(option, min, max) check_validity send_request FUNCTION_SET_DISTANCE_CALLBACK_THRESHOLD, [option, min, max], 'k S S', 8, '' end
Sets a sampling point value to a specific position of the lookup table. The lookup table comprises 128 equidistant analog values with corresponding distances.
If you measure a distance of 50cm at the analog value 2048, you should call this function with (64, 5000). The utilized analog-to-digital converter has a resolution of 12 bit. With 128 sampling points on the whole range, this means that every sampling point has a size of 32 analog values. Thus the analog value 2048 has the corresponding sampling point 64 = 2048/32.
Sampling points are saved on the EEPROM of the Distance IR Bricklet and loaded again on startup.
- .. note
-
An easy way to calibrate the sampling points of the Distance IR Bricklet is implemented in the Brick Viewer. If you want to calibrate your Bricklet it is highly recommended to use this implementation.
# File lib/tinkerforge/bricklet_distance_ir.rb, line 152 def set_sampling_point(position, distance) check_validity send_request FUNCTION_SET_SAMPLING_POINT, [position, distance], 'C S', 8, '' end