从0入门自主空中机器人-5-Airsim-PX4仿真环境搭建

AirSim项目地址:https://github.com/microsoft/AirSim

AirSim官方教程:Home - AirSim (microsoft.github.io)

CSDN参考教程: AirSim学习(1)安装Unreal Engine和AirSim

视频教程:

1.AirSim-Windows环境配置

注:Windows环境需要自行安装VS2022或以上版本,Unreal Engine >= 4.27(推荐4.27),以及wsl2(在Windows上运行Airsim,在wsl或者vm虚拟机上通过ros控制无人机)

  • 安装虚拟引擎

    • 1.【下载并安装EPIC】 ,虽然虚幻引擎是开源和免费下载的,但仍然需要注册

    • 2.运行Epic Games Launcher,打开左侧窗格中的 “虚幻引擎” 选项卡。单击右上角的 “安装” 按钮,该按钮应显示下载 UE> = 4.27(推荐4.27) 的选项。选择安装位置以满足您的需求,如下图所示。如果您安装了多个版本的Unreal,请单击该版本的启动按钮旁边的向下箭头,以确保将您使用的版本设置为 “当前”。

      Note: 如果您有UE 4.16或更旧的项目,请参阅 [升级指南](https:// microsoft.github.io/AirSim/unreal_upgrade/) 来升级您的项目。

  • 安装VS2022

    • 1.Visual Studio2022安装教程 (qq.com)。确保选择安装时勾选了C ++桌面开发 和**Windows 10 SDK 10.0.19041(如果是Windows11还需要选择Windows11的SDK)**进行桌面开发 (默认情况下应选择),然后选择最新的。NET Framework SDK下的 “单个组件” 选项卡,同时安装VS 2022。

  • Windows安装wsl2

1.1Build Airsim

  1. 下载源码到本地

    1
    2
    3
    git clone https://github.com/microsoft/AirSim.git
    # 国内网络环境不好时选择下面
    git clone https://github.moeyy.xyz/https://github.com/microsoft/AirSim.git
  2. 安装依赖项并编译

    1. 【下载并安装Cmake】,安装时记得勾选添加到环境中

    2. 安装Cmake完后重启电脑以生效环境,打开Developer Command Prompt for VS 2022,在Windows搜索中搜索然后使用管理员打开,cd AirSim记得cd进入Airsim所在的目录

    3. 输入.\build.cmd进行编译Airsim,如果在下面的过程中卡壳了,Ctrl+C终止编译,然后重新编译,直到下载完成并正常编译完成

  1. 使用VS2022编译打开

    1. 使用Developer Command Prompt for VS 2022进入AirSim\Unreal\Environments\Blocks,执行.\update_from_git.bat

    2. 双击打开Blocks.sln,会自动启动VS2022

    3. 右键Block设为为启动项目

    4. 编译器选择DebugGame Editor,然后启动本地Windows调试器,编译完成自动打开虚幻引擎

    5. 点击运行,点击**‘是’选择仿真车,点击’否’**选择仿真无人机,当出现下面的画面则表示环境搭建成功

1.2自定义场景

创建一个自定义的虚拟场景,并通过Airsim加载

  1. 打开Epic -> 点击虚幻商城 -> 搜索环境类型的素材(推荐免费:搜索"Assetsville Town或City Park" -> 筛选结果选择免费 -> 添加致购物车进行购买)-> 点击上方的,选择刚才购买的City Park创建工程,选定路径

    image-20240611162643650

    image-20240611163507427

  2. 导入完成后双击打开该工程

    注:如果加载过程中黑屏或者卡住可以重启Unreal Engine,如果实在打开太卡或太慢可以选择上面购买City Park第二个那个轻量版地图

    image-20240611192338259

  3. 关闭工程,将AirSim\Unreal\Plugins文件夹复制到刚才的工程目录下,然后双击CityParkEnvironmentMini.uproject,弹出Rebuild窗口,点击

    image-20240611195006551

  4. 重新编译完成后,右下角弹出更新插件,点击管理插件即可看到AirSim插件添加到工程中,然后点击功能栏的设置 -> 世界场景设置 -> 游戏模式重载选择AirSimGameMode

    image-20240611195450875

    image-20240611195844988

  5. 点击运行,如果出现弹窗,选择启动车辆仿真,启动无人机仿真,按F1查看帮助

    image-20240611200347055

    1. 修改配置文件,前往C:\User\{你的用户名}\Documents\AirSim\Settings.json,文件配置项参考https://microsoft.github.io/AirSim/settings/,以下是推荐的配置文件,需要搭配第二部分的设置使用,其中LocalHostIp为Windows本机的IP地址,通过cmd指令输入ipconfig进行查看,一般为192.168.1.x,或者其他,你可以通过WSL或者VM虚拟机ping一下看看通不通

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      {
      "SettingsVersion": 1.2,
      "SimMode": "Multirotor",
      "ClockType": "SteppableClock",
      "Vehicles": {
      "PX4": {
      "VehicleType": "PX4Multirotor",
      "UseSerial": false,
      "LockStep": true,
      "UseTcp": true,
      "TcpPort": 4560,
      "ControlIp": "remote",
      "ControlPortLocal": 14540,
      "ControlPortRemote": 14580,
      "LocalHostIp": "192.168.1.116",
      "Sensors":{
      "Barometer":{
      "SensorType": 1,
      "Enabled": true,
      "PressureFactorSigma": 0.0001825
      },
      "Imu": {
      "SensorType": 2,
      "Enabled" : true,
      "AngularRandomWalk": 0.3,
      "GyroBiasStabilityTau": 500,
      "GyroBiasStability": 4.6,
      "VelocityRandomWalk": 0.24,
      "AccelBiasStabilityTau": 800,
      "AccelBiasStability": 36
      },
      "Gps": {
      "SensorType": 3,
      "Enabled" : true,
      "EphTimeConstant": 0.9,
      "EpvTimeConstant": 0.9,
      "EphInitial": 25,
      "EpvInitial": 25,
      "EphFinal": 0.1,
      "EpvFinal": 0.1,
      "EphMin3d": 3,
      "EphMin2d": 4,
      "UpdateLatency": 0.2,
      "UpdateFrequency": 50,
      "StartupDelay": 1
      },
      "Distance": {
      "SensorType": 5,
      "Enabled" : true,
      "MinDistance": 0.2,
      "MaxDistance": 40,
      "X": 0, "Y": 0, "Z": -1,
      "Yaw": 0, "Pitch": 0, "Roll": 0,
      "DrawDebugPoints": false
      }
      },
      "Parameters": {
      "NAV_RCL_ACT": 0,
      "NAV_DLL_ACT": 0,
      "COM_OBL_ACT": 1,
      "LPE_LAT": 47.641468,
      "LPE_LON": -122.140165
      }
      }
      }
      }

    2. WSL2或者VM虚拟机环境配置

    注:WSL或VM虚拟机需要如下环境

    1. 复制一份AirSim工程目录到WSLVM虚拟机

      1
      2
      3
      4
      5
      6
      7
      8
      # Ubuntu18
      sudo apt-get install ros-melodic-tf2-sensor-msgs ros-melodic-tf2-geometry-msgs ros-melodic-mavros*
      # Ubuntu20
      sudo apt-get install ros-noetic-tf2-sensor-msgs ros-noetic-tf2-geometry-msgs ros-noetic-mavros*
      # 初试化AirSim
      cd AirSim
      sudo bash ./setup.sh
      sudo bash ./build.sh
    2. 编译ROS包

      1
      2
      3
      4
      5
      6
      # ROS1
      cd AirSim/ros
      catkin_make
      # ROS2
      cd AirSim/ros2
      colcon build
    3. 配置~/.bashrc环境

      1
      2
      3
      4
      5
      # ip地址为运行airsim的Windows的ip
      echo "export PX4_SIM_HOST_ADDR=192.168.1.116" >> ~/.bashrc
      # 记得查看一下自己的路径对不对
      echo "source ~/Airsim/ros/devel/setup.sh" >> ~/.bashrc
      source ~/.bashrc
    4. 开放防火墙(如果没有装防火墙就不需要这一步了)

      1
      2
      sudo ufw allow 4560
      sudo ufuw allow 10049
  6. 在Window中启动AirSim(记得修改配置文件后重新启动),窗口显示等待TCP连接

    image-20240612155513180

  7. WSL或者VM虚拟机中启动PX4仿真ROS&rivz

    1
    2
    cd ~/PX4-Autopilot
    make px4_sitl_default none_iris

    出现下面的提示则说明PX4与AirSim连接成功

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    PX4 SIM HOST: 192.168.1.117
    INFO [simulator] Simulator using TCP on remote host 192.168.1.117 port 4560
    WARN [simulator] Please ensure port 4560 is not blocked by a firewall.
    INFO [simulator] Waiting for simulator to accept connection on TCP port 4560
    INFO [simulator] Simulator connected on TCP port 4560.
    INFO [commander] LED: open /dev/led0 failed (22)
    INFO [init] Mixer: etc/mixers/quad_w.main.mix on /dev/pwm_output0
    INFO [init] setting PWM_AUX_OUT none
    INFO [mavlink] mode: Normal, data rate: 4000000 B/s on udp port 18570 remote port 14550
    INFO [mavlink] mode: Onboard, data rate: 4000000 B/s on udp port 14580 remote port 14540
    INFO [mavlink] mode: Onboard, data rate: 4000 B/s on udp port 14280 remote port 14030
    INFO [mavlink] mode: Gimbal, data rate: 400000 B/s on udp port 13030 remote port 13280
    INFO [logger] logger started (mode=all)
    INFO [logger] Start file log (type: full)
    INFO [logger] [logger] ./log/2024-06-12/08_20_41.ulg
    INFO [logger] Opened full log file: ./log/2024-06-12/08_20_41.ulg
    INFO [mavlink] MAVLink only on localhost (set param MAV_{i}_BROADCAST = 1 to enable network)
    INFO [mavlink] MAVLink only on localhost (set param MAV_{i}_BROADCAST = 1 to enable network)
    INFO [px4] Startup script returned successfully
    pxh> INFO [tone_alarm] home set
    INFO [tone_alarm] notify negative

    启动ROS&rivz

    1
    2
    roslaunch airsim_ros_pkgs airsim_node.launch output:=screen host:=$PX4_SIM_HOST_ADDR
    roslaunch airsim_ros_pkgs rviz.launch

    通过rostopic list查看有没有一下主题即可说明是否连接正常

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    laohanba@ubuntu:~/Airsim$ rostopic list
    /airsim_node/PX4/altimeter/Barometer
    /airsim_node/PX4/distance/Distance
    /airsim_node/PX4/environment
    /airsim_node/PX4/global_gps
    /airsim_node/PX4/gps/Gps
    /airsim_node/PX4/imu/Imu
    /airsim_node/PX4/magnetometer/magnetometer
    /airsim_node/PX4/odom_local_ned
    /airsim_node/PX4/vel_cmd_body_frame
    /airsim_node/PX4/vel_cmd_world_frame
    /airsim_node/gimbal_angle_euler_cmd
    /airsim_node/gimbal_angle_quat_cmd
    /airsim_node/origin_geo_point
    /rosout
    /rosout_agg
    /tf
    /tf_static

各个话题和订阅者功能如下:

  • Publishers:#

    • /airsim_node/origin_geo_point airsim_ros_pkgs/GPSYaw 与全球 NED 框架相对应的 GPS 坐标。这将在 airsim 的 settings.json 文件中的 OriginGeopoint 键下进行设置

    • /airsim_node/VEHICLE_NAME/global_gps sensor_msgs/NavSatFix 这是无人机在 airsim 中的当前 GPS 坐标

    • /airsim_node/VEHICLE_NAME/odom_local_ned nav_msgs/Odometry NED 框架(默认名称:oddom_local_ned,发射名称和框架类型可配置)中与起飞点的测距。

    • /airsim_node/VEHICLE_NAME/CAMERA_NAME/IMAGE_TYPE/camera_info sensor_msgs/CameraInfo

    • /airsim_node/VEHICLE_NAME/CAMERA_NAME/IMAGE_TYPE sensor_msgs/Image RGB 或浮点图像,取决于 settings.json 中要求的图像类型。

    • /tf tf2_msgs/TFMessage

    • /airsim_node/VEHICLE_NAME/altimeter/SENSOR_NAME airsim_ros_pkgs/Altimeter 这是高度计当前的高度、压力和温度读数 QNH

    • /airsim_node/VEHICLE_NAME/imu/SENSOR_NAME sensor_msgs::Imu IMU 传感器数据

    • /airsim_node/VEHICLE_NAME/magnetometer/SENSOR_NAME sensor_msgs::MagneticField 测量磁场矢量/罗盘

    • /airsim_node/VEHICLE_NAME/distance/SENSOR_NAME sensor_msgs::Range 测量与活动者的距离,如红外线或红外信号

    • /airsim_node/VEHICLE_NAME/lidar/SENSOR_NAME sensor_msgs::PointCloud2 激光雷达点云

  • Subscribers:#

  • Services:#

  • Parameters:#

    • /airsim_node/world_frame_id [string] Set in: $(airsim_ros_pkgs)/launch/airsim_node.launch 默认值:world_ned 设置为 "world_enu "可自动切换到 ENU 框架

    • /airsim_node/odom_frame_id [string] Set in: $(airsim_ros_pkgs)/launch/airsim_node.launch 默认值: odom_local_ned 如果将 world_frame_id 设置为 “world_enu”,则默认 odom 名称将改为 “odom_local_enu”

    • /airsim_node/coordinate_system_enu [boolean] Set in: $(airsim_ros_pkgs)/launch/airsim_node.launch 默认值:false 如果将 world_frame_id 设置为 “world_enu”,该设置将默认为 true

    • /airsim_node/update_airsim_control_every_n_sec [double] Set in: $(airsim_ros_pkgs)/launch/airsim_node.launch 默认值:0.01 秒。计时器回调频率,用于从 airsim 更新无人机 Odom 和状态,以及发送控制命令。当前 RPClib 与虚幻引擎的接口最大频率为 50 赫兹。ROS 中的定时器回调以最大速率运行,因此最好不要触及此参数

    • /airsim_node/update_airsim_img_response_every_n_sec [double] Set in: $(airsim_ros_pkgs)/launch/airsim_node.launch 默认值:0.01 秒。从 airsim 中所有摄像机接收图像的定时器回调频率。速度取决于请求图像的数量和分辨率。ROS 中的定时回调以最大可能的速度运行,因此最好不要触及此参数

    • /airsim_node/publish_clock [double] Set in: $(airsim_ros_pkgs)/launch/airsim_node.launch 默认值:false 设置为 true 时,将发布 ros /clock 主题

注:启动PX4连接成功后,可以通过QGC地面站一键起飞无人机

gif3