A .srv file defines a service in ROS. It specifies the request and response data structures for communication between nodes.
Click to reveal answer
beginner
How is a .srv file structured?
A .srv file has two parts separated by a line with ---. The first part defines the request fields, and the second part defines the response fields.
Click to reveal answer
beginner
Why do we use --- in a .srv file?
The --- line separates the request message from the response message in a .srv file, making clear what data is sent and what is received.
Click to reveal answer
beginner
Can you give an example of a simple .srv file?
Example:
int32 a
int32 b
---
int32 sum
This defines a service where the request sends two integers a and b, and the response returns their sum.
Click to reveal answer
intermediate
How does a ROS node use a .srv file?
A ROS node uses a .srv file to create a service server or client. The server waits for requests and sends responses, while the client sends requests and waits for responses.
Click to reveal answer
What separates the request and response parts in a .srv file?
A===
B...
C---
D;;;
✗ Incorrect
The line with --- separates the request and response sections in a .srv file.
Which of these is true about .srv files?
AThey define request and response for services.
BThey define launch configurations.
CThey define parameters for nodes.
DThey define messages for topics.
✗ Incorrect
.srv files define the request and response structure for ROS services.
In a .srv file, what does the first part before --- represent?
AResponse message
BRequest message
CService name
DService type
✗ Incorrect
The first part before --- defines the request message fields.
What data types can you use inside a .srv file?
AStandard ROS message types like int32, float64, string, etc.
BOnly integers
COnly strings
DOnly boolean
✗ Incorrect
.srv files support standard ROS message types such as int32, float64, string, bool, and arrays.
What happens if a ROS node sends a service request?
AIt shuts down the service.
BIt publishes a message on a topic.
CIt starts a new node.
DIt waits for a response from the service server.
✗ Incorrect
When a node sends a service request, it waits for the server to respond with the service response.
Explain the structure and purpose of a .srv file in ROS.
Think about how two nodes talk using a question and answer format.
You got /4 concepts.
Describe how a ROS node uses a .srv file to provide or use a service.
Imagine one node asking a question and another node answering.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a .srv file in ROS?
easy
A. To define the structure of a service with request and response parts
B. To list all the topics a node subscribes to
C. To specify the launch configuration for nodes
D. To store parameters for the robot
Solution
Step 1: Understand what a .srv file represents
A .srv file defines a service interface in ROS, which includes a request and a response part.
Step 2: Match the purpose to the options
Only To define the structure of a service with request and response parts correctly states that .srv files define the structure of a service with request and response parts.
Final Answer:
To define the structure of a service with request and response parts -> Option A
Quick Check:
.srv files define service structure = C [OK]
Hint: Remember .srv files split request and response with --- [OK]
Common Mistakes:
Confusing .srv files with launch files
Thinking .srv files list topics
Assuming .srv files store parameters
2. Which of the following is the correct syntax to separate request and response fields in a .srv file?
easy
A. Using a colon ':' between request and response
B. Using a comma ',' between request and response
C. Using a semicolon ';' between request and response
D. Using three dashes '---' between request and response
Solution
Step 1: Recall the .srv file format
The .srv file separates request and response fields with three dashes '---'.
Step 2: Identify the correct separator
Only Using three dashes '---' between request and response correctly uses '---' to separate the two parts.
Final Answer:
Using three dashes '---' between request and response -> Option D
Quick Check:
Request and response separated by --- = A [OK]
Hint: Look for '---' as the separator in .srv files [OK]
Common Mistakes:
Using commas or semicolons instead of ---
Confusing with YAML or JSON syntax
Missing the separator line entirely
3. Given this .srv file content:
int32 a
int32 b
---
int32 sum
What will the response contain when the service is called with request values a=3 and b=4 if the service adds them?
medium
A. sum = 12
B. sum = 34
C. sum = 7
D. sum = 0
Solution
Step 1: Understand the request and response fields
The request has two integers a and b; the response has one integer sum.
Step 2: Apply the service logic
The service adds a and b, so sum = 3 + 4 = 7.
Final Answer:
sum = 7 -> Option C
Quick Check:
3 + 4 = 7 [OK]
Hint: Add request fields to get response sum [OK]
Common Mistakes:
Concatenating numbers as strings
Multiplying instead of adding
Returning zero by mistake
4. Identify the error in this .srv file:
float64 x
float64 y
float64 z
---
float64 result
medium
A. No error, this is a valid .srv file
B. Response field should be on the same line as request fields
C. Missing separator '---' between request and response
D. Request fields must be integers, not float64
Solution
Step 1: Check the separator
The separator '---' is present between request and response fields.
Step 2: Validate field types and format
Using float64 for request and response fields is valid in .srv files.
Final Answer:
No error, this is a valid .srv file -> Option A
Quick Check:
Correct separator and valid types = A [OK]
Hint: Look for '---' and valid types to confirm syntax [OK]
Common Mistakes:
Thinking separator is missing when it is present
Believing response must be on same line
Assuming only integers allowed
5. You want to define a service that takes a string command and an integer timeout as request, and returns a boolean success and a string message as response. Which of these .srv files correctly defines this?
hard
A. string command
int32 timeout
---
bool success, string message
B. string command
int32 timeout
---
bool success
string message
C. string command
int32 timeout
bool success
string message
D. string command
int32 timeout
---
string message
bool success
Solution
Step 1: Check for correct separator and field order
The request fields are before '---' and response fields after. string command
int32 timeout
---
bool success
string message correctly uses '---' and lists request then response fields.