1. FastDDS 各项依赖版本
官方文档地址:Fast DDS 官方文档
支持的 Ubuntu 系统版本:Ubuntu 20.04, Ubuntu 22.04
1.1 Ubuntu20.04 + ROS2-Foxy版本
foonathan_memory_vendor 版本:v1.2.2
Fast-CDR 版本:v1.0.13
Fast-DDS 版本:v2.14.3
FastDDS-gen 版本:v2.5.2
2. 源码编译安装 FastDDS
2.1 前置依赖条件
• CMake, g++, pip3, wget and git
sudo apt update
sudo apt install cmake g++ python3-pip wget git
○ Asio ,TinyXML2libraries, OpenSSL
sudo apt install libasio-dev libtinyxml2-dev libssl-dev libp11-dev
○ Gtest
git clone https://github.com/google/googletest.git
cd googletest/
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
sudo cmake --build . --target install -j8
2.2 编译
2.2.1 创建工作目录
Bash
mkdir ~/Fast-DDS
2.2.2 安装所有依赖项
2.2.2.1 Foonathan memory
cd ~/Fast-DDS
git clone https://github.com/eProsima/foonathan_memory_vendor.git
mkdir foonathan_memory_vendor/build
cd foonathan_memory_vendor/build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/
sudo cmake --build . --target install
2.2.2.2 Fast CDR
cd ~/Fast-DDS
git clone https://github.com/eProsima/Fast-CDR.git
mkdir Fast-CDR/build
cd Fast-CDR/build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/ -DBUILD_SHARED_LIBS=ON
sudo cmake --build . --target install
2.2.2.3 Fast DDS
cd ~/Fast-DDS
git clone https://github.com/eProsima/Fast-DDS.git
git checkout 2.14.3
mkdir Fast-DDS/build
cd Fast-DDS/build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/ -DBUILD_SHARED_LIBS=ON
sudo cmake --build . -j16 --target install
2.2.2.4 编译Fast DDS Gen
Fast-DDS-Gen 是一个 Java 工具,用于生成与 eProsima Fast DDS 库兼容的 TypeSupport 代码。该工具通过解析 IDL(接口定义语言)文件,自动生成数据类型的源代码,从而简化了 Fast DDS 应用程序的开发过程。
mkdir -p ~/Fast-DDS/src
cd ~/Fast-DDS/src
git clone --recursive https://github.com/eProsima/Fast-DDS-Gen.git fastddsgen
cd fastddsgen
./gradlew assemble
注意:库路径 --环境变量(如果指定安装到~/Fast-DDS/install/中才执行)
export LD_LIBRARY_PATH=~/Fast-DDS/install/lib
#或者放到.bashrc
echo 'export LD_LIBRARY_PATH=~/Fast-DDS/install/lib' >>~/.bashrc
2.3 简单运行example :HelloWorldExample(测试FastDDS通讯是否有异常)
• 开一个终端
#export LD_LIBRARY_PATH=~/Fast-DDS/install/lib 视情况添加
cd ~/Fast-DDS/install/examples/cpp/dds/HelloWorldExample/bin
./DDSHelloWorldExample publisher 100
• 新开一个终端
#export LD_LIBRARY_PATH=~/Fast-DDS/install/lib 视情况添加
cd ~/Fast-DDS/install/examples/cpp/dds/HelloWorldExample/bin
./DDSHelloWorldExample subscriber
3. 如何使用FastDDS-Gen 生成代码?
FastDDS-Gen 是一个 Java 工具,用于生成与 eProsima Fast DDS 库兼容的 TypeSupport 代码。通过解析 IDL(接口定义语言)文件,FastDDS-Gen 自动生成数据类型的源代码,简化了 Fast DDS 应用程序的开发过程。
3.1 创建 IDL 文件
首先,创建一个 IDL 文件,描述你希望在 Fast DDS 中使用的数据类型。
例如,创建一个简单的 Test.idl 文件,内容如下:
struct TestMessage{
string message;
long id;
};
3.2 使用 FastDDS-Gen 生成代码
/home/glr/Fast-DDS/src/fastddsgen/scripts/fastddsgen -example CMake Test.idl
生成的文件将包括 Test.hpp、 TestPubSubTypes.hpp等。(如下图所示)
3.3 编译和运行示例应用程序
Fast-DDS-Gen 还可以生成示例应用程序:
mkdir build
cd build
cmake ..
make -j10
运行生成的示例应用程序:
./Test publisher
./Test subscriber
4. 报错及问题分析
4.1 正常使用Fast—DDS报错(未找到fastcdr 可能指定安装到~/Fast-DDS/install/)
设置 CMAKE_PREFIX_PATH 或 fastcdr_DIR
为了让 CMake 找到 fastcdr 的配置文件,您可以设置 CMAKE_PREFIX_PATH 或 fastcdr_DIR 环境变量。这里推荐使用 fastcdr_DIR,因为它更具体:
设置 fastcdr_DIR
export fastcdr_DIR=~/Fast-DDS/install/lib/cmake/fastcdr
然后重新运行 CMake:
Bash
cd ~/Code/dds_tutorial/examples/01-hellofishros/build
cmake ..
还有一种方法
如果您选择使用 CMAKE_PREFIX_PATH 来帮助 CMake 找到 fastcdr 库,可以通过设置该环境变量来指定库的安装前缀路径。CMAKE_PREFIX_PATH 是一个包含多个路径的列表,CMake 会在这些路径中查找所需的包配置文件和模块。
设置 CMAKE_PREFIX_PATH
您可以将 fastcdr 的安装目录添加到 CMAKE_PREFIX_PATH 中,这样 CMake 就会在这个路径下寻找 fastcdrConfig.cmake 或 fastcdr-config.cmake 文件。以下是具体步骤:
临时设置(仅在当前终端会话中有效)
在命令行中临时设置 CMAKE_PREFIX_PATH 环境变量:
export CMAKE_PREFIX_PATH=~/Fast-DDS/install:$CMAKE_PREFIX_PATH
然后重新运行 CMake:
cd ~/Code/dds_tutorial/examples/01-hellofishros/build
cmake …
4.2 安装Fastdds-gen报无java错误
glr@glr-desktop:~/Fast-DDS/src/fastddsgen$ ./gradlew assemble
ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
在 Ubuntu 上安装 OpenJDK
sudo apt update
sudo apt install -y openjdk-11-jdk # 或者 openjdk-8-jdk, 根据需求选择版本
4.3 在Ubuntu20.04编译FastDDS可能遇到的问题
问题:CMake 3.20 or higher is required. You are running version 3.16.3(Cmake版本不支持)
• 在CMake官网下载对应版本的安装包进行安装
• 打开cmake下载的官网:https://cmake.org/files/
sudo wget https://cmake.org/files/v3.23/cmake-3.23.0.tar.gz
# 解压
sudo tar -zxvf cmake-3.23.0.tar.gz
# cmake-3.23.0文件夹
cd cmake-3.23.0
# 安装
sudo ./configure
4.4 版本导致问题
版本问题1:编译Fast-DDS源码,报错找不到foonathan_memoryConfig.cmake错误
#由于版本的问题导致cmake文件无法获取,建议选择checkout 版本 v1.0.0
版本问题2:Build程序报错 does not name a type
版本不匹配问题,建议切换fast-dds版本与fast-cdr 等依赖对应