Files

74 lines
3.9 KiB
Python

"""Publish first-hit clearing rays and measured hazards to Nav2 ObstacleLayer.
Collision free space is not ground support or permission to drive. Those
independent gates keep the existing observed terrain and semantic evidence.
"""
import math
import struct
import numpy as np
from detector_exclusion import DetectorExclusion
from geometry_msgs.msg import TransformStamped
from sensor_msgs.msg import PointCloud2, PointField
def cloud(stamp, frame, points):
message = PointCloud2()
message.header.stamp, message.header.frame_id = stamp, frame
message.height, message.width = 1, len(points)
message.fields = [PointField(name=name, offset=4*i, datatype=PointField.FLOAT32, count=1)
for i, name in enumerate(('x','y','z'))]
message.is_bigendian, message.is_dense = False, True
message.point_step, message.row_step = 12, 12*len(points)
message.data = b''.join(struct.pack('<fff', float(p[0]), float(p[1]), 0.) for p in points)
return message
class ObservedCollision:
def __init__(self, node, max_step, origin_count):
self.node, self.max_step = node, max_step
self.detector_exclusion = DetectorExclusion()
self.rays = [node.create_publisher(PointCloud2, '/observed_rays_'+str(i), 2)
for i in range(origin_count)]
self.hazards = node.create_publisher(PointCloud2, '/observed_hazards', 2)
self.search = node.create_publisher(PointCloud2, '/observed_search_hazards', 2)
def publish(self, request, terrain, elapsed, *, search_terrain, detected=()):
rays = request['range_rays']
age = float(request['geometry_age_seconds'])+elapsed
if not math.isfinite(age) or age < 0 or len(rays['origins']) != len(self.rays):
raise ValueError('Invalid observed collision age/origins')
if age > .8:
return False
stamp = self.node.observation_stamp(request['simulation_time_seconds'], age)
transforms=[]
for i, origin in enumerate(rays['origins']):
tf = TransformStamped()
tf.header.frame_id, tf.child_frame_id, tf.header.stamp = 'map','range'+str(i),stamp
tf.transform.translation.x, tf.transform.translation.y = map(float, origin[:2])
tf.transform.rotation.w = 1.
transforms.append(tf)
self.node.tf.sendTransform(transforms)
for i, (publisher, origin) in enumerate(zip(self.rays, rays['origins'])):
points=[[end[0]-origin[0],end[1]-origin[1]]
for end, owner in zip(rays['ends'], rays['origin_indices']) if owner==i]
publisher.publish(cloud(stamp,'range'+str(i),points))
# The body collision map contains physical/detector obstacles. Ground
# permission and rejected support patches are search costs: treating
# them as solid objects blocks an overhanging camera during BackUp/Spin.
# Smac retains both; every native proposal still passes the separate
# full ground-contact/semantic gates before reaching the actuator.
hazards = terrain[terrain[:,3]>self.max_step+1e-4,:2]
search = search_terrain[search_terrain[:,3]>self.max_step+1e-4,:2]
reserve = self.detector_exclusion.points(detected, request['pose'])
# Reserve is a search constraint, not an observed solid object. It may
# already enclose the body when a new detection arrives. Making it
# lethal locally prevents native BackUp from leaving that reserve.
# Keep original returns in the body map; the independent exact sweep
# guard still enforces the reserve and forbids closer motion when the
# rover is already inside it.
detected_xy = np.asarray(detected, dtype=float).reshape(-1,3)[:,:2]
self.hazards.publish(cloud(stamp,'map',np.concatenate((hazards,detected_xy))))
self.search.publish(cloud(stamp,'map',np.concatenate((search,reserve))))
return True