Skip to content

Commit 1b99138

Browse files
authored
Add client/service example using nav_msgs/srv/GetMap (#1247)
This PR adds comprehensive examples demonstrating client/service patterns using the `nav_msgs/srv/GetMap` service type, which is commonly used in robotics for retrieving occupancy grid map data. The examples complement the existing AddTwoInts service examples by showing how to work with more complex message types. - Adds GetMap service server example that provides sample occupancy grid data - Adds GetMap client example that requests and analyzes map data with visualization - Updates documentation with detailed usage instructions and explanations Fix: #1246
1 parent d9a0440 commit 1b99138

File tree

3 files changed

+432
-12
lines changed

3 files changed

+432
-12
lines changed

example/services/README.md

Lines changed: 192 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ ROS 2 services provide a request-response communication pattern where clients se
1313

1414
## Service Examples
1515

16-
### Service Server (`service/service-example.js`)
16+
### AddTwoInts Service
17+
18+
#### Service Server (`service/service-example.js`)
1719

1820
**Purpose**: Demonstrates creating a service server that adds two integers.
1921

@@ -27,9 +29,9 @@ ROS 2 services provide a request-response communication pattern where clients se
2729
- **Features**:
2830
- Service introspection (ROS 2 Iron+) for monitoring service calls
2931
- Proper response handling using `response.template` and `response.send()`
30-
- **Run Command**: `node service/service-example.js`
32+
- **Run Command**: `node example/services/service/service-example.js`
3133

32-
### Service Client (`client/client-example.js`)
34+
#### Service Client (`client/client-example.js`)
3335

3436
**Purpose**: Demonstrates creating a service client that sends requests to the AddTwoInts service.
3537

@@ -44,19 +46,57 @@ ROS 2 services provide a request-response communication pattern where clients se
4446
- Service availability checking with `waitForService()`
4547
- Service introspection configuration (ROS 2 Iron+)
4648
- Asynchronous request handling with callbacks
47-
- **Run Command**: `node client/client-example.js`
49+
- **Run Command**: `node example/services/client/client-example.js`
50+
51+
### GetMap Service
52+
53+
#### Service Server (`service/getmap-service-example.js`)
54+
55+
**Purpose**: Demonstrates creating a service server that provides occupancy grid map data.
56+
57+
- **Service Type**: `nav_msgs/srv/GetMap`
58+
- **Service Name**: `get_map`
59+
- **Functionality**:
60+
- Provides a sample 10x10 occupancy grid map
61+
- Returns map metadata including resolution, origin, and frame_id
62+
- Includes realistic map data with free space and obstacles
63+
- Updates timestamp on each request
64+
- **Features**:
65+
- Complex message types (OccupancyGrid with nested structures)
66+
- Realistic navigation map data for robotics applications
67+
- Service introspection support (ROS 2 Iron+)
68+
- Detailed logging of map properties
69+
- **Run Command**: `node example/services/service/getmap-service-example.js`
70+
71+
#### Service Client (`client/getmap-client-example.js`)
72+
73+
**Purpose**: Demonstrates creating a service client that requests map data from the GetMap service.
74+
75+
- **Service Type**: `nav_msgs/srv/GetMap`
76+
- **Service Name**: `get_map`
77+
- **Functionality**:
78+
- Sends empty request (GetMap requests have no parameters)
79+
- Receives and analyzes occupancy grid map data
80+
- Displays comprehensive map information and statistics
81+
- Shows ASCII visualization for small maps
82+
- **Features**:
83+
- Handling complex ROS 2 message types (OccupancyGrid)
84+
- Map data analysis (cell distribution, metadata extraction)
85+
- Educational output showing map properties
86+
- Visual representation of map data
87+
- **Run Command**: `node example/services/client/getmap-client-example.js`
4888

4989
## How to Run the Examples
5090

51-
### Running the Complete Service Example
91+
### Running the AddTwoInts Service Example
5292

5393
1. **Prerequisites**: Ensure ROS 2 is installed and sourced
5494

5595
2. **Start the Service Server**: In one terminal, run:
5696

5797
```bash
58-
cd example/services
59-
node service/service-example.js
98+
cd /path/to/rclnodejs
99+
node example/services/service/service-example.js
60100
```
61101

62102
You should see:
@@ -68,8 +108,8 @@ ROS 2 services provide a request-response communication pattern where clients se
68108
3. **Start the Client**: In another terminal, run:
69109

70110
```bash
71-
cd example/services
72-
node client/client-example.js
111+
cd /path/to/rclnodejs
112+
node example/services/client/client-example.js
73113
```
74114

75115
4. **Expected Output**:
@@ -89,6 +129,88 @@ ROS 2 services provide a request-response communication pattern where clients se
89129
Result: object { sum: 79n }
90130
```
91131

132+
### Running the GetMap Service Example
133+
134+
1. **Prerequisites**: Ensure ROS 2 is installed and sourced
135+
136+
2. **Start the GetMap Service Server**: In one terminal, run:
137+
138+
```bash
139+
cd /path/to/rclnodejs
140+
node example/services/service/getmap-service-example.js
141+
```
142+
143+
You should see:
144+
145+
```
146+
Introspection configured
147+
GetMap service is ready. Waiting for requests...
148+
Service provides a 10x10 occupancy grid map
149+
```
150+
151+
3. **Start the GetMap Client**: In another terminal, run:
152+
153+
```bash
154+
cd /path/to/rclnodejs
155+
node example/services/client/getmap-client-example.js
156+
```
157+
158+
4. **Expected Output**:
159+
160+
**Service Server Terminal**:
161+
162+
```
163+
Incoming GetMap request: object {}
164+
Sending map with dimensions: 10 x 10
165+
Map resolution: 0.05 meters/pixel
166+
Number of occupied cells: 3
167+
Sending response: object Map data size: 100
168+
--
169+
```
170+
171+
**Client Terminal**:
172+
173+
```
174+
GetMap service found. Requesting map...
175+
Sending: object {}
176+
Response received: object
177+
Map metadata:
178+
Frame ID: map
179+
Timestamp: 1756479378.889000000
180+
Resolution: 0.05000000074505806 meters/pixel
181+
Dimensions: 10x10
182+
Origin position: x: -2.5 y: -2.5 z: 0
183+
Map data size: 100 cells
184+
Cell distribution:
185+
Free cells (0): 97
186+
Occupied cells (100): 3
187+
Unknown cells (-1): 0
188+
189+
ASCII Map Representation:
190+
. = free space, # = occupied, ? = unknown
191+
..........
192+
..........
193+
..#.......
194+
...#......
195+
....#.....
196+
..........
197+
..........
198+
..........
199+
..........
200+
..........
201+
```
202+
203+
### Quick Test Script
204+
205+
For convenience, you can test the GetMap examples using the provided test script from the project root:
206+
207+
```bash
208+
cd /path/to/rclnodejs
209+
./test_getmap_examples.sh
210+
```
211+
212+
This script automatically starts the service, tests the client, and cleans up.
213+
92214
### Single Run vs Continuous Operation
93215

94216
- **Client**: Runs once, sends a request, receives response, then shuts down
@@ -101,32 +223,58 @@ ROS 2 services provide a request-response communication pattern where clients se
101223
- **Request-Response**: Synchronous communication where clients wait for responses
102224
- **Service Discovery**: Clients check if services are available before sending requests
103225
- **Error Handling**: Proper handling of service unavailability
226+
- **Empty Requests**: Services like GetMap that don't require input parameters
227+
- **Complex Responses**: Handling nested message structures like OccupancyGrid
104228

105229
### ROS 2 Service Features
106230

107231
- **BigInt Support**: Using JavaScript BigInt for ROS 2 integer types
108232
- **Service Introspection**: Monitoring service calls and events (ROS 2 Iron+)
109233
- **Quality of Service**: Configurable QoS profiles for reliable communication
234+
- **Complex Message Types**: Working with navigation messages (nav_msgs)
235+
- **Nested Structures**: Handling messages with headers, metadata, and arrays
110236

111237
### Programming Patterns
112238

113239
- **Async/Await**: Modern JavaScript patterns for asynchronous operations
114240
- **Callback Handling**: Response processing using callback functions
115241
- **Resource Management**: Proper node shutdown and cleanup
242+
- **Data Analysis**: Processing and interpreting received data
243+
- **Visualization**: Converting data to human-readable formats
244+
245+
### Robotics Applications
246+
247+
- **Navigation**: Map data retrieval for path planning and localization
248+
- **Occupancy Grids**: Understanding spatial data representation
249+
- **Coordinate Systems**: Working with map origins, resolutions, and frames
250+
- **Sensor Data**: Processing grid-based sensor information
116251

117252
## Service Introspection (ROS 2 Iron+)
118253

119-
Both examples include service introspection capabilities for ROS 2 distributions newer than Humble:
254+
All examples include service introspection capabilities for ROS 2 distributions newer than Humble:
255+
256+
### AddTwoInts Service
120257

121258
- **Service Server**: Configured with `CONTENTS` introspection to log full request/response data
122259
- **Client**: Configured with `METADATA` introspection to log service call metadata
123260

124-
To monitor service events, use:
261+
To monitor AddTwoInts service events:
125262

126263
```bash
127264
ros2 topic echo "/add_two_ints/_service_event"
128265
```
129266

267+
### GetMap Service
268+
269+
- **Service Server**: Configured with `CONTENTS` introspection to log full request/response data
270+
- **Client**: Configured with `METADATA` introspection to log service call metadata
271+
272+
To monitor GetMap service events:
273+
274+
```bash
275+
ros2 topic echo "/get_map/_service_event"
276+
```
277+
130278
## Message Types and Data Handling
131279

132280
### AddTwoInts Service Definition
@@ -140,12 +288,44 @@ int64 b
140288
int64 sum
141289
```
142290

143-
### JavaScript Implementation Details
291+
**JavaScript Implementation Details**:
144292

145293
- **BigInt Usage**: ROS 2 `int64` maps to JavaScript `BigInt` type
146294
- **Response Template**: Use `response.template` to get the proper response structure
147295
- **Response Sending**: Call `response.send(result)` to send the response back
148296

297+
### GetMap Service Definition
298+
299+
```
300+
# Request
301+
# Empty - no parameters needed
302+
---
303+
# Response
304+
nav_msgs/OccupancyGrid map
305+
```
306+
307+
**OccupancyGrid Structure**:
308+
309+
```
310+
std_msgs/Header header
311+
builtin_interfaces/Time stamp
312+
string frame_id
313+
MapMetaData info
314+
builtin_interfaces/Time map_load_time
315+
float32 resolution
316+
uint32 width
317+
uint32 height
318+
geometry_msgs/Pose origin
319+
int8[] data
320+
```
321+
322+
**JavaScript Implementation Details**:
323+
324+
- **Empty Requests**: GetMap requests are empty objects `{}`
325+
- **Complex Responses**: OccupancyGrid contains nested headers, metadata, and data arrays
326+
- **Map Data**: Array of int8 values (0=free, 100=occupied, -1=unknown)
327+
- **Coordinate Systems**: Origin defines the real-world position of the map's (0,0) cell
328+
149329
## Troubleshooting
150330

151331
### Common Issues

0 commit comments

Comments
 (0)