AddForbiddenArea
Service type: map_manager/srv/AddForbiddenArea
Service name: ${MM}/add_forbidden_area
Description
Add a new forbidden area.
Request
| Field | Type | Description |
|---|---|---|
floor_id | string | Unique floor identifier. |
area | map_manager/msg/ForbiddenArea | Area data. |
Response
| Field | Type | Description |
|---|---|---|
success | bool | Whether the operation succeeded. |
message | string | Result details or an error message. |
area_id | int32 | Unique area identifier. |
wall_ids | int32[] | Identifiers of the generated virtual walls. |
Examples
Example 1
Bash
# Add a rectangular forbidden area
ros2 service call ${MM}/add_forbidden_area map_manager/srv/AddForbiddenArea \
"{floor_id: '1F', area: {id: 1, boundary: {points: [{x: 0.0, y: 0.0, z: 0.0}, {x: 2.0, y: 0.0, z: 0.0}, {x: 2.0, y: 2.0, z: 0.0}, {x: 0.0, y: 2.0, z: 0.0}]}}}"
# Add a triangular forbidden area
ros2 service call ${MM}/add_forbidden_area map_manager/srv/AddForbiddenArea \
"{floor_id: '1F', area: {id: 2, boundary: {points: [{x: 5.0, y: 5.0, z: 0.0}, {x: 7.0, y: 5.0, z: 0.0}, {x: 6.0, y: 7.0, z: 0.0}]}}}"
Example 2
Python
from map_manager.srv import AddForbiddenArea
from map_manager.msg import ForbiddenArea
from geometry_msgs.msg import Polygon, Point32
import rclpy
from rclpy.node import Node
import math
class ForbiddenAreaManager(Node):
def __init__(self):
super().__init__('forbidden_area_manager')
self.client = self.create_client(
AddForbiddenArea, '/add_forbidden_area')
self.client.wait_for_service()
self.next_id = 1
def add_area(self, floor_id: str, points: list, 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=float(p[2]) if len(p) > 2 else 0.0)
for p in points
])
request = AddForbiddenArea.Request()
request.floor_id = floor_id
request.area = ForbiddenArea(id=area_id, boundary=polygon)
future = self.client.call_async(request)
rclpy.spin_until_future_complete(self, future)
result = future.result()
if result.success:
print(f"Forbidden area added successfully: ID={result.area_id}")
print(f"Generated virtual walls: {list(result.wall_ids)}")
else:
print(f"Failed to add: {result.message}")
return result
def add_rectangle(self, floor_id: str, x1: float, y1: float,
x2: float, y2: float, area_id: int = None):
"""Add a rectangular forbidden area"""
points = [(x1, y1), (x2, y1), (x2, y2), (x1, y2)]
return self.add_area(floor_id, points, area_id)
def add_circle_approx(self, floor_id: str, cx: float, cy: float,
radius: float, segments: int = 16, area_id: int = None):
"""Add an approximately circular forbidden area"""
points = []
for i in range(segments):
angle = 2 * math.pi * i / segments
x = cx + radius * math.cos(angle)
y = cy + radius * math.sin(angle)
points.append((x, y))
return self.add_area(floor_id, points, area_id)
rclpy.init()
manager = ForbiddenAreaManager()
# Add a rectangular forbidden area
manager.add_rectangle('1F', 0.0, 0.0, 2.0, 2.0)
# Add a custom polygonal forbidden area
manager.add_area('1F', [(5.0, 5.0), (7.0, 5.0), (8.0, 7.0), (6.0, 8.0), (4.0, 7.0)])
# Add an approximately circular forbidden area
manager.add_circle_approx('1F', 10.0, 10.0, 1.5, segments=12)
rclpy.shutdown()
Example 3
C++
#include <rclcpp/rclcpp.hpp>
#include <map_manager/srv/add_forbidden_area.hpp>
#include <map_manager/msg/forbidden_area.hpp>
#include <geometry_msgs/msg/polygon.hpp>
class ForbiddenAreaManager : public rclcpp::Node {
public:
ForbiddenAreaManager() : Node("forbidden_area_manager"), next_id_(1) {
client_ = create_client<map_manager::srv::AddForbiddenArea>(
"/add_forbidden_area");
client_->wait_for_service();
}
bool add_rectangle(const std::string& floor_id,
double x1, double y1, double x2, double y2,
int area_id = -1) {
if (area_id < 0) area_id = next_id_++;
auto request = std::make_shared<map_manager::srv::AddForbiddenArea::Request>();
request->floor_id = floor_id;
request->area.id = area_id;
geometry_msgs::msg::Point32 p1, p2, p3, p4;
p1.x = x1; p1.y = y1; p1.z = 0;
p2.x = x2; p2.y = y1; p2.z = 0;
p3.x = x2; p3.y = y2; p3.z = 0;
p4.x = x1; p4.y = y2; p4.z = 0;
request->area.boundary.points = {p1, p2, p3, p4};
auto future = client_->async_send_request(request);
if (rclcpp::spin_until_future_complete(shared_from_this(), future) ==
rclcpp::FutureReturnCode::SUCCESS) {
auto result = future.get();
RCLCPP_INFO(get_logger(), "Forbidden area %s: ID=%d, generated %zu virtual walls",
result->success ? "Added successfully" : "Failed to add",
result->area_id, result->wall_ids.size());
return result->success;
}
return false;
}
private:
rclcpp::Client<map_manager::srv::AddForbiddenArea>::SharedPtr client_;
int next_id_;
};
Example 4
Text
Forbidden area (4 vertices) Four virtual walls are generated automatically
┌─────┐ wall_1: P0 → P1
│ │ → wall_2: P1 → P2
│ │ wall_3: P2 → P3
└─────┘ wall_4: P3 → P0
Costmap Effect
The forbidden area is represented in the costmap as follows:
- Boundary: lethal obstacles (
LETHAL_OBSTACLE) - Interior: filled with lethal obstacles
Notes
- List vertices in clockwise or counterclockwise order.
- Supply at least three vertices.
- Self-intersecting polygon edges are not supported.
- The costmap is updated immediately after the area is added.