LoginSignup
0
2

More than 1 year has passed since last update.

ros c++ で hello world

Last updated at Posted at 2021-07-28

ros c++ でhello worldをする手順です。

開発環境の設定

$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace
$ cd ~/catkin_ws
$ catkin_make

パッケージの作成

$ cd ~/catkin_ws/src
$ catkin_create_pkg my_hello std_msgs roscpp
$ cd my_hello

ビルド設定ファイルの修正

CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(my_hello)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)
catkin_package(
  CATKIN_DEPENDS roscpp std_msgs
)
include_directories(
  ${catkin_INCLUDE_DIRS}
)
add_executable(my_hello src/my_hello.cpp)
target_link_libraries(
  my_hello
  ${catkin_LIBRARIES}
)

ソースファイルの作成

src/my_hello.cpp
#include <ros/ros.h>

int main(int argc, char **argv) {
  ros::init(argc, argv, "my_hello");
  ros::NodeHandle n;
  ros::Rate r(10);
  while (ros::ok())
  {
    ROS_INFO("Hello world");
    r.sleep();
  }
  return 0;
}

パッケージのビルド

$ cd ~/catkin_ws
$ catkin_make

パッケージの実行

$ source ~/catkin_ws/devel/setup.bash
$ rosrun my_hello my_hello

[ INFO] [1627477251.605944564]: Hello world
[ INFO] [1627477251.706078609]: Hello world
...

参考

こちらを参考に作成しました。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2