自定義msg
將消息導(dǎo)入Unity的步驟如下所示:
1.Unity的菜單“Robotics→Generate ROS Messages…”選擇。
2.在“ROS message path”中選擇“catkin_ws/src”。
然后就可以看到path下的msg都會顯示在Unity下面
然后點擊“MyString.msg”中的“Build msg”。這樣“MyString.msg”將被轉(zhuǎn)換成c#腳本“MyStringMsg”,并在Project窗口中輸出“RosMessages”。
Topic話題
這一小節(jié)我們主要來說Topic的發(fā)布和訂閱,首先我們來看一下發(fā)布者的Unity編程。
1.在Hierarchy窗口的“+→Create Empty”中創(chuàng)建空GameObject,命名為“Publisher”。
2.在“Publisher”中追加新腳本“ChatterPublisher”,編輯如下
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using MyStringMsg = RosMessageTypes.Hello.MyStringMsg;
public class ChatterPublisher : MonoBehaviour
{
private ROSConnection ros;
// 初始化時被調(diào)用
void Start()
{
// 向ROS連接注冊Topic話題
ros = ROSConnection.instance;
ros.RegisterPublisher< MyStringMsg >("chatter");
}
// 每幀更新
void FixedUpdate()
{
// 發(fā)送msg信息
MyStringMsg msg = new MyStringMsg("Hello Unity!");
ros.Send("chatter", msg);
}
}
而接收者和發(fā)布者類似,都在Hierarchy窗口的“+→Create Empty”中創(chuàng)建空GameObject,命名為“Subscriber”。
在“Subscriber”中添加新的腳本“ChatterSubscriber”,編輯如下。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using MyStringMsg = RosMessageTypes.Hello.MyStringMsg;
public class ChatterSubscriber : MonoBehaviour{
void Start(){
// 向ROS連接注冊Subscribe
ROSConnection.instance.Subscribe< MyStringMsg >("chatter", Callback);
}
void Callback(MyStringMsg msg){
Debug.Log(msg.data);
}
}
同時我們可以在ROS當(dāng)中訂閱這些信息,運行
roscore
rosparam set ROS_IP 127.0.0.1
rosparam set ROS_TCP_PORT 10000
rosrun ros_tcp_endpoint default_server_endpoint.py
# roslauch ros_tcp_endpoint endpoint.launch tcp_ip:=127.0.0.1 tcp_port:=10000 # 將127.0.0.1
然后寫一個listener.py的訂閱器。
-
編程
+關(guān)注
關(guān)注
88文章
3683瀏覽量
94885 -
ROS
+關(guān)注
關(guān)注
1文章
285瀏覽量
17570 -
Unity
+關(guān)注
關(guān)注
1文章
128瀏覽量
22310
發(fā)布評論請先 登錄
如何將dxf導(dǎo)入Allegro

如何將solidworks文件導(dǎo)入到labview中
如何將pcan view 中讀到的數(shù)據(jù)導(dǎo)入labview中?
如何將示例項目導(dǎo)入MCUXpresso IDE?
如何將FPGA里的數(shù)據(jù)導(dǎo)入dsp板子里去呢?
如何將ECC密鑰導(dǎo)入HSE FW?
如何將Unity著色器移植到通用渲染管道
如何將Arm Neon C#內(nèi)部函數(shù)與Unity Burst編譯器一起使用
如何將AD庫轉(zhuǎn)換導(dǎo)入到PADS中使用
如何將Klayout Cell動態(tài)導(dǎo)入Lumerical Multiphysics

如何將python文件導(dǎo)入到ROS系統(tǒng)中

評論