PX4仿真环境搭建

  1. 环境:ROS-Humble+Gazebo-Harmonic+PX4

  2. ROS-Humble安装 1

#小鱼一键安装
wget http://fishros.com/install -O fishros && . fishros
  1. PX4仿真环境安装 2

cd
git clone https://github.com/PX4/PX4-Autopilot.git --recursive
bash ./PX4-Autopilot/Tools/setup/ubuntu.sh
cd PX4-Autopilot/
make px4_sitl

请注意,上述命令将为您的 Ubuntu 版本安装推荐的模拟器。如果您想安装 PX4 但保留现有的模拟器安装,请使用标志–no-sim-tools运行上面的命令。

还必须安装一些 Python 依赖项(使用 pip 或 apt)

pip install --user -U empy==3.3.4 pyros-genmsg setuptools
  1. 安装Micro XRCE-DDS客户端与代理端

git clone https://github.com/eProsima/Micro-XRCE-DDS-Agent.git
cd Micro-XRCE-DDS-Agent
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig /usr/local/lib/
#更改文件权限为可执行
sudo chmod +x MicroXRCEAgent

使用用于连接到模拟器上运行的 uXRCE-DDS 客户端的设置启动代理:

cd /path/to/Micro-XRCE-DDS-Agent/build
./MicroXRCEAgent udp4 -p 8888
  1. 安装编译px4_msgs和px4_ros_com

mkdir -p ~/ws_precision_visland/src/
cd ~/ws_precision_visland/src/
git clone https://github.com/PX4/px4_msgs.git
git clone https://github.com/PX4/px4_ros_com.git
cd ..
colcon build

cd
sudo vim .bashrc
source ws_precision_visland/install/setup.bash	#保存退出
source .bashrc
ros2 launch px4_ros_com sensor_combined_listener.launch.py	#测试
  1. 安装usb_com3

sudo apt-get install ros-humble-usb-cam
ros2 launch usb_cam camera.launch.py 
'''
会报错
Caught exception in launch (see debug for traceback): Caught exception when trying to load file of format [py]: No module named 'pydantic'
'''
pip install pydantic  -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
ros2 launch usb_cam camera.launch.py 
'''
还是会报错
找到/opt//ros/humble/share/usb_cam/launch下的 camera_config.py,并使用vim打开,并修改里面的配置文件
移除 @root_validator 避免 Pydantic 2.5 版本中 @root_validator 使用上的限制和不兼容问题
'''

sudo vim /opt//ros/humble/share/usb_cam/launch/camera_config.py

打开camera_config.py后,键入shift/:,输入清除全文命令%d,然后粘贴复制以下内容:

# Copyright 2023 usb_cam Authors
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#    * Neither the name of the usb_cam Authors nor the names of its
#      contributors may be used to endorse or promote products derived from
#      this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
 
 
from pathlib import Path
from typing import List, Optional
 
from ament_index_python.packages import get_package_share_directory
#from pydantic import BaseModel, root_validator, validator
from pydantic import BaseModel, model_validator, validator
 
USB_CAM_DIR = get_package_share_directory('usb_cam')
 
 
class CameraConfig(BaseModel):
    name: str = 'camera1'
    param_path: Path = Path(USB_CAM_DIR, 'config', 'params_1.yaml')
    remappings: Optional[List[str]] = []
    namespace: Optional[str] = None
 
    @validator('param_path')
    def validate_param_path(cls, value):
        if value and not value.exists():
            raise FileNotFoundError(f'Could not find parameter file: {value}')
        return value
 
#    @root_validator
#   def validate_root(cls, values):
#        name = values.get('name')
#        remappings = values.get('remappings')
#        if name and not remappings:
#            # Automatically set remappings if name is set
#            remappings = [
#                ('image_raw', f'{name}/image_raw'),
#                ('image_raw/compressed', f'{name}/image_compressed'),
#                ('image_raw/compressedDepth', f'{name}/compressedDepth'),
#               ('image_raw/theora', f'{name}/image_raw/theora'),
#               ('camera_info', f'{name}/camera_info'),
#            ]
#        values['remappings'] = remappings
#        return value
    @validator('name')
    def validate_name(cls, value):
        if not value:
            raise ValueError("Name is required")
        return value
 
    def __init__(self, **data):
        super().__init__(**data)
        if self.name:
            # Automatically set remappings if name is set
            self.remappings.extend([
                ('image_raw', f'{self.name}/image_raw'),
                ('image_raw/compressed', f'{self.name}/image_compressed'),
                ('image_raw/compressedDepth', f'{self.name}/compressedDepth'),
                ('image_raw/theora', f'{self.name}/image_raw/theora'),
                ('camera_info', f'{self.name}/camera_info'),
            ])
  1. ROS2与Gazebo交互4

创建 ROS 2 与 Gazebo 通信(下视摄像头图像传输,将Gazebo话题转成ROS2话题)yaml参数文件:

gedit ~/.bashrc
export WORKSPACE=/path/to/ws_precision_visland
source .bashrc
echo $WORKSPACE		#检查环境变量是否配置成功

mkdir -p $WORKSPACE/src/ros_gz_bridge/config
cd $WORKSPACE/src/ros_gz_bridge/config
touch camera_bridge_config.yaml
sudo vim camera_bridge_config.yaml

camera_bridge_config.yaml参数文件:

# 桥接配置:Gazebo到ROS 2
- ros_topic_name: "camera/image_raw"
  gz_topic_name: "camera/rgb/image"
  ros_type_name: "sensor_msgs/msg/Image"
  gz_type_name: "ignition.msgs.Image"
  subscriber_queue: 10
  publisher_queue: 10
  lazy: false
  direction: GZ_TO_ROS

# 桥接配置:ROS 2到Gazebo
- ros_topic_name: "camera/image_raw"
  gz_topic_name: "camera/rgb/image"
  ros_type_name: "sensor_msgs/msg/Image"
  gz_type_name: "ignition.msgs.Image"
  subscriber_queue: 10
  publisher_queue: 10
  lazy: false
  direction: ROS_TO_GZ

运行:

cd /path/to/PX4-Autopilot
PX4_GZ_WORLD=aruco make px4_sitl gz_x500_mono_cam_down	
gz topic -e -t /camera	#必须在命令行中运行,才能在Gazebo UI中使用Image Display查看图片。
ros2 run ros_gz_bridge parameter_bridge --ros-args -p config_file:=$WORKSPACE/src/ros_gz_bridge/config/camera_bridge_config.yaml

在Gazebo UI中查看/camera话题内容
在Gazebo UI中查看/camera话题内容

  1. 安装QGC5

在终端中输入:

sudo usermod -a -G dialout $USER
sudo apt-get remove modemmanager -y
sudo apt install gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-gl -y
sudo apt install libqt5gui5 -y
sudo apt install libfuse2 -y

注销并再次登录以启用用户权限的更改。
点击link下载QGroundControl.AppImage

cd /path/to/QGroundControl.AppImage
sudo chmod +x ./QGroundControl.AppImage
./QGroundControl.AppImage #运行

目前对于Gazebo-Harmonic似乎并不支持硬件在环仿真(HIL),后续还会用上虚拟操纵杆6

  1. 使用GPU渲染仿真画面7

gedit ~/.bashrc
#在.bashrc文件中添加
export __NV_PRIME_RENDER_OFFLOAD=1
export __GLX_VENDOR_LIBRARY_NAME=nvidia
  1. 安装ArUco软件包8

cd $WORKSPACE/src
git clone https://mirror.ghproxy.com/https://github.com/pal-robotics/aruco_ros.git -b humble-devel

#修改$WORKSPACE/src/aruco_ros/aruco_ros/launch/single.launch.py
#注意,修改.launch.py文件后还是需要colcon build一下
##修改remappings,为camera发布的topic
aruco_single = Node(
    package='aruco_ros',
    executable='single',
    parameters=[aruco_single_params],
    remappings=[('/camera_info', '/camera1/camera_info'),
               ('/image', '/camera1/image_raw')],
)
##修改 marker_id,为生成的marker值
marker_id_arg = DeclareLaunchArgument(
    'marker_id', default_value='582',
    description='Marker ID. '
)
##修改 marker_size,为生成的marker尺寸
marker_size_arg = DeclareLaunchArgument(
    'marker_size', default_value='0.10',
    description='Marker size in m. '
)

cd ..
#Warning:此处编译需要依次编译,aruco_ros依赖于aruco和aruco_msgs
colcon build --packages-select aruco
colcon build --packages-select aruco_msgs
colcon build --packages-select aruco_ros

source install/setup.bash

ros2 launch usb_cam camera.launch.py # 终端1、启动相机
ros2 launch aruco_ros single.launch.py # 终端2、启动aruco_ros
ros2 topic echo /aruco_single/pose # 终端3、查看topic输出
#查看话题,输出结果图像:/aruco_single/result

ros2仿真Gazebo(gz)所在地址:/path/to/PX4-Autopilot/Tools/simulation/gz,然后找到PX4固件中存储aruco.sdf的地方,改变aruco码,与$WORKSPACE/src/aruco_ros/aruco_ros/launch/single.launch.py文件中的marker_id和marker_size一致。目前该ArUco识别仅支持字典Original ArUco.
![Aruco码生成图](https://i-blog.csdnimg.cn/direct/534e48bc9028457aaa0c7ecde576e4a3.png![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/8b5fe28a02b94b2fa0b1983f748797cd.png在这里插入图片描述

aruco码在线生成网址:link
SVGtoPNG在线网址:link
在线图片编辑器:link

  1. 运行测试

#运行MicroXRCEAgen客户端
cd /path/to/Micro-XRCE-DDS-Agent/build
./MicroXRCEAgent udp4 -p 8888

#运行PX4仿真环境
cd /path/to/PX4-Autopilot
PX4_GZ_WORLD=aruco make px4_sitl gz_x500_mono_cam_down	

#打通Gazebo与ROS2图像通信
ros2 run ros_gz_bridge parameter_bridge --ros-args -p config_file:=$WORKSPACE/src/ros_gz_bridge/config/camera_bridge_config.yaml

#打开QGC
cd /path/to/QGroundControl.AppImage
./QGroundControl.AppImage 

ros2 launch aruco_ros single.launch.py # 终端2、启动aruco_ros
ros2 topic echo /aruco_single/pose # 终端3、查看topic输出
ros2 run rqt_image_view rqt_image_view # 查看aruco识别结果

#运行offboard测试程序中的起飞测试程序
cd $WORKSPACE
source install/setup.bash 
ros2 run px4_ros_com offboard_control

![运行结果图](https://i-blog.csdnimg.cn/direct/f6f15c5797ab4f2a9278055e605a3591.png在这里插入图片描述


  1. 小鱼一键安装:link ↩︎

  2. PX4官方文档(v1.15):link ↩︎

  3. ROS2使用usb_cam:link ↩︎

  4. Gazebo-Harmonic官方手册:link ↩︎

  5. QGC官方手册:link ↩︎

  6. 使用GPU渲染画面:link ↩︎

  7. 开启QGC虚拟摇杆:link ↩︎

  8. 安装ArUco软件包:link ↩︎

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐