Skip to content

Commit 3f06921

Browse files
authored
Update Part 8. Unity Publish & ROS Subscribe MSGS.md
1 parent a71b562 commit 3f06921

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed
Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,83 @@
1-
x
1+
# Unity Section
2+
[Go To Main Page
3+
](https://github.com/matsive/Unity_Robotics_ROS2/edit/main/README.md)
4+
## Part 8. Unity Publish & ROS Subscribe MSGS
5+
6+
**Unity Script for Publishing Messages:**
7+
- A C# Script `UnityMessageToTCP.cs` was create and the following code was written in it.
8+
```
9+
using System.Collections;
10+
using System.Collections.Generic;
11+
using UnityEngine;
12+
using Unity.Robotics.ROSTCPConnector;
13+
using RosMessageTypes.MatsiveR2msgs;
14+
15+
public class NewBehaviourScript : MonoBehaviour
16+
{
17+
ROSConnection ros;
18+
public string topicName = "CubePos";
19+
20+
// The game object
21+
public GameObject cube;
22+
// Publish the cube's position and rotation every N seconds
23+
public float publishMessageFrequency = 0.5f;
24+
25+
// Used to determine how much time has elapsed since the last message was published
26+
private float timeElapsed;
27+
28+
void Start()
29+
{
30+
// start the ROS connection
31+
ros = ROSConnection.GetOrCreateInstance();
32+
ros.RegisterPublisher<UnityCubePositionMsg>(topicName);
33+
}
34+
35+
private void Update()
36+
{
37+
timeElapsed += Time.deltaTime;
38+
39+
if (timeElapsed > publishMessageFrequency)
40+
{
41+
cube.transform.rotation = Random.rotation;
42+
43+
UnityCubePositionMsg cubePos = new UnityCubePositionMsg(
44+
new double[] { (double)cube.transform.position.x },
45+
new double[] { (double)cube.transform.position.y },
46+
new double[] { (double)cube.transform.position.z },
47+
new double[] { (double)cube.transform.rotation.x },
48+
new double[] { (double)cube.transform.rotation.y },
49+
new double[] { (double)cube.transform.rotation.z }
50+
51+
);
52+
53+
// Finally send the message to server_endpoint.py running in ROS
54+
ros.Publish(topicName, cubePos);
55+
56+
timeElapsed = 0;
57+
}
58+
}
59+
}
60+
```
61+
- Create new empty gameobject and name it `RosPublisher`.
62+
- Add the created script to the new gameobject **RosPublisher** in the scene by inspection-> add component -> UnityMessageToTCP.cs or dragging it to the empty area below add component in inspection.<br />
63+
- In that Component add the Cube gameobject by clicking the empty place and selecting the cube or dragging the cube to that empty place.<br />
64+
- After doing this running the Unity Game will make the cube rotate and also show error message in the terminal as ROS2 is not connected to Unity yet.
65+
66+
<!-- ![image](https://github.com/user-attachments/assets/9cb4d73e-218e-4f22-8706-817278693ebe) -->
67+
<p float="left">
68+
<img src="https://github.com/user-attachments/assets/9cb4d73e-218e-4f22-8706-817278693ebe" width="600" />
69+
</p>
70+
71+
To make the connection between Unity and ROS2. The ROS2 package for subscribing to the unity messages need to run 1st. Then running the play button in unity will establish the connection. like below in terminal.
72+
73+
74+
`INFO] [1729734446.178315156] [UnityEndpoint]: Starting server on 0.0.0.0:10000` <br />
75+
`[INFO] [1729734446.523227541] [UnityEndpoint]: Connection from 127.0.0.1` <br />
76+
`[INFO] [1729734446.549483530] [UnityEndpoint]: RegisterSubscriber(/tf, <class 'tf2_msgs.msg._tf_message.TFMessage'>) OK` <br />
77+
`[INFO] [1729734446.554115496] [UnityEndpoint]: RegisterPublisher(/CubePos, <class 'matsive_r2msgs.msg._unity_cube_position.UnityCubePosition'>) OK `<br />
78+
79+
<!-- ![image](https://github.com/user-attachments/assets/73507b37-4efd-4c8f-bc99-721f44133598)-->
80+
<p float="left">
81+
<img src="https://github.com/user-attachments/assets/73507b37-4efd-4c8f-bc99-721f44133598" width="600" />
82+
</p>
83+

0 commit comments

Comments
 (0)