cadet.sources

CARLA input source for CADET.

What this is for:

Implements the cadet.core.source.Source interface against a running CARLA simulator. It attaches to the ego vehicle that ScenarioRunner has already spawned, mounts an RGB camera the same way the original open-loop braking client did, and yields frames. It also exposes the small set of CARLA actuations the braking client needs (apply control, set target velocity, locate/destroy the obstacle).

What it contains:
  • CarlaSource: open/next_frame/close/metadata plus the ego handle and actuation helpers.

How it fits into CADET:

Built from the resolved config’s source section (host/port/timeout, camera w/h/fov) and source.scenario (ego_vehicle, target_object). The braking client runtime (cadet.roles.client) drives it. carla is imported lazily so this module imports fine where CARLA is absent (unit tests).

How to configure it:

simulator_host is a placeholder in the example configs; pass the real address at run time with --set source.carla.host=<HOST_IP>.

class cadet.sources.carla.CarlaSource(config, logger=None)

Bases: Source

Frame source backed by a running CARLA simulator.

prepare()

Connect to the simulator. Does not need the ego to exist yet.

Split out of open() so a client can finish everything that does not depend on the ego - including connecting to its server and loading its own model - and announce itself ready before the scenario is started. The ego exists only for a short window once the scenario runs, and a client still setting up when it appeared could miss it entirely.

Return type:

None

await_scene(timeout_s)

Poll until the ego the sim hook spawned exists; True if it appeared.

This is the client waiting for the scenario rather than the other way round. Polling is the only option available: CARLA 0.9.10 offers no actor-spawn notification.

Return type:

bool

Parameters:

timeout_s (float)

open()

Attach to the ego (if not already) and set up the camera + obstacle.

Return type:

None

snapshot()

Return (frame_id, elapsed_s, dt_s) of the current world tick, or None.

Lets a client gate control on the simulation clock (one command per tick) and use the tick dt for rate-dependent logic, instead of the loop’s wall rate.

next_frame()

Return the next Frame, or None if none is available now.

None is not by itself the end of the stream. A file or a generator has nothing left when it returns None, but a live source returns None whenever nothing has arrived yet - which is always true on the first tick, before its first sensor callback. Ask exhausted() to tell the two apart.

Return type:

Optional[Frame]

close()

Release backend resources.

Return type:

None

exhausted()

Never: a running simulator keeps producing frames until it is closed.

next_frame returns None whenever the camera queue is momentarily empty, which it always is on the first tick after the camera is spawned. Reading that as the end of the stream ends a run before it has seen a single frame.

Return type:

bool

metadata()

Return static source metadata (resolution, fps, map, …).

Return type:

Dict[str, Any]

apply_control(throttle, steer, brake)

Apply a vehicle control to the ego.

Return type:

None

Parameters:
  • throttle (float)

  • steer (float)

  • brake (float)

set_target_velocity(speed_mps)

Set the ego’s velocity along its heading (the baseline’s speed kick).

Return type:

None

Parameters:

speed_mps (float)

ego_is_alive()

Whether the ego actor still exists in the simulator.

A scenario destroys the actors it spawned when it ends, and an app that is waiting on the ego - for it to brake, or to come to a stop - is then waiting for something that cannot happen. Asked of the actor rather than inferred from a stale reading: a destroyed actor keeps answering get_location with the last position it had, so a client watching only the position cannot tell the difference between stopped and gone.

True when the simulator cannot be asked, because an unanswerable question is not evidence that the ego has gone.

The world is asked for the actor by id, rather than the actor handle being asked about itself. A handle whose actor the server has already destroyed goes on answering: is_alive stayed true and get_location kept returning the last position, so a client trusting either kept braking a vehicle that was no longer there. Measured on the distributed tier, where the scenario reported it could not destroy the ego because the ego was already gone.

Return type:

bool

vehicle_location()

Current ego world location.

vehicle_speed_mps()

Current ego speed magnitude in m/s.

Return type:

float

original_client_loop_poll(log)

The original client’s per-tick debug polling.

Ported verbatim from open_loop_run_braking.py’s loop tail: two get_transform RPCs (ego + camera), the relative offset, four debug prints and the ego bounding-box read - run every control tick. This is the remaining per-tick cost (RPC round-trips + console I/O) that set the original client’s loop rate; it is the real work, not a sleep. log carries the prints to the node log, the same I/O the original sent to stdout.

Return type:

None

poll_control()

The ego’s current control (get_control RPC), as the original read per frame.

apply_walker_control(dx, dy, dz, speed)

Drive the obstacle as a walker (V2I pedestrian): direction vector + speed.

The V2I client drives the crossing pedestrian itself, as the original did; the obstacle here is a walker.pedestrian.* actor, so it takes a WalkerControl rather than a VehicleControl.

spawn_fixed_camera(x, y, z, pitch, yaw, width, height, fov)

Spawn an RGB camera at a FIXED world pose (not ego-attached); return (cam, queue).

For the V2I infrastructure (roadside) feed: the one client process owns this camera in addition to its ego-attached one, as the original V2IClient did. The queue yields (rgb_array, capture_time) with the same BGRA->RGB conversion the ego path uses; it drops frames when full (keep-latest), like the ego camera.

obstacle_location()

Current obstacle world location, or None when there is no obstacle.

For V2V the obstacle is the scripted lead vehicle; the client reads its location to drive it (cruise/brake) and to measure the headway gap.

obstacle_speed_mps()

Current obstacle speed magnitude in m/s (0.0 when there is no obstacle).

Return type:

float

obstacle_velocity()

Current obstacle velocity vector (vx, vy, vz) m/s, or None if no obstacle.

For the V2I ground-truth pedestrian TTE (evaluation only): the signed y-velocity gives the ped’s time to the crossing line.

apply_obstacle_control(throttle, brake)

Apply a throttle/brake control to the obstacle (V2V leader actuation).

Return type:

None

Parameters:
  • throttle (float)

  • brake (float)

set_obstacle_target_velocity(speed_mps)

Set the obstacle’s velocity along its heading (the V2V leader cruise kick).

Return type:

None

Parameters:

speed_mps (float)

destroy_obstacle()

Destroy the obstacle actor (called when braking begins).

Return type:

None

Replay frame source (images folder or video file).

What this is for:

A cadet.core.source.Source that replays recorded frames (a folder of images or a single video file) at a configured fps, optionally looping. This is the “Dataset / replay” source mode: deterministic, no simulator.

What it contains:
  • FrameReader: the seam that decides where the next frame comes from, and the two readers behind it - ImageFolderReader over a sorted list of image files, and CaptureReader over an OpenCV VideoCapture, which takes a video path, a camera index or a stream URL alike.

  • ReplaySource: open/next_frame/close/metadata over the frames. Everything that is not the reader lives here: the per-frame pacing, the frame counting and the stop condition.

How it fits into CADET:

Built from a source of kind: replay (see cadet.config.schema.ReplaySource). Images are read with OpenCV (imported lazily). cadet.sources.live.LiveSource is this source with a different reader - a capture on a device or a stream instead of on a directory - so it inherits the pacing and the counting unchanged. The same app runs over any of them, or over cadet.sources.synthetic.SyntheticSource, unchanged.

class cadet.sources.replay.FrameReader

Bases: ABC

Where the next frame comes from, and nothing else.

A reader hands back raw BGR images in OpenCV’s own order; the pacing, the RGB conversion, the frame counting and the stop condition all belong to the source above it. read returning None means “no image this call” - what that means for the run is the source’s decision, not the reader’s.

abstractmethod open()

Acquire the underlying frames, raising if there are none to acquire.

Return type:

None

abstractmethod read()

Return the next BGR image, or None if there is none to return.

Return type:

Optional[Any]

close()

Release whatever open() acquired. Nothing to do by default.

Return type:

None

describe()

Reader-specific fields to fold into the source’s metadata.

Return type:

Dict[str, Any]

class cadet.sources.replay.ImageFolderReader(path, loop=False)

Bases: FrameReader

Frames from a directory of image files, read in sorted order.

Parameters:
  • path (str)

  • loop (bool)

open()

Acquire the underlying frames, raising if there are none to acquire.

Return type:

None

read()

Return the next BGR image, or None if there is none to return.

Return type:

Optional[Any]

describe()

Reader-specific fields to fold into the source’s metadata.

Return type:

Dict[str, Any]

class cadet.sources.replay.CaptureReader(target, rewind_on_end=False)

Bases: FrameReader

Frames from an OpenCV capture: a video file, a camera index or a stream URL.

target is handed to cv2.VideoCapture as given, which is what makes the three cases one reader: an int is a capture device index, a str is a path or a URL. rewind_on_end seeks back to the first frame when a read comes up empty, which is how a finite recording loops; a device or a stream has nothing to seek back to, so it leaves it off.

Parameters:

rewind_on_end (bool)

open()

Acquire the underlying frames, raising if there are none to acquire.

Return type:

None

read()

Return the next BGR image, or None if there is none to return.

Return type:

Optional[Any]

close()

Release whatever open() acquired. Nothing to do by default.

Return type:

None

describe()

Reader-specific fields to fold into the source’s metadata.

Return type:

Dict[str, Any]

class cadet.sources.replay.ReplaySource(config, logger=None)

Bases: Source

Replay frames from a folder of images or a video file.

open()

Acquire the backend (connect to sim, open file, start sensor).

Return type:

None

next_frame()

Return the next Frame, or None if none is available now.

None is not by itself the end of the stream. A file or a generator has nothing left when it returns None, but a live source returns None whenever nothing has arrived yet - which is always true on the first tick, before its first sensor callback. Ask exhausted() to tell the two apart.

Return type:

Optional[Frame]

close()

Release backend resources.

Return type:

None

metadata()

Return static source metadata (resolution, fps, map, …).

Return type:

Dict[str, Any]

Synthetic frame source (generated frames, for tests).

What this is for:

A cadet.core.source.Source that generates frames in-process — no files, no hardware, no CARLA. Used by framework tests and as the simplest way to exercise an app end-to-end deterministically.

What it contains:
  • SyntheticSource: emits count frames (0 = unbounded) of a configured size, each stamped with its index, at an optional fps.

How it fits into CADET:

Built from a source of kind: synthetic (see cadet.config.schema.SyntheticSource). The same app runs over this or cadet.sources.replay.ReplaySource unchanged.

class cadet.sources.synthetic.SyntheticSource(config, logger=None)

Bases: Source

Generates count frames of size width x height.

open()

Acquire the backend (connect to sim, open file, start sensor).

Return type:

None

next_frame()

Return the next Frame, or None if none is available now.

None is not by itself the end of the stream. A file or a generator has nothing left when it returns None, but a live source returns None whenever nothing has arrived yet - which is always true on the first tick, before its first sensor callback. Ask exhausted() to tell the two apart.

Return type:

Optional[Frame]

close()

Release backend resources.

Return type:

None

metadata()

Return static source metadata (resolution, fps, map, …).

Return type:

Dict[str, Any]