# nav2_uwb_params.yaml — Nav2 configuration for UWB-based localization (Issue #599).
#
# Key differences from the AMCL/SLAM config (saltybot_nav2_slam):
#   • AMCL is NOT used.  The uwb_pose_bridge_node broadcasts map→odom TF directly
#     from the UWB-IMU EKF fused pose (/saltybot/pose/fused_cov).
#   • DWB controller capped at 1.0 m/s for safe differential-drive operation.
#   • Global costmap uses a rolling window — no pre-built static map required.
#     UWB provides the global (map-frame) position.
#   • Costmap obstacle layer: /scan (RPLIDAR A1/A2) only.
#   • lifecycle_manager_localization is intentionally empty; the bridge node
#     keeps the TF alive without Nav2 lifecycle management.

bt_navigator:
  ros__parameters:
    use_sim_time: false
    global_frame: map
    robot_base_frame: base_link
    odom_topic: /odom
    bt_loop_duration: 10
    default_server_timeout: 20
    enable_groot_monitoring: false
    default_nav_to_pose_bt_xml: >-
      $(find-pkg-share nav2_bt_navigator)/behavior_trees/navigate_to_pose_w_replanning_and_recovery.xml
    default_nav_through_poses_bt_xml: >-
      $(find-pkg-share nav2_bt_navigator)/behavior_trees/navigate_through_poses_w_replanning_and_recovery.xml
    plugin_lib_names:
      - nav2_compute_path_to_pose_action_bt_node
      - nav2_compute_path_through_poses_action_bt_node
      - nav2_follow_path_action_bt_node
      - nav2_spin_action_bt_node
      - nav2_wait_action_bt_node
      - nav2_back_up_action_bt_node
      - nav2_drive_on_heading_bt_node
      - nav2_clear_costmap_service_bt_node
      - nav2_is_stuck_condition_bt_node
      - nav2_goal_updated_condition_bt_node
      - nav2_initial_pose_received_condition_bt_node
      - nav2_rate_controller_bt_node
      - nav2_distance_controller_bt_node
      - nav2_speed_controller_bt_node
      - nav2_truncate_path_action_bt_node
      - nav2_remove_passed_goals_action_bt_node
      - nav2_planner_selector_bt_node
      - nav2_controller_selector_bt_node
      - nav2_goal_checker_selector_bt_node

bt_navigator_rclcpp_node:
  ros__parameters:
    use_sim_time: false

# ── Controller (DWB — differential drive, max 1.0 m/s) ─────────────────────────
controller_server:
  ros__parameters:
    use_sim_time: false
    controller_frequency: 20.0
    min_x_velocity_threshold: 0.001
    min_y_velocity_threshold: 0.5
    min_theta_velocity_threshold: 0.001
    failure_tolerance: 0.3
    progress_checker_plugin: "progress_checker"
    goal_checker_plugins: ["general_goal_checker"]
    controller_plugins: ["FollowPath"]

    progress_checker:
      plugin: "nav2_core::SimpleProgressChecker"
      required_movement_radius: 0.5
      movement_time_allowance: 15.0

    general_goal_checker:
      stateful: true
      plugin: "nav2_core::SimpleGoalChecker"
      xy_goal_tolerance: 0.20
      yaw_goal_tolerance: 0.15

    FollowPath:
      plugin: "dwb_core::DWBLocalPlanner"
      debug_trajectory_details: false
      # ── Velocity limits (1.0 m/s max per Issue #599) ──────────────────
      min_vel_x: 0.0
      max_vel_x: 1.0          # ← hard cap for safe indoor operation
      min_vel_y: 0.0
      max_vel_y: 0.0           # differential drive — no lateral motion
      min_vel_theta: -1.5
      max_vel_theta: 1.5
      min_speed_xy: 0.0
      max_speed_xy: 1.0
      min_speed_theta: 0.0
      # ── Acceleration limits ────────────────────────────────────────────
      acc_lim_x: 1.0
      acc_lim_y: 0.0
      acc_lim_theta: 2.0
      decel_lim_x: -1.0
      decel_lim_y: 0.0
      decel_lim_theta: -2.0
      # ── Trajectory sampling ────────────────────────────────────────────
      vx_samples: 20
      vy_samples: 1            # differential drive — only 1 lateral sample
      vtheta_samples: 40
      sim_time: 1.5
      linear_granularity: 0.05
      angular_granularity: 0.05
      transform_tolerance: 0.3
      trans_stopped_velocity: 0.25
      short_circuit_trajectory_evaluation: true
      stateful: true
      # ── Critic weights ─────────────────────────────────────────────────
      critics:
        - "RotateToGoal"
        - "Oscillation"
        - "BaseObstacle"
        - "GoalAlign"
        - "PathAlign"
        - "PathDist"
        - "GoalDist"
      BaseObstacle.scale: 0.02
      PathAlign.scale: 32.0
      PathAlign.forward_point_distance: 0.1
      GoalAlign.scale: 24.0
      GoalAlign.forward_point_distance: 0.1
      PathDist.scale: 32.0
      GoalDist.scale: 24.0
      RotateToGoal.scale: 32.0
      RotateToGoal.slowing_factor: 5.0
      RotateToGoal.lookahead_time: -1.0

controller_server_rclcpp_node:
  ros__parameters:
    use_sim_time: false

# ── Global planner (NavFn A*) ────────────────────────────────────────────────
planner_server:
  ros__parameters:
    use_sim_time: false
    expected_planner_frequency: 10.0
    planner_plugins: ["GridBased"]
    GridBased:
      plugin: "nav2_navfn_planner/NavfnPlanner"
      tolerance: 0.3
      use_astar: true
      allow_unknown: true

planner_server_rclcpp_node:
  ros__parameters:
    use_sim_time: false

# ── Recovery behaviors ───────────────────────────────────────────────────────
behavior_server:
  ros__parameters:
    use_sim_time: false
    costmap_topic: local_costmap/costmap_raw
    footprint_topic: local_costmap/published_footprint
    cycle_frequency: 10.0
    behavior_plugins: ["spin", "backup", "drive_on_heading", "wait"]
    spin:
      plugin: "nav2_behaviors/Spin"
    backup:
      plugin: "nav2_behaviors/BackUp"
    drive_on_heading:
      plugin: "nav2_behaviors/DriveOnHeading"
    wait:
      plugin: "nav2_behaviors/Wait"
    global_frame: odom
    robot_base_frame: base_link
    transform_timeout: 0.1
    simulate_ahead_time: 2.0
    max_rotations: 1.0
    max_backup_dist: 0.3
    backup_speed: 0.15

behavior_server_rclcpp_node:
  ros__parameters:
    use_sim_time: false

# ── Costmaps ─────────────────────────────────────────────────────────────────
#
# Both costmaps use only /scan (RPLIDAR).  No depth camera source here —
# saltybot_depth_costmap adds that layer separately when enabled.

local_costmap:
  local_costmap:
    ros__parameters:
      use_sim_time: false
      update_frequency: 10.0
      publish_frequency: 5.0
      global_frame: odom
      robot_base_frame: base_link
      rolling_window: true
      width: 4
      height: 4
      resolution: 0.05
      robot_radius: 0.35
      plugins: ["obstacle_layer", "inflation_layer"]
      obstacle_layer:
        plugin: "nav2_costmap_2d::ObstacleLayer"
        enabled: true
        observation_sources: scan
        scan:
          topic: /scan
          max_obstacle_height: 2.0
          min_obstacle_height: 0.0
          clearing: true
          marking: true
          data_type: "LaserScan"
          raytrace_max_range: 8.0
          raytrace_min_range: 0.0
          obstacle_max_range: 6.0
          obstacle_min_range: 0.0
      inflation_layer:
        plugin: "nav2_costmap_2d::InflationLayer"
        enabled: true
        inflation_radius: 0.55
        cost_scaling_factor: 10.0
        inflate_unknown: false
        inflate_around_unknown: true
      track_unknown_space: false
      always_send_full_costmap: true

  local_costmap_client:
    ros__parameters:
      use_sim_time: false

  local_costmap_rclcpp_node:
    ros__parameters:
      use_sim_time: false

global_costmap:
  global_costmap:
    ros__parameters:
      use_sim_time: false
      update_frequency: 2.0
      publish_frequency: 1.0
      global_frame: map
      robot_base_frame: base_link
      # Rolling window — UWB provides global position, no static map needed.
      rolling_window: true
      width: 20
      height: 20
      resolution: 0.05
      robot_radius: 0.35
      plugins: ["obstacle_layer", "inflation_layer"]
      obstacle_layer:
        plugin: "nav2_costmap_2d::ObstacleLayer"
        enabled: true
        observation_sources: scan
        scan:
          topic: /scan
          max_obstacle_height: 2.0
          min_obstacle_height: 0.0
          clearing: true
          marking: true
          data_type: "LaserScan"
          raytrace_max_range: 8.0
          raytrace_min_range: 0.0
          obstacle_max_range: 6.0
          obstacle_min_range: 0.0
      inflation_layer:
        plugin: "nav2_costmap_2d::InflationLayer"
        enabled: true
        inflation_radius: 0.55
        cost_scaling_factor: 10.0
        inflate_unknown: false
        inflate_around_unknown: true
      track_unknown_space: false
      always_send_full_costmap: false

  global_costmap_client:
    ros__parameters:
      use_sim_time: false

  global_costmap_rclcpp_node:
    ros__parameters:
      use_sim_time: false

# ── Velocity smoother ────────────────────────────────────────────────────────
velocity_smoother:
  ros__parameters:
    use_sim_time: false
    smoothing_frequency: 20.0
    scale_velocities: false
    feedback: "OPEN_LOOP"
    max_velocity: [1.0, 0.0, 1.5]     # [x, y, theta] — capped at 1.0 m/s
    min_velocity: [-0.3, 0.0, -1.5]
    max_accel: [1.0, 0.0, 2.0]
    max_decel: [-1.0, 0.0, -2.0]
    odom_topic: /odom
    odom_duration: 0.1
    deadband_velocity: [0.0, 0.0, 0.0]
    velocity_timeout: 1.0

# ── Lifecycle managers ───────────────────────────────────────────────────────
#
# Localization lifecycle is intentionally empty — the uwb_pose_bridge_node
# manages the map→odom TF independently (no AMCL, no SLAM Toolbox).

lifecycle_manager_navigation:
  ros__parameters:
    use_sim_time: false
    autostart: true
    node_names:
      - "controller_server"
      - "planner_server"
      - "behavior_server"
      - "bt_navigator"
      - "velocity_smoother"

lifecycle_manager_localization:
  ros__parameters:
    use_sim_time: false
    autostart: true
    node_names: []   # UWB pose bridge handles TF directly — no AMCL

map_server:
  ros__parameters:
    use_sim_time: false
    yaml_filename: ""    # No static map — global costmap is rolling window
