SyntheticArm: a robot arm with a fault at a known time¶
SyntheticArm makes a labelled joint stream from the URDF of a robot arm, with a fault that starts at a known time. It is a robot dataset by nature, so its examples are robot arms. The overview of the section is on Datasets.
A robot arm with a fault at a known time¶
A labelled joint stream from the real URDF of a Panda arm. Torque is the turning force the motors apply at each joint; on a moving arm it is not free, it is fixed by the motion through the arm's own dynamics. The stream has a periodic motion, torques from the library's inverse dynamics, sensor noise, and from second 2 a 5 kg payload on the last link.
import numpy as np
from dense_armor.core.signals.datasets import SyntheticArm
ds = SyntheticArm("test/fixtures/urdf/panda.urdf", period_s=2.0, rate_hz=50.0, n_cycles=2,
fault_at_s=2.0, fault="payload", payload_mass=5.0, seed=0)
r0, r1 = [], []
for sig, y in ds.stream():
q, qd, qdd = ds.trajectory(sig.t)
r = float(sig["tau_1"] - ds.nominal_torque(q, qd, qdd)[1])
(r1 if y else r0).append(r)
print(ds.payload_link, round(np.mean(r0), 3), round(np.std(r0), 3), round(np.mean(r1), 2))
The residual is what is left of the measured torque once the expected one is subtracted:
On a healthy arm it is noise: mean -0.004 N·m and standard deviation 0.051 N·m, exactly the torque noise the stream adds. On a faulty arm it carries the fault: it jumps to a mean of -12.54 N·m.
The expected torque comes from the arm's inverse dynamics,
with \(q\) the joint positions, \(\dot q\) the joint velocities, \(\ddot q\) the joint accelerations, \(M(q)\) the mass matrix (how the arm's inertia couples the joints), \(C(q, \dot q)\,\dot q\) the Coriolis and centrifugal torques, and \(g(q)\) the gravity torques. The payload adds its own gravity torque
where \(m\) is the payload mass, \(g\) the acceleration of gravity and \(J_z(q)\) the vertical row of
the Jacobian of the link that carries the payload (here panda_link7, the flange): the vertical
speed of that link for a unit joint velocity. y is 0 before the fault and 1 after, so the
stream also scores detectors.
A model that does not learn the fault¶
A recursive least-squares model estimates the residual offset, the constant value the model
adds to its prediction to best fit the samples it has seen; on a healthy arm it is about zero.
Protected asks an anomaly detector before each sample and skips learning on the ones the
detector flags; the detector is a Hampel scorer wrapped in AnomalyGate, which also keeps it
from learning flagged samples. The same model without protection is shown next to it for
comparison.
from dense_armor.roles.anomaly_detector import AnomalyGate
from dense_armor.roles.protection import Protected
from dense_armor.core.online.anomaly.filters import HampelScorer
from dense_armor.core.signals.datasets import SyntheticArm
from dense_armor.core.online.learn.online_dynamics import RecursiveLeastSquares
ds = SyntheticArm("test/fixtures/urdf/panda.urdf", period_s=2.0, rate_hz=50.0, n_cycles=2,
fault_at_s=2.0, fault="payload", payload_mass=5.0, seed=0)
gate = AnomalyGate(HampelScorer(radius=10, n_sigmas=5.0, feature="r"))
prot = Protected(model=RecursiveLeastSquares(feature_keys=["one"]), detector=gate)
bare = RecursiveLeastSquares(feature_keys=["one"])
for sig, y in ds.stream():
q, qd, qdd = ds.trajectory(sig.t)
x = {"r": float(sig["tau_1"] - ds.nominal_torque(q, qd, qdd)[1]), "one": 1.0}
gate.learn_one(x)
prot.learn_one(x, x["r"])
bare.learn_one(x, x["r"])
print(round(prot.model.predict_one({"one": 1.0}), 3), round(bare.predict_one({"one": 1.0}), 3))
After the fault, the protected model still says the healthy offset is -0.003 N·m; the unprotected one has averaged the fault in and says -6.271 N·m, about half of the -12.5 N·m step because half of its samples came after the fault. In the test suite the same run flags 99 of the 100 faulty samples and 1 of the 100 healthy ones. A model protected this way can be used to keep estimating "normal" while the robot is in trouble.
API reference¶
synthetic_arm ¶
Labelled joint stream from a real URDF, with a fault after a chosen time.
The arm follows a periodic sinusoidal trajectory inside the joint limits of the URDF. Torques come from inverse dynamics,
tau = mass_matrix(q) @ qdd + bias_forces(q, qd) + gravity_forces(q),
computed by :class:~dense_armor.dynamics.urdf_dynamics.RigidBodyModel
from the same URDF file. Gaussian sensor noise is added to q, qd
and tau. From fault_at_s a fault adds a torque:
"friction": a viscous termb * qd[j]on one joint;"payload": an extra massmat the origin of the last link of the chain, whose gravity torque ism * g * J_z(q), withJ_zthe vertical row of the link's translational Jacobian (the sameV = m g zconvention asgravity_forces).
The label y is 0 before fault_at_s and 1 after.
Examples:
>>> from dense_armor.core.signals.datasets import SyntheticArm
>>> ds = SyntheticArm(
... "test/fixtures/urdf/panda.urdf",
... period_s=1.0, rate_hz=50.0,
... fault_at_s=0.5, fault="payload", seed=0,
... )
SyntheticArm ¶
SyntheticArm(
urdf_path: str | Path,
period_s: float,
rate_hz: float,
n_cycles: int = 4,
fault_at_s: float | None = None,
fault: str | None = None,
fault_joint: int | None = None,
friction_b: float = 20.0,
payload_mass: float = 2.0,
payload_link: str | None = None,
noise_std: tuple[float, float, float] = (
0.001,
0.005,
0.05,
),
seed: int | None = 0,
)
Periodic joint stream from a URDF with an optional fault.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
urdf_path
|
str | Path
|
URDF readable by
:class: |
required |
period_s
|
float
|
period of one sinusoidal cycle, in seconds. |
required |
rate_hz
|
float
|
sampling rate, in Hz. |
required |
n_cycles
|
int
|
number of cycles generated. |
4
|
fault_at_s
|
float | None
|
time from the start when the fault begins, in
seconds. |
None
|
fault
|
str | None
|
|
None
|
fault_joint
|
int | None
|
index of the joint with the |
None
|
friction_b
|
float
|
viscous coefficient for |
20.0
|
payload_mass
|
float
|
mass added for |
2.0
|
payload_link
|
str | None
|
link carrying the payload. Default: the last link of the longest chain. |
None
|
noise_std
|
tuple[float, float, float]
|
standard deviation of the Gaussian sensor noise on
|
(0.001, 0.005, 0.05)
|
seed
|
int | None
|
seed of the random generator. |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
on bad arguments. |
Examples:
>>> from dense_armor.core.signals.datasets import SyntheticArm
>>> ds = SyntheticArm(
... "test/fixtures/urdf/panda.urdf",
... period_s=1.0, rate_hz=50.0,
... fault_at_s=0.5, fault="payload", seed=0,
... )
Source code in dense_armor/core/signals/datasets/synthetic_arm.py
trajectory ¶
Sinusoidal position, velocity and acceleration at t.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
time from the start, in seconds. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, ndarray]
|
|
Source code in dense_armor/core/signals/datasets/synthetic_arm.py
nominal_torque ¶
Inverse dynamics of the arm, without noise or fault.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
ndarray
|
joint positions. |
required |
qd
|
ndarray
|
joint velocities. |
required |
qdd
|
ndarray
|
joint accelerations. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
``mass_matrix(q) @ qdd + bias_forces(q, qd) |
ndarray
|
|
Source code in dense_armor/core/signals/datasets/synthetic_arm.py
payload_torque ¶
Gravity torque of the payload mass, m * g * J_z(q).
stream ¶
Yield (Signal, y) pairs at rate_hz.
Each :class:Signal has channels q_j, qd_j, tau_j
for every joint; t is the sample time in seconds; y is
0 before fault_at_s and 1 after.
Yields:
| Type | Description |
|---|---|
tuple[Signal, int]
|
|
Source code in dense_armor/core/signals/datasets/synthetic_arm.py
Details¶
SyntheticArm: sinusoidal joint trajectories at 30 % of each joint's range, inverse dynamics withRigidBodyModel, compiled once per URDF and process; faults:"friction"(viscousb * qdon one joint) or"payload"(a mass on the last link of the longest chain, or onpayload_link).Protectedpasses the timestamp only to models that accept it;HampelScorer.thresholdis itsn_sigmas, whichAnomalyGateuses to decide.Protectedpasses the timestamp only to models that accept it;HampelScorer.thresholdis itsn_sigmas, whichAnomalyGateuses to decide.