3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ROS プログラムからパッケージのパス取得 (Python/C++)

Last updated at Posted at 2021-07-13

はじめに

ROSでプログラムからパッケージのパスを取得する方法を記載します。
roslibというライブラリーを使うとそのようなことができるみたいです。

プログラム

  • Pythonでパッケージのパス取得
get_path.py
#!/usr/bin/python3

import roslib.packages

pkg_name = 'roslib'

path = roslib.packages.get_pkg_dir(pkg_name)

print(path)
実行結果
$ rosrun get_package_path get_path.py
/opt/ros/noetic/share/roslib
  • c++でパッケージのパス取得
get_path.cpp
#include <ros/package.h>
#include <iostream>

int main(int argc, char** argv) {

  std::string package = "roslib";

  std::string path = ros::package::getPath(package); 

  std::cout << path << std::endl;

  return 0;
}

  • 上記c++プログラムのcmakeファイル(※roslib依存)
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(get_ros_package_path)

find_package(catkin REQUIRED roslib)

catkin_package(
)

include_directories(
  ${catkin_INCLUDE_DIRS}
)

add_executable(get_path src/get_path.cpp)
target_link_libraries(get_path
  ${catkin_LIBRARIES}
)
実行結果
$ rosrun get_package_path get_path
/opt/ros/noetic/share/roslib
3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?