Class: Tinkerforge::BrickletLoadCell

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

Overview

Measures weight with a load cell

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

253
DEVICE_DISPLAY_NAME =

:nodoc:

'Load Cell Bricklet'
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.

CALLBACK_WEIGHT is only triggered if the weight has changed since the last triggering.

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

18
FUNCTION_GET_WEIGHT =

:nodoc:

1
FUNCTION_SET_WEIGHT_CALLBACK_PERIOD =

:nodoc:

2
FUNCTION_GET_WEIGHT_CALLBACK_PERIOD =

:nodoc:

3
FUNCTION_SET_WEIGHT_CALLBACK_THRESHOLD =

:nodoc:

4
FUNCTION_GET_WEIGHT_CALLBACK_THRESHOLD =

:nodoc:

5
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

6
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

7
FUNCTION_SET_MOVING_AVERAGE =

:nodoc:

8
FUNCTION_GET_MOVING_AVERAGE =

:nodoc:

9
FUNCTION_LED_ON =

:nodoc:

10
FUNCTION_LED_OFF =

:nodoc:

11
FUNCTION_IS_LED_ON =

:nodoc:

12
FUNCTION_CALIBRATE =

:nodoc:

13
FUNCTION_TARE =

:nodoc:

14
FUNCTION_SET_CONFIGURATION =

:nodoc:

15
FUNCTION_GET_CONFIGURATION =

:nodoc:

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

'>'
RATE_10HZ =

:nodoc:

0
RATE_80HZ =

:nodoc:

1
GAIN_128X =

:nodoc:

0
GAIN_64X =

:nodoc:

1
GAIN_32X =

:nodoc:

2

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) ⇒ BrickletLoadCell

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 65

def initialize(uid, ipcon)
  super uid, ipcon

  @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[CALLBACK_WEIGHT] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_WEIGHT_REACHED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_WEIGHT] = 'l'
  @callback_formats[CALLBACK_WEIGHT_REACHED] = 'l'
end

Instance Method Details

#calibrate(weight) ⇒ Object

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 in grams.

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.



205
206
207
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 205

def calibrate(weight)
  send_request(FUNCTION_CALIBRATE, [weight], 'L', 0, '')
end

#get_configurationObject

Returns the configuration as set by BrickletLoadCell#set_configuration.



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

def get_configuration
  send_request(FUNCTION_GET_CONFIGURATION, [], '', 2, 'C C')
end

#get_debounce_periodObject

Returns the debounce period as set by BrickletLoadCell#set_debounce_period.



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

def get_debounce_period
  send_request(FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 4, 'L')
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|



251
252
253
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 251

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

#get_moving_averageObject

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



175
176
177
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 175

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

#get_weightObject

Returns the currently measured weight in grams.

If you want to get the weight periodically, it is recommended to use the callback CALLBACK_WEIGHT and set the period with BrickletLoadCell#set_weight_callback_period.



99
100
101
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 99

def get_weight
  send_request(FUNCTION_GET_WEIGHT, [], '', 4, 'l')
end

#get_weight_callback_periodObject

Returns the period as set by BrickletLoadCell#set_weight_callback_period.



115
116
117
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 115

def get_weight_callback_period
  send_request(FUNCTION_GET_WEIGHT_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_weight_callback_thresholdObject

Returns the threshold as set by BrickletLoadCell#set_weight_callback_threshold.



137
138
139
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 137

def get_weight_callback_threshold
  send_request(FUNCTION_GET_WEIGHT_CALLBACK_THRESHOLD, [], '', 9, 'k l l')
end

#is_led_onObject

Returns true if the led is on, false otherwise.



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

def is_led_on
  send_request(FUNCTION_IS_LED_ON, [], '', 1, '?')
end

#led_offObject

Turns the LED off.



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

def led_off
  send_request(FUNCTION_LED_OFF, [], '', 0, '')
end

#led_onObject

Turns the LED on.



180
181
182
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 180

def led_on
  send_request(FUNCTION_LED_ON, [], '', 0, '')
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



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

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

#set_configuration(rate, gain) ⇒ Object

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.

The default rate is 10Hz and the default gain is 128x.



234
235
236
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 234

def set_configuration(rate, gain)
  send_request(FUNCTION_SET_CONFIGURATION, [rate, gain], 'C C', 0, '')
end

#set_debounce_period(debounce) ⇒ Object

Sets the period in ms with which the threshold callback

  • CALLBACK_WEIGHT_REACHED

is triggered, if the threshold

  • BrickletLoadCell#set_weight_callback_threshold

keeps being reached.

The default value is 100.



152
153
154
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 152

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

#set_moving_average(average) ⇒ Object

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

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

The range for the averaging is 1-40.

The default value is 4.



170
171
172
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 170

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

#set_weight_callback_period(period) ⇒ Object

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

CALLBACK_WEIGHT is only triggered if the weight has changed since the last triggering.

The default value is 0.



110
111
112
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 110

def set_weight_callback_period(period)
  send_request(FUNCTION_SET_WEIGHT_CALLBACK_PERIOD, [period], 'L', 0, '')
end

#set_weight_callback_threshold(option, min, max) ⇒ Object

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

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



132
133
134
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 132

def set_weight_callback_threshold(option, min, max)
  send_request(FUNCTION_SET_WEIGHT_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 0, '')
end

#tareObject

Sets the currently measured weight as tare weight.



210
211
212
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 210

def tare
  send_request(FUNCTION_TARE, [], '', 0, '')
end