"""motor_daemon.launch.py — Launch the Orin motor control daemon (Issue #523).

Usage:
  # Default (ttyTHS1, 50 Hz, 200ms watchdog):
  ros2 launch saltybot_motor_daemon motor_daemon.launch.py

  # Override serial port:
  ros2 launch saltybot_motor_daemon motor_daemon.launch.py serial_port:=/dev/ttyTHS0

  # Slower send rate for testing:
  ros2 launch saltybot_motor_daemon motor_daemon.launch.py send_rate_hz:=10.0

  # Custom velocity scaling:
  ros2 launch saltybot_motor_daemon motor_daemon.launch.py speed_scale:=500.0
"""

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description() -> LaunchDescription:
    pkg = get_package_share_directory("saltybot_motor_daemon")
    params_file = os.path.join(pkg, "config", "motor_daemon_params.yaml")

    return LaunchDescription([
        DeclareLaunchArgument("serial_port",         default_value="/dev/ttyTHS1"),
        DeclareLaunchArgument("baud_rate",            default_value="921600"),
        DeclareLaunchArgument("send_rate_hz",         default_value="50.0"),
        DeclareLaunchArgument("watchdog_timeout",     default_value="0.2"),
        DeclareLaunchArgument("status_query_period",  default_value="1.0"),
        DeclareLaunchArgument("speed_scale",          default_value="1000.0"),
        DeclareLaunchArgument("steer_scale",          default_value="-500.0"),

        Node(
            package="saltybot_motor_daemon",
            executable="motor_daemon_node",
            name="motor_daemon_node",
            output="screen",
            emulate_tty=True,
            parameters=[
                params_file,
                {
                    "serial_port":        LaunchConfiguration("serial_port"),
                    "baud_rate":          LaunchConfiguration("baud_rate"),
                    "send_rate_hz":       LaunchConfiguration("send_rate_hz"),
                    "watchdog_timeout":   LaunchConfiguration("watchdog_timeout"),
                    "status_query_period": LaunchConfiguration("status_query_period"),
                    "speed_scale":        LaunchConfiguration("speed_scale"),
                    "steer_scale":        LaunchConfiguration("steer_scale"),
                },
            ],
        ),
    ])
