[Unit]
Description=SaltyBot ESP32-S3 BALANCE UART Serial Bridge (bd-wim1)
Documentation=https://gitea.vayrette.com/seb/saltylab-firmware/issues/bd-wim1
# Requires package: saltybot_esp32_serial (merged in bd-wim1 → PR #727)
#
# Publishes:
#   /saltybot/attitude     String JSON  (pitch, motor_cmd, vbat_mv, state)
#   /saltybot/balance_state String
#   /can/battery           BatteryState
#   /can/vesc/left/state   Float32MultiArray  [erpm, voltage_v, current_a, temp_c]
#   /can/vesc/right/state  Float32MultiArray
#   /can/connection_status String
#
# Subscribes:
#   /cmd_vel               Twist   → DRIVE command to ESP32 BALANCE
#   /estop                 Bool    → ESTOP command
#   /saltybot/arm          Bool    → ARM command
#   /saltybot/pid_update   String  → PID gains update
After=network-online.target
# Wait for /dev/esp32-balance (created by 80-esp32.rules udev rule).
# TAG+="systemd" in the udev rule makes systemd track dev-esp32\x2dbalance.device.
# If device is not present at boot the node's auto-reconnect loop handles it.
Wants=network-online.target

[Service]
Type=simple
User=orin
Group=dialout

# ROS2 Humble environment + workspace overlay
Environment="ROS_DOMAIN_ID=42"
Environment="ROS_DISTRO=humble"
Environment="SALTYBOT_WS=/opt/saltybot/jetson/ros2_ws/install"

ExecStart=/opt/saltybot/scripts/ros2-launch.sh \
    saltybot_esp32_serial esp32_balance.launch.py

# The node auto-reconnects on serial disconnect (2 s retry).
# Restart=on-failure covers node crash; RestartSec gives the device time to re-enumerate.
Restart=on-failure
RestartSec=5s
StartLimitInterval=60s
StartLimitBurst=5

TimeoutStartSec=30s
TimeoutStopSec=10s

# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=saltybot-esp32-serial

[Install]
WantedBy=saltybot.target
