Bird
Raised Fist0
ROSframework~10 mins

Service definition (.srv files) in ROS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Service definition (.srv files)
Create .srv file
↓
Define Request fields
↓
Add separator ---
↓
Define Response fields
↓
Save .srv file
↓
Build package to generate code
↓
Use service in ROS nodes
This flow shows how to create a ROS service definition file by specifying request and response fields separated by '---', then building and using it.
Execution Sample
ROS
int32 a
int32 b
---
int32 sum
Defines a service with two integers as request and one integer as response representing their sum.
Execution Table
StepActionContent ProcessedResult
1Read lineint32 aAdd 'a' as int32 request field
2Read lineint32 bAdd 'b' as int32 request field
3Read line---Switch to response fields
4Read lineint32 sumAdd 'sum' as int32 response field
5End of fileNo more linesService definition complete
💡 Reached end of .srv file after defining request and response fields
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Request Fields[][{"name":"a","type":"int32"}][{"name":"a","type":"int32"},{"name":"b","type":"int32"}][{"name":"a","type":"int32"},{"name":"b","type":"int32"}][{"name":"a","type":"int32"},{"name":"b","type":"int32"}][{"name":"a","type":"int32"},{"name":"b","type":"int32"}]
Response Fields[][][][][{"name":"sum","type":"int32"}][{"name":"sum","type":"int32"}]
Parsing Mode'request''request''request''response''response''response'
Key Moments - 3 Insights
Why is there a '---' line in the .srv file?
The '---' line separates the request fields from the response fields, as shown in execution_table step 3 where parsing mode switches from request to response.
Can the request and response have different numbers of fields?
Yes, request and response fields are independent; the example shows 2 request fields and 1 response field, confirmed in variable_tracker.
What happens if you forget to add the '---' separator?
The parser will treat all lines as request fields and never switch to response, causing errors or missing response definition, as parsing mode never changes in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the '---' line do?
AEnds the service definition
BSeparates request and response fields
CStarts the request fields
DIs ignored by the parser
💡 Hint
Check execution_table row 3 where action is 'Switch to response fields'
According to variable_tracker, what is the parsing mode after step 4?
A'response'
B'request'
C'end'
D'error'
💡 Hint
Look at the 'Parsing Mode' row after step 4 in variable_tracker
If you add a new field 'string name' before the '---', how does the request fields list change?
AIt causes an error
BIt adds 'name' to response fields
CIt adds 'name' to request fields
DIt removes existing fields
💡 Hint
Request fields accumulate before '---' as shown in variable_tracker rows for request fields
Concept Snapshot
Service definition (.srv files):
- Define request fields first (type and name per line)
- Use '---' line to separate request and response
- Define response fields after '---'
- Save and build package to generate service code
- Use service in ROS nodes for request-response communication
Full Transcript
A ROS service definition file (.srv) is a simple text file that defines the data sent to and received from a service. It has two parts: request fields and response fields, separated by a line with three dashes '---'. Each field is defined by a type and a name on its own line. The parser reads the file line by line, adding fields to the request until it sees '---', then switches to adding response fields. After the file is complete, the ROS build system generates code to use this service in nodes. This process allows nodes to communicate by sending requests and receiving responses with defined data structures.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To define the structure of a service with request and response parts -> Option A
  4. 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

  1. Step 1: Recall the .srv file format

    The .srv file separates request and response fields with three dashes '---'.
  2. Step 2: Identify the correct separator

    Only Using three dashes '---' between request and response correctly uses '---' to separate the two parts.
  3. Final Answer:

    Using three dashes '---' between request and response -> Option D
  4. 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

  1. Step 1: Understand the request and response fields

    The request has two integers a and b; the response has one integer sum.
  2. Step 2: Apply the service logic

    The service adds a and b, so sum = 3 + 4 = 7.
  3. Final Answer:

    sum = 7 -> Option C
  4. 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

  1. Step 1: Check the separator

    The separator '---' is present between request and response fields.
  2. Step 2: Validate field types and format

    Using float64 for request and response fields is valid in .srv files.
  3. Final Answer:

    No error, this is a valid .srv file -> Option A
  4. 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

  1. 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.
  2. Step 2: Validate field types and syntax

    string command int32 timeout --- bool success string message lists request fields (string, int32) and response fields (bool, string) correctly without syntax errors.
  3. Final Answer:

    string command int32 timeout --- bool success string message -> Option B
  4. Quick Check:

    Request before --- and response after = D [OK]
Hint: Request fields above ---; response fields below --- [OK]
Common Mistakes:
  • Placing response fields before separator
  • Missing separator line
  • Using commas inside .srv files
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard