ROS ํ™˜๊ฒฝ์—์„œ C++ ๊ธฐ๋ฐ˜ Publisher๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž

์ž‘์—… ๊ฒฝ๋กœ ์ƒ์„ฑ

์ž‘์—… ๊ฒฝ๋กœ๋Š” ๋ณธ์ธ์ด ์›ํ•˜๋Š” ๊ณณ์— ๋งŒ๋“ค๋ฉด ๋œ๋‹ค
๋‚˜๋Š” ~๊ฒฝ๋กœ์— ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค

# -p ์˜ต์…˜์„ ๋ถ™์ด๋ฉด ๋’ค์— src ํด๋”๊นŒ์ง€ ๋™์‹œ์— ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ
$ mkdir -p ~/ros_ws/src
$ cd ~/ros_ws/src


ํŒจํ‚ค์ง€ ์ƒ์„ฑ

ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์ž

# catkin_create_pkg {ํŒจํ‚ค์ง€๋ช…} {์˜์กด์„ฑํŒจํ‚ค์ง€1} {์˜์กด์„ฑํŒจํ‚ค์ง€2}
$ catkin_create_pkg ros_simple_tutorial roscpp std_msgs
$ cd ros_simple_tutorial


ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ์ด ๋˜์–ด์žˆ๋‹ค.

๐Ÿ“‚ ros_simple_tutorial
โ”ฃ ๐Ÿ“‚ include / ๐Ÿ“‚ ros_simple_tutorial
โ”ฃ ๐Ÿ“‚ src
โ”ฃ ๐Ÿ“œ CMakeLists.txt
โ”— ๐Ÿ“œ package.xml


Publisher ์ฝ”๋“œ ์ž‘์„ฑ

src ํด๋” ์•ˆ์—์„œ pub.cpp ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ์ž

$ cd src
$ gedit pub.cpp # pub.cpp๋ฅผ gedit ๋ง๊ณ  vscode ๋“ฑ ๋ณธ์ธ์ด ํŽธํ•œ ํˆด๋กœ ์ˆ˜์ •ํ•˜์ž

์ „์ฒด ์ฝ”๋“œ

#include <ros/ros.h>
#include <std_msgs/String.h>

int main(int argc, char** argv)
{
  ros::init(argc, argv, "pub_node");
  ros::NodeHandle nh;

  ros::Publisher pub = nh.advertise<std_msgs::String>("message", 10);
  std_msgs::String msg;

  ros::Rate r(1);
  while (ros::ok())
  {
    msg.data = "bigbigpark";

    pub.publish(msg);

    r.sleep();
  }

  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}
)

์‹คํ–‰ํ•˜๊ธฐ

$ roscore
$ source ~/ros_ws/devel/setup.bash
$ rosrun ros_simple_tutorial pub 

ํ† ํ”ฝ ์ถœ๋ ฅํ•˜๊ธฐ

ํ† ํ”ฝ์ด ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธํ•˜์ž

$ source ~/ros_ws/devel/setup.bash
$ rostopic echo /message

์—ฌ๊ธฐ์„œ ์–ด๋–ค ํ† ํ”ฝ์ด ์ง€๊ธˆ ROS ํ†ต์‹  ์ƒ์— publish๋˜๊ณ  ์žˆ๋Š” ์ง€๋Š” ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค

$ rostopic list

Leave a comment