Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

ros python service server

#!/usr/bin/env python

from beginner_tutorials.srv import AddTwoInts,AddTwoIntsResponse
import rospy

def handle_add_two_ints(req):
    print "Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b))
    return AddTwoIntsResponse(req.a + req.b)

def add_two_ints_server():
    rospy.init_node('add_two_ints_server')
    s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
    print "Ready to add two ints."
    rospy.spin()

if __name__ == "__main__":
    add_two_ints_server()
Comment

ros python service client

#!/usr/bin/env python

import sys
import rospy
from beginner_tutorials.srv import *

def add_two_ints_client(x, y):
    rospy.wait_for_service('add_two_ints')
    try:
        add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
        resp1 = add_two_ints(x, y)
        return resp1.sum
    except rospy.ServiceException, e:
        print "Service call failed: %s"%e

def usage():
    return "%s [x y]"%sys.argv[0]

if __name__ == "__main__":
    if len(sys.argv) == 3:
        x = int(sys.argv[1])
        y = int(sys.argv[2])
    else:
        print usage()
        sys.exit(1)
    print "Requesting %s+%s"%(x, y)
    print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))
Comment

PREVIOUS NEXT
Code Example
Python :: python url shortener 
Python :: python define an array of dictonary 
Python :: qtablewidget not editable python 
Python :: networkx draw graph with weight 
Python :: vscode pylint missing module docstring 
Python :: scipy cosine distance 
Python :: split word python 
Python :: pandas sort dataframe by column 
Python :: moving file in python 
Python :: how to make a def in python 
Python :: plot multiple axes matplotlib 
Python :: get dataframe column names 
Python :: pyqt open file dialog 
Python :: what does int do in python 
Python :: python int to binary string 
Python :: DHT22 raspberry pi zero connector 
Python :: python open all files of type csv 
Python :: pyqt menubar example 
Python :: how to remove an element in a list by index python 
Python :: set type of column pandas 
Python :: make zipfile from directory py 
Python :: python make an object hashable 
Python :: python spammer 
Python :: remove element from dictionary python 
Python :: pygame.draw.rect() 
Python :: read csv pandas 
Python :: install coverage python 
Python :: python array append 
Python :: kivy button on click 
Python :: how to make lists in python 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =