"""esp32_balance.launch.py — Launch the Orin↔ESP32-S3 BALANCE USB-serial bridge.

bd-wim1: replaces can_bridge_node (CANable2/python-can) with USB-CDC serial.
The freed CANable2 slot is used by Here4 GPS (bd-p47c).

USB device
----------
  CH343 USB-CDC chip on ESP32-S3 BALANCE board
  VID:PID  1a86:55d3
  Symlink  /dev/esp32-balance  (via udev rule in jetson/docs/pinout.md)
  Baud     460800

Usage
-----
  # Default:
  ros2 launch saltybot_esp32_serial esp32_balance.launch.py

  # Override port (e.g. direct ACM path):
  ros2 launch saltybot_esp32_serial esp32_balance.launch.py serial_port:=/dev/ttyACM0

  # Custom velocity scales:
  ros2 launch saltybot_esp32_serial esp32_balance.launch.py speed_scale:=800.0 steer_scale:=-400.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_share = get_package_share_directory("saltybot_esp32_serial")
    params_file = os.path.join(pkg_share, "config", "esp32_balance_params.yaml")

    serial_port_arg = DeclareLaunchArgument(
        "serial_port",
        default_value="/dev/esp32-balance",
        description="USB-CDC serial port for ESP32-S3 BALANCE (CH343, 1a86:55d3)",
    )
    speed_scale_arg = DeclareLaunchArgument(
        "speed_scale",
        default_value="1000.0",
        description="linear.x (m/s) → motor speed units",
    )
    steer_scale_arg = DeclareLaunchArgument(
        "steer_scale",
        default_value="-500.0",
        description="angular.z (rad/s) → steer units",
    )

    node = Node(
        package="saltybot_esp32_serial",
        executable="esp32_balance_node",
        name="esp32_balance_node",
        output="screen",
        parameters=[
            params_file,
            {
                "serial_port": LaunchConfiguration("serial_port"),
                "speed_scale": LaunchConfiguration("speed_scale"),
                "steer_scale": LaunchConfiguration("steer_scale"),
            },
        ],
    )

    return LaunchDescription([
        serial_port_arg,
        speed_scale_arg,
        steer_scale_arg,
        node,
    ])
