### Firmware update for motor control drives

Updates a single EtherCAT actuator at a given slave position. The script resets the target slave into bootloader mode and can perform one or both of the following operations in the same bootloader session, depending on the arguments provided:

- **Firmware only**: pass `--cpu1-hex` and `--cm-hex` to upload the corresponding core firmware files.
- **Environment only**: pass `--env-file` to upload a new environment without touching the firmware.
- **Firmware and environment**: pass both firmware hex files and an environment file to update everything in one shot. The environment is written right after the firmware cores have been uploaded.

If none of `--cpu1-hex`, `--cm-hex` or `--env-file` is provided, the script exits with an error since there is nothing to do.

**Pre-execution steps:**

Before running this script you must release the EtherCAT interface and run the
command as `root`. Follow these steps in order:

1. Stop the module manager and the EtherCAT service so the NIC is freed:

   ```bash
   pal module_manager stop
   sudo systemctl stop ethercat.service
   ```

2. Verify that the interface is now available under the name `ethercat0`:

   ```bash
   ip a
   ```

   If the interface appears as `DOWN`, bring it up:

   ```bash
   sudo ip link set ethercat0 up
   ```

3. The script requires root privileges, so switch to a root shell before
   launching it:

   ```bash
   sudo su
   ```

**Usage:**
```bash
ros2 run ethercat_loader update_single_actuator <slave_position> [OPTIONS]
```

**Arguments:**

| Argument | Default | Description |
|---|---|---|
| `slave_position` | (required) | Slave position in the EtherCAT chain |
| `--nic-name` | `ethercat0` | EtherCAT interface name |
| `--cpu1-hex` | `None` | Path to the cpu1 `.hex` firmware file. If omitted, the cpu1 firmware is not updated. |
| `--cm-hex` | `None` | Path to the cm `.hex` firmware file. If omitted, the cm firmware is not updated. |
| `--env-file` | `None` | Path to the environment file. If omitted, the environment is not updated. |

**Post-execution steps:**

Once the update has finished:

1. Power off the actuator.
2. Restart the EtherCAT service and the module manager so the system goes back
   to its normal operating state:

   ```bash
   sudo systemctl start ethercat.service
   pal module_manager start
   ```

---