BB.Message behaviour (bb v0.34.0)

Copy Markdown View Source

Message envelope and behaviour for payload types.

Messages in BB are wrapped in a standard envelope containing timing, coordinate frame, and payload data.

The :robot field identifies the publishing robot module and is filled in automatically by BB.PubSub.publish/3 just before dispatch. It is nil on freshly-constructed messages that have not yet been published, and lets subscribers that listen to more than one robot attribute each delivered message to its source.

The :arm_epoch field carries the robot's arm epoch as it stood when the message was sent, and is nil on everything else. BB.Actuator's send functions stamp it — deliberately at send time rather than in new/3, so a command built while disarmed and sent after arming carries the epoch it was actually sent under. BB.Actuator.Server refuses a command whose epoch is missing or belongs to an earlier arming session, which is what stops a command outliving the arming session that authorised it. See BB.Safety.epoch/1.

Usage

Use the use BB.Message macro to define a payload type:

defmodule MyPayload do
  defstruct [:value]

  use BB.Message,
    schema: [
      value: [type: :float, required: true]
    ]
end

{:ok, msg} = MyPayload.new(:base_link, value: 1.5)

The macro will:

  • Validate struct fields match schema keys at compile time
  • Implement the schema/0 callback
  • Generate a new/2 helper function

Note: defstruct must be defined before use BB.Message.

Checks across fields

A schema validates each field on its own. A payload whose fields constrain one another - a buffer whose size follows from its dimensions, say - also implements validate/1, which new/4 calls with the built payload, so the check holds however the message is constructed.

When the reading was taken

new/4 stamps the envelope with the time it is called. A sensor that knows when its reading was actually taken - a hardware timestamp, or a frame that spent time in a pipe before it could be parsed - passes :monotonic_time and :wall_time instead, since those are what consumers measure age and latency against.

Summary

Callbacks

Returns a compiled Spark.Options schema for this payload type

Check a payload's fields against one another, once each has passed the schema. Return an exception on failure, as the schema does.

Functions

Create a new message with validated payload.

Like new/4 but raises on validation error.

Get the payload schema from a message.

Types

t()

@type t() :: %BB.Message{
  arm_epoch: BB.Safety.Controller.epoch() | nil,
  frame_id: atom(),
  monotonic_time: integer(),
  node: node(),
  payload: struct(),
  robot: module() | nil,
  wall_time: integer()
}

Callbacks

schema()

@callback schema() :: Spark.Options.t()

Returns a compiled Spark.Options schema for this payload type

validate(payload)

(optional)
@callback validate(payload :: struct()) :: :ok | {:error, Exception.t()}

Check a payload's fields against one another, once each has passed the schema. Return an exception on failure, as the schema does.

Functions

new(payload_module, frame_id, attrs, opts \\ [])

@spec new(module(), atom(), keyword(), keyword()) :: {:ok, t()} | {:error, term()}

Create a new message with validated payload.

Validates the attributes against the payload module's schema, and with its validate/1 if it has one, then wraps the resulting struct in a message envelope. The envelope is stamped with the local node name, and with the current monotonic and wall-clock times unless opts says when the reading was taken, so messages remain interpretable across nodes and after recording.

Options

  • :monotonic_time (integer/0) - When the reading was taken, from System.monotonic_time(:nanosecond). Defaults to now.

  • :wall_time (integer/0) - When the reading was taken, from System.system_time(:nanosecond). Defaults to now.

Examples

alias BB.Message.Geometry.Pose
alias BB.Math.Transform

{:ok, msg} = BB.Message.new(Pose, :end_effector, [
  transform: Transform.identity()
])

new!(payload_module, frame_id, attrs, opts \\ [])

@spec new!(module(), atom(), keyword(), keyword()) :: t()

Like new/4 but raises on validation error.

Examples

msg = BB.Message.new!(Pose, :end_effector, [
  transform: Transform.identity()
])

schema(message)

@spec schema(t()) :: Spark.Options.t()

Get the payload schema from a message.

Examples

BB.Message.schema(msg)  #=> %Spark.Options{...}