How to Use PX4 and ROS2 in Docker

Before

env

win11
WSL2
Ubuntu 20.04 (wsl)
docker

Ubuntu 20.04
ROS2 Foxy
PX4-Autopilot master

download

dowload PX4、px4_ros_com_ros2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# in windows powershell

# cd to the source path
cd code

mkdir -p px4_src/shared/px4_ros_com_ros2/src

# px4_ros_com
# https://docs.px4.io/v1.12/en/ros/ros2_comm.html#build-ros-2-workspace
git clone https://github.com/PX4/px4_ros_com.git ~/px4_src/shared/px4_ros_com_ros2/src/px4_ros_com
git clone https://github.com/PX4/px4_msgs.git ~/px4_src/shared/px4_ros_com_ros2/src/px4_msgs

# PX4-Autopilot
git clone https://github.com/PX4/PX4-Autopilot.git ~/px4_src/PX4-Autopilot

download docker images

base px4io/px4-dev-simulation-focal, wih Gazeb and etc

1
2
3
4
# in windows powershell

docker pull px4io/px4-dev-ros2-foxy

run docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# in Ubuntu 20.04 (wsl)

# /mnt/d/code/px4_src is the path of PX4 and px4_ros_com_ros2
docker run -it --privileged \
--env LOCAL_USER_ID="$(id -u)" \
-v /mnt/d/code/px4_src:/src/:rw \
-v /tmp/.X11-unix:/tmp/.X11-unix:ro \
-e DISPLAY=unix$DISPLAY \
-e GDK_SCALE \
-e GDK_DPI_SCALE \
-p 14570:14570/udp \
--name=px4_foxy \
px4io/px4-dev-ros2-foxy:latest \
bash

build px4_ros_com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# in Ubuntu 20.04 (wsl) 

# run docker Terminal
docker exec -it px4_foxy bash

# in docker bash

cd /src/shared/px4_ros_com_ros2/src
source /opt/ros/foxy/setup.bash

# use colcon build source
colcon build

# before use micrortps_agent need to source this bash
source install/setup.bash

build PX4 SITL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# in Ubuntu 20.04 (wsl)

# run docker Terminal
docker exec -it px4_foxy bash

# in docker bash

# I need the proxy (qwq)
export https_proxy=http://192.168.31.21:7890
export http_proxy=http://192.168.31.21:7890
export all_proxy=socks5://192.168.31.21:7890

cd /src/PX4-Autopilot/
make px4_sitl_rtps gazebo

now we can see Gazebo and pxh terminal

run UDP micrortps_agent

1
2
3
4
5
6
7
8
# in Ubuntu 20.04 (wsl)

# run docker Terminal
docker exec -it px4_foxy bash

source /src/shared/px4_ros_com_ros2/src/install/setup.bash
micrortps_agent -t UDP

run listener

1
2
3
4
5
6
7
8
# in Ubuntu 20.04 (wsl)

# run docker Terminal
docker exec -it px4_foxy bash

source /src/shared/px4_ros_com_ros2/src/install/setup.bash
ros2 launch px4_ros_com sensor_combined_listener.launch.py

takeoff the drone

1
2
3
# in pxh terminal

commander takeoff

if run commander takeoff see Failsafe enabled: no datalink, and fail to takeoff.

imput

1
2
param set NAV_RCL_ACT 0
param set NAV_DLL_ACT 0