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 :: add element to list python 
Python :: add an index column in range dataframe 
Python :: return key from value dictionary python 
Python :: python array use numpy arange 
Python :: select python interpreter vscode 
Python :: str in python 
Python :: round down decimal python 
Python :: how to add elements in a dictionary in python 
Python :: store message sent by user in string discord py 
Python :: program to count the number of occurrences of a elementes in a list python 
Python :: python len 
Python :: false python 
Python :: delete function python 
Python :: how to check list is empty or not 
Python :: filter dictionary python 
Python :: how to get data from django session 
Python :: how to use multiple keys for single value in dictionary python 
Python :: Simple example of python strip function 
Python :: shape in python 
Python :: how to check if user pressed enter in python 
Python :: python socket get client ip address 
Python :: text to speech module python 
Python :: a int and float python 
Python :: NEW CALENDAR MODULE 
Python :: pandas explode 
Python :: install multiple versions of python 
Python :: parse receipt python 
Python :: python dataframe limit rows 
Python :: functional conflict definition 
Python :: python global keyword 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =