Making a Sensor
Check first whether existing sensors already give you the observation. Many observations are a source followed by one or more transforms, declared in a bot or scene file with no new code; see Sensor Graphs. Write a new sensor when you need a measurement no source provides, or a transformation the built-in types do not cover.
Choosing an approach
| You need | Use |
|---|---|
| A new pipeline built from registered types | Declare it in a .superdex_bot or .superdex_scene file. |
| Values computed by a C++ function at every graph step | A SensorFunction; no class of your own. |
| A new source or transform that can be declared in files | A C++ class derived from SensorBase, registered on the RoboticsContext. |
| Sensor observations in Python | Built-in or C++ sensors, read from Python; see C++ sensors, driven from Python. |
| A Python object created and owned with the bot, whose methods you call yourself | A Python-only sensor; see Sensors written in Python. It is not a graph node. |
Writing a C++ sensor
A graph-enabled sensor derives from SensorBase and implements three steps of
its life:
class SensorMyForce final : public SensorBase {
public:
// Attached to a link actor; an unattached (scene-level) sensor uses the
// SensorBase constructor without an actor.
SensorMyForce(Actor* link, Params const& params, Error& error);
std::string_view GetTypeName() const override {
return "SENSOR_MY_FORCE";
}
SensorStepResult Step(StepTime const& now, Error& error) override;
private:
SensorFormat InitSensor(SensorScope const& scope, Error& error) override;
void ResetSensor() override;
};
- Describe the output in
InitSensor. Return theSensorFormatthe sensor will publish: its kind, row-major shape, and axes with labels and units. The format is fixed from then on, and the base class allocates the output storage from it. A transform also checks itsInputs()here, so a miswired graph fails at initialization rather than on the first step. - Publish in
Step. A source acquires from the scene; a transform reads its inputs' already-publishedLatest()samples. ReturnNoUpdatewhen there is nothing new, for example when an input has not published yet or a sampling period is not due. When the values derive from an input, publish withPublishSample(now, values, error, &source)so the result keeps the input's capture time; see Two timestamps per sample. - Clear episode state in
ResetSensor.Reset()itself is final and clears the published sample; a stateless sensor implements an emptyResetSensor().
If a consumer needs more than the format, such as the identity of each row, expose typed metadata
that callers read with TryGetSensorMetadata<T>(), as the vertex-force and
joint-wrench sources do.
Making it declarable
To use the new type in bot and scene files, register it on the
RoboticsContext before loading a scene. RegisterSensor<T>() needs a static
TypeName() and a constructor with the uniform factory signature, which loads
the sensor's own parameters:
// paramArgs is a params file path or inline JSON; empty means default Params.
SensorMyForce(Actor* link, std::string_view paramArgs, Error& error);
static std::string_view TypeName() { return "SENSOR_MY_FORCE"; }
botsContext->RegisterSensor<SensorMyForce>();
RegisterSensorType(typeName, factory) registers any other factory with the same
(Actor*, std::string_view paramArgs, Error&) signature. After registration,
the type name can appear in a link's sensors list or a scene's sensors list,
with inputs and params like any built-in type.
Computing a source in a function
When the values come from code you already have, SensorFunction wraps a C++
function as a graph node. It publishes whatever the function writes at every
graph step, including the reset step. The function returns the sample its
values derive from, to keep that sample's capture time, or std::nullopt for
values captured at the current step:
SensorFunction joint_sum(
SensorFunction::ValuesFormat(1),
[&bot](Span<float> out) -> std::optional<SensorSample> {
out[0] = SumOfJointPositions(bot); // Your own computation.
return std::nullopt; // Captured at this step.
},
error);
SensorFunction::ValuesFormat(width) is the format of width plain values.
Add the function node to a graph like any other sensor.
Python sensors
C++ sensors, driven from Python
Most Python code uses sensors with a C++ implementation, either built-in types
or your own C++ types, and does not define sensors of its own. A sensor does not
need Python bindings of its own to be used this way: Python refers to it by its
graph name and reads it through the methods every sensor shares, such as
latest(), copy_latest_values() and get_format(). The built-in sensors also
have typed Python classes, so their parameters can be read and changed with
get_params() and set_params().
A typical session runs in this order:
- Register any custom C++ type from C++. A scene that declares an unregistered type loads without that sensor and logs a warning.
- Load the SuperDex scene. Its selected prefabs define a graph that is checked before bots are constructed. Loading then binds their sensors and any ready scene-level sensors, with no outputs selected yet. Selected bots may load later; see Building from a scene.
- Select outputs with
add_sensor_outputs. - Step the physics scene, and read samples as shown in Reading the latest sample.
Python never steps the graph itself. BotScene steps it once after every
scene.step(), so sensors and physics advance together; see
Stepping. A sensor failure does not interrupt
scene.step(): call check_sensor_graph() before using the observations, as
described in Errors during stepping.
Only sensors declared in bot and scene files take part in the graph; one created
later with create_sensor does not.
Some parts of the sensor system are available only from C++: building a
SensorGraph directly, SensorFunction, registering C++ types, the
publication timestamps, and typed metadata such as vertex positions.
Sensors written in Python
A Python-only sensor is a plain Python class registered with
register_python_sensor(context, type_name, factory). What that gives you:
- Declaration and creation. The type joins the same table as C++ types, so a
link's
sensorslist or a scene'ssensorslist can declare it, andcreate_sensoron the context or a bot creates it at runtime. The factory receives the link actor, orNonefor a scene-level sensor, and the params string. - Ownership. The context owns the sensor, destroys it with its bot or the
context, and finds it by name or type like any other sensor.
get_python_sensor(context, handle)returns your instance.
What it does not do:
- It is not a graph node. It declares no output format and publishes no
samples, so it has no capture or publication time, cannot be selected with
add_sensor_outputs, cannot feed another sensor, and is not recorded by the graph's recording hooks. Selecting it or naming it as an input fails when the graph initializes. Declared but unselected, it is ignored by the graph. - The framework does not reset it. A Python sensor must define
reset(), and creation fails without one, but only the sensor graph resets sensors. Callreset()yourself between episodes. - It is not portable. A bot or scene file that declares a Python type gets that sensor only where the type is registered. SuperDex Unreal and C++-only applications skip it with a warning.
Use a Python-only sensor to prototype a measurement, or to keep Python measurement code with the bot that owns it, calling its methods from your own loop after stepping, alongside the graph's outputs. When the measurement must feed other sensors, be recorded with the graph, or run outside Python, write it as a C++ sensor.
Before you rely on it
- State units and frames in the format; downstream consumers check them, not just the shape.
- Return
NoUpdaterather than republishing an old value, so consumers can tell a new capture from a held one. - Remember that published samples are borrowed until the next publication, reset or destruction.
- Serialize graph stepping, mutation and reads on one thread.