Class: Tinkerforge::BrickletLaserRangeFinder

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

Overview

Measures distance up to 40m with laser light

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

255
DEVICE_DISPLAY_NAME =

:nodoc:

'Laser Range Finder Bricklet'
CALLBACK_DISTANCE =

This callback is triggered periodically with the period that is set by BrickletLaserRangeFinder#set_distance_callback_period. The parameter is the distance value of the sensor.

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

20
CALLBACK_VELOCITY =

This callback is triggered periodically with the period that is set by BrickletLaserRangeFinder#set_velocity_callback_period. The parameter is the velocity value of the sensor.

CALLBACK_VELOCITY is only triggered if the velocity has changed since the last triggering.

21
CALLBACK_DISTANCE_REACHED =

This callback is triggered when the threshold as set by BrickletLaserRangeFinder#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 BrickletLaserRangeFinder#set_debounce_period.

22
CALLBACK_VELOCITY_REACHED =

This callback is triggered when the threshold as set by BrickletLaserRangeFinder#set_velocity_callback_threshold is reached. The parameter is the velocity value of the sensor.

If the threshold keeps being reached, the callback is triggered periodically with the period as set by BrickletLaserRangeFinder#set_debounce_period.

23
FUNCTION_GET_DISTANCE =

:nodoc:

1
FUNCTION_GET_VELOCITY =

:nodoc:

2
FUNCTION_SET_DISTANCE_CALLBACK_PERIOD =

:nodoc:

3
FUNCTION_GET_DISTANCE_CALLBACK_PERIOD =

:nodoc:

4
FUNCTION_SET_VELOCITY_CALLBACK_PERIOD =

:nodoc:

5
FUNCTION_GET_VELOCITY_CALLBACK_PERIOD =

:nodoc:

6
FUNCTION_SET_DISTANCE_CALLBACK_THRESHOLD =

:nodoc:

7
FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD =

:nodoc:

8
FUNCTION_SET_VELOCITY_CALLBACK_THRESHOLD =

:nodoc:

9
FUNCTION_GET_VELOCITY_CALLBACK_THRESHOLD =

:nodoc:

10
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

11
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

12
FUNCTION_SET_MOVING_AVERAGE =

:nodoc:

13
FUNCTION_GET_MOVING_AVERAGE =

:nodoc:

14
FUNCTION_SET_MODE =

:nodoc:

15
FUNCTION_GET_MODE =

:nodoc:

16
FUNCTION_ENABLE_LASER =

:nodoc:

17
FUNCTION_DISABLE_LASER =

:nodoc:

18
FUNCTION_IS_LASER_ENABLED =

:nodoc:

19
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:

'>'
MODE_DISTANCE =

:nodoc:

0
MODE_VELOCITY_MAX_13MS =

:nodoc:

1
MODE_VELOCITY_MAX_32MS =

:nodoc:

2
MODE_VELOCITY_MAX_64MS =

:nodoc:

3
MODE_VELOCITY_MAX_127MS =

:nodoc:

4

Constants inherited from Device

Device::RESPONSE_EXPECTED_ALWAYS_FALSE, 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, #registered_callbacks, #uid

Instance Method Summary collapse

Methods inherited from Device

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

Constructor Details

#initialize(uid, ipcon) ⇒ BrickletLaserRangeFinder

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 84

def initialize(uid, ipcon)
  super uid, ipcon

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_GET_DISTANCE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_VELOCITY] = 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_VELOCITY_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_VELOCITY_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_VELOCITY_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_VELOCITY_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_SET_MODE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_ENABLE_LASER] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_DISABLE_LASER] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_IS_LASER_ENABLED] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[CALLBACK_DISTANCE] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_VELOCITY] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_DISTANCE_REACHED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_VELOCITY_REACHED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_DISTANCE] = 'S'
  @callback_formats[CALLBACK_VELOCITY] = 's'
  @callback_formats[CALLBACK_DISTANCE_REACHED] = 'S'
  @callback_formats[CALLBACK_VELOCITY_REACHED] = 's'
end

Instance Method Details

#disable_laserObject

Deactivates the laser of the LIDAR.



297
298
299
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 297

def disable_laser
  send_request(FUNCTION_DISABLE_LASER, [], '', 0, '')
end

#enable_laserObject

Activates the laser of the LIDAR.

We recommend that you wait 250ms after enabling the laser before the first call of BrickletLaserRangeFinder#get_distance to ensure stable measurements.



292
293
294
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 292

def enable_laser
  send_request(FUNCTION_ENABLE_LASER, [], '', 0, '')
end

#get_debounce_periodObject

Returns the debounce period as set by BrickletLaserRangeFinder#set_debounce_period.



244
245
246
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 244

def get_debounce_period
  send_request(FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 4, 'L')
end

#get_distanceObject

Returns the measured distance. The value has a range of 0 to 4000 and is given in cm.

The Laser Range Finder Bricklet knows different modes. Distances are only measured in the distance measurement mode, see BrickletLaserRangeFinder#set_mode. Also the laser has to be enabled, see BrickletLaserRangeFinder#enable_laser.

If you want to get the distance periodically, it is recommended to use the callback CALLBACK_DISTANCE and set the period with BrickletLaserRangeFinder#set_distance_callback_period.



131
132
133
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 131

def get_distance
  send_request(FUNCTION_GET_DISTANCE, [], '', 2, 'S')
end

#get_distance_callback_periodObject

Returns the period as set by BrickletLaserRangeFinder#set_distance_callback_period.



162
163
164
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 162

def get_distance_callback_period
  send_request(FUNCTION_GET_DISTANCE_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_distance_callback_thresholdObject

Returns the threshold as set by BrickletLaserRangeFinder#set_distance_callback_threshold.



200
201
202
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 200

def get_distance_callback_threshold
  send_request(FUNCTION_GET_DISTANCE_CALLBACK_THRESHOLD, [], '', 5, '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’ or ‘d’.

The device identifier numbers can be found :ref:‘here <device_identifier>`. |device_identifier_constant|



314
315
316
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 314

def get_identity
  send_request(FUNCTION_GET_IDENTITY, [], '', 25, 'Z8 Z8 k C3 C3 S')
end

#get_modeObject

Returns the mode as set by BrickletLaserRangeFinder#set_mode.



284
285
286
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 284

def get_mode
  send_request(FUNCTION_GET_MODE, [], '', 1, 'C')
end

#get_moving_averageObject

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



262
263
264
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 262

def get_moving_average
  send_request(FUNCTION_GET_MOVING_AVERAGE, [], '', 2, 'C C')
end

#get_velocityObject

Returns the measured velocity. The value has a range of -12800 to 12700 and is given in 1/100 m/s.

The Laser Range Finder Bricklet knows different modes. Velocity is only measured in the velocity measurement modes, see BrickletLaserRangeFinder#set_mode. Also the laser has to be enabled, see BrickletLaserRangeFinder#enable_laser.

If you want to get the velocity periodically, it is recommended to use the callback CALLBACK_VELOCITY and set the period with BrickletLaserRangeFinder#set_velocity_callback_period.



146
147
148
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 146

def get_velocity
  send_request(FUNCTION_GET_VELOCITY, [], '', 2, 's')
end

#get_velocity_callback_periodObject

Returns the period as set by BrickletLaserRangeFinder#set_velocity_callback_period.



178
179
180
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 178

def get_velocity_callback_period
  send_request(FUNCTION_GET_VELOCITY_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_velocity_callback_thresholdObject

Returns the threshold as set by BrickletLaserRangeFinder#set_velocity_callback_threshold.



222
223
224
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 222

def get_velocity_callback_threshold
  send_request(FUNCTION_GET_VELOCITY_CALLBACK_THRESHOLD, [], '', 5, 'k s s')
end

#is_laser_enabledObject

Returns true if the laser is enabled, false otherwise.



302
303
304
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 302

def is_laser_enabled
  send_request(FUNCTION_IS_LASER_ENABLED, [], '', 1, '?')
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



319
320
321
322
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 319

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

#set_debounce_period(debounce) ⇒ Object

Sets the period in ms with which the threshold callbacks

  • CALLBACK_DISTANCE_REACHED,

  • CALLBACK_VELOCITY_REACHED,

are triggered, if the thresholds

  • BrickletLaserRangeFinder#set_distance_callback_threshold,

  • BrickletLaserRangeFinder#set_velocity_callback_threshold,

keep being reached.

The default value is 100.



239
240
241
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 239

def set_debounce_period(debounce)
  send_request(FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 0, '')
end

#set_distance_callback_period(period) ⇒ Object

Sets the period in ms with which the CALLBACK_DISTANCE callback is triggered periodically. A value of 0 turns the callback off.

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

The default value is 0.



157
158
159
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 157

def set_distance_callback_period(period)
  send_request(FUNCTION_SET_DISTANCE_CALLBACK_PERIOD, [period], 'L', 0, '')
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)"

The default value is (‘x’, 0, 0).



195
196
197
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 195

def set_distance_callback_threshold(option, min, max)
  send_request(FUNCTION_SET_DISTANCE_CALLBACK_THRESHOLD, [option, min, max], 'k S S', 0, '')
end

#set_mode(mode) ⇒ Object

The LIDAR has five different modes. One mode is for distance measurements and four modes are for velocity measurements with different ranges.

The following modes are available:

  • 0: Distance is measured with resolution 1.0 cm and range 0-400 cm

  • 1: Velocity is measured with resolution 0.1 m/s and range is 0-12.7 m/s

  • 2: Velocity is measured with resolution 0.25 m/s and range is 0-31.75 m/s

  • 3: Velocity is measured with resolution 0.5 m/s and range is 0-63.5 m/s

  • 4: Velocity is measured with resolution 1.0 m/s and range is 0-127 m/s

The default mode is 0 (distance is measured).



279
280
281
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 279

def set_mode(mode)
  send_request(FUNCTION_SET_MODE, [mode], 'C', 0, '')
end

#set_moving_average(distance_average_length, velocity_average_length) ⇒ Object

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

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

The range for the averaging is 0-30.

The default value is 10.



257
258
259
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 257

def set_moving_average(distance_average_length, velocity_average_length)
  send_request(FUNCTION_SET_MOVING_AVERAGE, [distance_average_length, velocity_average_length], 'C C', 0, '')
end

#set_velocity_callback_period(period) ⇒ Object

Sets the period in ms with which the CALLBACK_VELOCITY callback is triggered periodically. A value of 0 turns the callback off.

CALLBACK_VELOCITY is only triggered if the velocity value has changed since the last triggering.

The default value is 0.



173
174
175
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 173

def set_velocity_callback_period(period)
  send_request(FUNCTION_SET_VELOCITY_CALLBACK_PERIOD, [period], 'L', 0, '')
end

#set_velocity_callback_threshold(option, min, max) ⇒ Object

Sets the thresholds for the CALLBACK_VELOCITY_REACHED callback.

The following options are possible:

"Option", "Description"

"'x'",    "Callback is turned off"
"'o'",    "Callback is triggered when the velocity is *outside* the min and max values"
"'i'",    "Callback is triggered when the velocity is *inside* the min and max values"
"'<'",    "Callback is triggered when the velocity is smaller than the min value (max is ignored)"
"'>'",    "Callback is triggered when the velocity is greater than the min value (max is ignored)"

The default value is (‘x’, 0, 0).



217
218
219
# File 'lib/tinkerforge/bricklet_laser_range_finder.rb', line 217

def set_velocity_callback_threshold(option, min, max)
  send_request(FUNCTION_SET_VELOCITY_CALLBACK_THRESHOLD, [option, min, max], 'k s s', 0, '')
end