Class: Tinkerforge::BrickletDistanceIR

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

Overview

Measures distance up to 150cm with infrared light

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

25
DEVICE_DISPLAY_NAME =

:nodoc:

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

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

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

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

18
FUNCTION_GET_DISTANCE =

:nodoc:

1
FUNCTION_GET_ANALOG_VALUE =

:nodoc:

2
FUNCTION_SET_SAMPLING_POINT =

:nodoc:

3
FUNCTION_GET_SAMPLING_POINT =

:nodoc:

4
FUNCTION_SET_DISTANCE_CALLBACK_PERIOD =

:nodoc:

5
FUNCTION_GET_DISTANCE_CALLBACK_PERIOD =

:nodoc:

6
FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD =

:nodoc:

7
FUNCTION_GET_ANALOG_VALUE_CALLBACK_PERIOD =

:nodoc:

8
FUNCTION_SET_DISTANCE_CALLBACK_THRESHOLD =

:nodoc:

9
FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD =

:nodoc:

10
FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD =

:nodoc:

11
FUNCTION_GET_ANALOG_VALUE_CALLBACK_THRESHOLD =

:nodoc:

12
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

13
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

14
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) ⇒ BrickletDistanceIR

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 76

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

Instance Method Details

#get_analog_valueObject

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, while BrickletDistanceIR#get_analog_value gives back raw unfiltered analog values. The only reason to use BrickletDistanceIR#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.



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

def get_analog_value
  check_validity

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

#get_analog_value_callback_periodObject

Returns the period as set by BrickletDistanceIR#set_analog_value_callback_period.



197
198
199
200
201
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 197

def get_analog_value_callback_period
  check_validity

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

#get_analog_value_callback_thresholdObject

Returns the threshold as set by BrickletDistanceIR#set_analog_value_callback_threshold.



245
246
247
248
249
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 245

def get_analog_value_callback_threshold
  check_validity

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

#get_debounce_periodObject

Returns the debounce period as set by BrickletDistanceIR#set_debounce_period.



269
270
271
272
273
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 269

def get_debounce_period
  check_validity

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

#get_distanceObject

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.



112
113
114
115
116
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 112

def get_distance
  check_validity

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

#get_distance_callback_periodObject

Returns the period as set by BrickletDistanceIR#set_distance_callback_period.



179
180
181
182
183
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 179

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 BrickletDistanceIR#set_distance_callback_threshold.



221
222
223
224
225
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 221

def get_distance_callback_threshold
  check_validity

  send_request FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD, [], '', 13, 'k S 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|



285
286
287
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 285

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

#get_sampling_point(position) ⇒ Object

Returns the distance to a sampling point position as set by BrickletDistanceIR#set_sampling_point.



161
162
163
164
165
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 161

def get_sampling_point(position)
  check_validity

  send_request FUNCTION_GET_SAMPLING_POINT, [position], 'C', 10, 'S'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



290
291
292
293
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 290

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

#set_analog_value_callback_period(period) ⇒ Object

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.



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

def set_analog_value_callback_period(period)
  check_validity

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

#set_analog_value_callback_threshold(option, min, max) ⇒ Object

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


238
239
240
241
242
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 238

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

#set_debounce_period(debounce) ⇒ Object

Sets the period with which the threshold callbacks

  • CALLBACK_DISTANCE_REACHED,

  • CALLBACK_ANALOG_VALUE_REACHED

are triggered, if the thresholds

  • BrickletDistanceIR#set_distance_callback_threshold,

  • BrickletDistanceIR#set_analog_value_callback_threshold

keep being reached.



262
263
264
265
266
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 262

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.

The CALLBACK_DISTANCE callback is only triggered if the distance has changed since the last triggering.



172
173
174
175
176
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 172

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


214
215
216
217
218
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 214

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_sampling_point(position, distance) ⇒ Object

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.



153
154
155
156
157
# File 'lib/tinkerforge/bricklet_distance_ir.rb', line 153

def set_sampling_point(position, distance)
  check_validity

  send_request FUNCTION_SET_SAMPLING_POINT, [position, distance], 'C S', 8, ''
end