micro-rosとUnityの連携(ROS2経由)に挑戦
前回までのあらすじ
Unity Robotics Hubを導入し、UnityからのトピックがROS2で流れていることが確認できた。
次はraspi-picowから送信されるトピックをUnity上で受信する仕掛けに挑戦する
参考にした書籍
Unity Robotics Hubの公式サイト
Unity側の準備
サブスクライバーを作ってみる
今回はトピックを購読する側のプログラムを作成する。

(MySubscriber)

micro_ros_servo_node_publisher
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using RosMessageTypes.Std;
public class MySubscriber : MonoBehaviour
{
ROSConnection ros;
// Start is called before the first frame update
void Start()
{
// ROSコネクションの取得
ros = ROSConnection.GetOrCreateInstance();
// サブスクライバーの登録
ros.Subscribe<UInt16Msg>("micro_ros_servo_node_publisher", OnSubscribe);
}
// サブスクライブごとに呼ばれる
void OnSubscribe(UInt16Msg msg)
{
Debug.Log(String.Format("Subscribe : {0:X4}", msg));
}
// Update is called once per frame
void Update()
{
}
}
ROS2側の準備
ROS-TCP-Endpointを起動しておく
ros2 run ros_tcp_endpoint default_server_endpoint --ros-args -p ROS_IP:=0.0.0.0


Unity側のプロジェクトを実行

0xC40=3136
0xC50=3152
0xC60=3168

きちんとUnity側で受信できている!!
つまりraspi-picow上のmicro-rosからエージェントを通じて、Unityのノードで受信できていることが確認できた!!