Power Sensor Component
A power sensor is a device that reports measurements of the voltage, current and power consumption in your robot’s system. Integrate this component to monitor your power levels.
Configuration
For configuration information, click on your sensor’s model:
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 |
Control your power sensor with Viam’s client SDK libraries
To get started using Viam’s SDKs to connect to and control your robot, go to your robot’s page on the Viam app, navigate to the Code sample tab, select your preferred programming language, and copy the sample code generated.
Location secret
By default, the sample code does not include your robot location secret. We strongly recommend that you add your location secret as an environment variable and import this variable into your development environment as needed.
To show your robot’s location secret in the sample code, toggle Include secret on the Code sample tab. You can also see your location secret on the locations page.
Caution
Do not share your location secret, part secret, or robot address publicly. Sharing this information could compromise your system security by allowing unauthorized access to your robot, or to the computer running your robot.
When executed, this sample code will create a connection to your robot as a client. Once connected, you can control your robot 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 robot.
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.powersensor 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. |
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.
# 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.
# 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.
# 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)
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!