Last active
June 21, 2024 05:43
-
-
Save bayodesegun/d71eda74018e9d208ebc2f1c478346e4 to your computer and use it in GitHub Desktop.
Sample ROS Service Server and Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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