终篇:RK1808在BuildRoot中增加ROS支持

前两篇文章中,其实已经把ROS运行起来了,跑了官方的程序(原来出现的source问题,已经在第二篇评论出了解决方案)只是官方程序是放在整个sdk中编译的,需要写Config.in文件,mk文件等,比较麻烦,于是就在折腾直接使用交叉编译链,使用catkin_make命令来实现编译,然后把devel的文件夹传到RK1808中就可以直接运行的方法,经过几天的折腾,终于搞成功了,在此记录一下。

开发环境使用vscode,安装好相关组件,因为kinetic使用的是python2.7,我的系统是Ubuntu20.04,默认的是Python3.9,所以使用Anaconda3进行管理Python版本,使用conda创建了一个python=2.7.18的Python环境,再安装catking-tools和catkin_pkg(这个不知道有没有用到,反正是安装了,也没有去研究)。建立workspace和对应节点程序:

终篇:RK1808在BuildRoot中增加ROS支持_第1张图片

 程序hello.cpp代码:


#include 
#include 

int main(int argc, char** argv) {
    ros::init(argc, argv, "test");
    ros::NodeHandle n;
    ros::Publisher image_pub = n.advertise("testCount", 1);
    ROS_INFO("ros hello !");
    uint32_t _t_time = ros::Time::now().nsec;
    uint32_t _n_time = _t_time;
    ros::Rate loop_rate(10);
    while (ros::ok())
    {
        _n_time = ros::Time::now().nsec;
        if (_n_time - _t_time > 1000000) {
            _t_time = _n_time;
            std_msgs::Int32 i;
            i.data = _t_time;
            image_pub.publish(i);
        }
        ros::spinOnce();
        loop_rate.sleep();
    }

    return 0;
}

手里有RK1808和RV1126两块板子,所以考虑了编译兼容,增加了envs.cmake.in

#------------------------------------------------------------------------
# 交叉编译环境配置,用于RK1808或者RV1126的编译
# 
#------------------------------------------------------------------------
if(RV1126)
    message("- INFO def RV1126")
    #交叉编译环境参数
    set(_SDK_ENV_PATH /files/tools/rv1126_rv1109_linux_sdk_v1.8.0_20210224)
    set(_SDK_ENV_HOST_PATH ${_SDK_ENV_PATH}/buildroot/output/rockchip_rv1126_rv1109/host)
    set(_SDK_ENV_HOST_SYSROOT ${_SDK_ENV_HOST_PATH}/arm-buildroot-linux-gnueabihf/sysroot)
    set(_SDK_ENV_TARGET_PATH ${_SDK_ENV_PATH}/buildroot/output/rockchip_rv1126_rv1109/target)

    # 交叉编译链
    set(_TOOLS_CHAIN ${_SDK_ENV_PATH}/prebuilts/gcc/linux-x86)
    set(_TOOLS_CHAIN_BIN ${_TOOLS_CHAIN}/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin)

    #
    set(CMAKE_C_EXEC /arm-linux-gnueabihf-gcc)
    set(CMAKE_CPP_EXEC /arm-linux-gnueabihf-g++)
    set(_CXX_FLAGS "-fpermissive -fopenmp -Wl,-Map=gcc.map")
else()
    message("- INFO def RK1808")
    #交叉编译环境参数
    set(_SDK_ENV_PATH /tools/rk1808-tb-96aiot-sdk-codes20200605/rk1808gitgithub)
    set(_SDK_ENV_HOST_PATH ${_SDK_ENV_PATH}/buildroot/output/rockchip_rk1808/host)
    set(_SDK_ENV_HOST_SYSROOT ${_SDK_ENV_HOST_PATH}/aarch64-buildroot-linux-gnu/sysroot)
    set(_SDK_ENV_TARGET_PATH ${_SDK_ENV_PATH}/buildroot/output/rockchip_rk1808/target)
    # 交叉编译链
    set(_TOOLS_CHAIN ${_SDK_ENV_PATH}/buildroot/output/rockchip_rk1808)
    set(_TOOLS_CHAIN_BIN ${_TOOLS_CHAIN}/host/bin)
    set(CMAKE_C_EXEC /aarch64-linux-gcc)
    set(CMAKE_CPP_EXEC /aarch64-linux-g++)
    set(_CXX_FLAGS "-fpermissive ")
endif(RV1126)

set(_SDK_ENV_HOST_ROS_PATH ${_SDK_ENV_HOST_PATH}/opt/ros/kinetic)
set(_SDK_ENV_HOST_SYSROOT_ROS_PATH ${_SDK_ENV_HOST_SYSROOT}/opt/ros/kinetic)
set(_SDK_ENV_TARGET_ROS_PATH ${_SDK_ENV_TARGET_PATH}/opt/ros/kinetic)

#C编译链
set(CMAKE_C_COMPILER ${_TOOLS_CHAIN_BIN}${CMAKE_C_EXEC})
#C++编译链
set(CMAKE_CXX_COMPILER ${_TOOLS_CHAIN_BIN}${CMAKE_CPP_EXEC})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
# -std=c++14  -march=armv7 -ftree-vectorize -mfpu=neon -mcpu=cortex-a7 -O2 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_CXX_FLAGS}")

set(PATH "$PATH:${_TOOLS_CHAIN_BIN}")

由于编译命令参数多,所以增加了一个编译脚本,build.sh :

#交叉编译器位置
if [ "$1" = "clean" ]; then
    rm -rf build/ devel/
elif [ "$1" = "-h" ]; then
    echo "clean : remove all"
    echo "rv1126 : complier for rv1126"
else
if [ "$1" = "rv1126" ]; then
    #for rv1126
    echo "use rv1126 envs!"
    _ENV_PATH=/home/cubar/anaconda3/envs/py2
    #SDK库的根目录
    _env_sdk_path=/tools/rv1126_rv1109_linux_sdk_v1.8.0_20210224
    _env_sdk_output_path=$_env_sdk_path/buildroot/output/rockchip_rv1126_rv1109
    _env_sdk_host=$_env_sdk_output_path/host
    _env_sdk_host_sysroot=$_env_sdk_host/arm-buildroot-linux-gnueabihf/sysroot
    _env_sdk_target_path=$_env_sdk_output_path/target
    _env_use_1126="-DRV1126=1 "
else
    #for rk1808
    echo "use rk1808 envs!"
    _ENV_PATH=/home/cubar/anaconda3/envs/py2
    #SDK库的根目录
    _env_sdk_path=/tools/rk1808-tb-96aiot-sdk-codes20200605/rk1808gitgithub
    _env_sdk_output_path=$_env_sdk_path/buildroot/output/rockchip_rk1808
    _env_sdk_host=$_env_sdk_output_path/host
    _env_sdk_host_sysroot=$_env_sdk_host/aarch64-buildroot-linux-gnu/sysroot
    _env_sdk_target_path=$_env_sdk_output_path/target
fi

_env_sdk_host_ros=$_env_sdk_host/opt/ros/kinetic
_env_sdk_host_sysroot_ros_path=$_env_sdk_host_sysroot/opt/ros/kinetic
_env_sdk_target_ros_path=$_env_sdk_target_path/opt/ros/kinetic
#staging
_env_sdk_staging_ros_path=$_env_sdk_output_path/staging/opt/ros/kinetic

source $_env_sdk_host_ros/setup.bash

export PYTHONPATH=$PYTHONPATH:$_env_sdk_target_ros_path/lib/python2.7/site-packages

catkin_make \
    $_env_use_1126 \
    -DCATKIN_ENABLE_TESTING=0 \
    -DPYTHON_EXECUTABLE=$_ENV_PATH/bin/python \
    -DPYTHON_VERSION=2.7.18 
fi

直接使用./build.sh rv1126进行编译,然后把devel文件夹整个传到板子上。运行就可以了:

使用rostopic echo testCount查看程序输出。

终篇:RK1808在BuildRoot中增加ROS支持_第2张图片

 至此,在RV1126/RK1808上交叉编译ROS程序顺利通过。

你可能感兴趣的:(rk1808,ROS,Linux,ROS,c++,rockchips,kinetic,rk1808)