Skip to content

tactics2d.sensor

RenderManager

This class manages the rendering of the scenario.

By RenderManager, the sensors are registered and can be bound to the participants. The rendering is conducted by the pygame library.

Attributes:

Name Type Description
fps int

The frame rate of the rendering. Defaults to 60.

windows_size Tuple[int, int]

The size of the rendering window. Defaults to (800, 800).

layout_style str

The style of the layout of the rendering window. The available choices are ["hierarchy", "block"]. Defaults to "hierarchy".

off_screen bool

Whether to render the scenario off screen. Defaults to False.

graphic_driver str

The graphic driver used by the pygame library. This attribute is read-only.

__init__(fps=60, windows_size=(800, 800), layout_style='hierarchy', off_screen=False)

Initialize the render manager.

Parameters:

Name Type Description Default
fps int

The frame rate of the rendering.

60
windows_size Tuple[int, int]

The size of the rendering window.

(800, 800)
layout_style str

The style of the layout of the rendering window. The available choices are ["hierarchy", "block"].

'hierarchy'
off_screen bool

Whether to render the scenario off screen.

False

add_sensor(sensor, main_sensor=False)

This function adds a sensor to the render manager.

Parameters:

Name Type Description Default
sensor SensorBase

The sensor instance to be added.

required
main_sensor bool

Whether the sensor is the main sensor for display. This argument only take effect when the the layout style is hierarchical.

False

Raises:

Type Description
KeyError

If the sensor has conflicted id with registered sensors.

bind(id_, participant_id)

This function binds a registered sensor with a participant.

Parameters:

Name Type Description Default
id_ int

The id of the sensor.

required
participant_id int

The id of the participant.

required

Raises:

Type Description
KeyError

If the sensor is not registered in the manager.

close()

Close the render manager.

get_bind_id(id_)

This function gets the id of the participant that the sensor is bound to.

Parameters:

Name Type Description Default
id_ int

The id of the sensor.

required

Returns:

Type Description
int

The id of the participant that the sensor is bound to. If the sensor is not bound to any participant, return None.

get_observation()

Get the observation of all the sensors.

Returns:

Name Type Description
list list

A list of 3d arrays.

is_bound(id_)

This function checks whether the sensor is bound to a participant.

Parameters:

Name Type Description Default
id_ int

The id of the sensor.

required

Returns:

Type Description
bool

If the sensor is bound to a participant, return True. Otherwise, return False.

remove_sensor(id_)

This function removes a sensor from the manager.

Parameters:

Name Type Description Default
id_ int

The id of the sensor.

required

render()

Render the observation of all the sensors.

reset()

Reset the render manager.

unbind(id_)

This function unbinds a registered sensor from its bound participant.

Parameters:

Name Type Description Default
id_ int

The id of the sensor.

required

update(participants, participant_ids, frame=None)

Sync the viewpoint of the sensors with their bound participants. Update the observation of all the sensors.

Parameters:

Name Type Description Default
participants dict

The dictionary of all participants. The render manager will detect which of them is alive.

required
frame int

Update the sensors to the given frame. If None, the sensors will update to the current frame. The default unit is millisecond.

None

SensorBase

Bases: ABC

This class define an interface for all the pseudo sensors provided in tactics2d.

Attributes:

Name Type Description
id_ int

The unique identifier of the sensor.

map_ Map

The map that the sensor is attached to.

perception_range Union[float, Tuple[float]]

The distance from the sensor to its maximum detection range in (left, right, front, back). When this value is undefined, the sensor is assumed to detect the whole map. Defaults to None.

window_size Tuple[int, int]

The size of the rendering window. Defaults to (200, 200).

off_screen bool

Whether to render the sensor off screen. Defaults to True.

scale float

The scale of the rendering window.

bind_id int

The unique identifier of the participant that the sensor is bound to.

surface Surface

The rendering surface of the sensor. This attribute is read-only.

heading float

The heading of the sensor. This attribute is read-only.

position Point

The position of the sensor. This attribute is read-only.

max_perception_distance float

The maximum detection range of the sensor. This attribute is read-only.

__init__(id_, map_, perception_range=None, window_size=(200, 200), off_screen=True)

Initialize the sensor.

Parameters:

Name Type Description Default
id_ int

The unique identifier of the sensor.

required
map_ Map

The map that the sensor is attached to.

required
perception_range Union[float, Tuple[float]]

The distance from the sensor to its maximum detection range in (left, right, front, back). When this value is undefined, the sensor is assumed to detect the whole map.

None
window_size Tuple[int, int]

The size of the rendering window.

(200, 200)
off_screen bool

Whether to render the sensor off screen.

True

get_observation() abstractmethod

This function is used to get the observation of the sensor from the viewpoint.

set_bind_id(bind_id)

This function is used to set the bind id of the sensor.

Parameters:

Name Type Description Default
bind_id int

The unique identifier of the participant that the sensor is bound to.

required

update(participants, position=None, heading=None) abstractmethod

This function is used to update the sensor's location and observation.

Parameters:

Name Type Description Default
participants Dict[int, Participant]

The participants in the scenario.

required
position Point

The position of the sensor. Defaults to None.

None
heading float

The heading of the sensor. Defaults to None.

None

SingleLineLidar

Bases: SensorBase

This class implements a pseudo single line LiDAR.

The default parameters refer to LiDAR STL-06P. This LiDAR sensor has only one scan line. Its documentation is here.

Attributes:

Name Type Description
id_ int

The unique identifier of the LiDAR.

map_ Map

The map that the LiDAR is attached to.

perception_range Union[float, Tuple[float]]

The distance from the LiDAR to its maximum detection range in (left, right, front, back). When this value is undefined, the LiDAR is assumed to detect the whole map. Defaults to 12.0.

window_size Tuple[int, int]

The size of the rendering window. Defaults to (200, 200).

off_screen bool

Whether to render the LiDAR off screen. Defaults to True.

scale float

The scale of the rendering window.

freq_scan float

The frequency of the LiDAR scanning a full round. Defaults to 10.0. This attribute is read-only should only be set in the initialization.

freq_detect float

The frequency of the LiDAR sending and receiving the signal. Defaults to 5000.0. This attribute is read-only should only be set in the initialization.

bind_id int

The unique identifier of the participant that the sensor is bound to.

surface Surface

The rendering surface of the sensor. This attribute is read-only.

heading float

The heading of the LiDAR. This attribute is read-only.

position Point

The position of the LiDAR. This attribute is read-only.

max_perception_distance float

The maximum detection range of the LiDAR. This attribute is read-only.

__init__(id_, map_, perception_range=12.0, freq_scan=10.0, freq_detect=5000.0, window_size=(200, 200), off_screen=True)

Initialize the single line lidar.

Parameters:

Name Type Description Default
id_ int

The unique identifier of the LiDAR.

required
map_ Map

The map that the LiDAR is attached to.

required
perception_range float

The distance from the LiDAR to its maximum detection range.

12.0
freq_scan float

The frequency of the LiDAR scanning a full round.

10.0
freq_detect float

The frequency of the LiDAR sending and receiving the signal.

5000.0
window_size Tuple[int, int]

The size of the rendering window.

(200, 200)
off_screen bool

Whether to render the LiDAR off screen.

True

get_observation()

Get the lidar points at current frame. The points are sorted counter clockwise. The points are given in the global coordinate system.

Returns:

Type Description
ndarray

The lidar points at current frame.

TopDownCamera

Bases: SensorBase

This class implements a pseudo camera with top-down view RGB semantic segmentation image.

Attributes:

Name Type Description
id_ int

The unique identifier of the camera.

map_ Map

The map that the camera is attached to.

perception_range Union[float, Tuple[float]]

The distance from the camera to its maximum detection range in (left, right, front, back). When this value is undefined, the camera is assumed to detect the whole map. Defaults to None.

window_size Tuple[int, int]

The size of the rendering window. Defaults to (200, 200).

off_screen bool

Whether to render the camera off screen. Defaults to True.

scale float

The scale of the rendering window.

bind_id int

The unique identifier of the participant that the sensor is bound to.

surface Surface

The rendering surface of the sensor. This attribute is read-only.

heading float

The heading of the camera. This attribute is read-only.

position Point

The position of the camera. This attribute is read-only.

max_perception_distance float

The maximum detection range of the camera. This attribute is read-only.

__init__(id_, map_, perception_range=None, window_size=(200, 200), off_screen=True)

Initialize the top-down camera.

Parameters:

Name Type Description Default
id_ int

The unique identifier of the camera.

required
map_ Map

The map that the camera is attached to.

required
perception_range Union[float, tuple]

The distance from the camera to its maximum detection range in (left, right, front, back). When this value is undefined, the camera is assumed to detect the whole map.

None
window_size Tuple[int, int]

The size of the rendering window.

(200, 200)
off_screen bool

Whether to render the camera off screen.

True

get_observation()

This function is used to get the observation of the camera from the viewpoint.

Returns:

Type Description
ndarray

The observation of the camera.

update(participants, participant_ids, frame=None, position=None, heading=None)

This function is used to update the camera's location and observation.

Parameters:

Name Type Description Default
participants _type_

The participants in the scenario.

required
participant_ids list

The ids of the participants in the scenario.

required
frame int

The frame of the scenario. If None, the camera will update to the current frame.

None
position Point

The position of the camera.

None
heading float

The heading of the camera.

None