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.

The CALLBACK_WEIGHT callback 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::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) ⇒ BrickletLoadCell

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



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
93
94
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 67

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

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.

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.



220
221
222
223
224
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 220

def calibrate(weight)
  check_validity

  send_request FUNCTION_CALIBRATE, [weight], 'L', 8, ''
end

#get_configurationObject

Returns the configuration as set by BrickletLoadCell#set_configuration.



258
259
260
261
262
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 258

def get_configuration
  check_validity

  send_request FUNCTION_GET_CONFIGURATION, [], '', 10, 'C C'
end

#get_debounce_periodObject

Returns the debounce period as set by BrickletLoadCell#set_debounce_period.



165
166
167
168
169
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 165

def get_debounce_period
  check_validity

  send_request FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 12, '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’, ‘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|



274
275
276
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 274

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 BrickletLoadCell#set_moving_average.



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

def get_moving_average
  check_validity

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

#get_weightObject

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.



101
102
103
104
105
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 101

def get_weight
  check_validity

  send_request FUNCTION_GET_WEIGHT, [], '', 12, 'l'
end

#get_weight_callback_periodObject

Returns the period as set by BrickletLoadCell#set_weight_callback_period.



119
120
121
122
123
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 119

def get_weight_callback_period
  check_validity

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

#get_weight_callback_thresholdObject

Returns the threshold as set by BrickletLoadCell#set_weight_callback_threshold.



143
144
145
146
147
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 143

def get_weight_callback_threshold
  check_validity

  send_request FUNCTION_GET_WEIGHT_CALLBACK_THRESHOLD, [], '', 17, 'k l l'
end

#is_led_onObject

Returns true if the led is on, false otherwise.



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

def is_led_on
  check_validity

  send_request FUNCTION_IS_LED_ON, [], '', 9, '?'
end

#led_offObject

Turns the LED off.



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

def led_off
  check_validity

  send_request FUNCTION_LED_OFF, [], '', 8, ''
end

#led_onObject

Turns the LED on.



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

def led_on
  check_validity

  send_request FUNCTION_LED_ON, [], '', 8, ''
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



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

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.



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

def set_configuration(rate, gain)
  check_validity

  send_request FUNCTION_SET_CONFIGURATION, [rate, gain], 'C C', 8, ''
end

#set_debounce_period(debounce) ⇒ Object

Sets the period with which the threshold callback

  • CALLBACK_WEIGHT_REACHED

is triggered, if the threshold

  • BrickletLoadCell#set_weight_callback_threshold

keeps being reached.



158
159
160
161
162
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 158

def set_debounce_period(debounce)
  check_validity

  send_request FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 8, ''
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.



176
177
178
179
180
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 176

def set_moving_average(average)
  check_validity

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

#set_weight_callback_period(period) ⇒ Object

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.



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

def set_weight_callback_period(period)
  check_validity

  send_request FUNCTION_SET_WEIGHT_CALLBACK_PERIOD, [period], 'L', 8, ''
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)"


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

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

#tareObject

Sets the currently measured weight as tare weight.



227
228
229
230
231
# File 'lib/tinkerforge/bricklet_load_cell.rb', line 227

def tare
  check_validity

  send_request FUNCTION_TARE, [], '', 8, ''
end