Help us understand the problem. What is going on with this article?

ROSでのコマンドライン引数取得方法(rosrun)

More than 1 year has passed since last update.

概要

rosrunする際にファイルパスをコマンドラインから取得したかったのですが、
適当なサンプルが発見できなかったので備忘録程度に記しておきます。

環境

  • OS : Linux Mint 18.1 “Sarah” MATE 64bit
  • ROS : Kinetic Kame

ソースコード

args_node.cpp
#include <ros/ros.h>
#include <string>

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

    std::string param = "";

    nh.getParam("param", param);
    std::cout << "parameter : " << param << std::endl;

    return 0;
}

実行

ubuntu@ubuntu ~/catkin $ rosrun args args_node _param:=hoge
parameter : hoge

疑問点

この方法だと、例えば変数が数値の場合

_param:=1.0

に値が取得できません。その際には変数paramをint型に変更する必要があります。
すべてstringで取得できれば、その後エラー処理もできるし良いと思うんですが、だれか
良い方法を知りませんか。

終わりに

roslaunchでもlaunchファイルにパラメータ書いておけば、
上記のような方法で取得できるはずです(未確認)。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away