# `BB.Robot.State`
[🔗](https://github.com/beam-bots/bb/blob/main/lib/bb/robot/state.ex#L5)

ETS-backed mutable state for robot instances.

This module manages joint configurations, velocities, and computed transforms
for robot instances. Each robot instance has its own ETS table for
concurrent read access.

## Configurations, not positions

A joint's configuration is a point in its own configuration space, and its
shape depends on the joint's type:

| Joint type | DoF | Configuration | Velocity |
|---|---|---|---|
| `:fixed` | 0 | `0.0`, the only one it has | `0.0` |
| `:revolute`, `:continuous` | 1 | `float` angle in radians | `float` rad/s |
| `:prismatic` | 1 | `float` displacement in metres | `float` m/s |
| `:planar` | 3 | `BB.Math.Transform2D` | `BB.Message.Geometry.Twist2D` |
| `:floating` | 6 | `BB.Math.Transform` | `BB.Message.Geometry.Twist` |

"Position" is the wrong word for a 4x4 homogeneous transform, which is why
these functions say "configuration" — the standard term for a point in a
robot's configuration space, and correct for every joint type.

Writes are whole-configuration and atomic. There is no API for setting part of
a floating joint, because a partial update invites inconsistent intermediate
states and the natural producer of such a configuration is an estimator
emitting a complete pose.

A fixed joint has zero degrees of freedom, so its configuration space is a
single point and `0.0` is the only value it accepts. It is still present in
`get_all_configurations/1` so that map can be handed straight back to
`set_configurations/2`.

## Usage

    # Create state for a robot instance
    {:ok, state} = BB.Robot.State.new(robot)

    # Set/get a joint configuration
    :ok = BB.Robot.State.set_configuration(state, :shoulder, 0.5)
    {:ok, angle} = BB.Robot.State.get_configuration(state, :shoulder)

    # Get every joint's configuration as a map
    configurations = BB.Robot.State.get_all_configurations(state)

    # Clean up when done
    :ok = BB.Robot.State.delete(state)

## History

A state created with the `:history` option also records every configuration
written to it against a `System.monotonic_time(:nanosecond)` timestamp, so
`get_all_configurations_at/2` can say where every joint was at a past
moment. A running robot's state has history unless the robot is configured
with `joint_history false`; see `BB.Dsl.JointHistory`.

This is for consumers with latency of their own. A camera frame captured at
`t` and delivered 40ms later should be paired with the robot's configuration
at `t`, not at delivery.

Each joint's history is its own series, kept for `keep` nanoseconds back from
its newest sample. The newest sample older than that is kept too, so a joint
that has been still for longer than `keep` can still be answered for. Fixed
joints have no history, since they have nothing to record.

A lookup resolves each joint independently:

- **At a sample** - that sample.
- **Between two samples** - interpolated, if interpolation is enabled and
  the samples are no more than `max_gap` apart; otherwise the earlier
  sample.
- **After the joint's newest sample** - the newest sample. Joint-state
  producers commonly publish only when a joint moves, so silence means it
  hasn't. This is also what a lookup gets when the sample that would answer
  it is still in flight.
- **Before the joint's oldest sample** - a `BB.Error.State.OutsideJointHistory`
  error.

A lookup later than now is refused outright, rather than extrapolated.

Timestamps are node-local, so a time taken on another node is meaningless
here.

# `configuration`

```elixir
@type configuration() :: float() | BB.Math.Transform2D.t() | BB.Math.Transform.t()
```

A joint's configuration, shaped to its type.

See the module documentation for which shape belongs to which joint type.

# `history_options`

```elixir
@type history_options() :: %{
  keep: non_neg_integer(),
  interpolation?: boolean(),
  max_gap: non_neg_integer()
}
```

How history is kept, with durations in integer nanoseconds.

`BB.Dsl.Info.joint_history/1` returns this shape from a robot's DSL.

# `t`

```elixir
@type t() :: %BB.Robot.State{
  history: history() | nil,
  robot: BB.Robot.t(),
  table: :ets.table()
}
```

# `velocity`

```elixir
@type velocity() ::
  float() | BB.Message.Geometry.Twist2D.t() | BB.Message.Geometry.Twist.t()
```

A joint's velocity, shaped to its type.

# `delete`

```elixir
@spec delete(t()) :: :ok
```

Delete a state table and free resources.

# `find_schema_for_parameter`

```elixir
@spec find_schema_for_parameter(t(), [atom()]) ::
  {:ok, [atom()], Spark.Options.t()} | {:error, :not_found}
```

Find the schema that applies to a given parameter path.

Searches for the longest matching schema prefix.

# `get_all_configurations`

```elixir
@spec get_all_configurations(t()) :: %{required(atom()) =&gt; configuration()}
```

Get every joint's configuration as a map.

Every joint in the robot is present, shaped to its own type.

## Examples

    iex> BB.Robot.State.get_all_configurations(state)
    %{shoulder: 0.5, elbow: -0.2, base: %BB.Math.Transform{}}

# `get_all_configurations_at`

```elixir
@spec get_all_configurations_at(t(), integer()) ::
  {:ok, %{required(atom()) =&gt; configuration()}}
  | {:error,
     BB.Error.State.JointHistoryDisabled.t()
     | BB.Error.State.OutsideJointHistory.t()}
```

Get every joint's configuration as it was at `monotonic_time`.

`monotonic_time` is a `System.monotonic_time(:nanosecond)` value. Every joint
in the robot is present, shaped to its own type, as with
`get_all_configurations/1`. See "History" in the module documentation for
how each joint is resolved.

Returns `BB.Error.State.JointHistoryDisabled` for a state with no history,
and `BB.Error.State.OutsideJointHistory` for a time later than now or older
than some joint's oldest sample.

## Examples

    {:ok, configurations} =
      BB.Robot.State.get_all_configurations_at(state, frame.monotonic_time)

# `get_all_velocities`

```elixir
@spec get_all_velocities(t()) :: %{required(atom()) =&gt; velocity()}
```

Get every joint's velocity as a map.

# `get_chain_configurations`

```elixir
@spec get_chain_configurations(t(), atom()) :: [{atom(), configuration()}]
```

Get the configurations of joints along a path from root to a target link.

Returns a list of `{joint_name, configuration}` tuples in traversal order.

# `get_configuration`

```elixir
@spec get_configuration(t(), atom()) ::
  {:ok, configuration()} | {:error, BB.Error.Kinematics.UnknownJoint.t()}
```

Get the current configuration of a joint.

## Examples

    {:ok, 0.5} = BB.Robot.State.get_configuration(state, :shoulder)
    {:ok, %BB.Math.Transform{}} = BB.Robot.State.get_configuration(state, :base)

# `get_parameter`

```elixir
@spec get_parameter(t(), [atom()]) :: {:ok, term()} | {:error, :not_found}
```

Get a parameter value by path.

Returns `{:ok, value}` if the parameter exists, `{:error, :not_found}` otherwise.

# `get_parameter_schema`

```elixir
@spec get_parameter_schema(t(), [atom()]) ::
  {:ok, Spark.Options.t()} | {:error, :not_found}
```

Get the registered schema for a path prefix.

Returns `{:ok, schema}` if found, `{:error, :not_found}` otherwise.

# `get_robot_state`

```elixir
@spec get_robot_state(t()) :: atom()
```

Get the current robot state machine state.

Returns the state atom (e.g., `:disarmed`, `:idle`, `:executing`).

# `get_velocity`

```elixir
@spec get_velocity(t(), atom()) ::
  {:ok, velocity()} | {:error, BB.Error.Kinematics.UnknownJoint.t()}
```

Get the current velocity of a joint.

# `list_parameters`

```elixir
@spec list_parameters(t(), [atom()]) :: [{[atom()], map()}]
```

List all parameters, optionally filtered by path prefix.

Returns a list of `{path, metadata}` tuples where metadata includes
the current value and schema information if registered.

# `new`

```elixir
@spec new(BB.Robot.t(), [{:history, history_options() | nil}]) :: {:ok, t()}
```

Create a new state table for a robot.

## Options

- `:history` - a `t:history_options/0` map to record configuration history,
  or `nil` (the default) not to. See "History" in the module documentation.

Returns `{:ok, state}` on success.

# `register_parameter_schema`

```elixir
@spec register_parameter_schema(t(), [atom()], Spark.Options.t()) :: :ok
```

Register a parameter schema for a component path.

The schema is stored and used for validation and metadata.

# `reset`

```elixir
@spec reset(t()) :: :ok
```

Reset all joints to their identity configurations and zero velocities.

# `set_configuration`

```elixir
@spec set_configuration(t(), atom(), configuration(), integer()) ::
  :ok
  | {:error,
     BB.Error.Kinematics.UnknownJoint.t() | BB.Error.Invalid.JointConfig.t()}
```

Set the configuration of a joint.

The value's shape must match the joint's type, so a `BB.Math.Transform2D`
aimed at a revolute joint is an error rather than a wrong pose.

`monotonic_time` is when the configuration was true, in
`System.monotonic_time(:nanosecond)`, and is what history records it
against. Pass the measurement's own time where there is one - a
`BB.Message`'s `monotonic_time`, say - rather than letting it default to
now.

## Examples

    :ok = BB.Robot.State.set_configuration(state, :shoulder, 0.5)
    :ok = BB.Robot.State.set_configuration(state, :base, transform)
    :ok = BB.Robot.State.set_configuration(state, :shoulder, 0.5, message.monotonic_time)

# `set_configurations`

```elixir
@spec set_configurations(t(), %{required(atom()) =&gt; configuration()}, integer()) ::
  :ok
  | {:error,
     BB.Error.Kinematics.UnknownJoint.t() | BB.Error.Invalid.JointConfig.t()}
```

Set multiple joint configurations at once.

Every value is validated before anything is written, so a rejected map leaves
the table untouched rather than applying part of itself.

`monotonic_time` is recorded in history against every configuration in the
map, as in `set_configuration/4`.

## Examples

    :ok = BB.Robot.State.set_configurations(state, %{
      shoulder: 0.5,
      elbow: -0.3,
      base: transform
    })

# `set_parameter`

```elixir
@spec set_parameter(t(), [atom()], term()) :: :ok
```

Set a parameter value by path.

This is a low-level function that does not validate or notify.
Use `BB.Parameter.set/3` for the validated, notifying version.

# `set_parameters`

```elixir
@spec set_parameters(t(), [{[atom()], term()}]) :: :ok
```

Set multiple parameters atomically.

This is a low-level function that does not validate or notify.

# `set_robot_state`

```elixir
@spec set_robot_state(t(), atom()) :: :ok
```

Set the robot state machine state.

# `set_velocities`

```elixir
@spec set_velocities(t(), %{required(atom()) =&gt; velocity()}) ::
  :ok
  | {:error,
     BB.Error.Kinematics.UnknownJoint.t() | BB.Error.Invalid.JointConfig.t()}
```

Set multiple joint velocities at once.

# `set_velocity`

```elixir
@spec set_velocity(t(), atom(), velocity()) ::
  :ok
  | {:error,
     BB.Error.Kinematics.UnknownJoint.t() | BB.Error.Invalid.JointConfig.t()}
```

Set the velocity of a joint.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
