BB.Message.Camera.Frame (bb_camera v0.3.0)

Copy Markdown View Source

One raw frame: pixels, and what is needed to read them.

A frame is an ordinary BB.Message with this as its payload. The envelope says when and where it was captured - monotonic_time and wall_time are the capture time, node the node that captured it, and frame_id the name of the camera sensor - and the payload carries everything else.

BB.Camera.Hub never puts a frame on BB.PubSub. Frames reach consumers through the hub's bounded interface, because megabytes of image data through a fan-out registry with no backpressure is the specific failure mode this package exists to avoid. Being a message is what lets a frame go anywhere else a message can, though, and that is deliberate: a recorder or a bridge could publish or serialise one, and an estimator receives one like any other input. Doing that at frame rate is that subsystem's decision, and its problem to bound.

Building one

BB.Message.new/4 with the capture time:

BB.Message.new(
  BB.Message.Camera.Frame,
  camera_name,
  [format: {:raw, :I420}, width: 640, height: 480, data: data],
  monotonic_time: captured_at,
  wall_time: captured_at_wall
)

Stamp the moment the frame was captured when you know it - for a port-based driver, when the frame's last byte left the port - rather than leaving the envelope to default to when the message happened to be built. monotonic_time is the one to measure intervals with and may be negative; wall_time is the one to display.

Dimensions

width and height are the frame's actual dimensions, which are not necessarily what the camera was asked for. Raw pixels don't describe themselves, so these and format are all a consumer has to read data by. A driver should take them from what its capture tool reports it is producing, not echo back its own request, and BB.Message.validate/1 refuses data whose size doesn't match them, however the message is built.

Sequence

sequence identifies the frame within its capture session and must strictly increase within that session. Consumers are promised a frame strictly newer than the last one they received, and that promise rests entirely on this number. A driver with its own counter should set it; one that leaves it nil gets it stamped by the hub.

The hub forgets the last sequence it saw whenever capture is confirmed started, so a driver may restart its count at zero for each session or carry a single counter across restarts - whichever its capture backend makes natural. Neither is more correct, and the hub accepts both.

Frame rates

requested_frame_rate is what the camera was asked for, and measured_frame_rate is what the frames before this one actually did, over the most recent window the hub measured. Both are stamped by the hub, so a driver sets neither; both can be nil, the first if the camera never said what it was asking for and the second until a window has closed.

They are named fields rather than metadata keys because every frame has them whatever the driver is. metadata is for what only some drivers know - a sensor's exposure time, say - and nothing should go in it that the framework puts on every frame.

Where the camera was

A camera on a gimbal is somewhere different in every frame, and by the time anyone asks where it was pointing, the joints have moved. So the hub stamps joint_configurations onto each frame as it passes through: every joint's configuration at the envelope's monotonic_time, looked up in the robot's joint history (see BB.Robot.State.get_all_configurations_at/2).

That makes monotonic_time matter beyond display. A driver that stamps it when the frame was captured gets the pose at capture, however long the frame then spent in a pipe or a decoder; one that leaves it to default gets the pose when the message was built.

attached_to says where the camera hangs - {:link, name}, {:joint, name} or :robot - which is what turns those configurations into a pose:

BB.Robot.Kinematics.forward_kinematics(
  robot,
  frame.payload.joint_configurations,
  link_name
)

The transform is deliberately not computed here. It is a chain of 4x4 Nx products, which on a small target costs far more per frame than reading the configurations does, and most frames are never asked where they were taken. Snapshotting the cheap, perishable part and leaving the expensive, reproducible part to whoever wants it is the whole trick.

Every joint is captured rather than only the camera's own chain. It costs one more table read per joint and answers questions the camera's chain can't, such as where the gripper was in this picture.

It is only as exact as the joint state behind it. History interpolates between a joint's reports, so a joint reporting at 100Hz is placed well within a 30fps frame interval; a slow encoder is not, and nothing here can make it so. A report still in flight when the frame is stamped isn't used: the joint's previous report is. Where history can't answer at all - the robot runs with joint_history false, or the frame predates the history, as around a BB.Robot.Runtime restart - the frame carries the configuration current as it reached the hub.

Summary

Types

t()

@type t() :: %BB.Message.Camera.Frame{
  attached_to: {:link, atom()} | {:joint, atom()} | :robot | nil,
  data: binary(),
  format: BB.Camera.Format.t(),
  height: pos_integer(),
  joint_configurations: %{required(atom()) => number()},
  measured_frame_rate: float() | nil,
  metadata: map(),
  requested_frame_rate: float() | nil,
  sequence: non_neg_integer() | nil,
  width: pos_integer()
}

Functions

new(frame_id, attrs)

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