查看: 855|回复: 0

成功实现树莓派ROS2与PX4飞控通过以太网连接

[复制链接]

38

主题

756

帖子

1499

积分

金牌飞友

Rank: 6Rank: 6

积分
1499
飞币
741
注册时间
2017-9-27
发表于 2024-7-27 20:36:09 | 显示全部楼层 |阅读模式
参考PX4官方文档,成功实现树莓派ROS2与PX4飞控通过以太网连接,其中还是有很多坑。

ROS 2 User Guide
The ROS 2-PX4 architecture provides a deep integration between ROS 2 and PX4, allowing ROS 2 subscribers or publisher nodes to interface directly with PX4 uORB topics.https://docs.px4.io/main/en/ros2/user_guide.html#setup-micro-xrce-dds-agent-client

1.

=========================================
Overview

The application pipeline for ROS 2 is very straightforward, thanks to the use of the uXRCE-DDS communications middleware.

ps: 这里是PX4 而且是v1.14以后的版本,而不是其他的开源飞控平台!!!

px4_msgsPX4 Autopilot 项目的 ROS 2 消息定义。构建此软件包将生成将 ROS 2 节点与 PX4 内部连接所需的所有接口。
https://github.com/PX4/px4_msgs?tab=readme-ov-file#px4_msgs

2 Build a packagePutting packages in a workspace is especially valuable because you can build many packages at once by running colcon build in the workspace root. Otherwise, you would have to build each package individually.
https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Creating-Your-First-ROS2-Package.html#build-a-package


Installation & Setup

The supported ROS 2 platforms for PX4 development are ROS 2 "Humble" on Ubuntu 22.04, and ROS 2 "Foxy" on Ubuntu 20.04.

ROS 2 "Humble" is recommended because it is the current ROS 2 LTS distribution. ROS 2 "Foxy" reached end-of-life in May 2023, but is still stable and works with PX4.

INFO

PX4 is not as well tested on Ubuntu 22.04 as it is on Ubuntu 20.04 (at time of writing), and Ubuntu 20.04 is needed if you want to use Gazebo Classic.

To setup ROS 2 for use with PX4:

    Install PX4 (to use the PX4 simulator)

    Install ROS 2

    Setup Micro XRCE-DDS Agent & Client

    Build & Run ROS 2 Workspace

    PS:基本的四步

Other dependencies of the architecture that are installed automatically, such as Fast DDS, are not covered.
Install PX4

You need to install the PX4 development toolchain in order to use the simulator.

PS:如果不是用模拟机呢?是否有必要弄一个模拟环境?

问题就在这里,实际的飞控如何设置?

Install ROS 2

To install ROS 2 and its dependencies:

Setup Micro XRCE-DDS Agent & Client

PS:最重要的部分

For ROS 2 to communicate with PX4, uXRCE-DDS client must be running on PX4, connected to a micro XRCE-DDS agent running on the companion computer.
Setup the Agent

The agent can be installed onto the companion computer in a number of ways. Below we show how to build the agent "standalone" from source and connect to a client running on the PX4 simulator.

To setup and start the agent:

    Open a terminal.

    Enter the following commands to fetch and build the agent from source:
    gitclone https://github.com/eProsima/Micro-XRCE-DDS-Agent.gitcdMicro-XRCE-DDS-Agentmkdirbuildcdbuildcmake..makesudomake installsudoldconfig /usr/local/lib/
    Start the agent with settings for connecting to the uXRCE-DDS client running on the simulator:
    MicroXRCEAgent udp4 -p 8888

The agent is now running, but you won't see much until we start PX4 (in the next step).

INFO

You can leave the agent running in this terminal! Note that only one agent is allowed per connection channel.
=========================================

PS:官方文档给的是模拟机上面的做法。针对实际飞控通过在飞控控制器中uxrce_dds_client相关命令来启动。

uxrce_dds_clientstart-tudp-p 8888 -h 192.168.0.3nsh> uxrce_dds_client Usage: uxrce_dds_client <command> [arguments...] Commands:
   start     [-t <val>]  Transport protocol                 values: serial|udp, default: udp     [-d <val>]  serial device                 values: <file:dev>     [-b <val>]  Baudrate (can also be p:<param_name>)default: 0     [-h <val>]  Agent IP. If not provided, defaults to UXRCE_DDS_AG_IP                 values: <IP>     [-p <val>]  Agent listening port. If not provided, defaults to UXRCE_DDS_PRT     [-l]        Restrict to localhost (use in combination with ROS_LOCALHOST_ONLY=1)     [-c]        Use custom participant config (profile_name="px4_participant")     [-n <val>]  Client DDS namespace
   stop  # 停止
   status        print status info  #查看状态,本文就是通过查看状态发现IP设置问题。===========官方文档模拟机上的做法未验证=====

PS:重要的是验证PX4 和 ROS2是否安装好agent and client

======================================


但是多处尝试仍然没有达到上面结果

20240723更新,在多次检查之后发现问题

首先检查飞控的IP设置如下图

成功实现树莓派ROS2与PX4飞控通过以太网连接w2.jpg

飞控的的静态IP192.168.0.4

检查飞控中DDS状态,如下图

发现DDS agent的IP为127.0.1.2 !!! 这个就是DDS client 与Agent 无法联通的根源了。

成功实现树莓派ROS2与PX4飞控通过以太网连接w3.jpg

检查PI中的我们已经设置好的IP地址如下图

成功实现树莓派ROS2与PX4飞控通过以太网连接w4.jpg

找到了问题,就是如何解决了,当然就是将飞控中的agentIP设置改为192.168.0.3
uxrce_dds_clientstart-tudp-p 8888 -h 192.168.0.3
如果xrce_dds_client已经启动需要先停止
uxrce_dds_client stop
再执行,看到期望已久的结果

成功实现树莓派ROS2与PX4飞控通过以太网连接w5.jpg

再看PI上面,收到了飞控相关的信息。

成功实现树莓派ROS2与PX4飞控通过以太网连接w6.jpg

=========上面的测试验证了Agent 和Client已经安装好能够连接上了。问题,飞控断电之后情况怎么样呢?IP又回到了原来的默认IP。

成功实现树莓派ROS2与PX4飞控通过以太网连接w7.jpg

如何来设置飞控中DDS agent 静态IP呢?

在QGC中找到UXRCE_DDS_AG_IP如下图所示

成功实现树莓派ROS2与PX4飞控通过以太网连接w8.jpg

其中的数字是IP的32进制的数字表示,

成功实现树莓派ROS2与PX4飞控通过以太网连接w9.jpg

这样只需要把要设置的IP192.168.0.3数字转为对应的32进制的,

成功实现树莓派ROS2与PX4飞控通过以太网连接w10.jpg

但是复制之后变成了下图

成功实现树莓派ROS2与PX4飞控通过以太网连接w11.jpg

重新启动飞控测试是否修改成功,如下图所示,修改成功

成功实现树莓派ROS2与PX4飞控通过以太网连接w12.jpg

=======以下是静态IP的设置===================

Get The Pixhawk Raspberry Pi CM4 Baseboard By Holybro Talking With PX4by Farhang Naderi | May 31, 2023 | Featured Post, Tips and Tutorials | 0 comments Summary参考这边文章可以实现飞控与树莓派之间相互PING通,表示两个硬件的连接已经正常了
https://px4.io/get-the-pixhawk-raspberry-pi-cm4-baseboard-by-holybro-talking-with-px4/

成功实现树莓派ROS2与PX4飞控通过以太网连接w13.jpg

成功实现树莓派ROS2与PX4飞控通过以太网连接w14.jpg

=========

PS:经过测试发现,如果树莓派断电,相关的IP设置数据不会被保存,PING192.168.0.4会不通。能否保存IP数据?

成功实现树莓派ROS2与PX4飞控通过以太网连接w15.jpg

PX4飞控可以保存IP数据,但是,前几次都无法ping通,一通乱操作之后又可以ping通。

=========================================

成功实现树莓派ROS2与PX4飞控通过以太网连接w16.jpg

PI中IP包括三类IP

===========================================

PS:下面这个方法试了两次都死机了。

成功实现树莓派ROS2与PX4飞控通过以太网连接w17.jpg

=================================

经过多处尝试,通过设置netplan,为PI配置了静态IP,注意要将

下图中的enp2s0修改为eth0!!!

成功实现树莓派ROS2与PX4飞控通过以太网连接w18.jpg

IP按照自己的实际调整为对应的IP.

成功实现树莓派ROS2与PX4飞控通过以太网连接w19.jpg

成功PING通如下图。

成功实现树莓派ROS2与PX4飞控通过以太网连接w20.jpg

设置方法如下:

To setup the Ubuntu Computer:

    In a terminal, create and open a netplan configuration file: /etc/netplan/01-network-manager-all.yaml Below we do this using the nano text editor.

    sudo nano /etc/netplan/01-network-manager-all.yaml
    Copy and paste the following configuration information into the file (note: the indentations are important!):

    network:
      version: 2
      renderer: NetworkManager
      ethernets:
        enp2s0: #调整为eth0
              addresses:
                  - 10.41.10.1/24  #IP根据实际设置调整
              nameservers:
                  addresses: [10.41.10.1]
              routes:
                  - to: 10.41.10.1
                    via: 10.41.10.1
    Save and exit the editor.

    Apply the netplan configuration by entering the following command into the Ubuntu terminal.

    sudo netplan apply

To setup the Ubuntu Computer:In a terminal, create and open a netplan configuration file: /etc/netplan/01-network-manager-all.yaml Below we do this using the nano text editor.sudo nano /etc/netplan/01-network-manager-all.yaml
https://docs.px4.io/main/zh/advanced_config/ethernet_setup.html


PI重启之后再次验证ip addr show,静态IP数据没有变化。

成功实现树莓派ROS2与PX4飞控通过以太网连接w21.jpg

Ping PX4飞控IP192.168.0.4 正常,如下图所示。

成功实现树莓派ROS2与PX4飞控通过以太网连接w22.jpg

至此PI与PX4飞控的IP设置完成,通过以太网实现正常连接。

总结通过PX4官方文档以太网设置就能够完成相关设置,只是其中一个“enp2s0”需要替换为“eth0”!!!

PX4 Ethernet SetupEthernet connectivity provides a fast, reliable, and flexible communication alternative to using USB or other serial connections.It can be used to connect to ground stations, companion computers, and other MAVLink systems. ernet - for example IP radios.
https://docs.px4.io/main/zh/advanced_config/ethernet_setup.html


=======================================

但是还是没有出现下面的正常测试结果,问题出在什么地方呢?

成功实现树莓派ROS2与PX4飞控通过以太网连接w23.jpg

分析,如下图,官方文档包括3部分,第1部分网络设置,第3部分是例子,只有可能是第2部分的设置不正确。这部分经过检查没有问题。

成功实现树莓派ROS2与PX4飞控通过以太网连接w24.jpg

PX4 MAVLink Serial Port Configuration

The Ethernet port configuration sets the properties of the serial link (which is how PX4 views the Ethernet connection). This includes the set of MAVLink messages that are streamed, the data rate, the UDP ports that a remote system can connect listen to, etc.

You must separately configure the PX4 IP address and other network settings (as shown previously).

PX4 configures the serial port to connect to a GCS via MAVLink, using the parameters shown below:
ParameterValueDescription
MAV_2_CONFIG1000Configure Ethernet port
MAV_2_BROADCAST1Broadcast HEARTBEAT messages
MAV_2_MODE0Send the "normal" set of MAVLink messages (i.e. the GCS set)
MAV_2_RADIO_CTL0Disable software throttling of MAVLink traffic
MAV_2_RATE100000Maximum sending rate
MAV_2_REMOTE_PRT14550MAVLink Remote Port of 14550 (GCS)
MAV_2_UDP_PRT14550MAVLink Network Port of 14550 (GCS)

Normally a companion computer would use port 14540 (rather than 14550) and stream the set of MAVLink messages specified in the Onboard profile. You can configure this setup by changing MAV_2_REMOTE_PRT and MAV_2_UDP_PRT to 14540 and MAV_2_MODE to 2 (Onboard). Note however that this will still work using the GCS profile.!!!!!!

For more information on MAVLink serial port configuration see MAVLink Peripherals (GCS/OSD/Companion)

成功实现树莓派ROS2与PX4飞控通过以太网连接w25.jpg

成功实现树莓派ROS2与PX4飞控通过以太网连接w26.jpg

==================================

Build ROS 2 Workspace

This section shows how create a ROS 2 workspace hosted in your home directory (modify the commands as needed to put the source code elsewhere).

The px4_ros_com and px4_msgs packages are cloned to a workspace folder, and then the colcon tool is used to build the workspace. The example is run using ros2 launch.

INFO

The example builds the ROS 2 Listener example application, located in px4_ros_com. px4_msgs is needed too so that the example can interpret PX4 ROS 2 topics.
Building the Workspace

To create and build the workspace:

    Open a new terminal.

    Create and navigate into a new workspace directory using:

    sh
    mkdir -p ~/ws_sensor_combined/src/
    cd ~/ws_sensor_combined/src/
    INFO

    A naming convention for workspace folders can make it easier to manage workspaces.

    Clone the example repository and px4_msgs to the /src directory (the main branch is cloned by default, which corresponds to the version of PX4 we are running):

    sh
    git clone https://github.com/PX4/px4_msgs.git
    git clone https://github.com/PX4/px4_ros_com.git
    ======安装上面两个需要反复多试几次才会成功========

    Source the ROS 2 development environment into the current terminal and compile the workspace using colcon:

    sh  humble
    cd ..
    source /opt/ros/humble/setup.bash
    colcon build

This builds all the folders under /src using the sourced toolchain.

    foxy

    cd ..

    source /opt/ros/foxy/setup.bash

    colcon build
Running the Example

To run the executables that you just built, you need to source local_setup.bash. This provides access to the "environment hooks" for the current workspace. In other words, it makes the executables that were just built available in the current terminal.

INFO

The ROS2 beginner tutorials recommend that you open a new terminal for running your executables.

In a new terminal:

    Navigate into the top level of your workspace directory and source the ROS 2 environment (in this case "Humble"):

    sh
    cd ~/ws_sensor_combined/
    source /opt/ros/humble/setup.bash

      humble

      foxy

    Source the local_setup.bash.

    sh
    source install/local_setup.bash
    Now launch the example. Note here that we use ros2 launch, which is described below.

    ros2 launch px4_ros_com sensor_combined_listener.launch.py

If this is working you should see data being printed on the terminal/console where you launched the ROS listener:

sh
RECEIVED DATA FROM SENSOR COMBINED
================================
ts: 870938190
gyro_rad[0]: 0.00341645
gyro_rad[1]: 0.00626475
gyro_rad[2]: -0.000515705
gyro_integral_dt: 4739
accelerometer_timestamp_relative: 0
accelerometer_m_s2[0]: -0.273381
accelerometer_m_s2[1]: 0.0949186
accelerometer_m_s2[2]: -9.76044
accelerometer_integral_dt: 4739

PS:出现上面的结果表示成功通讯了。

==========以上内容2024-07-23日全部测试通过===========
您需要登录后才可以回帖 登录 | 加入联盟

本版积分规则

快速回复 返回顶部 返回列表