Note: This tutorial assumes that you have completed the previous tutorials: creating a ROS package.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Xây dựng một gói ROS

Description: Hướng dẫn các công cụ xây dựng gói.

Tutorial Level: BEGINNER

Next Tutorial: Understanding ROS Nodes

Building Packages

Once all the system dependencies are installed, we can build our package that we just created.

Using rosmake

rosmake is just like the make command, but it does some special ROS magic. When you type rosmake beginner_tutorials, it builds the beginner_tutorials package, plus every package that it depends on, in the correct order. Since we listed rospy, roscpp, and std_msgs as dependencies when creating our ROS package, these packages (and their dependiencies, and so on) will be built by rosmake as well.

Usage:

rosmake [package]

Try:

$ rosmake beginner_tutorials

This previous command may take a while to finish. As it is running you should see some output like:

  • [ rosmake ] No package specified.  Building ['beginner_tutorials']
    [ rosmake ] Logging to directory
    [ rosmake ] /home/dbking/.ros/rosmake_output-2009-09-22-03-17-14
    [ rosmake ] [ 0 of 18  Completed ]
    [rosmake-0] >>> genmsg_cpp >>> [ make ]
    [rosmake-0] <<< genmsg_cpp <<< [PASS] [ 0.39 seconds ]
    [ rosmake ] [ 1 of 18  Completed ]
    ...
    ...
    ...
    [ rosmake ] [ 17 of 18  Completed ]
    [rosmake-0] >>> beginner_tutorials >>> [ make ]
    [rosmake-0] <<< beginner_tutorials <<< [PASS] [ 0.79 seconds ]

On Fuerte, since dependencies are greatly reduced, this takes almost no time and produces:

  • [ rosmake ] rosmake starting...                                                                     
    [ rosmake ] Packages requested are: ['beginner_tutorials']                                          
    [ rosmake ] Logging to directory /home/alex/.ros/rosmake/rosmake_output-20120603-082414             
    [ rosmake ] Expanded args ['beginner_tutorials'] to:
    ['beginner_tutorials']                         
    [rosmake-0] Starting >>> std_msgs [ make ]                                                          
    [rosmake-1] Starting >>> roslang [ make ]                                                           
    [rosmake-0] Finished <<< std_msgs ROS_NOBUILD in package std_msgs
     No Makefile in package std_msgs  
    [rosmake-1] Finished <<< roslang ROS_NOBUILD in package roslang
     No Makefile in package roslang     
    [rosmake-1] Starting >>> rospy [ make ]                                                             
    [rosmake-2] Starting >>> roscpp [ make ]                                                            
    [rosmake-1] Finished <<< rospy ROS_NOBUILD in package rospy
     No Makefile in package rospy           
    [rosmake-2] Finished <<< roscpp ROS_NOBUILD in package roscpp
     No Makefile in package roscpp        
    [rosmake-2] Starting >>> beginner_tutorials [ make ]                                                
    [rosmake-2] Finished <<< beginner_tutorials [PASS] [ 1.14 seconds ]                                 
    [ rosmake ] Results:                                                                                
    [ rosmake ] Built 5 packages with 0 failures.                                                       
    [ rosmake ] Summary output to directory                                                             
    [ rosmake ] /home/alex/.ros/rosmake/rosmake_output-20120603-082414  

rosmake multiple packages

We can also use rosmake to build multiple packages at once.

Usage:

rosmake [package1] [package2] [package3]

Review

Lets just list some of the commands we've used so far:

  • rosdep = ros+dep(endencies) : a tool to install package dependencies
  • rosmake = ros+make : makes (compiles) a ROS package

Xây dựng một gói

Miễn là tất cả các gói phụ thuộc đã được cài đặt, bây giờ chúng ta có thể xây dựng gói mới.

Chú ý: Nếu bạn cài đặt ROS dùng apt hoặc trình quản lý gói khác, bạn có thể có tất cả gói phụ thuộc.

Trước khi tiếp tục nhớ cài đặt tập tin nguồn cho môi trường của bạn nếu bạn chưa có. Trên Ubuntu sẽ giống như sau:

$ source /opt/ros/%YOUR_ROS_DISTRO%/setup.bash
$ source /opt/ros/kinetic/setup.bash             (cho phiên bản ROS Kinetic)

Dùng catkin_make

catkin_make Là một công cụ dòng lệnh với nhiều tiện ích . Bạn tưởng tưởng rằng catkin_make kết hợp giữa việc gọi cmakemake trong qui trình làm việc chuẩn của CMake.

Dùng:

# Trong một catkin workspace
$ catkin_make [make_targets] [-DCMAKE_VARIABLES=...]

Nếu bạn không quen làm việc với CMake workflow, các bước sẽ tiến hành như sau:

Chú ý: Nếu bạn chạy dòng lệnh dưới sẽ không thành công, chỉ là ví dụ minh họa các bước mà CMake sẽ thực thi.

# In a CMake project
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install  # (optionally)

Đây là qúa trình chạy cho mỗi dự án CMake. Trái ngược với dự án catkin có thể xây dựng với nhau trong workspaces. Building zero to many catkin packages in a workspace follows this work flow:

# In a catkin workspace
$ catkin_make
$ catkin_make install  # (optionally)

Các lệnh trên sẽ xây dựng bất kỳ dự án catkin tìm thấy trong thư mục src. Điều này tuân theo các khuyến nghị của REP128. Nếu mã nguồn của bạn ở một nơi khác, ví dụ như my_src thì bạn sẽ gọi catkin_make như sau:

Chú ý: Nếu bạn chạy dòng lệnh dưới sẽ không thành công, vì đường dẫn my_src không tồn tại.

# In a catkin workspace
$ catkin_make --source my_src
$ catkin_make install --source my_src  # (optionally)

catkin_make có thể cung cấp nhiều tính năng hơn, xem tài liệu: catkin/commands/catkin_make

Xây dựng gói

Bạn đã có một không gian làm việc catkin workspace và một gói catkin mới gọi là beginner_tutorials từ hướng dẫn trước đó, Creating a Package. Trỏ vào vùng làm việc catkin nếu bạn chưa có và tìm trong thư mục src:

$ cd ~/catkin_ws/
$ ls src
  • beginner_tutorials/  CMakeLists.txt@

Bạn sẽ thấy có một thư mục gọi là beginner_tutorials mà bạn đã tạo với catkin_create_pkg trong hướng dẫn trước đó. Bây giờ chúng ta có thể xây dựng gói đó sử dụng catkin_make:

$ catkin_make

Bạn sẽ thấy rất nhiều thông tin xuất ra từ cmake và make, tương tự như sau:

  • Base path: /home/user/catkin_ws
    Source space: /home/user/catkin_ws/src
    Build space: /home/user/catkin_ws/build
    Devel space: /home/user/catkin_ws/devel
    Install space: /home/user/catkin_ws/install
    ####
    #### Running command: "cmake /home/user/catkin_ws/src
    -DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel
    -DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install" in "/home/user/catkin_ws/build"
    ####
    -- The C compiler identification is GNU 4.2.1
    -- The CXX compiler identification is Clang 4.0.0
    -- Checking whether C compiler has -isysroot
    -- Checking whether C compiler has -isysroot - yes
    -- Checking whether C compiler supports OSX deployment target flag
    -- Checking whether C compiler supports OSX deployment target flag - yes
    -- Check for working C compiler: /usr/bin/gcc
    -- Check for working C compiler: /usr/bin/gcc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Using CATKIN_DEVEL_PREFIX: /tmp/catkin_ws/devel
    -- Using CMAKE_PREFIX_PATH: /opt/ros/kinetic
    -- This workspace overlays: /opt/ros/kinetic
    -- Found PythonInterp: /usr/bin/python (found version "2.7.1")
    -- Found PY_em: /usr/lib/python2.7/dist-packages/em.pyc
    -- Found gtest: gtests will be built
    -- catkin 0.5.51
    -- BUILD_SHARED_LIBS is on
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- ~~  traversing packages in topological order:
    -- ~~  - beginner_tutorials
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- +++ add_subdirectory(beginner_tutorials)
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/user/catkin_ws/build
    ####
    #### Running command: "make -j4" in "/home/user/catkin_ws/build"
    ####

Lưu ý rằng catkin_make lần đầu tiên hiển thị những đường dẫn mà nó sử dụng cho mỗi khoảng trống. Các không gian được mô tả trong REP128 và bằng tài liệu về các không gian làm việc catkin trên wiki: catkin/workspaces. Điều quan trọng cần lưu ý là vì những giá trị mặc định này, một số thư mục đã được tạo ra trong vùng làm việc catkin của bạn. Hãy xem ls:

$ ls
  • build
    devel
    src

Thư mục build là vị trí mặc định của không gian xây dựng (build space) và là nơi cmake và make được gọi để cấu hình và xây dựng các gói của bạn. Thư mục devel là vị trí mặc định của không gian phát triển (devel space), nơi mà tệp thực thi và thư viện của bạn đi trước khi bạn cài đặt gói của mình.

Now that you have built your ROS package let's talk more about ROS Nodes.

Wiki: vn/ROS/Tutorials/BuildingPackages (last edited 2017-04-05 07:00:47 by HoangGiang88)