Hi there,
I’ve writte a script that spins up multiple px4 instances:
#!/bin/bash
# Kill all running px4 processes
echo "Killing all px4 processes..."
pkill -9 px4
# Remove the stored PIDs from /tmp
rm -f /tmp/px4_vehicle_*.pid
echo "All px4 processes have been killed."
# Default number of vehicles
NUM_VEHICLES=2
# Directory paths
PX4_DIR="$HOME/PX4-Autopilot"
PX4_BIN="$PX4_DIR/build/px4_sitl_default/bin/px4"
RC_SCRIPT="$PX4_DIR/ROMFS/px4fmu_common/init.d-posix/rcS"
ROMFS_DIR="$PX4_DIR/ROMFS/px4fmu_common"
# Check for a '-n' flag to set the number of vehicles
while getopts "n:" flag; do
case "${flag}" in
n) NUM_VEHICLES=${OPTARG} ;;
*) echo "Usage: $0 [-n num_vehicles]" ;;
esac
done
# Function to launch a single PX4 instance
launch_px4() {
VEHICLE_ID=$1
echo "Launching vehicle $VEHICLE_ID..."
# Create a unique working directory for each vehicle
VEHICLE_WORK_DIR="/tmp/px4_vehicle_$VEHICLE_ID"
mkdir -p $VEHICLE_WORK_DIR
# Calculate the TCP port for this vehicle (starting from 4560)
TCP_PORT=$((4560 + VEHICLE_ID))
# Launch the PX4 process in the background with a unique working directory
$PX4_BIN -w $VEHICLE_WORK_DIR $ROMFS_DIR -s $RC_SCRIPT -i $VEHICLE_ID &
# Store the process ID of PX4 to a file
echo $! > /tmp/px4_vehicle_$VEHICLE_ID.pid
echo "Vehicle $VEHICLE_ID launched on TCP port $TCP_PORT"
}
# Launch PX4 instances for multiple vehicles in parallel
for VEHICLE_ID in $(seq 0 $((NUM_VEHICLES - 1)))
do
launch_px4 $VEHICLE_ID &
done
# Wait for all background processes to complete
wait
echo "All PX4 instances have been launched."
# Run the find_tcp script to display open ports
./examples/find_tcp
I do not understand why this does not open the tcp
port for the simulator while when I run:
make px4_sitl_default none_iris
, I see that it’s waiting to connect to a simulator on TCP 4560
. How can I make the PX4 connect to TCP 4560 as well when running from the build directory?