Previous
Movement Sensor
The power sensor API allows you to give commands to your power sensor components for getting measurements of voltage, current, and power consumption.
The power sensor component supports the following methods:
Method Name | Description |
---|---|
GetVoltage | Return the voltage reading of a specified device and whether it is AC or DC. |
GetCurrent | Return the current of a specified device and whether it is AC or DC. |
GetPower | Return the power reading in watts. |
GetReadings | Get the measurements or readings that this power sensor provides. |
Reconfigure | Reconfigure this resource. |
DoCommand | Execute model-specific commands that are not otherwise defined by the component API. |
GetResourceName | Get the ResourceName for this power sensor with the given name. |
Close | Safely shut down the resource and prevent further use. |
To get started using Viam’s SDKs to connect to and control your power sensor and the rest of your machine, go to your machine’s page on the Viam app, Navigate to the CONNECT tab’s Code sample page, select your preferred programming language, and copy the sample code.
To show your machine’s API key in the sample code, toggle Include API key.
We strongly recommend that you add your API key and machine address as an environment variable. Anyone with these secrets can access your machine, and the computer running your machine.
When executed, this sample code will create a connection to your machine as a client. Once connected, you can control your machine programmatically by adding API method calls as shown in the following examples.
The following examples assume you have a power sensor called "my_power_sensor"
configured as a component of your machine.
If your power sensor has a different name, change the name
in the code.
Import the power sensor package for the SDK you are using:
from viam.components.power_sensor import PowerSensor
import (
"go.viam.com/rdk/components/powersensor"
)
Return the voltage reading of a specified device and whether it is AC or DC.
Parameters:
extra
(Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.timeout
(float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.Returns:
Example:
my_power_sensor = PowerSensor.from_robot(robot=machine, name='my_power_sensor')
# Get the voltage reading from the power sensor
voltage, is_ac = await my_power_sensor.get_voltage()
print("The voltage is", voltage, "V, Is AC:", is_ac)
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
true
) or DC (false
).Example:
// Get the voltage from device in volts.
voltage, isAC, err := myPowerSensor.Voltage(context.Background(), nil)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
var voltageObject = await myPowerSensor.voltage();
double voltageInVolts = voltageObject.volts;
bool isItAC = voltageObject.isAc;
For more information, see the Flutter SDK Docs.
Return the current of a specified device and whether it is AC or DC.
Parameters:
extra
(Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.timeout
(float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.Returns:
Example:
my_power_sensor = PowerSensor.from_robot(robot=machine, name='my_power_sensor')
# Get the current reading from the power sensor
current, is_ac = await my_power_sensor.get_current()
print("The current is ", current, " A, Is AC: ", is_ac)
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
true
) or DC (false
).Example:
// Get the current reading from device in amps.
current, isAC, err := myPowerSensor.Current(context.Background(), nil)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
var currentObject = await myPowerSensor.current();
double amps = currentObject.amperes;
bool isItAC = currentObject.isAc;
For more information, see the Flutter SDK Docs.
Return the power reading in watts.
Parameters:
extra
(Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.timeout
(float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.Returns:
Example:
my_power_sensor = PowerSensor.from_robot(robot=machine, name='my_power_sensor')
# Get the power reading from the power sensor
power = await my_power_sensor.get_power()
print("The power is", power, "Watts")
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
// Get the power measurement from device in watts.
power, err := myPowerSensor.Power(context.Background(), nil)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
var power = await myPowerSensor.power();
For more information, see the Flutter SDK Docs.
Get the measurements or readings that this power sensor provides. If a sensor is not configured to have a measurement or fails to read a piece of data, it will not appear in the readings dictionary.
Parameters:
extra
(Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.timeout
(float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.Returns:
Example:
my_power_sensor = PowerSensor.from_robot(robot=machine, name='my_power_sensor')
# Get the readings provided by the sensor.
readings = await my_power_sensor.get_readings()
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
// Get the readings provided by the sensor.
readings, err := mySensor.Readings(context.Background(), nil)
For more information, see the Go SDK Docs.
Reconfigure this resource. Reconfigure must reconfigure the resource atomically and in place.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.deps
(Dependencies): The resource dependencies.conf
(Config): The resource configuration.Returns:
For more information, see the Go SDK Docs.
Execute model-specific commands that are not otherwise defined by the component API.
If you are implementing your own power sensor and add features that have no built-in API method, you can access them with DoCommand
.
Parameters:
command
(Mapping[str, ValueTypes]) (required): The command to execute.timeout
(float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.Returns:
Raises:
Example:
my_power_sensor = PowerSensor.from_robot(robot=machine, name="my_power_sensor")
command = {"cmd": "test", "data1": 500}
result = await my_power_sensor.do_command(command)
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.cmd
(map[string]interface{}): The command to execute.Returns:
Example:
myPowerSensor, err := power_sensor.FromRobot(machine, "my_power_sensor")
command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myPowerSensor.DoCommand(context.Background(), command)
For more information, see the Go SDK Docs.
Get the ResourceName
for this power sensor with the given name.
Parameters:
name
(str) (required): The name of the Resource.Returns:
Example:
my_power_sensor_name = PowerSensor.get_resource_name("my_power_sensor")
For more information, see the Python SDK Docs.
Safely shut down the resource and prevent further use.
Parameters:
Returns:
Example:
my_power_sensor = PowerSensor.from_robot(robot=machine, name="my_power_sensor")
await my_power_sensor.close()
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.Returns:
Example:
myPowerSensor, err := powersensor.FromRobot(machine, "my_power_sensor")
err = myPowerSensor.Close(context.Background())
For more information, see the Go SDK Docs.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!