class Tinkerforge::BrickletLoadCell
Measures weight with a load cell
Constants
- CALLBACK_WEIGHT
This callback is triggered periodically with the period that is set by
BrickletLoadCell#set_weight_callback_period
. The parameter is the weight as measured by the load cell.The
CALLBACK_WEIGHT
callback is only triggered if the weight has changed since the last triggering.- CALLBACK_WEIGHT_REACHED
This callback is triggered when the threshold as set by
BrickletLoadCell#set_weight_callback_threshold
is reached. The parameter is the weight as measured by the load cell.If the threshold keeps being reached, the callback is triggered periodically with the period as set by
BrickletLoadCell#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_load_cell.rb, line 66 def initialize(uid, ipcon) super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME @api_version = [2, 0, 0] @response_expected[FUNCTION_GET_WEIGHT] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_WEIGHT_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_WEIGHT_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_WEIGHT_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_WEIGHT_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_LED_ON] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_LED_OFF] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_IS_LED_ON] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_CALIBRATE] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_TARE] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_SET_CONFIGURATION] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_GET_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE @callback_formats[CALLBACK_WEIGHT] = [12, 'l'] @callback_formats[CALLBACK_WEIGHT_REACHED] = [12, 'l'] @ipcon.add_device self end
Public Instance Methods
To calibrate your Load Cell Bricklet you have to
-
empty the scale and call this function with 0 and
-
add a known weight to the scale and call this function with the weight.
The calibration is saved in the EEPROM of the Bricklet and only needs to be done once.
We recommend to use the Brick Viewer for calibration, you don't need to call this function in your source code.
# File lib/tinkerforge/bricklet_load_cell.rb, line 219 def calibrate(weight) check_validity send_request FUNCTION_CALIBRATE, [weight], 'L', 8, '' end
Returns the configuration as set by BrickletLoadCell#set_configuration
.
# File lib/tinkerforge/bricklet_load_cell.rb, line 257 def get_configuration check_validity send_request FUNCTION_GET_CONFIGURATION, [], '', 10, 'C C' end
Returns the debounce period as set by BrickletLoadCell#set_debounce_period
.
# File lib/tinkerforge/bricklet_load_cell.rb, line 164 def get_debounce_period check_validity send_request FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 12, 'L' 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_load_cell.rb, line 273 def get_identity send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S' end
Returns the length moving average as set by BrickletLoadCell#set_moving_average
.
# File lib/tinkerforge/bricklet_load_cell.rb, line 182 def get_moving_average check_validity send_request FUNCTION_GET_MOVING_AVERAGE, [], '', 9, 'C' end
Returns the currently measured weight.
If you want to get the weight periodically, it is recommended to use the CALLBACK_WEIGHT
callback and set the period with BrickletLoadCell#set_weight_callback_period
.
# File lib/tinkerforge/bricklet_load_cell.rb, line 100 def get_weight check_validity send_request FUNCTION_GET_WEIGHT, [], '', 12, 'l' end
Returns the period as set by BrickletLoadCell#set_weight_callback_period
.
# File lib/tinkerforge/bricklet_load_cell.rb, line 118 def get_weight_callback_period check_validity send_request FUNCTION_GET_WEIGHT_CALLBACK_PERIOD, [], '', 12, 'L' end
Returns the threshold as set by BrickletLoadCell#set_weight_callback_threshold
.
# File lib/tinkerforge/bricklet_load_cell.rb, line 142 def get_weight_callback_threshold check_validity send_request FUNCTION_GET_WEIGHT_CALLBACK_THRESHOLD, [], '', 17, 'k l l' end
Returns true if the led is on, false otherwise.
# File lib/tinkerforge/bricklet_load_cell.rb, line 203 def is_led_on check_validity send_request FUNCTION_IS_LED_ON, [], '', 9, '?' end
Turns the LED off.
# File lib/tinkerforge/bricklet_load_cell.rb, line 196 def led_off check_validity send_request FUNCTION_LED_OFF, [], '', 8, '' end
Turns the LED on.
# File lib/tinkerforge/bricklet_load_cell.rb, line 189 def led_on check_validity send_request FUNCTION_LED_ON, [], '', 8, '' end
Registers a callback with ID id
to the block block
.
# File lib/tinkerforge/bricklet_load_cell.rb, line 278 def register_callback(id, &block) callback = block @registered_callbacks[id] = callback end
The measurement rate and gain are configurable.
The rate can be either 10Hz or 80Hz. A faster rate will produce more noise. It is additionally possible to add a moving average (see BrickletLoadCell#set_moving_average
) to the measurements.
The gain can be 128x, 64x or 32x. It represents a measurement range of ±20mV, ±40mV and ±80mV respectively. The Load Cell Bricklet uses an excitation voltage of 5V and most load cells use an output of 2mV/V. That means the voltage range is ±15mV for most load cells (i.e. gain of 128x is best). If you don't know what all of this means you should keep it at 128x, it will most likely be correct.
The configuration is saved in the EEPROM of the Bricklet and only needs to be done once.
We recommend to use the Brick Viewer for configuration, you don't need to call this function in your source code.
# File lib/tinkerforge/bricklet_load_cell.rb, line 250 def set_configuration(rate, gain) check_validity send_request FUNCTION_SET_CONFIGURATION, [rate, gain], 'C C', 8, '' end
Sets the period with which the threshold callback
is triggered, if the threshold
keeps being reached.
# File lib/tinkerforge/bricklet_load_cell.rb, line 157 def set_debounce_period(debounce) check_validity send_request FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 8, '' end
Sets the period with which the CALLBACK_WEIGHT
callback is triggered periodically. A value of 0 turns the callback off.
The CALLBACK_WEIGHT
callback is only triggered if the weight has changed since the last triggering.
# File lib/tinkerforge/bricklet_load_cell.rb, line 111 def set_weight_callback_period(period) check_validity send_request FUNCTION_SET_WEIGHT_CALLBACK_PERIOD, [period], 'L', 8, '' end
Sets the thresholds for the CALLBACK_WEIGHT_REACHED
callback.
The following options are possible:
"Option", "Description" "'x'", "Callback is turned off" "'o'", "Callback is triggered when the weight is *outside* the min and max values" "'i'", "Callback is triggered when the weight is *inside* the min and max values" "'<'", "Callback is triggered when the weight is smaller than the min value (max is ignored)" "'>'", "Callback is triggered when the weight is greater than the min value (max is ignored)"
# File lib/tinkerforge/bricklet_load_cell.rb, line 135 def set_weight_callback_threshold(option, min, max) check_validity send_request FUNCTION_SET_WEIGHT_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 8, '' end
Sets the currently measured weight as tare weight.
# File lib/tinkerforge/bricklet_load_cell.rb, line 226 def tare check_validity send_request FUNCTION_TARE, [], '', 8, '' end