Skip to content

Commit

Permalink
Merge pull request microsoft#3222 from ahmed-elsaharti/docs-update
Browse files Browse the repository at this point in the history
Docs update : PX4 Multi-vehicle sim
  • Loading branch information
zimmy87 authored Feb 8, 2021
2 parents 1b36f57 + 2e50097 commit 4be4d1e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
13 changes: 13 additions & 0 deletions PX4Scripts/run_airsim_sitl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
instance_num=0
[ -n "$1" ] && instance_num="$1"
export PX4_SIM_MODEL=iris
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PARENT_DIR="$(dirname "$SCRIPT_DIR")"
BUILD_DIR=$PARENT_DIR/Firmware/ROMFS/px4fmu_common
instance_path=$PARENT_DIR/Firmware/build/px4_sitl_default
BIN_DIR=$PARENT_DIR/Firmware/build/px4_sitl_default/bin/px4
TEST_DATA=$PARENT_DIR/Firmware/test_data
working_dir="$instance_path/instance_$instance_num"
[ ! -d "$working_dir" ] && mkdir -p "$working_dir"
pushd "$working_dir" &>/dev/null
$BIN_DIR -i $instance_num $BUILD_DIR -s "etc/init.d-posix/rcS" -t $TEST_DATA
1 change: 1 addition & 0 deletions PX4Scripts/sitl_kill.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkill -x px4 || true
119 changes: 119 additions & 0 deletions docs/px4_multi_vehicle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Setting up multi-vehicle PX4 simulation

The [PX4 SITL stack](px4_sitl.md) comes with a `sitl_multiple_run.sh` shell script that runs multiple instances of the PX4 binary. This would allow the SITL stack to listen to connections from multiple AirSim vehicles on multiple TCP ports starting from 4560.
However, the provided script does not let us view the PX4 console. If you want to run the instances manually while being able to view each instance's console (**Recommended**) see [this section](px4_multi_vehicle.md#starting-sitl-instances-with-px4-console)

## Setting up multiple instances of PX4 Software-in-Loop

**Note** you have to build PX4 with `make px4_sitl_default none_iris` as shown [here](px4_sitl.md#setting-up-px4-software-in-loop) before trying to run multiple PX4 instances.

1. From your bash (or Cygwin) terminal go to the PX4 Firmware directory and run the `sitl_multiple_run.sh` script while specifying the number of vehicles you need
```
cd PX4
cd Firmware
./Tools/sitl_multiple_run.sh 2 # 2 here is the number of vehicles/instances
```
This starts multiple instances that listen to TCP ports 4560 to 4560+i where 'i' is the number of vehicles/instances specified
2. You should get a confirmation message that says that old instances have been stopped and new instances have been started
```
killing running instances
starting instance 0 in /cygdrive/c/PX4/home/PX4/Firmware/build/px4_sitl_default/instance_0
starting instance 1 in /cygdrive/c/PX4/home/PX4/Firmware/build/px4_sitl_default/instance_1
```
3. Now edit [AirSim settings](settings.md) file to make sure you have matching TCP port settings for the set number of vehicles and to make sure that both vehicles do not spawn on the same point.
For example, these settings would spawn two PX4Multirotors where one of them would try to connect to PX4 SITL at port `4560` and the other at port `4561`. It also makes sure the vehicles spawn at `0,1,0` and `0,-1,0` to avoid collision:
```json
{
"SettingsVersion": 1.2,
"SimMode": "Multirotor",
"Vehicles": {
"Drone1": {
"VehicleType": "PX4Multirotor",
"UseSerial": false,
"UseTcp": true,
"TcpPort": 4560,
"ControlPort": 14580,
"X": 0, "Y": 1, "Z": 0
},
"Drone2": {
"VehicleType": "PX4Multirotor",
"UseSerial": false,
"UseTcp": true,
"TcpPort": 4561,
"ControlPort": 14580,
"X": 0, "Y": -1, "Z": 0
}
}
}
```
You can add more than two vehicles but you will need to make sure you adjust the TCP port for each (ie: vehicle 3's port would be `4562` and so on..) and adjust the spawn point.
4. Now run your Unreal AirSim environment and it should connect to SITL PX4 via TCP.
If you are running the instances with the [PX4 console visible](px4_multi_vehicle.md#Starting-sitl-instances-with-px4-console), you should see a bunch of messages from each SITL PX4 window.
Specifically, the following messages tell you that AirSim is connected properly and GPS fusion is stable:
```
INFO [simulator] Simulator connected on UDP port 14560
INFO [mavlink] partner IP: 127.0.0.1
INFO [ecl/EKF] EKF GPS checks passed (WGS-84 origin set)
INFO [ecl/EKF] EKF commencing GPS fusion
```
If you do not see these messages then check your port settings.
5. You should also be able to use QGroundControl with SITL mode. Make sure
there is no Pixhawk hardware plugged in, otherwise QGroundControl will choose
to use that instead. Note that as we don't have a physical board, an RC cannot be connected directly to it. So the alternatives are either use XBox 360 Controller or connect your RC using USB (for example, in case of FrSky Taranis X9D Plus) or using trainer USB cable to your PC. This makes your RC look like a joystick. You will need to do extra set up in QGroundControl to use virtual joystick for RC control. You do not need to do this unless you plan to fly a drone manually in AirSim. Autonomous flight using the Python
API does not require RC, see [`No Remote Control`](px4_sitl.md#No-Remote-Control).
## Starting SITL instances with PX4 console
If you want to start your SITL instances while being able to view the PX4 console, you will need to run the shell scripts found [here](/PX4Scripts) rather than `sitl_multiple_run.sh`.
Here is how you would do so:
**Note** This script also assumes PX4 is built with `make px4_sitl_default none_iris` as shown [here](px4_sitl.md#setting-up-px4-software-in-loop) before trying to run multiple PX4 instances.
1. From your bash (or Cygwin) terminal go to the PX4 directory and get the scripts (place them in a subdirectory called Scripts win the PX4 directory as shown)
```
cd PX4
mkdir -p Scripts
cd Scripts
wget https://raw.githubusercontent.com/microsoft/AirSim/master/PX4Scripts/sitl_kill.sh
wget https://raw.githubusercontent.com/microsoft/AirSim/master/PX4Scripts/run_airsim_sitl.sh
```
**Note** the shell scripts expect the `Scripts` and `Firmware` directories to be within the same parent directory. Also, you may need to make the scripts executable by running `chmod +x sitl_kill.sh` and `chmod +x run_airsim_sitl.sh`.
2. Run the `sitl_kill.sh` script to kill all active PX4 SITL instances
```
./sitl_kill.sh
```
3. Run the `run_airsim_sitl.sh` script while specifying which instance you would like to run in the current terminal window (the first instance would be numbered 0)
```
./run_airsim_sitl.sh 0 # first instance = 0
```
You should see the PX4 instance starting and waiting for AirSim's connection as it would with a single instance.
```
______ __ __ ___
| ___ \ \ \ / / / |
| |_/ / \ V / / /| |
| __/ / \ / /_| |
| | / /^\ \ \___ |
\_| \/ \/ |_/
px4 starting.
INFO [px4] Calling startup script: /bin/sh /cygdrive/c/PX4/home/PX4/Firmware/etc/init.d-posix/rcS 0
INFO [dataman] Unknown restart, data manager file './dataman' size is 11798680 bytes
INFO [simulator] Waiting for simulator to connect on TCP port 4560
```
4. Open a new terminal and go to the Scripts directory and start the next instance
```
cd PX4
cd Scripts
./run_airsim_sitl.sh 1 # ,2,3,4,..,etc
```
5. Repeat step 4 for as many instances as you would like to start
6. Run your Unreal AirSim environment and it should connect to SITL PX4 via TCP (assuming your settings.json file has the right ports).
8 changes: 7 additions & 1 deletion docs/px4_sitl.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use the [Cygwin Toolchain](https://dev.px4.io/master/en/setup/dev_env_windows_cy
**Note** that every time you stop the unreal app you have to restart the `px4` app.


1. From your bash terminal follow [these steps for Linux](http://dev.px4.io/starting-installing-linux.html) and follow **all** the instructions under `NuttX based hardware` to install prerequisites. We've also included our own copy of the [PX4 build instructions](px4_build.md) which is a bit more concise about what we need exactly.
1. From your bash terminal follow [these steps for Linux](https://docs.px4.io/master/en/dev_setup/dev_env_linux.html) and follow **all** the instructions under `NuttX based hardware` to install prerequisites. We've also included our own copy of the [PX4 build instructions](px4_build.md) which is a bit more concise about what we need exactly.

2. Get the PX4 source code and build the posix SITL version of PX4:
```
Expand All @@ -16,6 +16,8 @@ use the [Cygwin Toolchain](https://dev.px4.io/master/en/setup/dev_env_windows_cy
cd Firmware
git checkout v1.10.1 # recommended version
```
**Note**: __Cygwin version [0.8](https://github.com/PX4/PX4-windows-toolchain/releases/download/v0.8/PX4.Windows.Cygwin.Toolchain.0.8.msi) is recommended for PX4 v1.10.1__
3. Use following command to build and start PX4 firmware in SITL mode:
```
make px4_sitl_default none_iris
Expand Down Expand Up @@ -163,6 +165,10 @@ param set NAV_RCL_ACT 0
param set NAV_DLL_ACT 0
```
## Setting up multi-vehicle simulation
You can simulate multiple drones in SITL mode using AirSim. However, this requires setting up multiple instances of the PX4 firmware simulator to be able to listen for each vehicle's connection on a separate TCP port (4560, 4561, etc). Please see [this dedicated page](px4_multi_vehicle.md) for instructions on setting up multiple instances of PX4 in SITL mode.
## Using VirtualBox Ubuntu
If you want to run the above posix_sitl in a `VirtualBox Ubuntu` machine then it will have a different ip address from localhost. So in this case you need to edit the [settings file](settings.md) and change the UdpIp and SitlIp to the ip address of your virtual machine
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ nav:
- "MavLink and PX4":
- "PX4 Setup for AirSim": 'px4_setup.md'
- "PX4 in SITL": 'px4_sitl.md'
- "PX4 Multi-vehicle in SITL": 'px4_multi_vehicle.md'
- "AirSim with Pixhawk": 'https://youtu.be/1oY8Qu5maQQ'
- "PX4 Setup with AirSim": 'https://youtu.be/HNWdYrtw3f0'
- "Debugging Attitude Estimation": 'https://www.youtube.com/watch?v=d_FyjKDWQfc&feature=youtu.be'
Expand Down

0 comments on commit 4be4d1e

Please sign in to comment.