【ROS】subscribeとpublishを同じノードで行う

プログラミング言語
スポンサーリンク

同じノード内で、トピックをsubscribeした後その内容に応じてトピックをpublishしたいという状況はよくあると思います。

ROS Wiki ではこの方法についてあまり触れていないので、ここにまとめておきます。

同じノードで行うためには、subscriberが呼び出すcallback関数の中でpublisherを使えるようにする必要があります。

そのため、以下のようにクラスを定義します。

#include "ros/ros.h"
#include "sensor_msgs/Image.h"

class SubPub
{
private:
  ros::NodeHandle n_;
  ros::Publisher pub_;
  ros::Subscriber sub_;

public:
  void callback(const sensor_msgs::Image &msg_sub);

  SubPub()
  {
    sub_ = n_.subscribe("/topic_sub", 1000, &SubPub::callback, this);
    pub_ = n_.advertise<sensor_msgs::Image>("/topic_pub", 1000);
  }
};

void SubPub::callback(const sensor_msgs::Image &msg_sub)
{
  sensor_msgs::Image msg_pub;

  // 処理

  pub_.publish(msg_pub);
}


int main(int argc, char *argv[])
{
  ros::init(argc, argv, "node_subpub");
  SubPub subpub;

  ros::spin();
}

/topic_sub というトピックをsubscribeし、/topic_pub というトピックをpublishするようにしています。

また、メッセージ型としては、sensor_msgs::Image をsubscribeし、同じ型をpublishするようにしています。

トピック名やメッセージ型は適宜変更してください。

上記コードのように、subscriberとpublisherをクラスのプライベートメンバ変数として持つことで、メンバ関数のcallbackがpublisherにアクセスできるようにしています。

実際に使うときには、上記main関数のように、クラスのインスタンスを1つ生成すればOKです。

プログラミング言語技術系

コメント

  1. 6ccpBPqZNu より:

    pnmCGlvuXPx9zLUI9Nfw3rQuXdgav7om8Ru6BwJsiX5BJUXxvTv79rVtPexZa6Pb8YdqZzNioiobn9Z2yoXB05Jns3U1rY4LYgqXuqanO4GECkJ0vgPd1X6bWQ6Eagb3W2PTNGy5NaFqkRO

  2. graliontorile より:

    Thanks for sharing superb informations. Your site is so cool. I’m impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found simply the info I already searched everywhere and simply couldn’t come across. What an ideal site.

  3. YAYrcDpMnRdxb6wi9o8uUuavcR9oXJbPdNzhFeZv5qTcleR0yZ1T32rBxv13ThWxutnDDjUh0q7xWKjzDDX6weLgA0o9oAspfaWTOJ94bCIqkfPf1A462mNuQLypgL3F14WklCrF0oSLJwVpjRXCQuCnl9RYk

  4. I’ve been absent for some time, but now I remember why I used to love this website. Thank you, I¦ll try and check back more often. How frequently you update your web site?

  5. xin gaming demo より:

    Wow! Thank you! I continuously needed to write on my blog something like that. Can I include a fragment of your post to my site?

  6. slot terbaik より:

    Some genuinely great info , Sword lily I discovered this.

  7. Magnificent site. Lots of useful information here. I am sending it to several pals ans also sharing in delicious. And obviously, thanks on your sweat!

  8. pragmatic slot より:

    Appreciate it for helping out, wonderful info. “Riches cover a multitude of woes.” by Menander.

  9. I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to design my own blog and would like to know where u got this from. cheers

  10. Fastwin77 より:

    I am glad to be a visitant of this unadulterated weblog! , thanks for this rare information! .

  11. Betwing88 より:

    Great blog! Do you have any tips for aspiring writers? I’m hoping to start my own blog soon but I’m a little lost on everything. Would you advise starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally confused .. Any recommendations? Kudos!

タイトルとURLをコピーしました