Class: Tinkerforge::BrickletVoltageCurrent

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

Overview

Measures power, DC voltage and DC current up to 720W/36V/20A

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

227
DEVICE_DISPLAY_NAME =

:nodoc:

'Voltage/Current Bricklet'
CALLBACK_CURRENT =

This callback is triggered periodically with the period that is set by BrickletVoltageCurrent#set_current_callback_period. The parameter is the current of the sensor.

The CALLBACK_CURRENT callback is only triggered if the current has changed since the last triggering.

22
CALLBACK_VOLTAGE =

This callback is triggered periodically with the period that is set by BrickletVoltageCurrent#set_voltage_callback_period. The parameter is the voltage of the sensor.

The CALLBACK_VOLTAGE callback is only triggered if the voltage has changed since the last triggering.

23
CALLBACK_POWER =

This callback is triggered periodically with the period that is set by BrickletVoltageCurrent#set_power_callback_period. The parameter is the power of the sensor.

The CALLBACK_POWER callback is only triggered if the power has changed since the last triggering.

24
CALLBACK_CURRENT_REACHED =

This callback is triggered when the threshold as set by BrickletVoltageCurrent#set_current_callback_threshold is reached. The parameter is the current of the sensor.

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

25
CALLBACK_VOLTAGE_REACHED =

This callback is triggered when the threshold as set by BrickletVoltageCurrent#set_voltage_callback_threshold is reached. The parameter is the voltage of the sensor.

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

26
CALLBACK_POWER_REACHED =

This callback is triggered when the threshold as set by BrickletVoltageCurrent#set_power_callback_threshold is reached. The parameter is the power of the sensor.

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

27
FUNCTION_GET_CURRENT =

:nodoc:

1
FUNCTION_GET_VOLTAGE =

:nodoc:

2
FUNCTION_GET_POWER =

:nodoc:

3
FUNCTION_SET_CONFIGURATION =

:nodoc:

4
FUNCTION_GET_CONFIGURATION =

:nodoc:

5
FUNCTION_SET_CALIBRATION =

:nodoc:

6
FUNCTION_GET_CALIBRATION =

:nodoc:

7
FUNCTION_SET_CURRENT_CALLBACK_PERIOD =

:nodoc:

8
FUNCTION_GET_CURRENT_CALLBACK_PERIOD =

:nodoc:

9
FUNCTION_SET_VOLTAGE_CALLBACK_PERIOD =

:nodoc:

10
FUNCTION_GET_VOLTAGE_CALLBACK_PERIOD =

:nodoc:

11
FUNCTION_SET_POWER_CALLBACK_PERIOD =

:nodoc:

12
FUNCTION_GET_POWER_CALLBACK_PERIOD =

:nodoc:

13
FUNCTION_SET_CURRENT_CALLBACK_THRESHOLD =

:nodoc:

14
FUNCTION_GET_CURRENT_CALLBACK_THRESHOLD =

:nodoc:

15
FUNCTION_SET_VOLTAGE_CALLBACK_THRESHOLD =

:nodoc:

16
FUNCTION_GET_VOLTAGE_CALLBACK_THRESHOLD =

:nodoc:

17
FUNCTION_SET_POWER_CALLBACK_THRESHOLD =

:nodoc:

18
FUNCTION_GET_POWER_CALLBACK_THRESHOLD =

:nodoc:

19
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

20
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

21
FUNCTION_GET_IDENTITY =

:nodoc:

255
AVERAGING_1 =

:nodoc:

0
AVERAGING_4 =

:nodoc:

1
AVERAGING_16 =

:nodoc:

2
AVERAGING_64 =

:nodoc:

3
AVERAGING_128 =

:nodoc:

4
AVERAGING_256 =

:nodoc:

5
AVERAGING_512 =

:nodoc:

6
AVERAGING_1024 =

:nodoc:

7
THRESHOLD_OPTION_OFF =

:nodoc:

'x'
THRESHOLD_OPTION_OUTSIDE =

:nodoc:

'o'
THRESHOLD_OPTION_INSIDE =

:nodoc:

'i'
THRESHOLD_OPTION_SMALLER =

:nodoc:

'<'
THRESHOLD_OPTION_GREATER =

:nodoc:

'>'
CONVERSION_TIME_140US =

:nodoc:

0
CONVERSION_TIME_204US =

:nodoc:

1
CONVERSION_TIME_332US =

:nodoc:

2
CONVERSION_TIME_588US =

:nodoc:

3
CONVERSION_TIME_1_1MS =

:nodoc:

4
CONVERSION_TIME_2_116MS =

:nodoc:

5
CONVERSION_TIME_4_156MS =

:nodoc:

6
CONVERSION_TIME_8_244MS =

:nodoc:

7

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

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 115

def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_GET_CURRENT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_VOLTAGE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_POWER] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CONFIGURATION] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CALIBRATION] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CALIBRATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CURRENT_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_CURRENT_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_VOLTAGE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_VOLTAGE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_POWER_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_POWER_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CURRENT_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_CURRENT_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_VOLTAGE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_VOLTAGE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_POWER_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_POWER_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_CURRENT] = [12, 'l']
  @callback_formats[CALLBACK_VOLTAGE] = [12, 'l']
  @callback_formats[CALLBACK_POWER] = [12, 'l']
  @callback_formats[CALLBACK_CURRENT_REACHED] = [12, 'l']
  @callback_formats[CALLBACK_VOLTAGE_REACHED] = [12, 'l']
  @callback_formats[CALLBACK_POWER_REACHED] = [12, 'l']

  @ipcon.add_device self
end

Instance Method Details

#get_calibrationObject

Returns the calibration as set by BrickletVoltageCurrent#set_calibration.



216
217
218
219
220
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 216

def get_calibration
  check_validity

  send_request FUNCTION_GET_CALIBRATION, [], '', 12, 'S S'
end

#get_configurationObject

Returns the configuration as set by BrickletVoltageCurrent#set_configuration.



196
197
198
199
200
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 196

def get_configuration
  check_validity

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

#get_currentObject

Returns the current.

If you want to get the current periodically, it is recommended to use the CALLBACK_CURRENT callback and set the period with BrickletVoltageCurrent#set_current_callback_period.



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

def get_current
  check_validity

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

#get_current_callback_periodObject

Returns the period as set by BrickletVoltageCurrent#set_current_callback_period.



234
235
236
237
238
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 234

def get_current_callback_period
  check_validity

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

#get_current_callback_thresholdObject

Returns the threshold as set by BrickletVoltageCurrent#set_current_callback_threshold.



294
295
296
297
298
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 294

def get_current_callback_threshold
  check_validity

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

#get_debounce_periodObject

Returns the debounce period as set by BrickletVoltageCurrent#set_debounce_period.



368
369
370
371
372
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 368

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|



384
385
386
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 384

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

#get_powerObject

Returns the power.

If you want to get the power periodically, it is recommended to use the CALLBACK_POWER callback and set the period with BrickletVoltageCurrent#set_power_callback_period.



180
181
182
183
184
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 180

def get_power
  check_validity

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

#get_power_callback_periodObject

Returns the period as set by BrickletVoltageCurrent#get_power_callback_period.



270
271
272
273
274
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 270

def get_power_callback_period
  check_validity

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

#get_power_callback_thresholdObject

Returns the threshold as set by BrickletVoltageCurrent#set_power_callback_threshold.



342
343
344
345
346
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 342

def get_power_callback_threshold
  check_validity

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

#get_voltageObject

Returns the voltage.

If you want to get the voltage periodically, it is recommended to use the CALLBACK_VOLTAGE callback and set the period with BrickletVoltageCurrent#set_voltage_callback_period.



169
170
171
172
173
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 169

def get_voltage
  check_validity

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

#get_voltage_callback_periodObject

Returns the period as set by BrickletVoltageCurrent#set_voltage_callback_period.



252
253
254
255
256
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 252

def get_voltage_callback_period
  check_validity

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

#get_voltage_callback_thresholdObject

Returns the threshold as set by BrickletVoltageCurrent#set_voltage_callback_threshold.



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

def get_voltage_callback_threshold
  check_validity

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

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



389
390
391
392
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 389

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

#set_calibration(gain_multiplier, gain_divisor) ⇒ Object

Since the shunt resistor that is used to measure the current is not perfectly precise, it needs to be calibrated by a multiplier and divisor if a very precise reading is needed.

For example, if you are expecting a measurement of 1000mA and you are measuring 1023mA, you can calibrate the Voltage/Current Bricklet by setting the multiplier to 1000 and the divisor to 1023.



209
210
211
212
213
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 209

def set_calibration(gain_multiplier, gain_divisor)
  check_validity

  send_request FUNCTION_SET_CALIBRATION, [gain_multiplier, gain_divisor], 'S S', 8, ''
end

#set_configuration(averaging, voltage_conversion_time, current_conversion_time) ⇒ Object

Sets the configuration of the Voltage/Current Bricklet. It is possible to configure number of averages as well as voltage and current conversion time.



189
190
191
192
193
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 189

def set_configuration(averaging, voltage_conversion_time, current_conversion_time)
  check_validity

  send_request FUNCTION_SET_CONFIGURATION, [averaging, voltage_conversion_time, current_conversion_time], 'C C C', 8, ''
end

#set_current_callback_period(period) ⇒ Object

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

The CALLBACK_CURRENT callback is only triggered if the current has changed since the last triggering.



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

def set_current_callback_period(period)
  check_validity

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

#set_current_callback_threshold(option, min, max) ⇒ Object

Sets the thresholds for the CALLBACK_CURRENT_REACHED callback.

The following options are possible:

"Option", "Description"

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


287
288
289
290
291
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 287

def set_current_callback_threshold(option, min, max)
  check_validity

  send_request FUNCTION_SET_CURRENT_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 8, ''
end

#set_debounce_period(debounce) ⇒ Object

Sets the period with which the threshold callbacks

  • CALLBACK_CURRENT_REACHED,

  • CALLBACK_VOLTAGE_REACHED,

  • CALLBACK_POWER_REACHED

are triggered, if the thresholds

  • BrickletVoltageCurrent#set_current_callback_threshold,

  • BrickletVoltageCurrent#set_voltage_callback_threshold,

  • BrickletVoltageCurrent#set_power_callback_threshold

keep being reached.



361
362
363
364
365
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 361

def set_debounce_period(debounce)
  check_validity

  send_request FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 8, ''
end

#set_power_callback_period(period) ⇒ Object

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

The CALLBACK_POWER callback is only triggered if the power has changed since the last triggering.



263
264
265
266
267
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 263

def set_power_callback_period(period)
  check_validity

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

#set_power_callback_threshold(option, min, max) ⇒ Object

Sets the thresholds for the CALLBACK_POWER_REACHED callback.

The following options are possible:

"Option", "Description"

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


335
336
337
338
339
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 335

def set_power_callback_threshold(option, min, max)
  check_validity

  send_request FUNCTION_SET_POWER_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 8, ''
end

#set_voltage_callback_period(period) ⇒ Object

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

The CALLBACK_VOLTAGE callback is only triggered if the voltage has changed since the last triggering.



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

def set_voltage_callback_period(period)
  check_validity

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

#set_voltage_callback_threshold(option, min, max) ⇒ Object

Sets the thresholds for the CALLBACK_VOLTAGE_REACHED callback.

The following options are possible:

"Option", "Description"

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


311
312
313
314
315
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 311

def set_voltage_callback_threshold(option, min, max)
  check_validity

  send_request FUNCTION_SET_VOLTAGE_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 8, ''
end