73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
"""Run map construction apart from the latency-sensitive control interpreter.
|
|
|
|
One outstanding observation, ordinary process IPC, unchanged ROS PointCloud2
|
|
and terrain implementation. All observation deadlines still belong to the
|
|
caller; completion never makes an old observation fresh.
|
|
"""
|
|
from concurrent.futures import ProcessPoolExecutor
|
|
import multiprocessing
|
|
import threading
|
|
import time
|
|
|
|
import rclpy
|
|
from rclpy.node import Node
|
|
|
|
from navigation.footprint import Footprint
|
|
from terrain_observation import TerrainObservation
|
|
|
|
_node = _thread = _terrain = None
|
|
|
|
|
|
def initialize(geometry, deceleration, robot_geometry, range_limits_m):
|
|
global _node, _thread, _terrain
|
|
rclpy.init()
|
|
_node = Node('missioncore_terrain_observation')
|
|
_terrain = TerrainObservation(_node, Footprint(geometry, deceleration=deceleration), robot_geometry,
|
|
range_limits_m=range_limits_m)
|
|
_thread = threading.Thread(target=rclpy.spin, args=(_node,), daemon=True)
|
|
_thread.start()
|
|
|
|
|
|
def ready():
|
|
deadline = time.monotonic()+8
|
|
while not (_terrain.scan.get_subscription_count() and _terrain.odom.get_subscription_count()
|
|
and _terrain.current_scan.get_subscription_count()):
|
|
if time.monotonic() >= deadline:
|
|
raise TimeoutError('Terrain observation subscriptions unavailable')
|
|
time.sleep(.02)
|
|
return True
|
|
|
|
|
|
def observe(value):
|
|
return _terrain.observe(value)
|
|
|
|
|
|
def close():
|
|
rclpy.try_shutdown()
|
|
_thread.join(timeout=2)
|
|
_node.destroy_node()
|
|
|
|
|
|
class TerrainProcess:
|
|
def __init__(self, geometry, deceleration, robot_geometry, *, range_limits_m=None):
|
|
self.pool = ProcessPoolExecutor(max_workers=1, mp_context=multiprocessing.get_context('spawn'),
|
|
initializer=initialize, initargs=(geometry, deceleration, robot_geometry, range_limits_m))
|
|
try:
|
|
self.pool.submit(ready).result(timeout=10)
|
|
except BaseException:
|
|
self.close()
|
|
raise
|
|
|
|
def observe(self, value):
|
|
# The enclosing single plan_lock prevents a sensor-frame queue.
|
|
return self.pool.submit(observe, value).result(timeout=2)
|
|
|
|
def close(self):
|
|
pool, self.pool = self.pool, None
|
|
if pool is None:
|
|
return
|
|
try:
|
|
pool.submit(close).result(timeout=3)
|
|
finally:
|
|
pool.shutdown(wait=True, cancel_futures=True)
|