본문 바로가기
  • 경제적 자유를 얻는 그날까지
엔지니어링/임베디드

[ ROS ] ROS CPP 패키지 생성하기

by 베니스상인 2021. 9. 25.

 

 

 

 

1. ROS 패키지 생성(CPP 의존성)

swift@swift-MacBookPro:~/Desktop$ cd ~/workspace/catkin_ws/src/

swift@swift-MacBookPro:~/workspace/catkin_ws/src$ catkin_create_pkg roscpp_test roscpp
Created file roscpp_test/package.xml
Created file roscpp_test/CMakeLists.txt
Created folder roscpp_test/include/roscpp_test
Created folder roscpp_test/src
Successfully created files in /home/swift/workspace/catkin_ws/src/roscpp_test. Please adjust the values in package.xml.

 

 

2. 디렉토리 생성

swift@swift-MacBookPro:~/workspace/catkin_ws/src$ cd roscpp_test/

swift@swift-MacBookPro:~/workspace/catkin_ws/src/roscpp_test$ mkdir scripts
swift@swift-MacBookPro:~/workspace/catkin_ws/src/roscpp_test$ mkdir launch
swift@swift-MacBookPro:~/workspace/catkin_ws/src/roscpp_test$ mkdir src

 

3. 에디터를 이용하여 package.xml 파일 정보 수정

 

- version, email, description 등을 수정해준다.

<?xml version="1.0"?>
<package format="2">
  <name>roscpp_test</name>
  <version>1.0.0</version>
  <description>The roscpp_test package</description>

  <!-- One maintainer tag required, multiple allowed, one person per tag -->
  <!-- Example:  -->
  <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
  <maintainer email="fpvholic0816@gmail.com">swift</maintainer>

 

 

4. cpp 코드 작성

swift@swift-MacBookPro:~/workspace/catkin_ws/src/roscpp_test$ cd src
swift@swift-MacBookPro:~/workspace/catkin_ws/src/roscpp_test/src$ vim roscpp_hello_world.cpp

 

코드의 내용은 아래와 같다.

 /*
  * Hello World Example using ROS and CPP
  */
 
 // Include the ROS library
 #include <ros/ros.h>
 
 // Main function
 int main(int argc, char** argv)
 { 
   // Initialize the ROS Node "roscpp_hello_world"
   ros::init(argc, argv, "roscpp_hello_world");
 
   // Instantiate the ROS Node Handler as nh
   ros::NodeHandle nh;
          
   // Print "Hello ROS!" to the terminal and ROS log file
   ROS_INFO_STREAM("Hello from ROS node " << ros::this_node::getName());
          
   // Program succesful
   return 0;
 }

 

 

5. 에디터를 이용하여 CMakeLists.txt 파일 정보 수정

- find_package

- catkin_package

- include directories

- add library( 필요시)

- add_executable : 실행할 노드이름

cmake_minimum_required(VERSION 3.0.2)
project(roscpp_test)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  roscpp
)

catkin_package(
  INCLUDE_DIRS
  CATKIN_DEPENDS roscpp
)

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/roscpp_test.cpp
# )

add_executable(${PROJECT_NAME}_node src/roscpp_hello_world.cpp)
target_link_libraries(${PROJECT_NAME}_node ${catkin_LIBRARIES})

 

 

 

6. 빌드

swift@swift-MacBookPro:~/workspace/catkin_ws$  catkin_make --directory ~/workspace/catkin_ws/ --pkg roscpp_test
Base path: /home/swift/workspace/catkin_ws
Source space: /home/swift/workspace/catkin_ws/src
Build space: /home/swift/workspace/catkin_ws/build
Devel space: /home/swift/workspace/catkin_ws/devel
Install space: /home/swift/workspace/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/swift/workspace/catkin_ws/build"
####
####
#### Running command: "make -j4 -l4" in "/home/swift/workspace/catkin_ws/build/roscpp_test"
####
Built target roscpp_test_node

swift@swift-MacBookPro:~/workspace/catkin_ws$

 

 

7. 빌드사항 업데이트

- 빌드된 패키지를 찾으려면 setup.sh를 shell에서 한번 실행해야 한다.

swift@swift-MacBookPro:~/workspace/catkin_ws/src/roscpp_test$ source ~/workspace/catkin_ws/devel/setup.bash

 

만약 terminal이 실행될 때 자동으로 실행되게 하려면 ~/.bashrc에 위 내용을 추가해 넣으면 된다.

 

 

8. 실행

swift@swift-MacBookPro:~/workspace/catkin_ws$ rosrun roscpp_test roscpp_test_node 
[ INFO] [1632551580.308809974]: Hello from ROS node /roscpp_hello_world

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90

댓글