BAT Script Usage Reference¶
The RflySim toolchain has developed numerous batch scripts based on batch processing technology, enabling users to quickly and effortlessly launch multi-vehicle, multi-type, and diverse unmanned system combination simulations—significantly accelerating unmanned system development and simulation workflows.
Common scripts:
- SITLRun.bat — Launch multi-vehicle Software-in-the-Loop (SITL) simulation
- HITLRun.bat — Launch multi-vehicle Hardware-in-the-Loop (HITL) simulation (enter the COM port number as prompted after connecting the flight controller)
For more scripts, refer to the *\PX4PSP\RflySimAPIs\BatScripts directory.
Simulation Parameter Configuration¶
CopterSim Simulation Function Area Parameters¶

The following parameters are configured in BAT scripts via the SET command and correspond one-to-one with the CopterSim GUI interface:
Parameter 1 · CopterID / START_INDEX¶
Sets the vehicle ID; used to specify the starting vehicle ID in multi-vehicle simulations.
Parameter 2 · 3DClassID / CLASS_3D_ID¶
Sets the 3D model style of the vehicle displayed in RflySim3D.
| Value | Meaning |
|---|---|
-1 |
Use the default 3D model (specified by the DLL file) |
ClassID |
Specify vehicle configuration (e.g., quadcopter, fixed-wing), using the default style for that configuration |
Unique3DClassID |
Specify a unique 3D style for a particular configuration |
For 3D style parameter reference, see Platform 3D Model Resource Index
Parameter 3 · Use DLL Model File / DLLModel¶
Select different DLL dynamic model files (located in *PX4PSP\CopterSim\external\model).
| Value | Meaning |
|---|---|
0 |
Do not use a DLL; use the model built on the CopterSim interface |
≥1 (index or name) |
Use the specified DLL model |
Parameter 4 · Simulation Mode / SimMode¶
Simulation Mode Values
| Value | Mode Name | Description |
|---|---|---|
0 |
PX4_HITL | PX4 official Hardware-in-the-Loop simulation |
1 |
PX4_SITL | PX4 official Software-in-the-Loop simulation |
2 |
PX4_SITL_RFLY | RflySim-customized SITL, supporting hundreds of vehicles in swarm simulation |
3 |
Simulink&DLL_SIL | Simulink-generated DLL simulation (integrated model) |
4 |
PX4_HITL_NET | Ethernet-based flight controller HIL (Full version only) |
5 |
EXT_HITL_COM | Serial connection to third-party flight controllers (Full version only) |
6 |
EXT_SIM_NET | Network connection to third-party flight controllers (Full version only) |
7 |
APM_SITL_COM | APM (Ardupilot) SITL (Full version only) |
8 |
PX4_SIH_COM | SIH mode via USB connection (Full version only) |
9 |
PX4_SIH_NET | SIH mode via LAN connection (Full version only) |
10 |
PX4_SIH_SITL | SIH Software-in-the-Loop (Full version only) |
11 |
PX4_SIH_FLY | SIH Hardware-in-the-Loop (Full version only) |
Parameter 5 · 3D Display Map / UE4_MAP¶
Select the 3D scene (located in *PX4PSP\CopterSim\external\map); supports both numeric indices and names.
Parameter 6 · Networked Simulation / IS_BROADCAST¶
Enable LAN-based networked simulation (also requires pressing the I key in RflySim3D).
| Value | Meaning |
|---|---|
0 |
Local machine only |
1 |
Broadcast to the entire network |
IP address |
Specify a single IP (0=127.0.0.1, 1=255.255.255.255) |
IP1,IP2,... |
Send to multiple IPs |
json |
Configure communication IPs and ports via a JSON file |
Parameter 7 · GPS Coordinates / isPosGps (Full version only)¶
| Value | Meaning |
|---|---|
0 |
Cartesian coordinate system (default) |
1 |
GPS/LLA coordinate system |
Parameter 8 · Initial Position¶
SET /a ORIGIN_POS_X=0 :: Initial X position (m)
SET /a ORIGIN_POS_Y=0 :: Initial Y position (m)
SET LatLongAlt=116.3,39.9,50 :: LLA coordinates (Full version only)
Parameter 9 · Yaw / ORIGIN_YAW¶
Initial heading angle (degrees), relative to true north.
SET /a ORIGIN_YAW=0
---
#### Parameter 10 · Flight Controller Selection
After launching the BAT script, enter the recommended COM port number in the command line.
---
#### Parameter 11 · Baud Rate / `BaudRate`
Serial communication baud rate; default is `921600`.
---
#### Parameter 12 · Communication Mode / `UDPSIMMODE`
??? note "UDPSIMMODE Communication Mode Values"
| Value | Mode | Applicable Scale | Description |
|:--:|------|:--------:|------|
| `0` | **UDP_Full** | <10 vehicles | Most complete data flow: Python → CopterSim → PX4 |
| `1` | **UDP_Simple** | <100 vehicles | Simplified structure, suitable for large-scale swarms |
| `2` | **Mavlink_Full** | <4 vehicles | Python directly sends MAVLink messages; high data volume but closest to real hardware |
| `3` | **Mavlink_Simple** | <8 vehicles | Filters out some message packets to reduce frequency |
| `4` | **Mavlink_NoSend** | Unlimited | CopterSim does not send MAVLink; requires pairing with telemetry serial port |
| `5` | **Mavlink_NoGPS** | — | Does not send GPS data; suitable for indoor SLAM simulation |
| `6` | **Mavlink_Vision** | — | NoGPS mode + automatically sends EKF2_AID_MASK parameter |
| `7` | **Redis_Full** | — | Full Redis mode (available only in full version) |
| `8` | **Redis_Simple** | — | Simplified Redis mode (available only in full version) |
---
### Other Key Parameters
#### Frame Configuration
| Parameter | Values | Description |
|------|------|------|
| `PX4SitlFrame` | Frame name | Corresponds to frame files under `Firmware\ROMFS\px4fmu_common\init.d-posix` |
#### Swarm Configuration
| Parameter | Values | Description |
|------|------|------|
| `TOTOAL_COPTER` | Integer | Total number of vehicles |
| `IsSysID` | `0`/`1` | Whether to assign CopterID based on flight controller SysID |
| `VEHICLE_INTERVAL` | Integer | Inter-vehicle spacing (m) |
#### GPS Origin Mode
| Parameter | Values | Description |
|------|------|------|
| `isBatLLAOrin` | `0` | Use default GPS origin from DLL/terrain file |
| | `1` | Set GPS origin via BAT script |
#### Vehicle Initial Pose
| Parameter | Description |
|------|------|
| `PosXStr` | When `isPosGps=0`: X position list; when `isPosGps=1`: latitude list |
| `PosYStr` | When `isPosGps=0`: Y position list; when `isPosGps=1`: longitude list |
| `YawStr` | Yaw angle list (deg) |
#### RflySim3D Initialization and Startup Configuration
BAT script shortcut key configuration:
```bat
start %PSP_PATH%\RflySimUE5\RflySim3D.exe -key=S,N1
BAT script console command configuration:
Main Program and Startup Parameter Configuration¶
Setting Simulation Scale¶
REM Max vehicle number 50
SET /a MAX_VEHICLE=50
SET /P VehicleNum=Please input UAV swarm number:
SET /A Evaluated=VehicleNum
if %Evaluated% EQU %VehicleNum% (
IF %VehicleNum% GTR 0 (
IF %VehicleNum% GTR %MAX_VEHICLE% (
ECHO The vehicle number is too large, which may cause a crash
pause
)
GOTO StartSim
)
ECHO Not a positive integer
GOTO Top
) ELSE (
ECHO Not a integer
GOTO Top
)
Limits the maximum simulation scale (default 50, adjustable). After entering the vehicle count, validation is performed before proceeding to simulation.
Starting Simulation¶
:StartSim
SET /A VehicleTotalNum=%VehicleNum% + %START_INDEX% - 1
if not defined TOTOAL_COPTER (
SET /A TOTOAL_COPTER=%VehicleTotalNum%
)
set /a sqrtNum=1
set /a squareNum=1
:loopSqrt
set /a squareNum=%sqrtNum% * %sqrtNum%
if %squareNum% EQU %TOTOAL_COPTER% ( goto loopSqrtEnd )
if %squareNum% GTR %TOTOAL_COPTER% ( goto loopSqrtEnd )
set /a sqrtNum=%sqrtNum%+1
goto loopSqrt
:loopSqrtEnd
Calculates the total number of swarm vehicles and assigns IDs using a recursive algorithm.
REM Start QGC and RflySim3D
tasklist|find /i "QGroundControl.exe" || start %PSP_PATH%\QGroundControl\QGroundControl.exe
tasklist|find /i "RflySim3D.exe" || start %PSP_PATH%\RflySim3D\RflySim3D.exe
choice /t 5 /d y /n >nul
tasklist|find /i "CopterSim.exe" && taskkill /im "CopterSim.exe"
cd %PSP_PATH%\CopterSim
```bat
REM Loop to start CopterSim instances
set /a cntr=%START_INDEX%
set /a endNum=%VehicleTotalNum%+1
set /a portNum=%UDP_START_PORT%+((%START_INDEX%-1)*2)
:loopBegin
set /a PosXX=((%cntr%-1) / %sqrtNum%)*%VEHICLE_INTERVAL% + %ORIGIN_POS_X%
set /a PosYY=((%cntr%-1) %% %sqrtNum%)*%VEHICLE_INTERVAL% + %ORIGIN_POS_Y%
start /realtime CopterSim.exe 1 %cntr% %portNum% %DLLModel% %SimMode% %UE4_MAP% %IS_BROADCAST% %PosXX% %PosYY% %ORIGIN_YAW% 1 %UDPSIMMODE%
choice /t 1 /d y /n >nul
set /a cntr=%cntr%+1
set /a portNum=%portNum%+2
if %cntr% EQU %endNum% goto loopEnd
goto loopBegin
:loopEnd
Within the loop, the initial position for each vehicle is calculated and a CopterSim instance is launched.
REM Compile and start PX4 SITL
SET /a ToolChainType=1
SET WINDOWSPATH=%PATH%
if %ToolChainType% EQU 1 (
wsl echo Starting PX4 Build; cd %PSP_PATH_LINUX%/Firmware; ...
) else (
cd %PSP_PATH%\CygwinToolchain
CALL setPX4Env.bat
bash -l -i -c '...'
)
SET PATH=%WINDOWSPATH%
Ending the Simulation¶
tasklist|find /i "CopterSim.exe" && taskkill /im "CopterSim.exe"
tasklist|find /i "QGroundControl.exe" && taskkill /f /im "QGroundControl.exe"
tasklist|find /i "RflySim3D.exe" && taskkill /f /im "RflySim3D.exe"
ECHO Start End.
Differences Between HITLRun and SITLRun¶
HITLRun.bat is largely identical to SITLRun.bat, with the following key differences:
| Difference | HITLRun | SITLRun |
|---|---|---|
| Port Configuration | Requires user input of COM serial port number | Automatically assigns UDP ports |
| QGC Startup Order | Starts after CopterSim (to avoid COM port conflicts) | Starts before CopterSim |
| Firmware Compilation | Not performed | Compiles PX4 in WSL |
Port Configuration (HITLRun-Specific)¶
1. Retrieve COM Port Information¶
2. Parse COM Port Information¶
for /f "tokens=1,2,3 delims=#" %%a in ("%ComInfoStr%") do (
set ComNumExe=%%a
set ComNameList=%%b
set ComInfoList=%%c
)
3. Display Available COM Ports¶
set remain=%ComInfoList%
echo All COM ports on this computer are:
:loopInfo
for /f "tokens=1* delims=;" %%a in ("%remain%") do (
echo %%a
set remain=%%b
)
if defined remain goto :loopInfo
4. Output Recommended Port List¶
if %ComNumExe% EQU 0 (
echo Warning: there is no available COM port
) else (
echo Recommended COM list input is: %ComNameList%
)
5. Input COM Port List¶

6. Parse and Validate Input¶
SET string=%ComNum%
set /a VehicleNum=0
:split
for /f "tokens=1,* delims=," %%i in ("%string%") do (
set subStr=%%i
set string=%%j
)
set /a eValue=subStr
if not %eValue% EQU %subStr% (
echo Error: Input '%subStr%' is not a integer!
goto EOF
)
set /a VehicleNum = VehicleNum +1
if not "%string%"=="" goto split
QGC Startup Order (HITLRun)¶
QGC is started after CopterSim parameters are configured, to prevent QGC from occupying the COM port.
start /realtime CopterSim.exe 1 %cntr% %CLASS_3D_ID% ... %subStr%:%BaudRate% %UDPSIMMODE% %IsSysID%
...
tasklist|find /i "QGroundControl.exe" || start %PSP_PATH%\QGroundControl\QGroundControl.exe -noComPix
---
## BAT Script Quick Reference Index
The table below lists all available BAT scripts and their key features:
### Basic Simulation Scripts
| Script Name | Simulation Type | Feature Description |
|--------|:--------:|----------|
| **HITLRun.bat** | HIL | Conventional hardware-in-the-loop; manual COM port input required |
| **SITLRun.bat** | SIL | Conventional software-in-the-loop; automatically starts based on aircraft count input |
| **HITLRunAuto.bat** | HIL | Automatically assigns COM ports; no manual input needed |
| **SITLRunDebug.bat** | SIL | PX4 official single-vehicle SITL debugging |
### Position Initialization Scripts
| Script Name | Simulation Type | Feature Description |
|--------|:--------:|----------|
| **HITLPos.bat** | HIL | Manual input of PosX/PosY/Yaw |
| **SITLPos.bat** | SIL | Manual input of PosX/PosY/Yaw |
| **HITLPosAlt.bat** | HIL | Supports initialization in air or underwater |
| **SITLPosAlt.bat** | SIL | Supports initialization in air or underwater |
| **HITLPosStr.bat** | HIL | Position specified as string within script |
| **SITLPosStr.bat** | SIL | Position specified as string within script |
| **HITLPosAltStr.bat** | HIL | Automatic input; supports air/underwater initialization |
| **SITLPosAltStr.bat** | SIL | Automatic input; supports air/underwater initialization |
| **SITLPosStrSel.bat** | SIL | String-based position + selective subset enabling |
### GPS Coordinate Scripts (Full Version Only)
| Script Name | Simulation Type | Feature Description |
|--------|:--------:|----------|
| **HITLPosStrGPS.bat** | HIL | Global GPS coordinate initialization |
| **SITLPosStrGPS.bat** | SIL | Global GPS coordinate initialization |
Key settings: `SET isPosGps=1` + `SET isBatLLAOrin=1`
### SysID Binding Scripts
| Script Name | Simulation Type | Feature Description |
|--------|:--------:|----------|
| **HITLRunSysID.bat** | HIL | CopterID automatically determined by flight controller SysID |
| **HITLPosSysID.bat** | HIL | SysID + manual position input |
| **HITLPosSysIDStr.bat** | HIL | SysID + string-based position |
Key setting: `SET /a IsSysID=1`
### Networked & Swarm Scripts
| Script Name | Simulation Type | Feature Description |
|--------|:--------:|----------|
| **HITLRunLANSwarm.bat** | HIL | LAN-based networked swarm flight (`IS_BROADCAST=1`) |
| **SITLRunLANSwarm.bat** | SIL | LAN-based networked swarm flight |
### Special Mode Scripts
| Script Name | Simulation Type | Feature Description |
|--------|:--------:|----------|
| **HITLRunFw.bat** | HIL | Fixed-wing mode (`DLLModel=FixWingModel`) |
| **SITLRunFw.bat** | SIL | Fixed-wing SITL |
| **HITLRunChange3D.bat** | HIL | Switch 3D vehicle model style |
| **SITLRunChange3D.bat** | SIL | Switch 3D vehicle model style |
| **HITLRunMAVLinkNoGPS.bat** | HIL | No-GPS mode (SLAM) |
| **SITLRunMAVLinkNoGPS.bat** | SIL | No-GPS mode (SLAM) |
| **HITLRunPX4Net.bat** | HIL | Network-connected flight controller (`PX4_HITL_NET`) |
| **HITLRunExtNet.bat** | HIL | Third-party flight controller (`EXT_SIM_NET`) |
| **HITLRunLowGPU.bat** | HIL | Low GPU mode (`UE4_MAP=LowGPU`) |
| **SITLRunLowGPU.bat** | SIL | Low GPU mode |
| **HITLRunNoUI.bat** | HIL | Headless background execution |
| **SITLRunNoUI.bat** | SIL | Headless background execution |
| **HITLRunUE5.bat** | HIL | Uses UE5-based 3D program |
| **SITLRunUE5.bat** | SIL | Uses UE5-based 3D program |
### ROS Integration Scripts
| Script Name | Simulation Type | Feature Description |
|--------|:--------:|----------|
| **HITLRunROS.bat** | HIL | HIL + automatic MAVROS startup |
| **SITLRunROS.bat** | SIL | SIL + automatic MAVROS startup |
### Utility & Environment Scripts
| Script Name | Function |
|--------|------|
| **CloseALL.bat** | One-click cleanup of all simulation processes + WSL |
| **StopBuild.bat** | Terminate WSL-related processes |
| **DistSim.bat** | Launch distributed simulation manager |
| **UdpPortFree.bat** | Port conflict detection and resolution |
| **Python38Env.bat** | Open command line with preconfigured Python 3.8 environment |
| **Python38Run.bat** | Open Python 3.8 environment in current directory |
| **StartCFclient.bat** | Launch cfclient |
| **WinWSL.bat** | Open WSL console + VcXsrv |
| **WslGUI.bat** | One-click enable WSL GUI support |
| **WslSwith2.bat** | WSL1/WSL2 switching and network configuration |
| **gpedit.bat** | Install Group Policy Editor on Windows Home Edition |
---
## Appendix: Common Batch Script Code Snippets
### 1) Self-Elevation to Administrator Privileges
```bat
NET SESSION >nul 2>&1 || powershell -Command "Start-Process cmd -ArgumentList '/c, ""%~f0""' -Verb RunAs" && exit /b
`NET SESSION` can only be successfully executed by administrators; if it fails, the script automatically elevates to administrator privileges and re-runs itself.
### 2) Silent Output and Error Redirection
```bat
someCommand >nul 2>&1
Suppresses both standard output and error output. Commonly used in combination: cmd1 || cmd2 (executes cmd2 only if cmd1 fails).
3) Process Query and Safe Termination¶
tasklist | find /i "CopterSim.exe" && taskkill /im "CopterSim.exe"
tasklist | find /i "QGroundControl.exe" && taskkill /f /im "QGroundControl.exe"
Queries the process first before terminating it to avoid "process not found" errors. /f forces termination.
4) WSL Environment Management¶
wsl --shutdown :: Shuts down all WSL distributions
wsl -d RflySim-20.04 :: Executes commands within the specified distribution
5) X Server and GUI Launch¶
6) Path and Working Directory¶
cd /d %PSP_PATH%\CopterSim :: Cross-drive directory switching
pushd "%~dp0" :: Switches to the directory where the script resides
7) Program Launch and Console Session Control¶
/min: Minimizes the window; /realtime: Sets high priority; /k: Keeps the window open after execution; /c: Exits after execution.
8) Delay and Throttling¶
Non-interactive wait for 2 seconds to allow processes time to start.
9) Environment Variables and Arithmetic Operations¶
set /a VEHICLE_INTERVAL=2
setlocal enabledelayedexpansion
echo !var! :: Delayed expansion, used within loops
%var%: Expands at parse time; !var!: Expands at execution time.
10) String and List Parsing¶
for /f "tokens=1,2,3 delims=#" %%a in ("%ComInfoStr%") do (
set ComNumExe=%%a
set ComNameList=%%b
set ComInfoList=%%c
)
11) Labels and Subroutines¶
12) Temporary PATH Injection¶
13) Pipelines and Conditional Execution¶
Starts a new instance only if the process is not found.