AddDangerousArea
服务类型: map_manager/srv/AddDangerousArea
服务名: ${MM}/add_dangerous_area
描述
添加限速区(危险区域)到指定楼层。限速区是一个多边形区域,机器人在此区域内需要降低速 度。 与禁区不同,限速区允许机器人通过,但会限制其最大速度。
请求 (Request)
| 字段 | 类型 | 描述 |
|---|---|---|
| floor_id | string | 楼层 ID(必需) |
| area | map_manager/msg/DangerousArea | 限速区数据(必须包含 id, boundary, speed_limit) |
响应 (Response)
| 字段 | 类型 | 描述 |
|---|---|---|
| success | bool | 操作是否成功 |
| message | string | 结果消息或错误信息 |
| area_id | int32 | 添加的限速区 ID |
使用示例
命令行调用
Bash
# 添加矩形限速区,限速 0.5 m/s
ros2 service call ${MM}/add_dangerous_area map_manager/srv/AddDangerousArea \
"{floor_id: '1F', area: {id: 1, boundary: {points: [{x: 0.0, y: 0.0, z: 0.0}, {x: 3.0, y: 0.0, z: 0.0}, {x: 3.0, y: 3.0, z: 0.0}, {x: 0.0, y: 3.0, z: 0.0}]}, speed_limit: 0.5}}"
# 添加走廊限速区,限速 0.3 m/s
ros2 service call ${MM}/add_dangerous_area map_manager/srv/AddDangerousArea \
"{floor_id: '1F', area: {id: 2, boundary: {points: [{x: 10.0, y: 0.0, z: 0.0}, {x: 15.0, y: 0.0, z: 0.0}, {x: 15.0, y: 2.0, z: 0.0}, {x: 10.0, y: 2.0, z: 0.0}]}, speed_limit: 0.3}}"
Python 示例
Python
from map_manager.srv import AddDangerousArea
from map_manager.msg import DangerousArea
from geometry_msgs.msg import Polygon, Point32
import rclpy
from rclpy.node import Node
class DangerousAreaManager(Node):
def __init__(self):
super().__init__('dangerous_area_manager')
self.client = self.create_client(
AddDangerousArea, '/add_dangerous_area')
self.client.wait_for_service()
self.next_id = 1
def add_area(self, floor_id: str, points: list, speed_limit: float,
area_id: int = None):
if area_id is None:
area_id = self.next_id
self.next_id += 1
polygon = Polygon(points=[
Point32(x=float(p[0]), y=float(p[1]), z=0.0)
for p in points
])
request = AddDangerousArea.Request()
request.floor_id = floor_id
request.area = DangerousArea(
id=area_id,
boundary=polygon,
speed_limit=speed_limit
)
future = self.client.call_async(request)
rclpy.spin_until_future_complete(self, future)
result = future.result()
if result.success:
print(f"限速区添加成功: ID={result.area_id}, 限速={speed_limit} m/s")
else:
print(f"添加失败: {result.message}")
return result
def add_rectangle(self, floor_id: str, x1: float, y1: float,
x2: float, y2: float, speed_limit: float,
area_id: int = None):
"""添加矩形限速区"""
points = [(x1, y1), (x2, y1), (x2, y2), (x1, y2)]
return self.add_area(floor_id, points, speed_limit, area_id)
rclpy.init()
manager = DangerousAreaManager()
# 添加矩形限速区(0.5 m/s)
manager.add_rectangle('1F', 0.0, 0.0, 3.0, 3.0, speed_limit=0.5)
# 添加自定义多边形限速区
manager.add_area('1F',
[(5.0, 5.0), (8.0, 5.0), (8.0, 8.0), (5.0, 8.0)],
speed_limit=0.3)
rclpy.shutdown()