Skip to main content
Lidar parts provide 2D laser scanning for obstacle detection, distance measurement, and SLAM mapping.

Lidar Drivers

RPLidar2

Adafruit RPLidar driver with filtering and batch processing. Constructor:
float
default:"0.0"
Minimum angle to include (degrees, 0-360)
float
default:"360.0"
Maximum angle to include (degrees, 0-360)
float
default:"sys.float_info.min"
Minimum distance to include (millimeters)
float
default:"sys.float_info.max"
Maximum distance to include (millimeters)
float
default:"0.0"
Angle that represents “forward” direction (degrees)
int
default:"CLOCKWISE"
Direction of angle increase: CLOCKWISE (1) or COUNTER_CLOCKWISE (-1)
float
default:"50"
Time in milliseconds to collect measurements in run() mode
bool
default:"false"
Enable debug logging
Methods:
() -> list
Collect measurements for batch_ms duration and return list of (distance, angle, time, scan, index) tuples
() -> list
Return most recent measurements from background thread
() -> None
Continuously poll lidar in background thread
() -> None
Stop lidar and release resources
Measurement Format: Each measurement is a tuple:
  • distance: Distance in millimeters (float)
  • angle: Angle in degrees (float), adjusted for forward_angle
  • timestamp: Time in seconds (float)
  • scan_number: Full scan sequence number (int)
  • index_in_scan: Index within current scan (int)
Performance:
  • Scan rate: ~7 scans/second
  • Measurement rate: ~1850 measurements/second
Installation:
Usage Example:

RPLidar (Legacy)

Simpler RPLidar interface (deprecated, use RPLidar2). Constructor:
float
default:"0"
Lower angle limit (degrees)
float
default:"360"
Upper angle limit (degrees)
Methods:
() -> np.ndarray
Returns sorted distances as NumPy array

YDLidar

YDLidar X4 driver. Constructor:
str
default:"/dev/ttyUSB0"
Serial port for lidar
Installation:
Methods:
() -> (distances, angles)
Returns tuple of (distance_list, angle_list)

Visualization

LidarPlot2

Plots lidar measurements as a polar plot image. Constructor:
tuple
default:"(500, 500)"
Output image size in pixels (width, height)
int
default:"PLOT_TYPE_CIRCLE"
PLOT_TYPE_CIRCLE or PLOT_TYPE_LINE
int
default:"3"
Size of measurement marks in pixels
float
default:"4000"
Maximum distance to plot (millimeters)
int
default:"COUNTER_CLOCKWISE"
Direction of increasing angles
float
default:"0"
Angle to rotate plot (degrees)
tuple
default:"(224, 224, 224)"
RGB background color
tuple
default:"(128, 128, 128)"
RGB border color
tuple
default:"(255, 64, 64)"
RGB measurement point color
Methods:
(measurements: list) -> PIL.Image
Convert measurements to polar plot image
Usage Example:

LidarPlot (Legacy)

Simpler plotting interface (deprecated, use LidarPlot2). Constructor:

SLAM Mapping

BreezySLAM

Simple SLAM implementation for map building. Constructor:
int
default:"500"
Map resolution in pixels
int
default:"10"
Map size in meters
Installation:
Methods:
(distances, angles, map_bytes) -> (x, y, theta)
Update SLAM with new scan and return position estimate
Usage Example:

BreezyMap

Map byte storage for BreezySLAM. Constructor:
Methods:
() -> bytearray
Return map bytes

MapToImage

Converts SLAM map bytes to PIL image. Constructor:
Methods:
(map_bytes: bytearray) -> PIL.Image
Convert map to image

Obstacle Detection

Simple Obstacle Detector

Configuration

Typical lidar configuration in myconfig.py:

Integration Example

Complete Lidar Setup

Troubleshooting

Lidar Not Found

Symptoms: RuntimeError: “No RPLidar is connected” Solutions:
  1. Check USB connection: ls /dev/ttyUSB*
  2. Verify permissions: sudo chmod 666 /dev/ttyUSB0
  3. Check power supply (lidar needs 5V)

Serial Port Conflicts

Symptoms: SerialException Solutions:
  1. Close other programs using the port
  2. Unplug and replug USB
  3. Reboot

Poor Quality Scans

Symptoms: Noisy or incomplete scans Solutions:
  1. Clean lidar lens
  2. Avoid reflective surfaces
  3. Check motor speed (should be steady)
  4. Reduce ambient light

Performance Tips

  1. Use threading: Always run lidar in threaded mode
  2. Filter data: Use angle and distance filters to reduce data
  3. Batch processing: Adjust batch_ms for your needs
  4. Visualization: Reduce plot resolution for better performance

Requirements