1. 程式人生 > >docker搭建ros-indigo-arm交叉編譯環境

docker搭建ros-indigo-arm交叉編譯環境

ROS執行環境:ARM ubuntu14.04 + ROS indigo
在arm環境下編譯ros應用程式,速度極慢,無法忍受,嘗試在x86機器上搭建docker+ros交叉編譯環境。

交叉編譯環境的搭建主要參考:

1 ARM ubuntu環境上安裝arm-indigo版本,後面需要將這個arm版本copy到交叉編譯環境上
2 x86環境上安裝docker
3 下載docker-ros映象, 載入啟動docker-ros映象
4 安裝 sudo apt-get install g++-arm-linux-gnueabihf
5 將docker環境中中x86版本的indigo替換成arm版本的indigo,
替換之前需要備份x86版本indigo的lib,因為這些庫檔案在執行ros時是需要的。
將x86版本indigo的lib儲存到localpath目錄下,設定export LD_LIBRARY_PATH=localpath,
實現執行ros命令時使用x86的lib庫。
6 將arm環境下的庫檔案/標頭檔案 copy到docker環境中,目錄可以自己設定比如:/$ENV{HOME}/cross-compiling/ubuntu-rootfs
7 配置rostoolchain.cmake檔案

複製程式碼

#File rostoolchain.cmake

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_COMPILER /usr/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++)
set(CMAKE_FIND_ROOT_PATH /opt/ros/indigo $ENV{HOME}/cross-compiling/ubuntu-rootfs)

set(CMAKE_LIBRARY_PATH $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/lib/arm-linux-gnueabihf $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/lib $ENV{HOME}/cross-compiling/ubuntu-rootfs/lib $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/local/lib)

set(CMAKE_INCLUDE_PATH $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/include $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/local/include)
set(LD_LIBRARY_PATH $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/lib/arm-linux-gnueabihf $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/lib $ENV{HOME}/cross-compiling/ubuntu-rootfs/lib $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/local/lib)

set(PYTHON_EXECUTABLE /usr/bin/python)
set(PCL_ROOT $ENV{HOME}/cross-compiling/ubuntu-rootfs/usr)

set(CMAKE_CROSSCOMPILING true)
message("${CMAKE_CROSSCOMPILING}")

# Have to set this one to BOTH, to allow CMake to find rospack


set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

複製程式碼

8 修改/opt/ros/indigo/share/catkin/cmake/tools/rt.cmake

該檔案的第34行

if(NOT (APPLE OR WIN32 OR MINGW OR ANDROID))修改為if(NOT (APPLE OR WIN32 OR MINGW OR ANDROID OR UNIX))

9 個人理解cmake交叉編譯過程就是修改cmake搜尋連結庫檔案位置的過程,正常編譯會搜尋x86庫檔案做在位置,
交叉編譯需要將搜尋位置切換到目標架構需要的庫檔案存放位置,比如/$ENV{HOME}/cross-compiling/ubuntu-rootfs
實現方法:
修改catkin_INCLUDE_DIRS catkin_LIBRARY_DIRS,將這兩個變數中指向x86系統的位置,修改為指向交叉編譯依賴檔案存放位置
/opt/ros/indigo/share/catkin/cmake/catkinConfig.cmake
這個檔案末增加如下處理:

複製程式碼

list(APPEND catkin_INCLUDE_DIRS ${CMAKE_INCLUDE_PATH})
list(APPEND catkin_LIBRARY_DIRS ${CMAKE_LIBRARY_PATH})

set(catkin_LIBRARIES_tmp ${catkin_LIBRARIES})
set(catkin_LIBRARIES "")
foreach(catkinlib ${catkin_LIBRARIES_tmp})
    string(LENGTH ${catkinlib} lengths)    
    if (${lengths} GREATER 12)
        string(SUBSTRING ${catkinlib} "/" 12 result)
        string(COMPARE EQUAL ${result} "/usr/lib/lib" compareResult)
        if (${compareResult})
            string(REPLACE "/usr/lib/lib" "$ENV{HOME}/cross-compiling/ubuntu-rootfs/usr/lib/lib" result_tem ${catkinlib})
            list(APPEND catkin_LIBRARIES ${result_tem})
        else()
            list(APPEND catkin_LIBRARIES ${catkinlib})
        endif()
    else()
        list(APPEND catkin_LIBRARIES ${catkinlib})
    endif()
endforeach()

複製程式碼

10 編譯命令

catkin_make -DCMAKE_TOOLCHAIN_FILE=/$filepath/rostoolchain.cmake