irsim.world.sensors#

Sensor classes for IR-SIM simulation.

This package contains: - contact2d: 2D contact sensor (contact bookkeeping of one object) - fmcw_lidar2d: 2D FMCW LiDAR sensor implementation - lidar2d: 2D LiDAR sensor implementation - sensor_factory: Sensor factory for creating sensors

Submodules#

Classes#

Contact2D

Contact sensor of one object: what the contact step recorded about it

FMCWLidar2D

Simulate a 2D FMCW LiDAR using ray intersections plus Doppler projection.

Lidar2D

Simulates a 2D Lidar sensor for detecting obstacles in the environment.

SensorFactory

Factory for sensors declared in YAML object configurations.

Package Contents#

class irsim.world.sensors.Contact2D(state: numpy.ndarray | None = None, obj_id: int = 0, force_scale: float = 0.05, marker_size: float = 6.0, linewidth: float = 1.5, alpha: float = 0.9, **kwargs)[源代码]#

Contact sensor of one object: what the contact step recorded about it (collision_mode: contact only), as a contact sensor mounted on the object reports.

Every object carries one as obj.contact, written by the environment and the contact solver, so its readings are always available: obj.contact.in_contact, partners, reports, force, contact_time, air_time, started and ended. Listing it under the object's sensors as type: 'contact2d' makes that one the object's sensor and draws it: a marker at each contact point and a line along the contact normal whose length is the contact force times force_scale.

参数:
  • state (np.ndarray) -- Initial state of the object [x, y, theta], kept for the common sensor interface; the readings do not depend on it.

  • obj_id (int) -- ID of the associated object.

  • force_scale (float) -- Length of the drawn force line per newton, in meters. Default 0.05.

  • marker_size (float) -- Size of the contact point markers. Default 6.

  • linewidth (float) -- Width of the force lines. Default 1.5.

  • alpha (float) -- Transparency of the drawing. Default 0.9.

  • **kwargs -- plot sub-dict (preferred) with the visualization options above and color (default: the palette marker color), plus the name / type key of the YAML entry.

变量:
  • sensor_type (str) -- "contact2d".

  • parent (ObjectBase) -- The object the sensor is mounted on.

  • in_contact (bool) -- Whether a contact was resolved against parent in the last step.

  • partners (list) -- The objects parent touched in the last step, each once.

  • records (list[Contact]) -- The contact records of the last step, in the solver's order.

  • force (np.ndarray) -- Net world-frame contact force (2, 1) on parent in the last step, in newtons.

  • contact_time (float) -- Seconds parent has been touching something.

  • air_time (float) -- Seconds parent has been free; one of the two timers runs while the other is held at zero.

  • started (bool) -- Whether the last step began a contact.

  • ended (bool) -- Whether the last step ended one.

sensor_type = 'contact2d'#
obj_id = 0#
parent: irsim.world.object_base.ObjectBase | None = None#
force_scale#
marker_size#
linewidth#
alpha#
color#
in_contact = False#
partners: list[Any] = []#
records: list[irsim.lib.algorithm.contact.Contact] = []#
force#
contact_time = 0.0#
air_time = 0.0#
started = False#
ended = False#
property reports: list[irsim.lib.algorithm.contact.ContactReport]#

Last step's contacts seen from parent's side.

clear() → None[源代码]#

Forget last step's contacts: flag, partners, records and force.

reset() → None[源代码]#

Forget everything, the timers included, as at the start of a run.

add(contact: irsim.lib.algorithm.contact.Contact) → None[源代码]#

Record a contact the solver resolved against parent.

add_force(force_xy: numpy.ndarray) → None[源代码]#

Accumulate a world-frame force (2,) or (2, 1) on parent.

tick(step_time: float) → None[源代码]#

Advance contact_time or air_time by one step and raise started or ended for a step that switched between them.

step(state: numpy.ndarray) → None[源代码]#

Keep the sensor state; the readings are written by the contact step.

plot(ax, state: numpy.ndarray | None = None, **kwargs) → None[源代码]#

Draw the contact points and force lines on a given axis.

Nothing is drawn on a 3D axis, since contacts are planar.

step_plot() → None[源代码]#

Move the markers and force lines to the last step's contacts.

plot_clear() → None[源代码]#

Remove the drawing from the axis.

class irsim.world.sensors.FMCWLidar2D(state: numpy.ndarray | None = None, obj_id: int = 0, motion_compensate: bool = False, velocity_noise_std: float = 0.0, **kwargs)[源代码]#

Bases: irsim.world.sensors.lidar2d.Lidar2D

Simulate a 2D FMCW LiDAR using ray intersections plus Doppler projection.

This sensor keeps the geometric scanning model of Lidar2D but replaces the optional Cartesian target velocity output with a scalar radial velocity for each beam. The radial velocity is defined along the beam direction and can be reported either in the sensor-relative frame or motion-compensated to the world frame.

In addition to scan geometry parameters inherited from Lidar2D, this sensor accepts:

参数:
  • motion_compensate (bool) -- When False, report target velocity relative to the sensor motion. When True, remove the ego sensor motion and report world-frame radial target velocity.

  • velocity_noise_std (float) -- Standard deviation of Gaussian noise applied to radial velocity measurements.

  • velocity_color (bool) -- Whether to color beams by radial velocity when plotting.

  • velocity_color_max (float) -- Magnitude at which the plotting color saturates.

  • velocity_linewidth (float) -- Line width for valid Doppler beams.

  • no_hit_linewidth (float) -- Line width for beams without valid returns.

  • no_hit_alpha (float) -- Alpha for invalid beams in plots.

  • show_velocity_markers (bool) -- Whether to draw colored endpoint markers for valid returns.

  • velocity_marker_size (float) -- Scatter marker size for valid endpoints.

  • zero_velocity_color (str) -- Plot color used near zero radial velocity.

  • positive_velocity_color (str) -- Plot color used for positive radial velocity.

  • negative_velocity_color (str) -- Plot color used for negative radial velocity.

  • no_hit_color (str) -- Plot color used for invalid beams.

Initialize the Lidar2D sensor.

sensor_type = 'fmcw_lidar2d'#
motion_compensate = False#
velocity_noise_std = 0.0#
velocity_color#
velocity_color_max#
velocity_linewidth#
no_hit_linewidth#
no_hit_alpha#
show_velocity_markers#
velocity_marker_size#
velocity_marker_edge_color#
velocity_marker_edge_width#
zero_velocity_color#
positive_velocity_color#
negative_velocity_color#
no_hit_color#
radial_velocity#
valid#
step(state)[源代码]#

Update ranges, validity flags, and radial velocities for every beam.

Beams are cast against obstacle boundary segments in one vectorized pass (irsim.lib.algorithm.ray_casting_2d.cast_rays()), then each valid beam's hit object supplies the Doppler (radial) velocity.

get_scan()[源代码]#

Get the FMCW scan with radial velocity and validity per beam.

返回:

Scan dictionary containing the standard LiDAR angular metadata plus

ranges, radial_velocity, valid, and intensities=None.

返回类型:

dict

class irsim.world.sensors.Lidar2D(state: numpy.ndarray | None = None, obj_id: int = 0, range_min: float = 0, range_max: float = 10, angle_range: float = pi, number: int = 100, scan_time: float = 0.1, noise: bool = False, std: float = 0.2, angle_std: float = 0.0, offset: list[float] | None = None, alpha: float = 0.3, has_velocity: bool = False, **kwargs)[源代码]#

Simulates a 2D Lidar sensor for detecting obstacles in the environment.

参数:
  • state (np.ndarray) -- Initial state of the sensor.

  • obj_id (int) -- ID of the associated object.

  • range_min (float) -- Minimum detection range.

  • range_max (float) -- Maximum detection range.

  • angle_range (float) -- Total angle range of the sensor.

  • number (int) -- Number of laser beams.

  • scan_time (float) -- Time taken for one complete scan.

  • noise (bool) -- Whether noise is added to measurements.

  • std (float) -- Standard deviation for range noise.

  • angle_std (float) -- Standard deviation for angle noise.

  • offset (list) -- Offset of the sensor from the object's position.

  • alpha (float) -- Transparency for plotting.

  • has_velocity (bool) -- Whether the sensor measures velocity.

  • **kwargs -- Additional arguments. color (str): Color of the sensor.

Attr:
  • sensor_type (str): Type of sensor ("lidar2d"). Default is "lidar2d".

  • range_min (float): Minimum detection range in meters. Default is 0. A return closer than this is reported at range_min: the beam counts as blocked but carries no measurement.

  • range_max (float): Maximum detection range in meters. Default is 10.

  • angle_range (float): Total angle range of the sensor in radians. Default is pi. Clipped to [0, 2*pi].

  • angle_min (float): Starting angle of the sensor's scan relative to the forward direction in radians. Calculated as -angle_range / 2.

  • angle_max (float): Ending angle of the sensor's scan relative to the forward direction in radians. Calculated as angle_range / 2.

  • angle_inc (float): Angular increment between each laser beam in radians. Calculated as angle_range / (number - 1) when multiple beams are used. A single-beam sensor has no increment and points straight ahead.

  • number (int): Number of laser beams. Default is 100.

  • scan_time (float): Time taken to complete one full scan in seconds. Default is 0.1.

  • noise (bool): Whether to add noise to the measurements. Default is False.

  • std (float): Standard deviation for range noise in meters. Effective only if noise is True. Default is 0.2.

  • angle_std (float): Standard deviation of each beam's direction in radians, applied when noise is True. Default is 0, which keeps the directions exact.

  • offset (np.ndarray): Offset of the sensor relative to the object's position, formatted as [x, y, theta]. Default is [0, 0, 0].

  • lidar_origin (np.ndarray): Origin position of the Lidar sensor, considering offset and the object's state.

  • alpha (float): Transparency level for plotting the laser beams. Default is 0.3.

  • has_velocity (bool): Whether the sensor measures the velocity of detected points. Default is False.

  • velocity (np.ndarray): Velocity data for each laser beam, formatted as (2, number) array. Effective only if has_velocity is True. Initialized to zeros.

  • time_inc (float): Time increment for each scan, simulating the sensor's time resolution. Default is 5e-4.

  • range_data (np.ndarray): Array storing range data for each laser beam. Initialized to range_max for all beams.

  • valid (np.ndarray): Boolean per beam, True for a usable return: a hit whose range lies inside [range_min, range_max]. Misses and returns inside the blind zone are False (the latter read range_min).

  • angle_list (np.ndarray): Array of angles corresponding to each laser beam, distributed linearly from angle_min to angle_max.

  • color (str): Color of the sensor's representation in visualizations. Default is palette_param.lidar.

  • obj_id (int): ID of the associated object, used to differentiate between multiple sensors or objects in the environment. Default is 0.

  • plot_patch_list (list): List storing plot patches (e.g., line collections) for visualization purposes.

  • plot_line_list (list): List storing plot lines for visualization purposes.

  • plot_text_list (list): List storing plot text elements for visualization purposes.

Initialize the Lidar2D sensor.

sensor_type = 'lidar2d'#
range_min = 0#
range_max = 10#
angle_range = 0.0#
angle_min = -0.0#
angle_max = 0.0#
angle_inc = 0.0#
number = 100#
scan_time = 0.1#
noise = False#
std = 0.2#
angle_std = 0.0#
offset#
alpha#
has_velocity = False#
velocity#
time_inc = 0.0#
range_data#
valid#
angle_list#
color#
obj_id = 0#
parent: irsim.world.object_base.ObjectBase | None = None#
plot_patch_list = []#
plot_line_list = []#
plot_text_list = []#
init_geometry(state)[源代码]#

Initialize the Lidar's scanning geometry.

参数:

state (np.ndarray) -- Current state of the sensor.

step(state: numpy.ndarray) → None[源代码]#

Update the Lidar's state and compute per-beam ranges via ray casting.

Each beam is intersected analytically against the boundary segments of nearby obstacles (polygons, linestrings, and map segments); the nearest hit along the beam is its range. This reproduces the previous geometry difference result to floating-point precision when the sensor origin is in free space, while avoiding the expensive GEOS overlay.

参数:

state (np.ndarray) -- New state of the sensor.

property cast_angles: numpy.ndarray#

Per-beam angles the last scan was cast along, in the sensor frame.

Equal to angle_list unless angle noise is on, in which case each beam carries that step's jitter. Consumers that pair a measured range with a direction (fog reveal, drawn beams) use these; the scan itself reports the nominal angle_list, as a real sensor does.

get_scan()[源代码]#

Get the 2D lidar scan data. refer to the ros topic scan: http://docs.ros.org/en/melodic/api/sensor_msgs/html/msg/LaserScan.html

返回:

Scan data including angles, ranges, and velocities.

返回类型:

dict

get_points()[源代码]#

Convert scan data to a point cloud.

返回:

Point cloud (2xN).

返回类型:

np.ndarray

get_offset()[源代码]#

Get the sensor's offset.

返回:

Offset as a list.

返回类型:

list

plot(ax, state: numpy.ndarray | None = None, **kwargs)[源代码]#

Plot the Lidar's detected lines on a given axis.

property state: numpy.ndarray#

Get the current state of the lidar sensor.

返回:

Current state of the sensor.

返回类型:

np.ndarray

step_plot()[源代码]#

Public method to update the lidar visualization, calls _step_plot.

set_laser_color(laser_indices, laser_color: str | None = None, alpha: float = 0.3)[源代码]#

Set a specific color of the selected lasers.

参数:
  • laser_indices (list) -- The indices of the lasers to set the color.

  • laser_color (str) -- The color to set the selected lasers. Default is palette_param.laser_highlight.

  • alpha (float) -- The transparency of the lasers. Default is 0.3.

plot_clear()[源代码]#

Clear the plot elements from the axis.

scan_to_pointcloud()[源代码]#

Convert the Lidar scan data to a point cloud.

Only beams with a usable return (valid) become points, at their nominal angles; misses, returns inside the blind zone and returns pushed out of band by noise are left out.

返回:

Point cloud (2xN), or None when no beam is valid.

返回类型:

np.ndarray

class irsim.world.sensors.SensorFactory[源代码]#

Factory for sensors declared in YAML object configurations.

The factory reads the name or type key from a sensor dictionary and creates the matching concrete sensor class. Currently supported names are "lidar2d", "fmcw_lidar2d" and "contact2d".

create_sensor(state: numpy.ndarray, obj_id: int, **kwargs: Any) → Any[源代码]#

Create a sensor instance from configuration kwargs.

参数:
  • state (np.ndarray) -- Initial sensor state.

  • obj_id (int) -- Associated object id.

  • **kwargs -- Sensor configuration; expects 'name' or 'type'.

返回:

A concrete sensor instance (e.g., Lidar2D).

返回类型:

Any

抛出:

NotImplementedError -- If the requested sensor type is not supported.