[ROS] C++ subscriber ์์ฑํ๊ธฐ
ROS ํ๊ฒฝ์์ C++ ๊ธฐ๋ฐ Subscriber๋ฅผ ์์ฑํด๋ณด์
์์ ๊ฒฝ๋ก ๋ฐ ํจํค์ง ์์ฑ
์ด์ publisher ๋ง๋๋ ํจํค์ง์ ์ด์ด์ง๋ ๊ฒ์ด๋ฏ๋ก ์์
๊ฒฝ๋ก ๋ฐ ํจํค์ง ์์ฑ์ ์๋ ๊ฒฝ๋ก๋ฅผ ์ฐธ๊ณ ํ์
โ[ROS] C++ publisher ์์ฑํ๊ธฐโ
Subscriber ์ฝ๋ ์์ฑ
src ํด๋ ์์์ sub.cpp ํ์ผ์ ๋ง๋ค์ด์ฃผ์
$ cd src
$ gedit sub.cpp
์ ์ฒด ์ฝ๋
#include <ros/ros.h>
#include <std_msgs/String.h>
void stringCallback(const std_msgs::String::Ptr& msg)
{
ROS_INFO("I got a message: %s", msg->data.c_str());
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "sub_node");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe("message", 10, stringCallback);
ros::spin();
return 0;
}
์ฝ๋๊ฐ ์ดํด๋์ง ์์ผ๋ฉด ์ง๋ฌธ ๋จ๊ฒจ์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค :)
CMakeLists.txt ์์
์๊น ros_simple_tutorial ํจํค์ง ๊ฒฝ๋ก์ ๊ฐ์ CMakeLists.txt๋ฅผ ์์ ํด์ฃผ์
$ gedit CMakeLists.txt
์๋ ์๋ ๊ฒ์ ์ญ์ ํ๊ณ ์๋๋ฅผ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ ํ๋ค
๊ฐํน ์ ์ผ ์ฒซ ๋ฒ์งธ ์ค์ธ cmake_minimum_required ์ด ์๋ฌ ๋๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ ์ด๋ด ๋๋ ํฐ๋ฏธ๋ ์ฐฝ์ $ cmake --version
์ ์
๋ ฅํ์ฌ ์์ ์ cmake ๋ฒ์ ์ ์
๋ ฅํด์ค๋ค
cmake_minimum_required(VERSION 3.0.9)
project(ros_simple_tutorial)
# add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES ros_simple_tutorial
# CATKIN_DEPENDS roscpp std_msgs
# DEPENDS system_lib
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(pub src/pub.cpp)
target_link_libraries(pub
${catkin_LIBRARIES}
)
add_executable(sub src/sub.cpp)
target_link_libraries(sub
${catkin_LIBRARIES}
)
์คํํ๊ธฐ
$ roscore
$ source ~/ros_ws/devel/setup.bash
$ rosrun ros_simple_tutorial pub
$ rosrun ros_simple_tutorial sub
Leave a comment