LoginSignup
2
0

【8日目】OSS連携してみたい!(6)PythonでAutowareのmsgをサブスクライブ

Last updated at Posted at 2023-12-07

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

前回までのあらすじ

OSS連携を試みるため、PythonでROS2のパブリッシャーとサブスクリプションを用いてHelloWorldしてみました。
今回はC++同様、Autowareのmsgをサブスクライブして、サブスクライブしたらログを表示するプログラムを作りたいと思います。

ROS2でモジュール作成

ROSのチュートリアルの内容を例によって参照します。

Autowareのカスタマイズ

パッケージの作成

例によってROSのチュートリアルを見ながら、簡単なサブスクリプションを作ります。

公式サイトの通り、ROS2のパッケージを作成します。

$ cd ~/aichallenge2023-racing/docker/aichallenge/aichallenge_ws/src/aichallenge_submit/
$ ros2 pkg create --build-type ament_python py_path_subscriber

ROSチュートリアルに従って、package.xmlとCMakeLists.txtを変更します。

ソースコード

ソースコードはこんな感じ。
Autowareのmsgのimportの仕方はまだよくわかってないです。

py_pathwithlaneid_sub_function.py
import rclpy
from rclpy.node import Node
from autoware_auto_planning_msgs.msg import PathWithLaneId


from std_msgs.msg import String


class MinimalSubscriber(Node):
    def __init__(self):
        super().__init__("minimal_subscriber")
        self.subscription = self.create_subscription(
            PathWithLaneId,
            "behavior_planning/path_with_lane_id",
            self.listener_callback,
            10,
        )
        self.subscription  # prevent unused variable warning

    def listener_callback(self, msg):
        self.get_logger().info("I heard PathWithLaneId")


def main(args=None):
    rclpy.init(args=args)

    minimal_subscriber = MinimalSubscriber()

    rclpy.spin(minimal_subscriber)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_subscriber.destroy_node()
    rclpy.shutdown()


if __name__ == "__main__":
    main()

パッケージのビルド

次に、Dockerを起動してコードをビルドします。

$ cd /aichallenge/
$ pip install setuptools==58.2.0
$ bash clean_build.sh 

以下のようにpy_path_subscriberのビルドができれば成功です。

パッケージの実行

参考記事同様、launchファイルから実行します。本大会ではAutoware-Microを使用しており、autoware_micro_awsim.launch.xmlを変更します。

autoware_micro_awsim.launch.xml
<!-- lane_driving -->
〜割愛〜
<group>
    <node pkg="cpp_pathwithlaneid_pubsub" exec="cpp_pathwithlaneid_pubsub"/>
</group> <!-- Simple Pub Sub -->
</group> <!-- lane_driving -->

実行してみます。

$ bash run_autoware.sh
$ bash run_awsim.sh

Screenshot from 2023-12-03 22-53-49.png
PythonでもAutowareのmsgをサブスクライブできることを確認しました。

今後の予定

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

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