LoginSignup
1
0

【4日目】OSS連携してみたい!(2)C++でAutowareのmsgをサブスクライブ

Last updated at Posted at 2023-12-03

この記事は筆者オンリーのAdvent Calendar 20234日目の記事です。

OSS連携を試みるため、ROS2の簡単なモジュールを数回に渡って作っていきたいと思います。

の続きです。

ROS2でモジュール作成

前回、ROS2のチュートリアルを使って自作パブリッシャーとサブスクライバーでHelloWorldを行いました。

今回はAutowareのメッセージをサブスクライブして、サブスクライブしたことを表示するデモを行いたいと思います。

とはいえ、こちらの記事のほぼ丸写し、今回丸写しだと動かなかったので一部改造しただけです。

環境作成

前回記事と、こちらの記事を参考にROSのモジュールを作成。
今回はPathがメッセージとしてパブリッシュされていない様子のため、PathWithLaneIdのメッセージをサブスクライブしてメッセージを受領したことを確認するように改造。

path_subscriber.hpp
#ifndef PATH_SUBSCRIBER_HPP_
#define PATH_SUBSCRIBER_HPP_

#include <rclcpp/rclcpp.hpp>
#include <autoware_auto_planning_msgs/msg/path_with_lane_id.hpp>

using PathWithLaneId = autoware_auto_planning_msgs::msg::PathWithLaneId;

class PathSubscriber : public rclcpp::Node
{
public:
    PathSubscriber();

    void topicCallback(const PathWithLaneId::ConstSharedPtr msg);

private:
    rclcpp::Subscription<PathWithLaneId>::SharedPtr sub_path_;
    PathWithLaneId::ConstSharedPtr path_;
};
#endif // PATH_SUBSCRIBER_HPP_

path_subscriber.cpp
#include "path_subscriber.hpp"
#include <memory>

using PathWithLaneId = autoware_auto_planning_msgs::msg::PathWithLaneId;
using std::placeholders::_1;

PathSubscriber::PathSubscriber() : Node("path_subscriber")
{
    sub_path_ = create_subscription<PathWithLaneId>("behavior_planning/path_with_lane_id", 10, std::bind(&PathSubscriber::topicCallback, this, _1));
}

void PathSubscriber::topicCallback(const PathWithLaneId::ConstSharedPtr msg)
{
    path_ = msg;
    RCLCPP_INFO(this->get_logger(), "I heard");
}

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<PathSubscriber>());
    rclcpp::shutdown();
    return 0;
}

以下の通り、ビルドされ正常終了することを確認。
Screenshot from 2023-11-30 21-54-57.png

実行

docker上で以下を実行

#AWSIMの起動
$ bash run_awsim.sh
#Autowareのビルド、起動
$ bash clean_build.sh
$ bash run_autoware.sh

以下のメッセージが出力され、Autowareのメッセージをサブスクライブしていることを確認しました。
Screenshot from 2023-11-30 23-29-09.png

今後の予定

1.Autowareのメッセージをパブリッシュし、既存のAutowareがサブスクライブするようにする。
2.OSS連携もできるように変更する。
という流れで行きたいと思います。

1
0
2

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
1
0