Power Sensor Component
A power sensor is a device that reports measurements of the voltage, current, and power consumption in your machine’s system. Integrate this component to monitor your power levels.
Related services
Supported models
To use your power sensor with Viam, check whether one of the following built-in models or modular resources supports your power sensor.
Built-in models
For configuration information, click on the model name:
Model | Description |
---|---|
fake | a digital power sensor for testing |
ina219 | INA219 power sensor; current and power monitor |
ina226 | INA226 power sensor; current and power monitor |
renogy | solar charge controller |
Modular resources
Search for additional power_sensor models that you can add from the Viam Registry:
For configuration information, click on the model name:
Add support for other models
If none of the existing models fit your use case, you can create a modular resource to add support for it.
Control your power sensor with Viam’s client SDK libraries
To get started using Viam’s SDKs to connect to and control 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 generated.
API key and API key ID
By default, the sample code does not include your machine API key and API key ID. We strongly recommend that you add your API key and API key ID as an environment variable and import this variable into your development environment as needed.
To show your machine’s API key and API key ID in the sample code, toggle Include secret on the CONNECT tab’s Code sample page.
Caution
Do not share your API key or machine address publicly. Sharing this information could compromise your system security by allowing unauthorized access to your machine, or to 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.
These 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"
)
API
The power sensor component supports the following methods:
Method Name | Description |
---|---|
GetCurrent | Return the current of a specified device and whether it is AC or DC. |
GetVoltage | Return the voltage of a specified device and whether it is AC or DC. |
GetPower | Return the power consumption of a specified device in watts. |
GetGeometries | Get all the geometries associated with the power sensor in its current configuration, in the frame of the power sensor. |
GetReadings | Get all measurements or readings that this power sensor provides. |
DoCommand | Execute model-specific commands that are not otherwise defined by the component API. |
Close | Safely shut down the resource and prevent further use. |
GetCurrent
Return the current of a specified device and whether it is AC or DC.
Parameters:
extra
(Optional[Dict[str,Any]]): Pass additional data and configuration options to the RPC call.timeout
(Optional[float]): Specify a time limit in seconds for how longget_current
should wait for a response.
Returns:
- (Tuple[float, bool]): A tuple which includes a float representing the current reading in amps, and a bool indicating whether the current is AC (
true
) or DC (false
).
For more information, see the Python SDK Docs.
my_power_sensor = PowerSensor.from_robot(robot=robot, 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)
Parameters:
ctx
Context: Control the lifecycle of the operation by handling timeouts and managing cancellations.extra
(Optional[Dict[str, Any]]): Pass additional data and configuration options to the RPC call.
Returns:
float64
: The measurement of the current, represented as a 64-bit float number.bool
: Indicate whether current is AC (true
) or DC (false
).error
: Report any errors that might occur during operation. For a successful operation,error
returnsnil
.
For more information, see the Go SDK Docs.
// Create a power sensor instance
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
if err != nil {
logger.Fatalf("cannot get power sensor: %v", err)
}
// Get the current reading from device in amps
current, isAC, err := myPowerSensor.Current(context.Background(), nil)
GetVoltage
Return the voltage reading of a specified device and whether it is AC or DC.
Parameters:
extra
(Optional[Dict[str, Any]]): Pass additional data and configuration options to the RPC call.timeout
(Optional[float]): Specify a time limit in seconds for how longget_voltage
should wait for a response.
Returns:
- (Tuple[float, bool]): A float representing the current reading in amps. A bool indicating whether the voltage is AC (
true
) or DC (false
).
For more information, see the Python SDK Docs.
my_power_sensor = PowerSensor.from_robot(robot=robot, 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)
Parameters:
ctx
Context: Control the lifecycle of the operation by handling timeouts and managing cancellations.extra
(Optional[Dict[str, Any]]): Pass additional data and configuration options to the RPC call.
Returns:
float64
: The measurement of the voltage, represented as a 64-bit float number.bool
: Indicate whether voltage is AC (true
) or DC (false
).error
: Report any errors that might occur during operation. For a successful operation,error
returnsnil
.
For more information, see the Go SDK Docs.
// Create a power sensor instance
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
if err != nil {
logger.Fatalf("cannot get power sensor: %v", err)
}
// Get the voltage from device in volts
voltage, isAC, err := myPowerSensor.Voltage(context.Background(), nil)
GetPower
Return the power reading in watts.
Parameters:
extra
(Optional[Dict[str, Any]]): Pass additional data and configuration options to the RPC call.timeout
(Optional[float]): Specify a time limit in seconds for how longget_power
should wait for a response.
Returns:
float
: The measurement of the power, represented as a float.
For more information, see the Python SDK Docs.
my_power_sensor = PowerSensor.from_robot(robot=robot, 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")
Parameters:
ctx
Context: Control the lifecycle of the operation by handling timeouts and managing cancellations.extra
(Optional[Dict[str, Any]]): Pass additional data and configuration options to the RPC call.
Returns:
float64
: The measurement of the power, represented as a 64-bit float number.error
: Report any errors that might occur during operation. For a successful operation,error
returnsnil
.
For more information, see the Go SDK Docs.
// Create a power sensor instance
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
if err != nil {
logger.Fatalf("cannot get power sensor: %v", err)
}
// Get the power measurement from device in watts
power, err := myPowerSensor.Power(context.Background(), nil)
GetReadings
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
(Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.timeout
(Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.
Returns:
- (Mapping[str, Any]): The measurements or readings that this power sensor provides.
For more information, see the Python SDK Docs.
my_power_sensor = PowerSensor.from_robot(robot=robot, name='my_power_sensor')
# Get the readings provided by the sensor.
readings = await my_power_sensor.get_readings()
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:
- (map[string]interface{}): The measurements or readings that this sensor provides.
- (error): Report any errors that might occur during operation.
For a successful operation,
error
returnsnil
.
For more information, see the Go SDK Docs.
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
// Get the readings provided by the sensor.
readings, err := myPowerSensor.Readings(context.Background(), nil)
GetGeometries
Get all the geometries associated with the power sensor in its current configuration, in the frame of the power sensor. The motion and navigation services use the relative position of inherent geometries to configured geometries representing obstacles for collision detection and obstacle avoidance while motion planning.
Parameters:
extra
(Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.timeout
(Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.
Returns:
- (List[Geometry]): The geometries associated with the power sensor, in any order.
For more information, see the Python SDK Docs.
my_power_sensor = PowerSensor.from_robot(robot=robot, name="my_power_sensor")
geometries = await my_power_sensor.get_geometries()
if geometries:
# Get the center of the first geometry
print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")
DoCommand
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
(Dict[str, Any]): The command to execute.
Returns:
- (Dict[str, Any]): Result of the executed command.
my_power_sensor = PowerSensor.from_robot(robot=robot, name="my_power_sensor")
reset_dict = {
"command": "reset",
"example_param": 30
}
do_response = await my_power_sensor.do_command(reset_dict)
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:
- (map[string]interface{}): Result of the executed command.
- (error): An error, if one occurred.
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
resp, err := myPowerSensor.DoCommand(ctx, map[string]interface{}{"command": "reset", "example_param": 30})
For more information, see the Go SDK Code.
Close
Safely shut down the resource and prevent further use.
Parameters:
- None
Returns:
- None
my_power_sensor = PowerSensor.from_robot(robot, "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:
- (error) : An error, if one occurred.
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
err := myPowerSensor.Close(ctx)
For more information, see the Go SDK Docs.
Troubleshooting
You can find additional assistance in the Troubleshooting section.
You can also ask questions on the Viam Community Slack and we will be happy to help.
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!