You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/ros_unity_integration/publisher.md
+116Lines changed: 116 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -103,4 +103,120 @@ public class RosPublisherExample : MonoBehaviour
103
103
104
104

105
105
106
+
## ROSSPublisherExample script
107
+
108
+
```csharp
109
+
usingRosMessageTypes.RoboticsDemo;
110
+
usingUnityEngine;
111
+
usingUnity.Robotics.ROSTCPConnector;
112
+
113
+
/// <summary>
114
+
///
115
+
/// </summary>
116
+
publicclassRosPublisherExample : MonoBehaviour
117
+
{
118
+
ROSConnectionros;
119
+
publicstringtopicName="pos_rot";
120
+
121
+
// The game object
122
+
publicGameObjectcube;
123
+
// Publish the cube's position and rotation every N seconds
124
+
publicfloatpublishMessageFrequency=0.5f;
125
+
126
+
// Used to determine how much time has elapsed since the last message was published
127
+
privatefloattimeElapsed;
128
+
129
+
voidStart()
130
+
{
131
+
// start the ROS connection
132
+
ros=ROSConnection.instance;
133
+
}
134
+
135
+
privatevoidUpdate()
136
+
{
137
+
timeElapsed+=Time.deltaTime;
138
+
139
+
if (timeElapsed>publishMessageFrequency)
140
+
{
141
+
cube.transform.rotation=Random.rotation;
142
+
143
+
MPosRotcubePos=newMPosRot(
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
+
usingRosMessageTypes.RoboticsDemo;
166
+
usingUnityEngine;
167
+
usingUnity.Robotics.ROSTCPConnector;
168
+
```
169
+
170
+
### Instantiating the ROSConnection class in unity
171
+
172
+
```csharp
173
+
voidStart()
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.
0 commit comments