"""
tank_driver.launch.py — Launch the SaltyTank ESC motor driver node.

Usage:
    ros2 launch saltybot_tank_driver tank_driver.launch.py
    ros2 launch saltybot_tank_driver tank_driver.launch.py serial_port:=/dev/ttyACM0
    ros2 launch saltybot_tank_driver tank_driver.launch.py drive_mode:=4wd
    ros2 launch saltybot_tank_driver tank_driver.launch.py slip_factor:=0.1

Emergency stop:
    ros2 topic pub /saltybot/e_stop std_msgs/Bool '{data: true}' --once
    ros2 topic pub /saltybot/e_stop std_msgs/Bool '{data: false}' --once
"""

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():
    pkg_share = get_package_share_directory("saltybot_tank_driver")
    params_file = os.path.join(pkg_share, "config", "tank_params.yaml")

    serial_port_arg = DeclareLaunchArgument(
        "serial_port",
        default_value="/dev/ttyUSB_tank",
        description="Serial port for ESC controller",
    )
    drive_mode_arg = DeclareLaunchArgument(
        "drive_mode",
        default_value="tracked",
        description="Drive mode: tracked | 4wd | 2wd",
    )
    slip_factor_arg = DeclareLaunchArgument(
        "slip_factor",
        default_value="0.3",
        description="Track slip coefficient [0.0, 0.99]",
    )

    tank_driver = Node(
        package="saltybot_tank_driver",
        executable="tank_driver_node",
        name="tank_driver",
        parameters=[
            params_file,
            {
                "serial_port": LaunchConfiguration("serial_port"),
                "drive_mode":  LaunchConfiguration("drive_mode"),
                "slip_factor": LaunchConfiguration("slip_factor"),
            },
        ],
        output="screen",
        emulate_tty=True,
    )

    return LaunchDescription([
        serial_port_arg,
        drive_mode_arg,
        slip_factor_arg,
        tank_driver,
    ])
