Building a ROS 2 C++ application on an embedded system without using colcon and without access to the opt path can be a bit challenging but feasible with some custom setup. Here’s a step-by-step guide to get you started:
1. Set Up Cross-compilation Environment
Ensure you have a cross-compilation environment set up on your host system (e.g., with tools like aarch64-linux-gnu-gcc for ARM). You would typically:
- Install the necessary cross-compiler tools.
- Have the ROS 2 libraries built for your embedded system.
2. Set Up the File System on the Embedded Device
Make sure your ROS 2 libraries and binaries are correctly placed on your embedded system’s file system. This should include all necessary dependencies.
3. Write Your ROS 2 Node
Create your ROS 2 node in C++. Here is an example talker.cpp:
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>
using namespace std::chrono_literals;
class Talker : public rclcpp::Node
{
public:
Talker() : Node("talker"), count_(0)
{
publisher_ = this->create_publisher<std_msgs::msg::String>("chatter", 10);
timer_ = this->create_wall_timer(500ms, std::bind(&Talker::timer_callback, this));
}
private:
void timer_callback()
{
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);
}
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
rclcpp::TimerBase::SharedPtr timer_;
size_t count_;
};
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<Talker>());
rclcpp::shutdown();
return 0;
}
4. Create a Custom CMakeLists.txt
Create a CMakeLists.txt for your project to compile the talker node:
cmake_minimum_required(VERSION 3.5)
project(talker)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(talker src/talker.cpp)
ament_target_dependencies(talker rclcpp std_msgs)
install(TARGETS
talker
DESTINATION lib/${PROJECT_NAME})
ament_package()
5. Cross-compile Your Application
Set up your build environment. Create a build directory and use CMake to configure and build the package:
mkdir -p build
cd build
# Set up your toolchain if necessary
# e.g., aarch64-linux-gnu-toolchain.cmake should define the toolchain and sysroot
cmake .. -DCMAKE_TOOLCHAIN_FILE=aarch64-linux-gnu-toolchain.cmake -DRCLCPP_DIR=/path/to/crosscompiled/rclcpp -DSTD_MSGS_DIR=/path/to/crosscompiled/std_msgs
# Now build the application
make
6. Deploy and Run
Copy the built binaries to the embedded system and run it:
scp build/talker user@embedded:/path/to/deploy
# On the embedded system
./talker
Notes
- Environment Variables: Ensure that environment variables like
LD_LIBRARY_PATH on the embedded device point to the directories containing ROS 2 libraries.
- Dependencies: Ensure all necessary runtime dependencies are present on the embedded system.
- Toolchain File: Create a toolchain file (e.g.,
aarch64-linux-gnu-toolchain.cmake) that sets up cross-compilation (CMAKE_SYSTEM_NAME, CMAKE_C_COMPILER, CMAKE_CXX_COMPILER, and any necessary flags).
- Path Adjustments: Modify
CMAKE_PREFIX_PATH in your CMake invocation to point to your cross-compiled ROS 2 installation if necessary.
By following these steps, you should be able to build and run a ROS 2 application on your embedded system without using colcon or needing to source a setup file.