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.

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

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

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

'>'

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

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



105
106
107
108
109
110
111
112
113
114
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
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 105

def initialize(uid, ipcon)
  super uid, ipcon

  @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[CALLBACK_CURRENT] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_VOLTAGE] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_POWER] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_CURRENT_REACHED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_VOLTAGE_REACHED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_POWER_REACHED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_CURRENT] = 'l'
  @callback_formats[CALLBACK_VOLTAGE] = 'l'
  @callback_formats[CALLBACK_POWER] = 'l'
  @callback_formats[CALLBACK_CURRENT_REACHED] = 'l'
  @callback_formats[CALLBACK_VOLTAGE_REACHED] = 'l'
  @callback_formats[CALLBACK_POWER_REACHED] = 'l'
end

Instance Method Details

#get_calibrationObject

Returns the calibration as set by BrickletVoltageCurrent#set_calibration.



230
231
232
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 230

def get_calibration
  send_request(FUNCTION_GET_CALIBRATION, [], '', 4, 'S S')
end

#get_configurationObject

Returns the configuration as set by BrickletVoltageCurrent#set_configuration.



214
215
216
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 214

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

#get_currentObject

Returns the current. The value is in mA and between -20000mA and 20000mA.

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



153
154
155
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 153

def get_current
  send_request(FUNCTION_GET_CURRENT, [], '', 4, 'l')
end

#get_current_callback_periodObject

Returns the period as set by BrickletVoltageCurrent#set_current_callback_period.



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

def get_current_callback_period
  send_request(FUNCTION_GET_CURRENT_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_current_callback_thresholdObject

Returns the threshold as set by BrickletVoltageCurrent#set_current_callback_threshold.



300
301
302
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 300

def get_current_callback_threshold
  send_request(FUNCTION_GET_CURRENT_CALLBACK_THRESHOLD, [], '', 9, 'k l l')
end

#get_debounce_periodObject

Returns the debounce period as set by BrickletVoltageCurrent#set_debounce_period.



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

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|



380
381
382
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 380

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

#get_powerObject

Returns the power. The value is in mW and between 0mV and 720000mW.

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



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

def get_power
  send_request(FUNCTION_GET_POWER, [], '', 4, 'l')
end

#get_power_callback_periodObject

Returns the period as set by BrickletVoltageCurrent#get_power_callback_period.



278
279
280
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 278

def get_power_callback_period
  send_request(FUNCTION_GET_POWER_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_power_callback_thresholdObject

Returns the threshold as set by BrickletVoltageCurrent#set_power_callback_threshold.



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

def get_power_callback_threshold
  send_request(FUNCTION_GET_POWER_CALLBACK_THRESHOLD, [], '', 9, 'k l l')
end

#get_voltageObject

Returns the voltage. The value is in mV and between 0mV and 36000mV.

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



163
164
165
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 163

def get_voltage
  send_request(FUNCTION_GET_VOLTAGE, [], '', 4, 'l')
end

#get_voltage_callback_periodObject

Returns the period as set by BrickletVoltageCurrent#set_voltage_callback_period.



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

def get_voltage_callback_period
  send_request(FUNCTION_GET_VOLTAGE_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_voltage_callback_thresholdObject

Returns the threshold as set by BrickletVoltageCurrent#set_voltage_callback_threshold.



322
323
324
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 322

def get_voltage_callback_threshold
  send_request(FUNCTION_GET_VOLTAGE_CALLBACK_THRESHOLD, [], '', 9, 'k l l')
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



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

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.



225
226
227
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 225

def set_calibration(gain_multiplier, gain_divisor)
  send_request(FUNCTION_SET_CALIBRATION, [gain_multiplier, gain_divisor], 'S S', 0, '')
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.

Averaging:

"Value", "Number of Averages"

"0",    "1"
"1",    "4"
"2",    "16"
"3",    "64"
"4",    "128"
"5",    "256"
"6",    "512"
">=7",  "1024"

Voltage/Current conversion:

"Value", "Conversion time"

"0",    "140µs"
"1",    "204µs"
"2",    "332µs"
"3",    "588µs"
"4",    "1.1ms"
"5",    "2.116ms"
"6",    "4.156ms"
">=7",  "8.244ms"

The default values are 3, 4 and 4 (64, 1.1ms, 1.1ms) for averaging, voltage conversion and current conversion.



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

def set_configuration(averaging, voltage_conversion_time, current_conversion_time)
  send_request(FUNCTION_SET_CONFIGURATION, [averaging, voltage_conversion_time, current_conversion_time], 'C C C', 0, '')
end

#set_current_callback_period(period) ⇒ Object

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

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

The default value is 0.



241
242
243
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 241

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

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



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

def set_current_callback_threshold(option, min, max)
  send_request(FUNCTION_SET_CURRENT_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 0, '')
end

#set_debounce_period(debounce) ⇒ Object

Sets the period in ms 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.

The default value is 100.



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

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

#set_power_callback_period(period) ⇒ Object

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

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

The default value is 0.



273
274
275
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 273

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

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



339
340
341
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 339

def set_power_callback_threshold(option, min, max)
  send_request(FUNCTION_SET_POWER_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 0, '')
end

#set_voltage_callback_period(period) ⇒ Object

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

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

The default value is 0.



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

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

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



317
318
319
# File 'lib/tinkerforge/bricklet_voltage_current.rb', line 317

def set_voltage_callback_threshold(option, min, max)
  send_request(FUNCTION_SET_VOLTAGE_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 0, '')
end