・プロジェクト作成
特記事項なし
・パッケージマネージャーでgitからインターフェースをインストール
公式は以下のリンク
https://github.com/Unity-Technologies/ROS-TCP-Connector
パッケージマネージャーを起動し AddPackage from git URL を選択
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/PakageManagerMenu.png)
選択すると以下のような入力欄が出てくるので入力欄に
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/image-1.png)
https://github.com/Unity-Technologies/ROS-TCP-Connector.git?path=/com.unity.robotics.ros-tcp-connector
を入力”Addで追加”
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/MenuAddedRobotics.png)
入力し正常に終了するとメニューにRoboticsが追加される。
・ROSの設定
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/rossettinMenuOnly.png)
追加されたメニューからROS Settingを選択。
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/rossettindlgCheck.png)
表示されたダイアログのProtocolをROS2に設定
・メッセージ登録
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/MessageMenuOnly.png)
追加されたメニューのGeneralRosMessageを選択。
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/MessageDlg2-1.png)
Browsでメッセージファイルのフォルダを選択する。
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/MessageDlg3.png)
Build msg でUnity用データの作成
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/MessageDlg4.png)
処理が終わるとRbild msgに代わる
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/MessageFile.png)
buildするとプロジェクトのAssetsの中にメッセージファイルの作成される。
・パブリッシャー、サブスクライバースクリプト作成
以下のリンク先のサンプルがある
https://github.com/Unity-Technologies/Unity-Robotics-Hub/tree/main/tutorials/ros_unity_integration
・メッセージファイルと通信プログラムの対応関係
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/SubscriberScriptMarge-1024x371.png)
メッセージファイルと、サブスクライバーのネームスペースとクラス対応関係は上の図の通り。パブリッシャーの時も同じ感じになる。
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/SubscriberScriptClass.png)
Usingを使用した場合はそのオブジェクトを適用
・ROS2とUnityの通信の対応関係
![](https://robot-creation-diary.com/wp-content/uploads/2022/04/SubscriberScriptMarge2-1024x215.png)
ROS2のチュートリアルのパブリシャーやサブスクライバーの名前と、Unityのサンプルでは名前が一致していないので名前を合わせる必要がある。Unityでは名前を変数に代入してい使うとInspecterで指定した名前が優先されるので注意が必要。上の図はROS2パブリッシャーUnityサブスクライバーであるが、逆の時も同様である。
・実行
特記事項なし