From cd225a0e566fc484466d428ffb077b4cb6c2bfdc Mon Sep 17 00:00:00 2001 From: likun17 Date: Wed, 9 Sep 2026 20:12:19 +0800 Subject: [PATCH] drivers/sensors: add voltage, current and power sensor types uORB has no type for electrical quantities, so power monitors can only use the legacy character drivers, which are deprecated and report their values in three incompatible unit systems. Add SENSOR_TYPE_VOLTAGE, SENSOR_TYPE_CURRENT and SENSOR_TYPE_POWER with their message structs in SI units (V, A, W). Signed-off-by: likun17 --- drivers/sensors/sensor.c | 3 +++ include/nuttx/uorb.h | 44 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c index fb6b89d99f0..f8861214f64 100644 --- a/drivers/sensors/sensor.c +++ b/drivers/sensors/sensor.c @@ -224,6 +224,9 @@ static const struct sensor_meta_s g_sensor_meta[] = {sizeof(struct sensor_pm10), "pm10"}, {sizeof(struct sensor_uv), "uv"}, {sizeof(struct sensor_eng), "eng"}, + {sizeof(struct sensor_voltage), "voltage"}, + {sizeof(struct sensor_current), "current"}, + {sizeof(struct sensor_power), "power"}, }; static const struct file_operations g_sensor_fops = diff --git a/include/nuttx/uorb.h b/include/nuttx/uorb.h index ea64f06579b..72e2e1ab636 100644 --- a/include/nuttx/uorb.h +++ b/include/nuttx/uorb.h @@ -590,11 +590,35 @@ #define SENSOR_TYPE_ENG 58 +/* Voltage + * A sensor of this type returns the measured voltage between two points. + * The value is in SI units volt(V). + */ + +#define SENSOR_TYPE_VOLTAGE 59 + +/* Current + * A sensor of this type returns the measured current flowing through a + * path. The value is in SI units ampere(A) and is signed: a negative value + * indicates that the current flows in the opposite direction. + */ + +#define SENSOR_TYPE_CURRENT 60 + +/* Power + * A sensor of this type returns the measured instantaneous active power. + * The value is in SI units watt(W). The accumulated energy and the AC + * power components (reactive, apparent and power factor) are not covered + * by this type. + */ + +#define SENSOR_TYPE_POWER 61 + /* The total number of sensor * please increase it if you added a new sensor type! */ -#define SENSOR_TYPE_COUNT 59 +#define SENSOR_TYPE_COUNT 62 /* The additional sensor open flags */ @@ -1033,6 +1057,24 @@ struct sensor_eng /* Type: ENG */ uint32_t stat; /* Status. bit3:0 - value 3:0 is valid or not */ }; +struct sensor_voltage /* Type: Voltage */ +{ + uint64_t timestamp; /* Unit is microseconds */ + float voltage; /* in SI units V */ +}; + +struct sensor_current /* Type: Current */ +{ + uint64_t timestamp; /* Unit is microseconds */ + float current; /* in SI units A, signed */ +}; + +struct sensor_power /* Type: Power */ +{ + uint64_t timestamp; /* Unit is microseconds */ + float power; /* Instantaneous active power in SI units W */ +}; + struct sensor_gnss /* Type: GNSS */ { uint64_t timestamp; /* Time since system start, Units is microseconds */