Skip to content

Commit 33f2438

Browse files
committed
Readme updated, client and service examples.
Signed-off-by: Piotr Jaroszek <[email protected]>
1 parent 9f22d6e commit 33f2438

File tree

3 files changed

+212
-2
lines changed

3 files changed

+212
-2
lines changed

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,66 @@ otherwise
130130
}
131131
```
132132

133+
**Creating a service**
134+
135+
1. Create service body:
136+
```c#
137+
public example_interfaces.srv.AddTwoInts_Response addTwoInts( example_interfaces.srv.AddTwoInts_Request msg)
138+
{
139+
example_interfaces.srv.AddTwoInts_Response response = new example_interfaces.srv.AddTwoInts_Response();
140+
response.Sum = msg.A + msg.B;
141+
return response;
142+
}
143+
```
144+
145+
1. Create a service with a service name and callback:
146+
```c#
147+
IService<example_interfaces.srv.AddTwoInts_Request, example_interfaces.srv.AddTwoInts_Response> service =
148+
ros2Node.CreateService<example_interfaces.srv.AddTwoInts_Request, example_interfaces.srv.AddTwoInts_Response>(
149+
"add_two_ints", addTwoInts);
150+
```
151+
152+
**Calling a service**
153+
154+
1. Create a client:
155+
```c#
156+
private IClient<example_interfaces.srv.AddTwoInts_Request, example_interfaces.srv.AddTwoInts_Response> addTwoIntsClient;
157+
...
158+
addTwoIntsClient = ros2Node.CreateClient<example_interfaces.srv.AddTwoInts_Request, example_interfaces.srv.AddTwoInts_Response>(
159+
"add_two_ints");
160+
```
161+
162+
1. Create a request and call a service:
163+
```c#
164+
example_interfaces.srv.AddTwoInts_Request request = new example_interfaces.srv.AddTwoInts_Request();
165+
request.A = 1;
166+
request.B = 2;
167+
var response = addTwoIntsClient.Call(request);
168+
```
169+
170+
1. You can also make an async call:
171+
```c#
172+
Task<example_interfaces.srv.AddTwoInts_Response> asyncTask = addTwoIntsClient.CallAsync(request);
173+
...
174+
asyncTask.ContinueWith((task) => { Debug.Log("Got answer " + task.Result.Sum); });
175+
```
133176
### Examples
134177

135178
1. Create a top-level object containing `ROS2UnityComponent.cs`. This is the central `Monobehavior` for `Ros2ForUnity` that manages all the nodes. Refer to class documentation for details.
179+
> **Note:** Each example script looks for `ROS2UnityComponent` in its own game object. However, this is not a requirement, just example implementation.
180+
181+
**Topics**
136182
1. Add `ROS2TalkerExample.cs` script to the very same game object.
137183
1. Add `ROS2ListenerExample.cs` script to the very same game object.
138-
> **Note:** Each example script looks for `ROS2UnityComponent` in its own game object. However, this is not a requirement, just example implementation.
139-
1. Once you start the project in Unity, you should be able to see two nodes talking with each other in Unity Editor's console or use `ros2 node list` and `ros2 topic echo /chatter` to verify ros2 communication.
184+
185+
Once you start the project in Unity, you should be able to see two nodes talking with each other in Unity Editor's console or use `ros2 node list` and `ros2 topic echo /chatter` to verify ros2 communication.
186+
187+
**Services**
188+
1. Add `ROS2ServiceExample.cs` script to the very same game object.
189+
1. Add `ROS2ClientExample.cs` script to the very same game object.
190+
191+
Once you start the project in Unity, you should be able to see client node calling an example service.
192+
140193
## Acknowledgements
141194

142195
Open-source release of ROS2 For Unity was made possible through cooperation with [Tier IV](https://tier4.jp). Thanks to encouragement, support and requirements driven by Tier IV the project was significantly improved in terms of portability, stability, core structure and user-friendliness.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2019-2021 Robotec.ai.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Collections;
16+
using System.Collections.Generic;
17+
using System.Threading.Tasks;
18+
using UnityEngine;
19+
using ROS2;
20+
21+
using addTwoIntsReq = example_interfaces.srv.AddTwoInts_Request;
22+
using addTwoIntsResp = example_interfaces.srv.AddTwoInts_Response;
23+
24+
/// <summary>
25+
/// An example class provided for testing of basic ROS2 client
26+
/// </summary>
27+
public class ROS2ClientExample : MonoBehaviour
28+
{
29+
private ROS2UnityComponent ros2Unity;
30+
private ROS2Node ros2Node;
31+
private IClient<addTwoIntsReq, addTwoIntsResp> addTwoIntsClient;
32+
private bool isRunning = false;
33+
private Task<addTwoIntsResp> asyncTask;
34+
35+
IEnumerator periodicAsyncCall()
36+
{
37+
while (ros2Unity.Ok())
38+
{
39+
40+
while (!addTwoIntsClient.IsServiceAvailable())
41+
{
42+
yield return new WaitForSecondsRealtime(1);
43+
}
44+
45+
addTwoIntsReq request = new addTwoIntsReq();
46+
request.A = Random.Range(0, 100);
47+
request.B = Random.Range(0, 100);
48+
49+
asyncTask = addTwoIntsClient.CallAsync(request);
50+
asyncTask.ContinueWith((task) => { Debug.Log("Got async answer " + task.Result.Sum); });
51+
52+
yield return new WaitForSecondsRealtime(1);
53+
}
54+
}
55+
56+
IEnumerator periodicCall()
57+
{
58+
while (ros2Unity.Ok())
59+
{
60+
61+
while (!addTwoIntsClient.IsServiceAvailable())
62+
{
63+
yield return new WaitForSecondsRealtime(1);
64+
}
65+
66+
addTwoIntsReq request = new addTwoIntsReq();
67+
request.A = Random.Range(0, 100);
68+
request.B = Random.Range(0, 100);
69+
var response = addTwoIntsClient.Call(request);
70+
71+
Debug.Log("Got sync answer " + response.Sum);
72+
73+
yield return new WaitForSecondsRealtime(1);
74+
}
75+
}
76+
77+
void Start()
78+
{
79+
ros2Unity = GetComponent<ROS2UnityComponent>();
80+
if (ros2Unity.Ok())
81+
{
82+
if (ros2Node == null)
83+
{
84+
ros2Node = ros2Unity.CreateNode("ROS2UnityClient");
85+
addTwoIntsClient = ros2Node.CreateClient<addTwoIntsReq, addTwoIntsResp>(
86+
"add_two_ints");
87+
}
88+
}
89+
}
90+
91+
void Update()
92+
{
93+
if (!isRunning)
94+
{
95+
isRunning = true;
96+
97+
// Async calls
98+
StartCoroutine(periodicAsyncCall());
99+
100+
// Sync calls
101+
StartCoroutine(periodicCall());
102+
}
103+
}
104+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2019-2021 Robotec.ai.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Collections;
16+
using System.Collections.Generic;
17+
using UnityEngine;
18+
using ROS2;
19+
20+
using addTwoIntsReq = example_interfaces.srv.AddTwoInts_Request;
21+
using addTwoIntsResp = example_interfaces.srv.AddTwoInts_Response;
22+
23+
/// <summary>
24+
/// An example class provided for testing of basic ROS2 service
25+
/// </summary>
26+
public class ROS2ServiceExample : MonoBehaviour
27+
{
28+
private ROS2UnityComponent ros2Unity;
29+
private ROS2Node ros2Node;
30+
private IService<addTwoIntsReq, addTwoIntsResp> addTwoIntsService;
31+
32+
void Start()
33+
{
34+
ros2Unity = GetComponent<ROS2UnityComponent>();
35+
if (ros2Unity.Ok())
36+
{
37+
if (ros2Node == null)
38+
{
39+
ros2Node = ros2Unity.CreateNode("ROS2UnityService");
40+
addTwoIntsService = ros2Node.CreateService<addTwoIntsReq, addTwoIntsResp>(
41+
"add_two_ints", addTwoInts);
42+
}
43+
}
44+
}
45+
46+
public example_interfaces.srv.AddTwoInts_Response addTwoInts( example_interfaces.srv.AddTwoInts_Request msg)
47+
{
48+
Debug.Log("Incoming Service Request A=" + msg.A + " B=" + msg.B);
49+
example_interfaces.srv.AddTwoInts_Response response = new example_interfaces.srv.AddTwoInts_Response();
50+
response.Sum = msg.A + msg.B;
51+
return response;
52+
}
53+
}

0 commit comments

Comments
 (0)