The second SITL vehicle won't connect in a swarm simulation
If the second SITL vehicle won't connect in a swarm simulation, the cause is the port, not a cable. Each instance's main port is 5760 + 10 × instance number. Look for the second vehicle on 5770.
With a single vehicle, there is no problem. You start the second vehicle, and the ground control station either never connects or still shows the first vehicle on screen. There is no cable and no radio; both are on the same computer.
The answer is almost always in the same place: the second vehicle isn't on the port you are trying to connect to. It has opened a different port, and you are knocking on the old one.
Each vehicle opens its own port
The instance number you give when starting the simulation determines which port the vehicle will listen on when it comes up. The first vehicle opens one port, and the second opens another port ten steps further on.
Each vehicle's main port (SERIAL0) is ten numbers ahead of the previous one. So looking for the second vehicle on the first vehicle's port, or on the port right next to it, gets you nowhere. In the figure below, notice how the blue ports jump by ten.
Each instance opens its own port: 5760 + 10 × instance number
This distinction looks small on paper, but when you build a swarm you have to track all five vehicles by hand. Once you miss a number, finding out which vehicle is on which port takes a long time.
Example: while preparing for a swarm mission, you started three vehicles with -I0, -I1, and -I2 and want to see all three on the ground station. You open three separate connections: 5760, 5770, and 5780. If you type 5771 instead of 5780 for the third vehicle, the connection is turned away immediately. If all three vehicles are in the process list, the problem isn't in the vehicle; it's the number in the address.
Two separate causes for the same symptom
"The second vehicle won't connect" describes two completely different situations at once, and the fix isn't the same for both. The fastest way to tell which one you have is to look at the process list.
| The process died | The process is healthy | |
|---|---|---|
| What happened | Two vehicles asked for the same port, and the second one closed as soon as it opened. | Both vehicles are up, and you can connect to both. |
| Where you see it | It's missing from the process list, and there is a connection error in the log. | Both show the same ID, and the station thinks there is a single vehicle. |
| First move | Separate the port numbers. | Make the ID unique. |
In the case on the left there isn't even a vehicle to look for, because the second vehicle shut down the moment it was born. In the case on the right both vehicles are up, and the problem is that the ground control station can't tell them apart.
A connection attempt to a number that no vehicle has opened doesn't hang; it is turned away instantly. So looking for a network or firewall problem is a waste of time.
Check the port yourself
You can start both vehicles and read directly from their own logs which port they opened. The command below starts both and filters the port line out of the log files.
cd ~/sitl
# First vehicle
./arducopter -I0 --model + --speedup 5 --defaults copter.parm \
--sysid 1 --home 37.1591,38.7969,500,0 > s0.log 2>&1 &
# Second vehicle: only -I and --sysid change
./arducopter -I1 --model + --speedup 5 --defaults copter.parm \
--sysid 2 --home 37.1591,38.7969,500,0 > s1.log 2>&1 &
sleep 15
grep "TCP port" s0.log s1.logOnce the port numbers are clear, it's time to connect. If you don't explicitly ask for the telemetry stream, no data arrives. If this line is skipped, the connection looks established but the screen stays empty.
from pymavlink import mavutil
for port in (5760, 5770):
arac = mavutil.mavlink_connection("tcp:127.0.0.1:%d" % port)
kalp = arac.wait_heartbeat(timeout=20)
if kalp is None:
print(port, "-> no heartbeat")
continue
arac.mav.request_data_stream_send(
arac.target_system, arac.target_component,
mavutil.mavlink.MAV_DATA_STREAM_ALL, 4, 1)
print(port, "-> system ID", arac.target_system)If you start both vehicles and run these two pieces of code, you get this: two different ports, two different system IDs:
s0.log:SERIAL0 on TCP port 5760
s1.log:SERIAL0 on TCP port 5770
5760 -> system ID 1
5770 -> system ID 2The most common mistake
Changing the instance number when starting the second vehicle but leaving the connection address as it was. Even if the number changes, if the address has a fixed port written in it, the vehicle tries to open that port, and since the first vehicle already holds it, the second vehicle closes as soon as it opens.
In this case you don't see "can't connect" on screen; you see nothing at all. When you look at the process list, the second vehicle isn't even there.
The system ID is the second trap
After you fix the ports, a second problem can appear. Both vehicles come up and you can connect to both, but the ground control station thinks they are a single vehicle. This happens because the system ID hasn't been made unique.
The name of the parameter that sets the ID changed between firmware versions. In a parameter list pulled from a newer version, you may not find the old name.
So if you are going to set the ID by hand, search the parameter list by pattern, not by name. If you can't find the name you expect, the parameter has most likely been renamed.
Error Messages and Their Meanings
The lines below are the exact text produced by the simulation and by Python.
bind failed on port 5760 - Address already in use
Two vehicles tried to open the same port. The vehicle that writes this line doesn't stay up; it shuts down immediately. That is why you can't see it in the process list. Increase the instance number, or remove the fixed port number from the connection address.
ConnectionRefusedError: [Errno 111] Connection refused
Nobody is listening on that port. Either the number is wrong or that vehicle never came up. Look at the process list first, then at the vehicle's log file.
Waiting for connection ....
The vehicle is healthy and ready to take commands. If you get stuck on this line, the problem is the port number you connected to.
Setting MAV_SYSID=2
This is not an error but a confirmation line. If you don't see it at startup, the ID flag you gave wasn't applied.
This issue stays in simulation
Everything here applies to a simulation running on a single computer. In a real swarm, the vehicles aren't on the same machine, so port collisions don't happen. There, vehicles are told apart by radio channel and system ID.
That is also why you build swarm logic in simulation first. You want to find ID and addressing mistakes on the bench, not in the air.
If you want to build the swarm from start to finish
The whole flow, from simulation setup to image processing and swarm scenarios, is covered step by step in the AI and Drone Simulation course. The lesson that continues the topic in this article is also part of that course.
AI and Drone Simulation Virtual Machine: comes with ArduPilot SITL, ROS Noetic, and Gazebo 11 installed. You start simulating without any setup hassle.
Was this article helpful?
Learn this topic from start to finish
Season plan: all six courses for 9 months, ₺790 a month. ₺390 a month for high school and university students, ₺351 the first month with your member code. You can stop in any month you like. Start the Season plan · I'm a student