pros_devices/
battery.rs

1//! Utilites for getting information about the robot's battery.
2
3use pros_core::{bail_on, map_errno};
4use pros_sys::{PROS_ERR, PROS_ERR_F};
5use snafu::Snafu;
6
7/// Get the robot's battery capacity.
8pub fn capacity() -> Result<f64, BatteryError> {
9    Ok(bail_on!(PROS_ERR_F, unsafe {
10        pros_sys::misc::battery_get_capacity()
11    }))
12}
13
14/// Get the current temperature of the robot's battery.
15pub fn temperature() -> Result<f64, BatteryError> {
16    Ok(bail_on!(PROS_ERR_F, unsafe {
17        pros_sys::misc::battery_get_temperature()
18    }))
19}
20
21/// Get the electric current of the robot's battery.
22pub fn current() -> Result<i32, BatteryError> {
23    Ok(bail_on!(PROS_ERR, unsafe {
24        pros_sys::misc::battery_get_current()
25    }))
26}
27
28/// Get the robot's battery voltage.
29pub fn voltage() -> Result<i32, BatteryError> {
30    Ok(bail_on!(PROS_ERR, unsafe {
31        pros_sys::misc::battery_get_voltage()
32    }))
33}
34
35#[derive(Debug, Snafu)]
36/// Errors that can occur when interacting with the robot's battery.
37pub enum BatteryError {
38    /// Another resource is already using the battery.
39    ConcurrentAccess,
40}
41
42map_errno! {
43    BatteryError {
44        EACCES => Self::ConcurrentAccess,
45    }
46}