自定義msg
將消息導(dǎo)入U(xiǎn)nity的步驟如下所示:
1.Unity的菜單“Robotics→Generate ROS Messages…”選擇。
2.在“ROS message path”中選擇“catkin_ws/src”。
然后就可以看到path下的msg都會(huì)顯示在Unity下面
然后點(diǎn)擊“MyString.msg”中的“Build msg”。這樣“MyString.msg”將被轉(zhuǎn)換成c#腳本“MyStringMsg”,并在Project窗口中輸出“RosMessages”。
Topic話題
這一小節(jié)我們主要來(lái)說(shuō)Topic的發(fā)布和訂閱,首先我們來(lái)看一下發(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;
// 初始化時(shí)被調(diào)用
void Start()
{
// 向ROS連接注冊(cè)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連接注冊(cè)Subscribe
ROSConnection.instance.Subscribe< MyStringMsg >("chatter", Callback);
}
void Callback(MyStringMsg msg){
Debug.Log(msg.data);
}
}
同時(shí)我們可以在ROS當(dāng)中訂閱這些信息,運(yùn)行
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
然后寫一個(gè)listener.py的訂閱器。
-
編程
+關(guān)注
關(guān)注
89文章
3704瀏覽量
96346 -
ROS
+關(guān)注
關(guān)注
1文章
290瀏覽量
18302 -
Unity
+關(guān)注
關(guān)注
1文章
129瀏覽量
23007
發(fā)布評(píng)論請(qǐng)先 登錄
如何將dxf導(dǎo)入Allegro

如何將solidworks文件導(dǎo)入到labview中
如何將示例項(xiàng)目導(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庫(kù)轉(zhuǎn)換導(dǎo)入到PADS中使用
如何將Klayout Cell動(dòng)態(tài)導(dǎo)入Lumerical Multiphysics

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

評(píng)論