Skip to content

Instantly share code, notes, and snippets.

@bayodesegun
Last active March 16, 2023 03:05
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayodesegun/d71eda74018e9d208ebc2f1c478346e4 to your computer and use it in GitHub Desktop.
Save bayodesegun/d71eda74018e9d208ebc2f1c478346e4 to your computer and use it in GitHub Desktop.
Sample ROS Service Server and Client
#! /usr/bin/env python
# remember to make this file executable (`chmod +x`) before trying to run it
import rospy
from std_srvs.srv import Trigger, TriggerResponse
def trigger_response(request):
return TriggerResponse(
success=True,
message="Hey, roger that; we'll be right there!"
)
rospy.init_node('sos_service')
my_service = rospy.Service('/fake_911', Trigger, trigger_response)
rospy.spin()
#! /usr/bin/env python
# remember to make this file executable (`chmod +x`) before trying to run it
import rospy
from std_srvs.srv import Trigger, TriggerRequest
# init a node as usual
rospy.init_node('sos_service_client')
# wait for this service to be running
# Ideally, this service should run 24/7, but remember it's fake :)
rospy.wait_for_service('/fake_911')
# Create the connection to the service. Remeber it's a Trigger service
sos_service = rospy.ServiceProxy('/fake_911', Trigger)
# Create an object of type TriggerRequest. We need a TriggerRequest for a Trigger service
# We don't need to pass any argument because it doesn't take any
sos = TriggerRequest()
# Now send the request through the connection
result = sos_service(sos)
# Done, let's see the result!
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment