Skip to main content

Simple library to communicate with Rd03D and HLK-LD2450 radar via serial on Raspberry Pi

Project description

Python RdLib Library for Rd03d and HLK-LD2450 for raspberrypi

For now library is using only serial communication with these radars. Might be necessary to set temporarily permission to the serial port:

  • sudo chmod 777 /dev/ttyS0

How to install that library?

  • pip install RdLib

Whole library is preconfigured to both radar. Just plug&play But to be specific here is documentation about implementation:

-Basic implementation without any configuration

from RdLib.Rd import Rd 
rd = Rd()

print(rd.get_angle())
    """
    Calculates the angle between the radar and the detected object in degrees.

    Returns:
        float: Angle in degrees.
    """

print(rd.get_coordinate())
    """
    Reads radar data and returns the 2D coordinates of the detected object.

    Returns:
        tuple: (x, y) coordinates in meters.
    """

print(rd.OutputDump())
    """
    Returns a full snapshot of radar data.

    Returned values:
        - x (float): X coordinate of the object
        - y (float): Y coordinate of the object
        - distance (float): Converted distance from the radar to the object
        - angle (float): Angle to the object in degrees
        - detection_mode (float): Current radar detection mode
        - raw_distance (float): Raw distance (before conversion)

    Returns:
        tuple: (x, y, distance, angle, detection_mode, raw_distance)
    """

print(rd.get_distance())
    """
    Reads radar data and calculates the distance to the detected object.

    Returns:
        float: Distance to the object in meters.
    """

print(rd.Kalman_Test(100,1,5,"Test2","Test2"))
     """
    Runs a Kalman filter test on distance measurements and visualizes the results.

    Parameters:
    - true_length (float): Expected distance in cm (used as a reference line on the chart).
    - measure_frequency (float): Delay between measurements in seconds.
    - quantity_measure (int): Number of total measurements.
    - chart_comment (str): Title of the chart.
    - file_name (str): Name used to save CSV and chart image.

    Behavior:
    - Collects distance measurements (raw and filtered).
    - Plots real-time updates of both raw and Kalman-filtered data.
    - Saves data to CSV and chart to PNG if configured to do so.
    """

print(rd.Calibrate_Kalman())
        """
    That function gives you raw data from radar compared to the filtered by kalman.
    Important you must turn on kalman by config.set(Kalman=True)

    Returns:
        tuple: A tuple containing two sub-tuples:
            - (distance_raw_x, distance_raw_y): Raw (unfiltered) distance values.
            - (distance_filtered_x, distance_filtered_y): Filtered distance values.
    """

-Advanced implementation with config. Example:

  from RdLib.Rd import Rd 
  from RdLib.config import config

  
  #Example of Entry Configuration
  config.set(Kalman_Save_csv=False)
  config.set(Kalman=True)
  config.set(distance_units="cm") 
  rd = Rd()

  print(rd.get_angle())
  print(rd.get_coordinate())
  print(rd.OutputDump())
  print(rd.get_distance())
  print(rd.Kalman_Test(100,1,5,"Test2","Test2"))
  print(rd.Calibrate_Kalman())

Description about all config settings:

    # If False, the Kalman filter is disabled in all types of measurements.
    # If True, the Kalman filter is enabled in all types of measurements.
    self.Kalman = False

    # Path where graphs from the Kalman test function will be saved.
    self.Kalman_Chart_Path = '~/Documents/Kalman_Test'

    # If False, the program will not save measurements to a CSV file at the path above.
    self.Kalman_Save_csv = False

    # Q matrix for the Kalman filter – adjusts filter responsiveness.
    self.Kalman_Q = np.diag([0.1, 0.1, 0.1, 0.1])

    # R matrix for the Kalman filter – represents measurement noise; higher values mean more trust in predictions than measurements.
    self.Kalman_R = np.diag([50, 50])

    # Path to the Raspberry Pi serial port.
    self.Serial_Port = '/dev/ttyS0'

    # Baud rate for communication with Rd03D and HLK-LD2450 radars.
    self.Serial_Speed = '256000'

    # Communication type: "Serial" or "Gpio" GPIO is not supported in that version
    self.ctype = "Serial"

    # Detection mode: 'S' for single target, 'M' for multiple targets. Multiple targets are not supported in that version
    self.Detection_Mode = 'S' 

    # Distance units: "in", "ft", "m", or "cm"
    self.distance_units = "m"

    # If True, enables more verbose output in the terminal for debugging purposes.
    self.debug = False

-You can fine-tune the radar behavior using the Kalman_Test() function, which helps visualize the impact of your settings with a chart. This is especially important to match your specific use case. Kalman_Q – This parameter controls the process noise. If you're tracking fast or unpredictable movements (e.g., people walking or running), increasing this value makes the filter more responsive to sudden changes. Default: 0.1 – a good universal value, but not optimal for all scenarios. Higher values → more responsive but noisier output. Lower values → smoother but slower response. Kalman_R – This defines the measurement noise. You can increase it if your radar gives very noisy readings, to make the filter rely more on predictions. Measurement frequency – Setting a higher frequency (i.e., faster sampling) increases the number of measurements per second. This gives more data but can also introduce more noise and interference. Since these budget radars are not highly precise, it's often better to use the lowest frequency that still meets your requirements.

  For that is usefull Kalman_Test()
  Example of using that function:
  
          from RdLib.Rd import Rd 
          from RdLib.config import config
          import numpy as np
          config.set(Kalman=True)
          self.Kalman_Chart_Path = 'YourPath' it will be easier if it is a folder only for measurements
          
          #rd.Kalman_Test(true_lenght,measure_frequency,quentity_measure,chart_comment,file_name)
          rd.Kalman_Test(100,1,5,"Test2","Test2")

-Chart example:

Chart_Example

Project details


Release history Release notifications | RSS feed

This version

0.9

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

RdLib-0.9.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

RdLib-0.9-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file RdLib-0.9.tar.gz.

File metadata

  • Download URL: RdLib-0.9.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for RdLib-0.9.tar.gz
Algorithm Hash digest
SHA256 18e33797815f520a73c7d07738b1b657ad53c41e05400ec068a8b0c8cc9f68ec
MD5 edf7c4affb57c7dbdd683fe68821b0e2
BLAKE2b-256 b91d24f76d828fa16d1197ee768a6f98552461f6ecf5bf48a7d5e17c0c03ae22

See more details on using hashes here.

File details

Details for the file RdLib-0.9-py3-none-any.whl.

File metadata

  • Download URL: RdLib-0.9-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for RdLib-0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 28f688ef8dbdbbb41be6f4f022f8923682a34c9c8efb30a4aaa3510a8eb26456
MD5 b22b2e2fe90fa97e15cfd88ad444f8b4
BLAKE2b-256 a46d3c5ac189349cd2b96e1141e767419fd30a4e993f0b1b9b1e3d655a037a1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page