class Tinkerforge::BrickletDistanceUS
Measures distance between 2cm and 400cm with ultrasound
Constants
- CALLBACK_DISTANCE
This callback is triggered periodically with the period that is set by
BrickletDistanceUS#set_distance_callback_period
. The parameter is the distance value of the sensor.The
CALLBACK_DISTANCE
callback is only triggered if the distance value has changed since the last triggering.- CALLBACK_DISTANCE_REACHED
This callback is triggered when the threshold as set by
BrickletDistanceUS#set_distance_callback_threshold
is reached. The parameter is the distance value of the sensor.If the threshold keeps being reached, the callback is triggered periodically with the period as set by
BrickletDistanceUS#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_us.rb, line 54 def initialize(uid, ipcon) super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME @api_version = [2, 0, 1] @response_expected[FUNCTION_GET_DISTANCE_VALUE] = 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_DISTANCE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_DISTANCE_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_SET_MOVING_AVERAGE] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_GET_MOVING_AVERAGE] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE @callback_formats[CALLBACK_DISTANCE] = [10, 'S'] @callback_formats[CALLBACK_DISTANCE_REACHED] = [10, 'S'] @ipcon.add_device self end
Public Instance Methods
Returns the debounce period as set by BrickletDistanceUS#set_debounce_period
.
# File lib/tinkerforge/bricklet_distance_us.rb, line 150 def get_debounce_period check_validity send_request FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 12, 'L' end
Returns the period as set by BrickletDistanceUS#set_distance_callback_period
.
# File lib/tinkerforge/bricklet_distance_us.rb, line 104 def get_distance_callback_period check_validity send_request FUNCTION_GET_DISTANCE_CALLBACK_PERIOD, [], '', 12, 'L' end
Returns the threshold as set by BrickletDistanceUS#set_distance_callback_threshold
.
# File lib/tinkerforge/bricklet_distance_us.rb, line 128 def get_distance_callback_threshold check_validity send_request FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD, [], '', 13, 'k S S' end
Returns the current distance value measured by the sensor. A small value corresponds to a small distance, a big value corresponds to a big distance. The relation between the measured distance value and the actual distance is affected by the 5V supply voltage (deviations in the supply voltage result in deviations in the distance values) and is non-linear (resolution is bigger at close range).
If you want to get the distance value periodically, it is recommended to use the CALLBACK_DISTANCE
callback and set the period with BrickletDistanceUS#set_distance_callback_period
.
# File lib/tinkerforge/bricklet_distance_us.rb, line 86 def get_distance_value check_validity send_request FUNCTION_GET_DISTANCE_VALUE, [], '', 10, '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_us.rb, line 184 def get_identity send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S' end
Returns the length moving average as set by BrickletDistanceUS#set_moving_average
.
# File lib/tinkerforge/bricklet_distance_us.rb, line 168 def get_moving_average check_validity send_request FUNCTION_GET_MOVING_AVERAGE, [], '', 9, 'C' end
Registers a callback with ID id
to the block block
.
# File lib/tinkerforge/bricklet_distance_us.rb, line 189 def register_callback(id, &block) callback = block @registered_callbacks[id] = callback end
Sets the period with which the threshold callbacks
are triggered, if the thresholds
keep being reached.
# File lib/tinkerforge/bricklet_distance_us.rb, line 143 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.
Der CALLBACK_DISTANCE
callback is only triggered if the distance value has changed since the last triggering.
# File lib/tinkerforge/bricklet_distance_us.rb, line 97 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 value is *outside* the min and max values" "'i'", "Callback is triggered when the distance value is *inside* the min and max values" "'<'", "Callback is triggered when the distance value is smaller than the min value (max is ignored)" "'>'", "Callback is triggered when the distance value is greater than the min value (max is ignored)"
# File lib/tinkerforge/bricklet_distance_us.rb, line 121 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