# ROS 与 SITL

## ROS 与 SITL[¶](https://ardupilot.org/dev/docs/ros-sitl.html#ros-with-sitl)

### 将 ArduPilot 连接到 ROS[¶](https://ardupilot.org/dev/docs/ros-sitl.html#connect-ardupilot-to-ros)

首先，一个好的 ROS 习惯是始终在工作区目录中工作，因此创建一个： 打开终端

```
mkdir -p ardupilot_ws/src
cd ardupilot_ws
catkin init
cd src
```

好的 ROS 已准备就绪，让我们启动一个 SITL 实例。 请参阅在 [Linux 上设置](https://ardupilot.org/dev/docs/setting-up-sitl-on-linux.html#setting-up-sitl-on-linux) SITL 以了解如何安装 SITL。 导航到您的 SITL 安装并启动直升机会话：

```
sim_vehicle.py -v ArduCopter --console --map
```

（–控制台显示 mavproxy 控制台，–地图以在地图上显示直升机）

现在你有一个使用 TCP 和 UDP 访问启动的 SITL 实例，你应该有一些这样的行：

```
"mavproxy.py" "--master" "tcp:127.0.0.1:5760" "--sitl" "127.0.0.1:5501" "--out" "127.0.0.1:14550" "--out" "127.0.0.1:14551" "--map" "--console"
```

这两个“–out”都是指由MAVProxy创建的UDP连接。我们将使用马夫罗斯的UDP访问。

回到您的 ROS 终端。让我们为启动文件创建一个新目录。

```
mkdir launch
cd launch
```

提示

编写启动文件比记住 ROS 命令更简单。

让我们复制 ArduPilot 的 MAVROS 默认启动文件：

```
roscp mavros apm.launch apm.launch
```

您的目录中应该有一个名为“apm.launch”的新文件。“roscp”命令只是在ROS包上调用系统命令的助手。 用你最喜欢的编辑器打开它，我的是 gedit。

```
gedit apm.launch
```

```
<launch>
    <!-- vim: set ft=xml noet : -->
    <!-- example launch script for ArduPilotMega based FCU's -->

    <arg name="fcu_url" default="/dev/ttyACM0:57600" /> <!-- Port et baudrate of the connexion with Pixhawk -->
    <arg name="gcs_url" default="" /> <!-- Retransmission to a GCS like Mavproxy does -->
    <arg name="tgt_system" default="1" /> <!-- MAVLink id of your drone, default is 1 -->
    <arg name="tgt_component" default="1" /> <!-- MAVLink component id of your drone, default is 1 -->
    <arg name="log_output" default="screen" /> <!-- Where ROS will output its message, screen is your current terminal -->

    <include file="$(find mavros)/launch/node.launch"> <!-- This launch file will launch another launch file -->
        <arg name="pluginlists_yaml" value="$(find mavros)/launch/apm_pluginlists.yaml" /> <!-- Mavros plugin configuration, we will modify that later -->
        <arg name="config_yaml" value="$(find mavros)/launch/apm_config.yaml" /> <!-- Mavros plugin list to use -->

        <arg name="fcu_url" value="$(arg fcu_url)" /> <!-- Pass the parameter to the other launch file -->
        <arg name="gcs_url" value="$(arg gcs_url)" />
        <arg name="tgt_system" value="$(arg tgt_system)" />
        <arg name="tgt_component" value="$(arg tgt_component)" />
        <arg name="log_output" value="$(arg log_output)" />
    </include>
</launch>
```

To connect to SITL we just need to modify the first line to . save you file and launch it with`<arg name="fcu_url" default="udp://127.0.0.1:14551@14555" />`

```
roslaunch apm.launch
```

You should see some verbose from MAVROS that read its configuration and some line that indicate a connexion:

```
[ INFO] [1496336768.500953284]: CON: Got HEARTBEAT, connected. FCU: ArduPilotMega / ArduCopter
[ INFO] [1496336768.536761724]: RC_CHANNELS message detected!
[ INFO] [1496336769.533950451]: VER: 1.1: Capabilities         0x0000000000001bcf
[ INFO] [1496336769.534021653]: VER: 1.1: Flight software:     03060000 (8a4a2722)
[ INFO] [1496336769.534146986]: VER: 1.1: Middleware software: 00000000 (        )
[ INFO] [1496336769.534195446]: VER: 1.1: OS software:         00000000 (        )
[ INFO] [1496336769.534280663]: VER: 1.1: Board hardware:      00000000
[ INFO] [1496336769.534309086]: VER: 1.1: VID/PID:             0000:0000
[ INFO] [1496336769.534331512]: VER: 1.1: UID:                 0000000000000000
[ WARN] [1496336769.534370049]: CMD: Unexpected command 520, result 0
[ INFO] [1496336778.533962739]: FCU: APM:Copter V3.6-dev (8a4a2722)
[ INFO] [1496336778.534247677]: FCU: Frame: QUAD
[ INFO] [1496336779.021134163]: PR: parameters list received
[ INFO] [1496336783.535151119]: WP: mission received
```

连接完成！

让我们使用 RQT 来了解 ArduPilot 信息在 ROS 中的显示方式。通常，MAVROS 将完成大部分翻译 MAVLink <–> ROS 打开另一个终端并启动 RQT

```
rqt
```

转到插件/主题/主题监视器 嘭！ !!!!您会看到 mavros 必须从 ArduPilot 信息中创建的所有主题，单击该框以查看当前值。 您可以在插件/机器人工具/运行时监视器中看到一切正常！

让我们尝试使用 mavros 更改模式： 转到插件/服务/服务调用方 将服务设置为 /mavros/set\_mode 将custom\_mode设置为“引导”，然后单击呼叫按钮 响应应该是真的，你可以在 /mavros/state 主题上查看该模式现在是 GUIDED 的。它应该在您的MAVProxy控制台中相同。

现在，您知道了使用 ArduPilot 的 ROS 的基础！ROS还有很多其他功能，您可以使用，例如绘图，3D可视化等。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yuelan-icus-organization.gitbook.io/pei-tao-ji-suan-ji/ros/ros-yu-sitl.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
