Class: Tinkerforge::BrickletDistanceUS

Inherits:
Device
  • Object
show all
Defined in:
lib/tinkerforge/bricklet_distance_us.rb

Overview

Measures distance between 2cm and 400cm with ultrasound

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

229
DEVICE_DISPLAY_NAME =

:nodoc:

'Distance US Bricklet'
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.

8
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.

9
FUNCTION_GET_DISTANCE_VALUE =

:nodoc:

1
FUNCTION_SET_DISTANCE_CALLBACK_PERIOD =

:nodoc:

2
FUNCTION_GET_DISTANCE_CALLBACK_PERIOD =

:nodoc:

3
FUNCTION_SET_DISTANCE_CALLBACK_THRESHOLD =

:nodoc:

4
FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD =

:nodoc:

5
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

6
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

7
FUNCTION_SET_MOVING_AVERAGE =

:nodoc:

10
FUNCTION_GET_MOVING_AVERAGE =

:nodoc:

11
FUNCTION_GET_IDENTITY =

:nodoc:

255
THRESHOLD_OPTION_OFF =

:nodoc:

'x'
THRESHOLD_OPTION_OUTSIDE =

:nodoc:

'o'
THRESHOLD_OPTION_INSIDE =

:nodoc:

'i'
THRESHOLD_OPTION_SMALLER =

:nodoc:

'<'
THRESHOLD_OPTION_GREATER =

:nodoc:

'>'

Constants inherited from Device

Device::DEVICE_IDENTIFIER_CHECK_MATCH, Device::DEVICE_IDENTIFIER_CHECK_MISMATCH, Device::DEVICE_IDENTIFIER_CHECK_PENDING, Device::RESPONSE_EXPECTED_ALWAYS_TRUE, Device::RESPONSE_EXPECTED_FALSE, Device::RESPONSE_EXPECTED_INVALID_FUNCTION_ID, Device::RESPONSE_EXPECTED_TRUE

Instance Attribute Summary

Attributes inherited from Device

#callback_formats, #expected_response_function_id, #expected_response_sequence_number, #high_level_callbacks, #registered_callbacks, #replaced, #uid

Instance Method Summary collapse

Methods inherited from Device

#check_validity, #dequeue_response, #enqueue_response, #get_api_version, #get_response_expected, #send_request, #set_response_expected, #set_response_expected_all

Constructor Details

#initialize(uid, ipcon) ⇒ BrickletDistanceUS

Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 55

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

Instance Method Details

#get_debounce_periodObject

Returns the debounce period as set by BrickletDistanceUS#set_debounce_period.



151
152
153
154
155
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 151

def get_debounce_period
  check_validity

  send_request FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 12, 'L'
end

#get_distance_callback_periodObject

Returns the period as set by BrickletDistanceUS#set_distance_callback_period.



105
106
107
108
109
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 105

def get_distance_callback_period
  check_validity

  send_request FUNCTION_GET_DISTANCE_CALLBACK_PERIOD, [], '', 12, 'L'
end

#get_distance_callback_thresholdObject

Returns the threshold as set by BrickletDistanceUS#set_distance_callback_threshold.



129
130
131
132
133
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 129

def get_distance_callback_threshold
  check_validity

  send_request FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD, [], '', 13, 'k S S'
end

#get_distance_valueObject

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.



87
88
89
90
91
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 87

def get_distance_value
  check_validity

  send_request FUNCTION_GET_DISTANCE_VALUE, [], '', 10, 'S'
end

#get_identityObject

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|



185
186
187
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 185

def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end

#get_moving_averageObject

Returns the length moving average as set by BrickletDistanceUS#set_moving_average.



169
170
171
172
173
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 169

def get_moving_average
  check_validity

  send_request FUNCTION_GET_MOVING_AVERAGE, [], '', 9, 'C'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



190
191
192
193
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 190

def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end

#set_debounce_period(debounce) ⇒ Object

Sets the period with which the threshold callbacks

  • CALLBACK_DISTANCE_REACHED,

are triggered, if the thresholds

  • BrickletDistanceUS#set_distance_callback_threshold,

keep being reached.



144
145
146
147
148
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 144

def set_debounce_period(debounce)
  check_validity

  send_request FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 8, ''
end

#set_distance_callback_period(period) ⇒ Object

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.



98
99
100
101
102
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 98

def set_distance_callback_period(period)
  check_validity

  send_request FUNCTION_SET_DISTANCE_CALLBACK_PERIOD, [period], 'L', 8, ''
end

#set_distance_callback_threshold(option, min, max) ⇒ Object

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)"


122
123
124
125
126
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 122

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

#set_moving_average(average) ⇒ Object

Sets the length of a ‘moving averaging <en.wikipedia.org/wiki/Moving_average>`__ for the distance value.

Setting the length to 0 will turn the averaging completely off. With less averaging, there is more noise on the data.



162
163
164
165
166
# File 'lib/tinkerforge/bricklet_distance_us.rb', line 162

def set_moving_average(average)
  check_validity

  send_request FUNCTION_SET_MOVING_AVERAGE, [average], 'C', 8, ''
end