Does anyone have experience using proximity sensors? I’m trying to get a constant speed and distance data between two moving rovers. Or would this be better served with other types of sensors?
I do not, but my AI response indicates that depending on your specs for accuracy and precision, there are better options such as ToF, Lidar, RF and visual (ie, camera). (I know this is an AI dump, but perhaps it can continue the thread on this topic, especially if you could report any success.)
Setup (untested AI response)
To measure speed and distance between two rover vehicles using sensors like LiDAR, ToF, RF/UWB, or cameras, here’s a step-by-step coding and setup guide for the most typical and versatile sensor, LiDAR, which can be adapted similarly for other sensor types:
Step 1: Choose Your Sensor and Hardware Setup
- Mount the LiDAR (e.g., a 2D spinning LiDAR or a solid-state ToF LiDAR) securely on each rover.
- Connect the sensors to your rover’s microcontroller or onboard computer (e.g., Raspberry Pi, NVIDIA Jetson).
- Ensure power supplies and data connections (USB, UART, SPI, or Ethernet) are correct.
Step 2: Install Required Libraries and Drivers
For LiDAR, common software frameworks are ROS (Robot Operating System) or standalone Python libraries.
On your onboard computer, install ROS 2 or ROS 1 if using it:
sudo apt update
sudo apt install ros-foxy-desktop # Example for ROS2 Foxy on Ubuntu 20.04
Install sensor-specific drivers or SDKs, for example, for RPLIDAR use:
sudo apt install ros-foxy-rplidar-ros
Step 3: Basic Python Code Outline to Read LiDAR Data
Example using RPLIDAR with Python:
from rplidar import RPLidar
PORT_NAME = '/dev/ttyUSB0' # Change to your sensor's port
def run():
lidar = RPLidar(PORT_NAME)
try:
for scan in lidar.iter_scans():
# scan is a list of tuples: (quality, angle, distance)
distances = [point[2] for point in scan]
angles = [point[1] for point in scan]
# Process distance and angle data here
print(f'Scan data distances: {distances[:5]}...') # sample output
break
except KeyboardInterrupt:
print('Stopping.')
lidar.stop()
lidar.disconnect()
if __name__ == '__main__':
run()
Step 4: Calculating Distance and Speed Between Two Rovers
- Receive or log LiDAR point cloud data on each rover.
- Use relative positioning algorithms like scan matching or SLAM (Simultaneous Localization And Mapping) to locate the other rover from sensor data.
- Compute distance using the detected angle and range data.
Speed formula:
Speed = Distance ÷ Time
Or,
v = d / t
Where:
v = speed
d = distance traveled
t = time taken
Speed estimation between two distance measurements:
v = (d₂ − d₁) ÷ (t₂ − t₁)
Where:
d₁ = distance at time t₁
d₂ = distance at time t₂
v = average speed between t₁ and t₂
Distance from speed and time:
Distance = Speed × Time
Or,
d = v × t
Time from distance and speed:
Time = Distance ÷ Speed
Or,
t = d / v
Notes:
- Use consistent units (e.g., meters and seconds or kilometers and hours)
- To convert m/s to km/h multiply by 3.6
- To convert km/h to m/s divide by 3.6
Python
import time
class SpeedEstimator:
def init(self):
self.last_distance = None
self.last_time = None
def update(self, current_distance):
current_time = time.time() # Current time in seconds
if self.last_distance is None:
# First measurement, no speed yet
self.last_distance = current_distance
self.last_time = current_time
return 0.0
# Calculate time elapsed
dt = current_time - self.last_time
if dt == 0:
return 0.0 # Avoid division by zero
# Calculate speed = distance change / time change
speed = (current_distance - self.last_distance) / dt
# Update last measurements
self.last_distance = current_distance
self.last_time = current_time
return speed # Speed in units per second (same as distance units)
Example usage:
speed_estimator = SpeedEstimator()
while True:
# Replace this with actual sensor distance reading:
sensor_distance = get_distance_from_sensor() # user-defined function
estimated_speed = speed_estimator.update(sensor_distance)
print(f"Estimated speed: {estimated_speed:.2f} units/sec")
time.sleep(0.1) # sample sensor at 10Hz
Step 5: Advanced - Use ROS & SLAM for Robust Tracking
- Use ROS packages like
robot_localization,hector_slam, orgmappingfor each rover. - Exchange position data between rovers with communication (e.g., WiFi, Zigbee).
- Use the fusion of LiDAR scans and odometry to track distance and relative speed.
Step 6: Adapting ToF, RF/UWB, or Camera Sensors
- For ToF sensors: Use their SDK or I2C interface to get distance readings similar to LiDAR.
- For RF UWB: Use ranging modules like Decawave DWM1001; measure time-of-flight to compute distance, then track movement over time.
- For stereo or depth cameras: Use OpenCV or RealSense SDK to get depth data; detect the other rover and calculate relative distance and speed similarly with frame-to-frame differences.
To create a complete example project for two rovers using a LiDAR sensor to measure speed and distance between them, here is the essential Python code that integrates LiDAR data reading with speed estimation.
This example uses the popular Slamtec RPLIDAR and the rplidar Python package, which is commonly used for LiDAR integration on rovers.
Prerequisites
Install the required library:
pip install rplidar-roboticia
Complete Python Code Example
import time
from rplidar import RPLidar
class SpeedDistanceEstimator:
def __init__(self):
self.last_distance = None
self.last_time = None
def update_distance(self, current_distance):
current_time = time.time()
if self.last_distance is None:
self.last_distance = current_distance
self.last_time = current_time
return 0.0 # no speed on first reading
dt = current_time - self.last_time
if dt == 0:
return 0.0 # prevent division by zero
# Speed = change in distance / change in time
speed = (current_distance - self.last_distance) / dt
self.last_distance = current_distance
self.last_time = current_time
return speed
def get_distance_to_other_rover(scan_data, target_angle=0, angle_tolerance=5):
"""
Extract the distance measurement at a specific angle ± tolerance.
For simplicity, this example gets the average distance in that range.
Args:
scan_data: list of (quality, angle, distance) tuples from LiDAR scan.
target_angle: the relative angle (in degrees) where the other rover is expected.
angle_tolerance: degrees on either side to average distances.
Returns:
Average distance in target angle sector in meters or None if no data.
"""
distances = [point[2] for point in scan_data if
target_angle - angle_tolerance <= point[1] <= target_angle + angle_tolerance]
if distances:
# LiDAR distance is typically in millimeters; convert to meters
avg_distance_m = sum(distances) / len(distances) / 1000.0
return avg_distance_m
else:
return None
def main():
PORT_NAME = '/dev/ttyUSB0' # Change to correct port for your LiDAR
lidar = RPLidar(PORT_NAME)
estimator = SpeedDistanceEstimator()
print("Starting LiDAR scan for distance and speed estimation... Press Ctrl+C to stop.")
try:
for scan in lidar.iter_scans():
# Get distance measurement at 0 degrees (example facing direction)
distance = get_distance_to_other_rover(scan, target_angle=0, angle_tolerance=5)
if distance is not None:
speed = estimator.update_distance(distance)
print(f"Distance to other rover: {distance:.2f} m | Estimated speed: {speed:.2f} m/s")
else:
print("No distance data at target angle")
time.sleep(0.1) # adjust sampling rate as needed
except KeyboardInterrupt:
print("Stopping scan...")
lidar.stop()
lidar.disconnect()
if __name__ == "__main__":
main()
Explanation and Notes
- LiDAR setup: Connect your RPLidar device and set the
PORT_NAMEto your device port. - Scan data: Each scan gives a list of tuples
(quality, angle, distance). - Target angle: You specify at which relative angle to look for the other rover’s reflection (0 degrees means directly forward).
- Distance unit: RPLidar distances are in millimeters; converted to meters in code.
- Speed calculation: Uses time difference and distance change for speed estimation.
- Run continuously: This loops indefinitely until interrupted.
This example covers:
- Initializing and reading raw LiDAR scan data.
- Extracting a targeted distance measurement by angle.
- Calculating relative speed from distance changes.
- Basic error checking (no data at target angle).
If rovers communicate, you can sync the “target angle” dynamically or coordinate positions.
Citations:
[1] Using Sensors: LIDAR - Isaac Sim Documentation Using Sensors: LIDAR — Isaac Sim Documentation
[2] LiDAR SLAM, LOAM, LeGO-LOAM on ROS 2 - LearnOpenCV Introduction to LiDAR SLAM: LOAM & LeGO-LOAM - Paper & Code Explained with ROS 2
[3] Adding Lidar | Articulated Robotics Adding Lidar | Articulated Robotics
[4] How do we add LIDAR to a ROS robot? - YouTube https://www.youtube.com/watch?v=eJZXRncGaGM
[5] LiDAR integration with ROS: quickstart guide and projects ideas LiDAR integration with ROS: quickstart guide and projects ideas
[6] Amit10311/Codes-link-for-Lidar-and-Camera - GitHub GitHub - Amit10311/Codes-link-for-Lidar-and-Camera: Codes links for implementation on LiDar and Camera
[7] [PDF] Lidar Simulation for Robotic Application Development - iCAVE2 https://icave2.cse.buffalo.edu/resources/sensor-modeling/Lidar%20Simulation%20for%20Robotic%20Application.pdf
[8] RobotecAI/o3de-rgl-gem: Robotec GPU Lidar for O3DE - GitHub GitHub - RobotecAI/o3de-rgl-gem: Robotec GPU Lidar for O3DE
[9] LIDAR - Hardware — FIRST Robotics Competition documentation LIDAR - Hardware — FIRST Robotics Competition documentation
[10] DF Robot LIDAR Sensors - Getting Started with LIDAR DF Robot LIDAR Sensors - Getting Started with LIDAR | DroneBot Workshop
This might be of interest:
