Skip to content

Commit b232e5f

Browse files
Update publisher.md
1 parent ec5b2e0 commit b232e5f

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

tutorials/ros_unity_integration/publisher.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,120 @@ public class RosPublisherExample : MonoBehaviour
103103
104104
![](images/tcp_1.gif)
105105

106+
## ROSSPublisherExample script
107+
108+
```csharp
109+
using RosMessageTypes.RoboticsDemo;
110+
using UnityEngine;
111+
using Unity.Robotics.ROSTCPConnector;
112+
113+
/// <summary>
114+
///
115+
/// </summary>
116+
public class RosPublisherExample : MonoBehaviour
117+
{
118+
ROSConnection ros;
119+
public string topicName = "pos_rot";
120+
121+
// The game object
122+
public GameObject cube;
123+
// Publish the cube's position and rotation every N seconds
124+
public float publishMessageFrequency = 0.5f;
125+
126+
// Used to determine how much time has elapsed since the last message was published
127+
private float timeElapsed;
128+
129+
void Start()
130+
{
131+
// start the ROS connection
132+
ros = ROSConnection.instance;
133+
}
134+
135+
private void Update()
136+
{
137+
timeElapsed += Time.deltaTime;
138+
139+
if (timeElapsed > publishMessageFrequency)
140+
{
141+
cube.transform.rotation = Random.rotation;
142+
143+
MPosRot cubePos = new MPosRot(
144+
cube.transform.position.x,
145+
cube.transform.position.y,
146+
cube.transform.position.z,
147+
cube.transform.rotation.x,
148+
cube.transform.rotation.y,
149+
cube.transform.rotation.z,
150+
cube.transform.rotation.w
151+
);
152+
153+
// Finally send the message to server_endpoint.py running in ROS
154+
ros.Send(topicName, cubePos);
155+
156+
timeElapsed = 0;
157+
}
158+
}
159+
}
160+
```
161+
162+
### Import Statements for Publisher and Messages
163+
164+
```csharp
165+
using RosMessageTypes.RoboticsDemo;
166+
using UnityEngine;
167+
using Unity.Robotics.ROSTCPConnector;
168+
```
169+
170+
### Instantiating the ROSConnection class in unity
171+
172+
```csharp
173+
void Start()
174+
{
175+
// start the ROS connection
176+
ros = ROSConnection.instance;
177+
}
178+
```
179+
To access the APIs needed to communicate with ROS, we need to create a variable of the class ROSConnection in the script. In the rest of the script, we will use `ros` variable to access the APIs needed to publish the message on the desired topic.
180+
181+
### Creating the message
182+
183+
```csharp
184+
timeElapsed += Time.deltaTime;
185+
186+
if (timeElapsed > publishMessageFrequency)
187+
{
188+
cube.transform.rotation = Random.rotation;
189+
190+
MPosRot cubePos = new MPosRot(
191+
cube.transform.position.x,
192+
cube.transform.position.y,
193+
cube.transform.position.z,
194+
cube.transform.rotation.x,
195+
cube.transform.rotation.y,
196+
cube.transform.rotation.z,
197+
cube.transform.rotation.w
198+
);
199+
```
200+
We create the message by instantiating a new variable of the message class. In this case we are instantiating a new `MPosRot` message class and assigning the current position and rotation of the cube to the message.
201+
202+
### Publishing the message
203+
204+
```csharp
205+
ros.Send(topicName, cubePos);
206+
```
207+
After we have created the message, we use the `Send` API to publish the topic.
208+
209+
### Send API
210+
```csharp
211+
public async void Send(string rosTopicName, Message message)
212+
```
213+
### Summary
214+
Its a non-static member of ROSConnection class. Its used to send a ROS message to the desired topic on the ROS machine.
215+
### Parameters
216+
Parameters | Description |
217+
| ------------- | ------------- |
218+
| `string rosTopicName` | Name of the topic to which the message will be published |
219+
| `Message message` | Message to be published on the topic.
220+
221+
106222
Continue to the [ROS Subscriber](subscriber.md) tutorial.

0 commit comments

Comments
 (0)