Repository navigation
Expand file tree
/
Copy pathharrier_node.py
More file actions
112 lines (85 loc) · 3.56 KB
/
Copy pathharrier_node.py
File metadata and controls
112 lines (85 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""HARRIER ROS2 node skeleton.
Follows anima_module.yaml:
inputs: sensor_msgs/Image (EO + IR)
outputs: vision_msgs/Detection2DArray, diagnostic_msgs/DiagnosticArray
The ROS2 imports are deferred so this file is importable on hosts
without rclpy installed — HARRIER unit tests only exercise the handler
logic, which is kept as pure Python.
"""
from __future__ import annotations
import os
from dataclasses import dataclass
from pathlib import Path
from anima_harrier.data.indraeye import IndraEyeModality
from anima_harrier.inference import HarrierPredictor, PredictRequest, PredictResponse
@dataclass(frozen=True)
class RosTopicMap:
eo_image_topic: str = "/harrier/eo/image_raw"
ir_image_topic: str = "/harrier/ir/image_raw"
detections_topic: str = "/harrier/detections"
diagnostics_topic: str = "/harrier/diagnostics"
metadata_topic: str = "/harrier/metadata"
def resolve_weights_from_env() -> Path | None:
weights = os.environ.get("HARRIER_WEIGHTS")
if not weights:
return None
return Path(weights)
def build_predictor_from_env() -> HarrierPredictor | None:
weights = resolve_weights_from_env()
if weights is None:
return None
backend = os.environ.get("HARRIER_BACKEND", "auto")
return HarrierPredictor(weights=weights, backend=backend)
def handle_frame(
predictor: HarrierPredictor,
modality: IndraEyeModality,
source: str,
) -> PredictResponse:
"""Pure-Python frame handler — the ROS2 subscriber delegates here."""
return predictor.predict(PredictRequest(modality=modality, source=source))
# ---------------------------------------------------------------------------
# ROS2 lifecycle (imported lazily so non-ROS hosts can still load this file)
# ---------------------------------------------------------------------------
def main() -> None: # pragma: no cover - requires rclpy
import rclpy # type: ignore
from rclpy.node import Node # type: ignore
from sensor_msgs.msg import Image # type: ignore
predictor = build_predictor_from_env()
topic_map = RosTopicMap()
class HarrierNode(Node):
def __init__(self) -> None:
super().__init__("harrier_node")
self.get_logger().info("HARRIER EO/IR UAV node starting")
self.topic_map = topic_map
self.predictor = predictor
self.eo_sub = self.create_subscription(
Image, topic_map.eo_image_topic, self.on_eo_frame, 10
)
self.ir_sub = self.create_subscription(
Image, topic_map.ir_image_topic, self.on_ir_frame, 10
)
def on_eo_frame(self, msg: "Image") -> None:
self._handle(msg, IndraEyeModality.EO)
def on_ir_frame(self, msg: "Image") -> None:
self._handle(msg, IndraEyeModality.IR)
def _handle(self, msg: "Image", modality: IndraEyeModality) -> None:
if self.predictor is None:
self.get_logger().warn(
"HARRIER_WEIGHTS not set — frame dropped on %s" % modality.value
)
return
# In the real node we'd buffer the Image into a tensor; here
# we note the contract and leave the tensor adapter to the
# ROS2 bridge.
self.get_logger().debug(
"received %s frame stamped=%s" % (modality.value, msg.header.stamp)
)
rclpy.init()
node = HarrierNode()
try:
rclpy.spin(node)
finally:
node.destroy_node()
rclpy.shutdown()
if __name__ == "__main__": # pragma: no cover
main()