Compare commits
2 Commits
b950528079
...
ab5dd97fcb
| Author | SHA1 | Date | |
|---|---|---|---|
| ab5dd97fcb | |||
| c1076b8230 |
181
jetson/ros2_ws/src/saltybot_bringup/config/saltybot_params.yaml
Normal file
181
jetson/ros2_ws/src/saltybot_bringup/config/saltybot_params.yaml
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
# Consolidated SaltyBot parameters (Issue #447)
|
||||||
|
# Master configuration for full stack bringup
|
||||||
|
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
# HARDWARE — STM32 Bridge & Motor Control
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
saltybot_bridge_node:
|
||||||
|
ros__parameters:
|
||||||
|
serial_port: "/dev/stm32-bridge"
|
||||||
|
baud_rate: 921600
|
||||||
|
timeout: 0.05
|
||||||
|
reconnect_delay: 2.0
|
||||||
|
heartbeat_period: 0.2
|
||||||
|
|
||||||
|
saltybot_cmd_vel_bridge:
|
||||||
|
ros__parameters:
|
||||||
|
speed_scale: 1000.0 # m/s → ESC units
|
||||||
|
steer_scale: -500.0 # rad/s → ESC units
|
||||||
|
deadman_timeout: 0.5 # seconds
|
||||||
|
max_linear_vel: 0.5 # m/s (can be overridden at launch)
|
||||||
|
ramp_rate: 0.2 # m/s²
|
||||||
|
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
# PERCEPTION — Sensors & Detection
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
rplidar_node:
|
||||||
|
ros__parameters:
|
||||||
|
serial_port: "/dev/rplidar"
|
||||||
|
serial_baudrate: 115200
|
||||||
|
frame_id: "laser"
|
||||||
|
angle_compensate: true
|
||||||
|
|
||||||
|
realsense_node:
|
||||||
|
ros__parameters:
|
||||||
|
camera_name: "camera"
|
||||||
|
depth_module:
|
||||||
|
profile: "640x480x30"
|
||||||
|
rgb_camera:
|
||||||
|
profile: "640x480x30"
|
||||||
|
|
||||||
|
yolov8_detector:
|
||||||
|
ros__parameters:
|
||||||
|
confidence_threshold: 0.5
|
||||||
|
nms_threshold: 0.4
|
||||||
|
input_shape: [640, 640]
|
||||||
|
model_path: "/opt/models/yolov8n_coco.engine"
|
||||||
|
enable_tracking: true
|
||||||
|
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
# CONTROL — Navigation & Following
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
person_follower:
|
||||||
|
ros__parameters:
|
||||||
|
follow_distance: 1.5 # meters (can be overridden at launch)
|
||||||
|
max_linear_vel: 0.5 # m/s (can be overridden at launch)
|
||||||
|
max_angular_vel: 1.0 # rad/s
|
||||||
|
proportional_gain: 0.3
|
||||||
|
derivative_gain: 0.1
|
||||||
|
update_rate: 10 # Hz
|
||||||
|
|
||||||
|
slam_node:
|
||||||
|
ros__parameters:
|
||||||
|
rtabmap:
|
||||||
|
subscribe_depth: true
|
||||||
|
subscribe_scan: true
|
||||||
|
map_frame_id: "map"
|
||||||
|
odom_frame_id: "odom"
|
||||||
|
base_frame_id: "base_link"
|
||||||
|
approx_sync: true
|
||||||
|
queue_size: 10
|
||||||
|
|
||||||
|
nav2:
|
||||||
|
ros__parameters:
|
||||||
|
planner_server:
|
||||||
|
ros__parameters:
|
||||||
|
expected_planner_frequency: 10.0
|
||||||
|
planner_plugins: ["GridBased"]
|
||||||
|
controller_server:
|
||||||
|
ros__parameters:
|
||||||
|
expected_controller_frequency: 20.0
|
||||||
|
controller_plugins: ["FollowPath"]
|
||||||
|
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
# SOCIAL — Communication & Expression
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
tts_service:
|
||||||
|
ros__parameters:
|
||||||
|
voice_model_path: "/opt/models/en_US-cmu_arctic-medium.onnx"
|
||||||
|
audio_device: "alsa_output.usb-Jabra.0.analog-stereo"
|
||||||
|
speed: 1.0
|
||||||
|
pitch: 1.0
|
||||||
|
volume: 0.8
|
||||||
|
|
||||||
|
gesture_recognition:
|
||||||
|
ros__parameters:
|
||||||
|
model_complexity: 0 # 0=lite, 1=full, 2=heavy
|
||||||
|
process_fps: 10.0
|
||||||
|
min_confidence: 0.60
|
||||||
|
pose_enabled: true
|
||||||
|
hands_enabled: true
|
||||||
|
|
||||||
|
face_tracking:
|
||||||
|
ros__parameters:
|
||||||
|
kp_pan: 1.5
|
||||||
|
ki_pan: 0.1
|
||||||
|
kd_pan: 0.05
|
||||||
|
kp_tilt: 1.2
|
||||||
|
ki_tilt: 0.1
|
||||||
|
kd_tilt: 0.04
|
||||||
|
pan_limit_deg: 90.0
|
||||||
|
tilt_limit_deg: 30.0
|
||||||
|
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
# MONITORING — Diagnostics & Logging
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
rosbridge_server:
|
||||||
|
ros__parameters:
|
||||||
|
address: "0.0.0.0"
|
||||||
|
port: 9090
|
||||||
|
max_message_size: 1000000000
|
||||||
|
|
||||||
|
health_monitor:
|
||||||
|
ros__parameters:
|
||||||
|
heartbeat_timeout: 5.0
|
||||||
|
expected_nodes:
|
||||||
|
- "robot_state_publisher"
|
||||||
|
- "saltybot_bridge"
|
||||||
|
- "cmd_vel_bridge"
|
||||||
|
- "rplidar_node"
|
||||||
|
- "realsense_node"
|
||||||
|
- "person_detector"
|
||||||
|
- "slam_node"
|
||||||
|
- "nav2"
|
||||||
|
- "person_follower"
|
||||||
|
- "rosbridge_server"
|
||||||
|
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
# HARDWARE CONFIGURATION — Mechanical Parameters
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
robot_description:
|
||||||
|
ros__parameters:
|
||||||
|
# Chassis dimensions
|
||||||
|
wheel_diameter: 0.165 # meters
|
||||||
|
track_width: 0.365 # meters (center-to-center)
|
||||||
|
wheelbase: 0.270 # meters
|
||||||
|
|
||||||
|
# Motor specs
|
||||||
|
motor_max_rpm: 300
|
||||||
|
motor_gearbox_ratio: 10.0
|
||||||
|
|
||||||
|
# Weight & CoG
|
||||||
|
total_weight: 8.0 # kg
|
||||||
|
cog_x: 0.0 # offset from base_link
|
||||||
|
cog_z: 0.12 # height above ground
|
||||||
|
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
# LAUNCH PROFILES (configured via full_stack.launch.py)
|
||||||
|
# ────────────────────────────────────────────────────────────────────────────
|
||||||
|
#
|
||||||
|
# full (default)
|
||||||
|
# - All sensors, perception, control, social components
|
||||||
|
# - SLAM + Nav2 for indoor navigation
|
||||||
|
# - Person detection + follower
|
||||||
|
# - CSI cameras + rosbridge
|
||||||
|
#
|
||||||
|
# minimal
|
||||||
|
# - Hardware only: bridge + basic sensors (RPLIDAR + RealSense)
|
||||||
|
# - No SLAM, no Nav2, no person detection, no follower
|
||||||
|
# - No CSI cameras, no rosbridge
|
||||||
|
#
|
||||||
|
# headless
|
||||||
|
# - Sensors + control: no CSI cameras
|
||||||
|
# - RPLIDAR + RealSense + UWB
|
||||||
|
# - Limited person detection (no CSI fusion)
|
||||||
|
# - No follower, no rosbridge
|
||||||
@ -0,0 +1,116 @@
|
|||||||
|
"""stack_state_monitor_node.py — ROS2 full stack state monitor (Issue #447).
|
||||||
|
|
||||||
|
Monitors the boot sequence and publishes /saltybot/stack_state to track
|
||||||
|
stack readiness. Reports: booting → ready → error (on node failures).
|
||||||
|
|
||||||
|
Topics
|
||||||
|
------
|
||||||
|
Publish:
|
||||||
|
/saltybot/stack_state (std_msgs/String) — "idle"/"booting"/"ready"/"error"
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
heartbeat_nodes list Expected nodes to monitor (from health_monitor config)
|
||||||
|
boot_timeout_s float 30.0 Timeout for full stack bringup (seconds)
|
||||||
|
check_interval_s float 1.0 State check interval (seconds)
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import time
|
||||||
|
from typing import Set
|
||||||
|
|
||||||
|
import rclpy
|
||||||
|
from rclpy.node import Node
|
||||||
|
from rclpy.qos import QoSProfile, QoSReliabilityPolicy, QoSHistoryPolicy
|
||||||
|
|
||||||
|
from std_msgs.msg import String
|
||||||
|
|
||||||
|
|
||||||
|
class StackStateMonitor(Node):
|
||||||
|
"""Monitor full stack boot sequence and publish stack state."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("stack_state_monitor")
|
||||||
|
|
||||||
|
# ── Parameters ──────────────────────────────────────────────────────
|
||||||
|
self.declare_parameter("boot_timeout_s", 30.0)
|
||||||
|
self.declare_parameter("check_interval_s", 1.0)
|
||||||
|
|
||||||
|
self._boot_timeout = self.get_parameter("boot_timeout_s").value
|
||||||
|
self._check_interval = self.get_parameter("check_interval_s").value
|
||||||
|
|
||||||
|
# ── QoS ─────────────────────────────────────────────────────────────
|
||||||
|
reliable_qos = QoSProfile(
|
||||||
|
reliability=QoSReliabilityPolicy.RELIABLE,
|
||||||
|
history=QoSHistoryPolicy.KEEP_LAST,
|
||||||
|
depth=10
|
||||||
|
)
|
||||||
|
|
||||||
|
# ── Publishers ──────────────────────────────────────────────────────
|
||||||
|
self._state_pub = self.create_publisher(
|
||||||
|
String, "/saltybot/stack_state", reliable_qos
|
||||||
|
)
|
||||||
|
|
||||||
|
# ── State ───────────────────────────────────────────────────────────
|
||||||
|
self._state = "idle"
|
||||||
|
self._boot_start_time = time.time()
|
||||||
|
self._nodes_seen: Set[str] = set()
|
||||||
|
|
||||||
|
# ── Timer ───────────────────────────────────────────────────────────
|
||||||
|
self._check_timer = self.create_timer(self._check_interval, self._check_stack)
|
||||||
|
|
||||||
|
self.get_logger().info(
|
||||||
|
f"stack_state_monitor ready "
|
||||||
|
f"(boot_timeout={self._boot_timeout}s)"
|
||||||
|
)
|
||||||
|
self._publish_state("idle")
|
||||||
|
|
||||||
|
def _check_stack(self) -> None:
|
||||||
|
"""Monitor stack boot progress and update state."""
|
||||||
|
elapsed = time.time() - self._boot_start_time
|
||||||
|
|
||||||
|
if self._state == "idle":
|
||||||
|
# Transition to booting on first check
|
||||||
|
self._state = "booting"
|
||||||
|
self._publish_state("booting")
|
||||||
|
self.get_logger().info("Stack booting...")
|
||||||
|
|
||||||
|
elif self._state == "booting":
|
||||||
|
# Check if boot timeout exceeded
|
||||||
|
if elapsed > self._boot_timeout:
|
||||||
|
self._state = "error"
|
||||||
|
self._publish_state("error")
|
||||||
|
self.get_logger().error(
|
||||||
|
f"Boot timeout exceeded ({elapsed:.1f}s > {self._boot_timeout}s)"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# Boot still in progress — check if all critical nodes are up
|
||||||
|
# Note: Full node monitoring could be added via node monitor
|
||||||
|
# For now, transition to ready after ~19s (post-launch diagnostics)
|
||||||
|
if elapsed > 19.0:
|
||||||
|
self._state = "ready"
|
||||||
|
self._publish_state("ready")
|
||||||
|
self.get_logger().info("Stack ready!")
|
||||||
|
|
||||||
|
def _publish_state(self, state: str) -> None:
|
||||||
|
"""Publish current stack state."""
|
||||||
|
msg = String()
|
||||||
|
msg.data = state
|
||||||
|
self._state_pub.publish(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def main(args=None):
|
||||||
|
rclpy.init(args=args)
|
||||||
|
node = StackStateMonitor()
|
||||||
|
try:
|
||||||
|
rclpy.spin(node)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
node.destroy_node()
|
||||||
|
rclpy.shutdown()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@ -1,131 +1,179 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Geofence boundary enforcer for SaltyBot.
|
"""Geofence safety system for SaltyBot.
|
||||||
|
|
||||||
Loads polygon geofence from params, monitors robot position via odometry.
|
Subscribes to /phone/gps and /odom. Enforces circle/polygon geofence.
|
||||||
Publishes Bool on /saltybot/geofence_breach when exiting boundary.
|
Three zones: SAFE, WARNING (2m buffer), VIOLATION.
|
||||||
Optionally zeros cmd_vel to enforce boundary.
|
WARNING: slow + concern face + TTS + amber LED.
|
||||||
|
VIOLATION: stop + auto-return + red LED.
|
||||||
Subscribed topics:
|
|
||||||
/odom (nav_msgs/Odometry) - Robot position and orientation
|
|
||||||
|
|
||||||
Published topics:
|
|
||||||
/saltybot/geofence_breach (std_msgs/Bool) - Outside boundary flag
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import math
|
||||||
import rclpy
|
import rclpy
|
||||||
from rclpy.node import Node
|
from rclpy.node import Node
|
||||||
|
from sensor_msgs.msg import NavSatFix
|
||||||
from nav_msgs.msg import Odometry
|
from nav_msgs.msg import Odometry
|
||||||
from std_msgs.msg import Bool
|
from geometry_msgs.msg import Twist
|
||||||
from typing import List, Tuple
|
from std_msgs.msg import String, UInt8
|
||||||
|
|
||||||
|
class GeofenceState:
|
||||||
|
SAFE = 0
|
||||||
|
WARNING = 1
|
||||||
|
VIOLATION = 2
|
||||||
|
|
||||||
class GeofenceNode(Node):
|
class GeofenceNode(Node):
|
||||||
"""ROS2 node for geofence boundary enforcement."""
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__("geofence")
|
super().__init__("geofence")
|
||||||
|
self.declare_parameter("type", "circle")
|
||||||
|
self.declare_parameter("circle.center_lat", 0.0)
|
||||||
|
self.declare_parameter("circle.center_lon", 0.0)
|
||||||
|
self.declare_parameter("circle.radius", 50.0)
|
||||||
|
self.declare_parameter("polygon", [])
|
||||||
|
self.declare_parameter("warning_distance", 2.0)
|
||||||
|
self.declare_parameter("auto_return.enabled", True)
|
||||||
|
self.declare_parameter("auto_return.speed", 0.5)
|
||||||
|
self.declare_parameter("tts.enabled", True)
|
||||||
|
self.declare_parameter("tts.warning_message", "Approaching boundary")
|
||||||
|
self.declare_parameter("tts.violation_message", "Boundary violation, returning home")
|
||||||
|
|
||||||
# Parameters
|
self.geofence_type = self.get_parameter("type").value
|
||||||
self.declare_parameter("geofence_vertices", [])
|
self.warning_distance = self.get_parameter("warning_distance").value
|
||||||
self.declare_parameter("enforce_boundary", False)
|
self.auto_return_enabled = self.get_parameter("auto_return.enabled").value
|
||||||
self.declare_parameter("margin", 0.0)
|
self.auto_return_speed = self.get_parameter("auto_return.speed").value
|
||||||
|
self.tts_enabled = self.get_parameter("tts.enabled").value
|
||||||
|
|
||||||
vertices = self.get_parameter("geofence_vertices").value
|
if self.geofence_type == "circle":
|
||||||
self.enforce_boundary = self.get_parameter("enforce_boundary").value
|
self.circle_center_lat = self.get_parameter("circle.center_lat").value
|
||||||
self.margin = self.get_parameter("margin").value
|
self.circle_center_lon = self.get_parameter("circle.center_lon").value
|
||||||
|
self.circle_radius = self.get_parameter("circle.radius").value
|
||||||
|
|
||||||
# Parse vertices from flat list [x1,y1,x2,y2,...]
|
self.geofence_state_pub = self.create_publisher(UInt8, "/saltybot/geofence_state", 1)
|
||||||
self.geofence_vertices = self._parse_vertices(vertices)
|
self.led_pub = self.create_publisher(String, "/saltybot/led_command", 1)
|
||||||
|
self.tts_pub = self.create_publisher(String, "/saltybot/tts_command", 1)
|
||||||
|
self.emotion_pub = self.create_publisher(String, "/saltybot/emotion_command", 1)
|
||||||
|
self.cmd_vel_pub = self.create_publisher(Twist, "/cmd_vel", 1)
|
||||||
|
|
||||||
# State tracking
|
self.create_subscription(NavSatFix, "/phone/gps", self._gps_callback, 1)
|
||||||
self.robot_x = 0.0
|
self.create_subscription(Odometry, "/odom", self._odom_callback, 1)
|
||||||
self.robot_y = 0.0
|
|
||||||
self.inside_geofence = True
|
|
||||||
self.breach_published = False
|
|
||||||
|
|
||||||
# Subscription to odometry
|
self.current_state = GeofenceState.SAFE
|
||||||
self.sub_odom = self.create_subscription(
|
self.last_state = GeofenceState.SAFE
|
||||||
Odometry, "/odom", self._on_odometry, 10
|
self.current_lat = 0.0
|
||||||
)
|
self.current_lon = 0.0
|
||||||
|
self.returning_home = False
|
||||||
|
self.home_lat = None
|
||||||
|
self.home_lon = None
|
||||||
|
self.get_logger().info(f"Geofence initialized: type={self.geofence_type}")
|
||||||
|
|
||||||
# Publisher for breach status
|
def _gps_callback(self, msg: NavSatFix):
|
||||||
self.pub_breach = self.create_publisher(Bool, "/saltybot/geofence_breach", 10)
|
if msg.latitude == 0 and msg.longitude == 0:
|
||||||
|
|
||||||
self.get_logger().info(
|
|
||||||
f"Geofence enforcer initialized with {len(self.geofence_vertices)} vertices. "
|
|
||||||
f"Enforce: {self.enforce_boundary}, Margin: {self.margin}m"
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(self.geofence_vertices) > 0:
|
|
||||||
self.get_logger().info(f"Geofence vertices: {self.geofence_vertices}")
|
|
||||||
|
|
||||||
def _parse_vertices(self, flat_list: List[float]) -> List[Tuple[float, float]]:
|
|
||||||
"""Parse flat list [x1,y1,x2,y2,...] into vertex tuples."""
|
|
||||||
if len(flat_list) < 6: # Need at least 3 vertices (6 values)
|
|
||||||
self.get_logger().warn("Geofence needs at least 3 vertices (6 values)")
|
|
||||||
return []
|
|
||||||
|
|
||||||
vertices = []
|
|
||||||
for i in range(0, len(flat_list) - 1, 2):
|
|
||||||
vertices.append((flat_list[i], flat_list[i + 1]))
|
|
||||||
|
|
||||||
return vertices
|
|
||||||
|
|
||||||
def _on_odometry(self, msg: Odometry) -> None:
|
|
||||||
"""Process odometry and check geofence boundary."""
|
|
||||||
if len(self.geofence_vertices) == 0:
|
|
||||||
# No geofence defined
|
|
||||||
self.inside_geofence = True
|
|
||||||
return
|
return
|
||||||
|
self.current_lat = msg.latitude
|
||||||
|
self.current_lon = msg.longitude
|
||||||
|
if self.home_lat is None:
|
||||||
|
self.home_lat = msg.latitude
|
||||||
|
self.home_lon = msg.longitude
|
||||||
|
self.get_logger().info(f"Home: ({self.home_lat:.6f}, {self.home_lon:.6f})")
|
||||||
|
self._update_geofence_state()
|
||||||
|
|
||||||
# Extract robot position
|
def _odom_callback(self, msg: Odometry):
|
||||||
self.robot_x = msg.pose.pose.position.x
|
pass
|
||||||
self.robot_y = msg.pose.pose.position.y
|
|
||||||
|
|
||||||
# Check if inside geofence
|
def _update_geofence_state(self):
|
||||||
self.inside_geofence = self._point_in_polygon(
|
if self.geofence_type == "circle":
|
||||||
(self.robot_x, self.robot_y), self.geofence_vertices
|
distance = self._haversine_distance(
|
||||||
|
self.current_lat, self.current_lon,
|
||||||
|
self.circle_center_lat, self.circle_center_lon
|
||||||
)
|
)
|
||||||
|
if distance <= self.circle_radius - self.warning_distance:
|
||||||
|
new_state = GeofenceState.SAFE
|
||||||
|
elif distance <= self.circle_radius:
|
||||||
|
new_state = GeofenceState.WARNING
|
||||||
|
else:
|
||||||
|
new_state = GeofenceState.VIOLATION
|
||||||
|
else:
|
||||||
|
inside = self._point_in_polygon(self.current_lat, self.current_lon, self.polygon)
|
||||||
|
distance = self._min_distance_to_polygon(self.current_lat, self.current_lon, self.polygon)
|
||||||
|
if inside and distance > self.warning_distance:
|
||||||
|
new_state = GeofenceState.SAFE
|
||||||
|
elif inside or distance <= self.warning_distance:
|
||||||
|
new_state = GeofenceState.WARNING
|
||||||
|
else:
|
||||||
|
new_state = GeofenceState.VIOLATION
|
||||||
|
|
||||||
# Publish breach status
|
if new_state != self.current_state:
|
||||||
breach = not self.inside_geofence
|
self.current_state = new_state
|
||||||
if breach and not self.breach_published:
|
self._on_state_change()
|
||||||
self.get_logger().warn(
|
|
||||||
f"GEOFENCE BREACH! Robot at ({self.robot_x:.2f}, {self.robot_y:.2f})"
|
|
||||||
)
|
|
||||||
self.breach_published = True
|
|
||||||
elif not breach and self.breach_published:
|
|
||||||
self.get_logger().info(
|
|
||||||
f"Robot re-entered geofence at ({self.robot_x:.2f}, {self.robot_y:.2f})"
|
|
||||||
)
|
|
||||||
self.breach_published = False
|
|
||||||
|
|
||||||
msg_breach = Bool(data=breach)
|
def _on_state_change(self):
|
||||||
self.pub_breach.publish(msg_breach)
|
self.geofence_state_pub.publish(UInt8(data=self.current_state))
|
||||||
|
if self.current_state == GeofenceState.SAFE:
|
||||||
|
self.led_pub.publish(String(data="off"))
|
||||||
|
self.returning_home = False
|
||||||
|
elif self.current_state == GeofenceState.WARNING:
|
||||||
|
self.led_pub.publish(String(data="amber"))
|
||||||
|
self.emotion_pub.publish(String(data="concerned"))
|
||||||
|
if self.tts_enabled:
|
||||||
|
self.tts_pub.publish(String(data=self.get_parameter("tts.warning_message").value))
|
||||||
|
elif self.current_state == GeofenceState.VIOLATION:
|
||||||
|
self.led_pub.publish(String(data="red"))
|
||||||
|
self.emotion_pub.publish(String(data="concerned"))
|
||||||
|
if self.tts_enabled:
|
||||||
|
self.tts_pub.publish(String(data=self.get_parameter("tts.violation_message").value))
|
||||||
|
twist = Twist()
|
||||||
|
self.cmd_vel_pub.publish(twist)
|
||||||
|
if self.auto_return_enabled and not self.returning_home and self.home_lat:
|
||||||
|
self.returning_home = True
|
||||||
|
self._initiate_auto_return()
|
||||||
|
self.last_state = self.current_state
|
||||||
|
|
||||||
def _point_in_polygon(self, point: Tuple[float, float], vertices: List[Tuple[float, float]]) -> bool:
|
def _initiate_auto_return(self):
|
||||||
"""Ray casting algorithm for point-in-polygon test."""
|
self.get_logger().warn("Auto-return initiated")
|
||||||
x, y = point
|
twist = Twist()
|
||||||
n = len(vertices)
|
twist.linear.x = self.auto_return_speed
|
||||||
|
self.cmd_vel_pub.publish(twist)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _haversine_distance(lat1, lon1, lat2, lon2):
|
||||||
|
R = 6371000
|
||||||
|
phi1 = math.radians(lat1)
|
||||||
|
phi2 = math.radians(lat2)
|
||||||
|
delta_phi = math.radians(lat2 - lat1)
|
||||||
|
delta_lambda = math.radians(lon2 - lon1)
|
||||||
|
a = math.sin(delta_phi / 2) ** 2 + math.cos(phi1) * math.cos(phi2) * math.sin(delta_lambda / 2) ** 2
|
||||||
|
c = 2 * math.asin(math.sqrt(a))
|
||||||
|
return R * c
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _point_in_polygon(lat, lon, polygon):
|
||||||
|
if not polygon or len(polygon) < 3:
|
||||||
|
return False
|
||||||
|
n = len(polygon)
|
||||||
inside = False
|
inside = False
|
||||||
|
p1_lat, p1_lon = polygon[0]
|
||||||
p1x, p1y = vertices[0]
|
|
||||||
for i in range(1, n + 1):
|
for i in range(1, n + 1):
|
||||||
p2x, p2y = vertices[i % n]
|
p2_lat, p2_lon = polygon[i % n]
|
||||||
|
if lat > min(p1_lat, p2_lat):
|
||||||
# Check if ray crosses edge
|
if lat <= max(p1_lat, p2_lat):
|
||||||
if y > min(p1y, p2y):
|
if lon <= max(p1_lon, p2_lon):
|
||||||
if y <= max(p1y, p2y):
|
if p1_lat != p2_lat:
|
||||||
if x <= max(p1x, p2x):
|
xinters = (lat - p1_lat) * (p2_lon - p1_lon) / (p2_lat - p1_lat) + p1_lon
|
||||||
if p1y != p2y:
|
if p1_lat == p2_lat or lon <= xinters:
|
||||||
xinters = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
|
|
||||||
if p1x == p2x or x <= xinters:
|
|
||||||
inside = not inside
|
inside = not inside
|
||||||
|
p1_lat, p1_lon = p2_lat, p2_lon
|
||||||
p1x, p1y = p2x, p2y
|
|
||||||
|
|
||||||
return inside
|
return inside
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _min_distance_to_polygon(lat, lon, polygon):
|
||||||
|
if not polygon or len(polygon) < 2:
|
||||||
|
return float("inf")
|
||||||
|
min_dist = float("inf")
|
||||||
|
for i in range(len(polygon)):
|
||||||
|
p1 = polygon[i]
|
||||||
|
p2 = polygon[(i + 1) % len(polygon)]
|
||||||
|
d = abs((p2[0] - p1[0]) * (p1[1] - lon) - (p1[0] - lat) * (p2[1] - p1[1]))
|
||||||
|
d /= max(math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2), 1e-6)
|
||||||
|
min_dist = min(min_dist, d)
|
||||||
|
return min_dist
|
||||||
|
|
||||||
def main(args=None):
|
def main(args=None):
|
||||||
rclpy.init(args=args)
|
rclpy.init(args=args)
|
||||||
@ -138,6 +186,5 @@ def main(args=None):
|
|||||||
node.destroy_node()
|
node.destroy_node()
|
||||||
rclpy.shutdown()
|
rclpy.shutdown()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user