You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python获取Pepper机器人前后左右方向的障碍物距离

Hey there! I’ve spent quite a bit of time working on Pepper’s navigation and obstacle detection, so I can walk you through exactly how to get those distance values for front, left, right, and rear obstacles. Pepper uses two main sensor systems for this: short-range ultrasonic proximity sensors and a long-range laser radar. Let’s break down both approaches.

Using Pepper's Sensors to Get Obstacle Distances

1. First: Connect to Pepper's NAOqi Session

Before accessing any sensors, you need to establish a connection to your Pepper robot via the NAOqi framework—this is the backbone of all its functionality. Here’s the basic Python code to set this up:

import qi

# Initialize the session
session = qi.Session()
try:
    # Replace with your Pepper's IP address (default port is 9559)
    session.connect("tcp://192.168.1.100:9559")
    print("Connected to Pepper successfully!")
except RuntimeError as e:
    print(f"Failed to connect: {e}")

2. Option 1: Short-Range Obstacle Detection (ALProximitySensor)

Pepper has ultrasonic proximity sensors on its head and body, ideal for detecting close-by obstacles (up to ~0.5 meters). You can directly query distance values for specific directions using the ALProximitySensor service.

Code Example

# Get the proximity sensor service proxy
prox_service = session.service("ALProximitySensor")

# Subscribe to the sensor to receive live data
prox_service.subscribe("MyProximitySubscriber")

# Fetch distance values for each direction
front_distance = prox_service.getDistance("Front")
left_distance = prox_service.getDistance("Left")
right_distance = prox_service.getDistance("Right")
back_distance = prox_service.getDistance("Back")

# Print normalized values
print(f"Front obstacle distance: {front_distance:.2f} (normalized)")
print(f"Left obstacle distance: {left_distance:.2f} (normalized)")
print(f"Right obstacle distance: {right_distance:.2f} (normalized)")
print(f"Back obstacle distance: {back_distance:.2f} (normalized)")

# Unsubscribe to free up robot resources
prox_service.unsubscribe("MyProximitySubscriber")

Key Notes

  • Values are normalized between 0 and 1: 0 means no obstacle at maximum range, 1 means an obstacle is at the minimum detectable distance (~0.1 meters).
  • To get actual meters, map the normalized value to the sensor’s physical range (e.g., actual_front_dist = front_distance * 0.5 for 0 to 0.5 meters).

3. Option 2: Long-Range Obstacle Detection (ALLaserSensor)

For navigation tasks requiring detection of obstacles farther away (up to ~5 meters), Pepper’s laser radar is the better choice. The ALLaserSensor returns a full 360-degree scan of distance points—you’ll just filter the data to target specific directions.

Code Example

import math

# Get the laser sensor service proxy
laser_service = session.service("ALLaserSensor")

# Subscribe to start receiving laser scan data
laser_service.subscribe("MyLaserSubscriber")

# Fetch full scan data (list of [angle_radians, distance_meters] pairs)
laser_data = laser_service.getLaserData()

# Helper function to filter distances for a target direction
def get_direction_distance(laser_data, target_angle_deg, angle_tolerance_deg=5):
    target_angle_rad = math.radians(target_angle_deg)
    tolerance_rad = math.radians(angle_tolerance_deg)
    # Collect all distances within the specified angle window
    relevant_distances = [
        dist for angle, dist in laser_data
        if (target_angle_rad - tolerance_rad) <= angle <= (target_angle_rad + tolerance_rad)
    ]
    # Return closest obstacle distance, or infinity if none found
    return min(relevant_distances) if relevant_distances else float('inf')

# Get distances for each direction
front_dist = get_direction_distance(laser_data, 0)  # 0° = front
left_dist = get_direction_distance(laser_data, 90)  # 90° = left
right_dist = get_direction_distance(laser_data, -90) # -90° = right
back_dist = get_direction_distance(laser_data, 180) # 180° = back

# Print actual meter values
print(f"Front obstacle distance: {front_dist:.2f} meters")
print(f"Left obstacle distance: {left_dist:.2f} meters")
print(f"Right obstacle distance: {right_dist:.2f} meters")
print(f"Back obstacle distance: {back_dist:.2f} meters")

# Unsubscribe when finished
laser_service.unsubscribe("MyLaserSubscriber")

Key Notes

  • The laser returns distances in actual meters, perfect for navigation path planning.
  • Adjust angle_tolerance_deg to widen/narrow the scan slice for each direction (e.g., 5° means you’ll use the closest obstacle in a 10° window).
  • A return value of inf means no obstacle was detected in that direction.

Final Tips

  • Combine both sensors for robust navigation: use the laser for long-range path planning and proximity sensors for close-quarters emergency avoidance.
  • Always unsubscribe from sensors when done to prevent resource leaks on the robot.
  • Double-check that sensors aren’t physically blocked (e.g., by cables or stickers) before testing.

内容的提问来源于stack exchange,提问作者Firas Fredj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:27:14