Last active
December 11, 2018 02:39
-
-
Save TheProjectsGuy/1ae5281bdff27703d640f5f7b35b884a to your computer and use it in GitHub Desktop.
Service server creates using Python for ROS
This file contains hidden or 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 | |
| # Import files | |
| import rospy | |
| from intro_pkg1.srv import * | |
| # Service server function | |
| def equation(request): | |
| # Response object | |
| response = FloatIOResponse() | |
| # Relation between request and response | |
| response.output = 2 * (request.input) + 1 | |
| rospy.loginfo("Request: {0}, response: {1}".format(request.input, response.output)) | |
| # Return the response object | |
| return response | |
| def main(): | |
| # Initialize the node | |
| rospy.init_node("Equation_server", anonymous=True) | |
| # Setup the server | |
| rospy.Service("equation_service", FloatIO, equation) | |
| rospy.loginfo("FloatIO service activated. Equation: Y = 2 * X + 1, Y is output and X is input") | |
| # Spin the node | |
| rospy.spin() | |
| # Main code that will be executed | |
| if __name__ == "__main__": | |
| try: | |
| # Main function | |
| main() | |
| except rospy.ROSInterruptException: | |
| rospy.logfatal("Node crashed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment