Search documentation of bb

# BB.Message behaviour (bb v0.8.0)

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.

## 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`.

# Summary

## Types

t()

## Callbacks

schema()

Returns a compiled Spark.Options schema for this payload type

## Functions

new(payload_module, frame_id, attrs)

Create a new message with validated payload.

new!(payload_module, frame_id, attrs)

Like `new/3` but raises on validation error.

schema(message)

Get the payload schema from a message.

# Types

# t()

    @type t() :: %BB.Message{frame_id: atom(), payload: struct(), timestamp: integer()}

# Callbacks

# schema()

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

Returns a compiled Spark.Options schema for this payload type

# Functions

# new(payload_module, frame_id, attrs)

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

Create a new message with validated payload.

Validates the attributes against the payload module's schema, then wraps the resulting struct in a message envelope with a fresh timestamp.

## Examples

    alias BB.Message.Geometry.Pose
    alias BB.Message.{Vec3, Quaternion}

    {:ok, msg} = BB.Message.new(Pose, :end_effector, [
      position: Vec3.new(1.0, 0.0, 0.5),
      orientation: Quaternion.identity()
    ])

# new!(payload_module, frame_id, attrs)

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

Like `new/3` but raises on validation error.

## Examples

    msg = BB.Message.new!(Pose, :end_effector, [
      position: Vec3.new(1.0, 0.0, 0.5),
      orientation: Quaternion.identity()
    ])

# schema(message)

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

Get the payload schema from a message.

## Examples

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