Ros2 service
Services are another method of communication for nodes. Services are based on a call-and-response model. While topics allow nodes to subscribe to data streams and get continual updates, services only provide data when they are specifically called by a client.
Basic Commands
# =================================================
# Service
# =================================================
# list all the services currently active in the system
ros2 service list <-t>
# -t option allow us to list all service that contain service type
# find out the type of a service
ros2 service type <serivce_name>
# if you want to find all the services of a specific type
ros service find <type_name>
# can call services from the command line, but we need to know the structure of the input arguments
ros2 interface show <type_name>.srv
# THe --- separates the request structure(above) from the response structure(below).
# call a service
ros2 service call <service_name> <service_type> <arguments>
Create a service
- init
- inherit Node and name it
- create service(service type, name, callback)
- define callback
Create a client
- init
- inherit Node and name it
- the type and name must match for the client and service to be able to communicate
- while loop in the constructor checks if a service matching the type and name of the client is available once a second.
- Create a new request object
- define send request method
- main
- init rclpy
- construct object
- send request
- spin_until_future_complete
- log resultx
Create custom msg and srv files
- create a new package
- mkdir msg srv
- msg definition
- srv definition
- change Cmakelist.txt
- add dependency in package.xml
- build