本教程将指导您如何在PX4仿真环境中为无人机添加激光雷达、双目相机和下视摄像头,以便进行算法测试和开发。

1. 创建传感器模型

首先,我们需要创建或确保已有所需的传感器模型。

1.1 创建文件夹结构

# 创建传感器模型目录
mkdir -p ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/iris_fusion
mkdir -p ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/kinect_self
mkdir -p ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/rplidar
# 如果下视摄像头模型不存在
mkdir -p ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/fpv_cam

1.2 创建双目相机(Kinect)模型

nano ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/kinect_self/kinect_self.sdf

填入以下内容:

<?xml version="1.0" ?>
<sdf version="1.5">
  <model name="kinect_self">
    <pose>0 0 0 0 0 0</pose>
    <link name="link">
      <inertial>
        <mass>0.01</mass>
        <inertia>
          <ixx>4.16666666667e-06</ixx>
          <iyy>5.20833333333e-07</iyy>
          <izz>3.85416666667e-06</izz>
        </inertia>
      </inertial>
      <collision name="collision">
        <geometry>
          <box>
            <size>0.023000 0.076000 0.032000</size>
          </box>
        </geometry>
      </collision>
      <visual name="visual">
        <geometry>
          <box>
            <size>0.023000 0.076000 0.032000</size>
          </box>
        </geometry>
        <material>
          <script>
            <uri>file://media/materials/scripts/gazebo.material</uri>
            <name>Gazebo/Red</name>
          </script>
        </material>
      </visual>
      <sensor type="depth" name="camera">
        <always_on>true</always_on>
        <update_rate>30.0</update_rate>
        <camera>
          <horizontal_fov>1.3962634</horizontal_fov>
          <image>
            <format>B8G8R8</format>
            <width>640</width>
            <height>480</height>
          </image>
          <clip>
            <near>0.4</near>
            <far>16.0</far>
          </clip>
        </camera>
        <plugin name="camera_plugin" filename="libgazebo_ros_openni_kinect.so">
          <baseline>0.2</baseline>
          <alwaysOn>true</alwaysOn>
          <updateRate>0.0</updateRate>
          <cameraName>camera_ir</cameraName>
          <imageTopicName>/camera/color/image_raw</imageTopicName>
          <cameraInfoTopicName>/camera/color/camera_info</cameraInfoTopicName>
          <depthImageTopicName>/camera/depth/image_raw</depthImageTopicName>
          <depthImageCameraInfoTopicName>/camera/depth/camera_info</depthImageCameraInfoTopicName>
          <pointCloudTopicName>/camera/depth/points</pointCloudTopicName>
          <frameName>camera_link</frameName>
          <pointCloudCutoff>0.5</pointCloudCutoff>
          <pointCloudCutoffMax>8.0</pointCloudCutoffMax>
          <distortionK1>0</distortionK1>
          <distortionK2>0</distortionK2>
          <distortionK3>0</distortionK3>
          <distortionT1>0</distortionT1>
          <distortionT2>0</distortionT2>
          <CxPrime>0</CxPrime>
          <Cx>0</Cx>
          <Cy>0</Cy>
          <focalLength>0</focalLength>
          <hackBaseline>0</hackBaseline>
        </plugin>
      </sensor>
      <gravity>0</gravity>
    </link>
  </model>
</sdf>

创建模型配置文件:

nano ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/kinect_self/model.config

填入以下内容:

<?xml version="1.0"?>
<model>
  <name>Kinect_self</name>
  <version>1.0</version>
  <sdf version="1.5">kinect_self.sdf</sdf>
  <description>
    Kinect 3D camera
  </description>
</model>

1.3 创建激光雷达(RPLidar)模型

nano ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/rplidar/rplidar.sdf

填入以下内容:

<?xml version="1.0" ?>
<sdf version="1.5">
  <model name="rplidar">
    <link name="link">
      <inertial>
        <pose>0 0 0 0 0 0</pose>
        <mass>0.19</mass>
        <inertia>
          <ixx>4.15e-6</ixx>
          <ixy>0</ixy>
          <ixz>0</ixz>
          <iyy>2.407e-6</iyy>
          <iyz>0</iyz>
          <izz>2.407e-6</izz>
        </inertia>
      </inertial>
      <visual name="visual">
        <geometry>
          <cylinder>
            <radius>0.04</radius>
            <length>0.05</length>
          </cylinder>
        </geometry>
        <material>
          <script>
            <uri>file://media/materials/scripts/gazebo.material</uri>
            <name>Gazebo/Black</name>
          </script>
        </material>
      </visual>
      <collision name="collision">
        <geometry>
          <cylinder>
            <radius>0.04</radius>
            <length>0.05</length>
          </cylinder>
        </geometry>
      </collision>
      <sensor name="laser" type="ray">
        <pose>0 0 0 0 0 0</pose>
        <visualize>true</visualize>
        <update_rate>10</update_rate>
        <ray>
          <scan>
            <horizontal>
              <samples>360</samples>
              <resolution>1</resolution>
              <min_angle>-3.14159</min_angle>
              <max_angle>3.14159</max_angle>
            </horizontal>
          </scan>
          <range>
            <min>0.2</min>
            <max>12.0</max>
            <resolution>0.05</resolution>
          </range>
          <noise>
            <type>gaussian</type>
            <mean>0.0</mean>
            <stddev>0.01</stddev>
          </noise>
        </ray>
        <plugin name="laser_controller" filename="libgazebo_ros_laser.so">
          <topicName>/laser/scan</topicName>
          <frameName>rplidar_link</frameName>
        </plugin>
      </sensor>
    </link>
  </model>
</sdf>

创建模型配置文件:

nano ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/rplidar/model.config

填入以下内容:

<?xml version="1.0"?>
<model>
  <name>RPLidar</name>
  <version>1.0</version>
  <sdf version="1.5">rplidar.sdf</sdf>
  <description>
    RPLidar A2 2D laser scanner
  </description>
</model>

1.4 创建下视摄像头(FPV Cam)模型

nano ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/fpv_cam/fpv_cam.sdf

填入以下内容:

<?xml version="1.0" ?>
<sdf version="1.5">
  <model name="fpv_cam">
    <pose>0 0 0.05 0 0 0</pose>
    <link name="link">
      <inertial>
        <mass>0.015</mass>
        <inertia>
          <ixx>1e-05</ixx>
          <iyy>1e-05</iyy>
          <izz>1e-05</izz>
        </inertia>
      </inertial>
      <collision name="collision">
        <geometry>
          <cylinder>
            <radius>0.025</radius>
            <length>0.025</length>
          </cylinder>
        </geometry>
      </collision>
      <visual name="visual">
        <geometry>
          <cylinder>
            <radius>0.025</radius>
            <length>0.025</length>
          </cylinder>
        </geometry>
        <material>
          <script>
            <uri>file://media/materials/scripts/gazebo.material</uri>
            <name>Gazebo/Blue</name>
          </script>
        </material>
      </visual>
      <sensor name="camera" type="camera">
        <camera>
          <horizontal_fov>2.0</horizontal_fov>
          <image>
            <width>640</width>
            <height>480</height>
          </image>
          <clip>
            <near>0.05</near>
            <far>100</far>
          </clip>
        </camera>
        <always_on>1</always_on>
        <update_rate>30</update_rate>
        <visualize>true</visualize>
        <plugin name="camera_controller" filename="libgazebo_ros_camera.so">
          <alwaysOn>true</alwaysOn>
          <updateRate>30.0</updateRate>
          <cameraName>fpv_cam</cameraName>
          <imageTopicName>image_raw</imageTopicName>
          <cameraInfoTopicName>camera_info</cameraInfoTopicName>
          <frameName>fpv_cam_link</frameName>
        </plugin>
      </sensor>
    </link>
  </model>
</sdf>

创建模型配置文件:

nano ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/fpv_cam/model.config

填入以下内容:

<?xml version="1.0"?>
<model>
  <name>FPV Camera</name>
  <version>1.0</version>
  <sdf version="1.5">fpv_cam.sdf</sdf>
  <description>
    A simple FPV camera
  </description>
</model>

1.5 创建集成无人机模型

nano ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/iris_fusion/iris_fusion.sdf

填入以下内容:

<?xml version='1.0'?>
<sdf version='1.5'>
  <model name='iris_fusion'>

    <include>
      <uri>model://iris</uri>
    </include>

    <!-- 双目相机 -->
    <include>
      <uri>model://kinect_self</uri>
      <pose>0.1 0 0 0 0 0</pose>
    </include>
    <joint name="kinect_self_joint" type="fixed">
      <child>kinect_self::link</child>
      <parent>iris::base_link</parent>
      <axis>
        <xyz>0 0 1</xyz>
        <limit>
          <upper>0</upper>
          <lower>0</lower>
        </limit>
      </axis>
    </joint>

    <!-- 激光雷达 -->
    <include>
      <uri>model://rplidar</uri>
      <pose>0 0 0.1 0 0 0</pose>
    </include>
    <joint name="rplidar_joint" type="fixed">
      <child>rplidar::link</child>
      <parent>iris::base_link</parent>
      <axis>
        <xyz>0 0 1</xyz>
        <limit>
          <upper>0</upper>
          <lower>0</lower>
        </limit>
      </axis>
    </joint>

    <!-- 下视摄像头 -->
    <include>
      <uri>model://fpv_cam</uri>
      <pose>0 0 0 0 1.57 0</pose>
    </include>
    <joint name="fpv_cam_joint" type="fixed">
      <child>fpv_cam::link</child>
      <parent>iris::base_link</parent>
      <axis>
        <xyz>0 0 1</xyz>
        <limit>
          <upper>0</upper>
          <lower>0</lower>
        </limit>
      </axis>
    </joint>

  </model>
</sdf>

创建模型配置文件:

nano ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/iris_fusion/model.config

填入以下内容:

<?xml version="1.0"?>
<model>
  <name>Iris with Sensors</name>
  <version>1.0</version>
  <sdf version='1.5'>iris_fusion.sdf</sdf>
  <description>
    This is a model of the Iris quadrotor with added sensors: Kinect, RPLidar and FPV camera.
  </description>
</model>

2. 修改启动文件

2.1 创建自定义启动文件

创建一个自定义启动文件,以免修改原始文件:

cp ~/PX4-Autopilot/launch/mavros_posix_sitl.launch ~/PX4-Autopilot/launch/mavros_posix_sitl_sensors.launch

2.2 编辑自定义启动文件

nano ~/PX4-Autopilot/launch/mavros_posix_sitl_sensors.launch

修改文件内容:

<?xml version="1.0"?>
<launch>
    <!-- MAVROS posix SITL environment launch script -->
    <!-- launches MAVROS, PX4 SITL, Gazebo environment, and spawns vehicle -->
    <!-- vehicle pose -->
    <arg name="x" default="0"/>
    <arg name="y" default="0"/>
    <arg name="z" default="0"/>
    <arg name="R" default="0"/>
    <arg name="P" default="0"/>
    <arg name="Y" default="0"/>
    <!-- vehicle model and world -->
    <arg name="est" default="ekf2"/>
    <arg name="vehicle" default="iris"/>
    <arg name="my_model" default="iris_fusion"/>
    <arg name="world" default="$(find mavlink_sitl_gazebo)/worlds/empty.world"/>
    <arg name="sdf" default="$(find mavlink_sitl_gazebo)/models/$(arg my_model)/$(arg my_model).sdf"/>

    <!-- gazebo configs -->
    <arg name="gui" default="true"/>
    <arg name="debug" default="false"/>
    <arg name="verbose" default="false"/>
    <arg name="paused" default="false"/>
    <arg name="respawn_gazebo" default="false"/>
    <!-- MAVROS configs -->
    <arg name="fcu_url" default="udp://:14540@localhost:14557"/>
    <arg name="respawn_mavros" default="false"/>
    <!-- PX4 configs -->
    <arg name="interactive" default="true"/>
    <!-- PX4 SITL and Gazebo -->
    <include file="$(find px4)/launch/posix_sitl.launch">
        <arg name="x" value="$(arg x)"/>
        <arg name="y" value="$(arg y)"/>
        <arg name="z" value="$(arg z)"/>
        <arg name="R" value="$(arg R)"/>
        <arg name="P" value="$(arg P)"/>
        <arg name="Y" value="$(arg Y)"/>
        <arg name="world" value="$(arg world)"/>
        <arg name="vehicle" value="$(arg vehicle)"/>
        <arg name="sdf" value="$(arg sdf)"/>
        <arg name="gui" value="$(arg gui)"/>
        <arg name="interactive" value="$(arg interactive)"/>
        <arg name="debug" value="$(arg debug)"/>
        <arg name="verbose" value="$(arg verbose)"/>
        <arg name="paused" value="$(arg paused)"/>
        <arg name="respawn_gazebo" value="$(arg respawn_gazebo)"/>
    </include>
    <!-- MAVROS -->
    <include file="$(find mavros)/launch/px4.launch">
        <!-- GCS link is provided by SITL -->
        <arg name="gcs_url" value=""/>
        <arg name="fcu_url" value="$(arg fcu_url)"/>
        <arg name="respawn_mavros" value="$(arg respawn_mavros)"/>
    </include>
</launch>

3. 创建启动脚本

为了方便启动,创建一个简单的启动脚本:

nano ~/start_px4_sensors.sh

填入以下内容:

#!/bin/bash

# 设置环境变量
source ~/PX4-Autopilot/Tools/simulation/gazebo-classic/setup_gazebo.bash ~/PX4-Autopilot ~/PX4-Autopilot/build/px4_sitl_default

# 设置ROS包路径
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot:~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic

# 创建临时链接以兼容旧的路径引用
if [ ! -d ~/PX4-Autopilot/Tools/mavlink_sitl_gazebo ]; then
    mkdir -p ~/PX4-Autopilot/Tools/mavlink_sitl_gazebo
    ln -s ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds ~/PX4-Autopilot/Tools/mavlink_sitl_gazebo/worlds
    ln -s ~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic/models ~/PX4-Autopilot/Tools/mavlink_sitl_gazebo/models
fi

# 启动仿真
roslaunch ~/PX4-Autopilot/launch/mavros_posix_sitl_sensors.launch

给脚本添加执行权限:

chmod +x ~/start_px4_sensors.sh

4. 运行仿真

~/start_px4_sensors.sh

5. 验证传感器数据

当仿真运行后,您可以通过以下命令检查传感器数据:

5.1 查看可用的话题

rostopic list

5.2 查看激光雷达数据

rostopic echo /laser/scan

5.3 查看前置相机(Kinect)图像

rosrun image_view image_view image:=/camera/color/image_raw

5.4 查看下视摄像头图像

rosrun image_view image_view image:=/fpv_cam/image_raw

5.5 查看点云数据

rosrun image_view image_view image:=/camera/depth/image_raw

Logo

欢迎加入西安开发者社区!我们致力于为西安地区的开发者提供学习、合作和成长的机会。参与我们的活动,与专家分享最新技术趋势,解决挑战,探索创新。加入我们,共同打造技术社区!

更多推荐