Search code examples
pythonpython-3.xros

ROS Services in python. Server and client node


Hi I create not ROS python code for the server and the client. But need some help in implementing the ROS part for both of them. The Server only need to print a message as a list starting with 3 numbers and then append a list of three elements (three integer numbers), first is from 1 to 10, and is the chip number , second 0 to 8 is the chip size and third is always 7. On the client side for the Chip number 1 and 10 the chip size should be 2 and third is 7. For the Chips number 2 to 9 Chip size is Random number between 0 and 8 and third number also always 7.

So I create non ROS code for the server and the client in python just need bit help in implementing it as a ROS Service. Here the server node first

!/usr/bin/env python3

from __future__ import print_function
import rospy
import numpy as np
from os import system
import time
import threading
from geometry_msgs.msg import Vector3
import time
import serial
import serial.tools.list_ports
from time import sleep
from std_msgs.msg import Float32
from std_msgs.msg import String

def led_service():
    print("Server Read Data:")

    list_1 =[1,0,7]
    list_2 =[2,1,7]
    list_3 =[3,2,7]
    list_4 =[4,3,7]
    list_5 =[5,4,7]
    list_6 =[6,5,7]
    list_7 =[7,6,7]
    list_8 =[8,7,7]
    list_9 =[9,8,7]
    list_10 =[10,8,7]

    var = [1, 2, 3] 

    var.extend(list_1)
    var.extend(list_2)
    var.extend(list_3)
    var.extend(list_4)
    var.extend(list_5)
    var.extend(list_6)
    var.extend(list_7)
    var.extend(list_8)
    var.extend(list_9)
    var.extend(list_10)

    print('message', var)

if __name__ == '__main__':
    rospy.init_node('set_led')
    led_service()

And the client

#!/usr/bin/env python3

from __future__ import print_function
from __future__ import print_function
import rospy
import sys

import numpy as np
from os import system
import time
import random
import threading
from geometry_msgs.msg import Vector3
import threading 
import time
import serial
import serial.tools.list_ports

from time import sleep
from std_msgs.msg import Float32
from std_msgs.msg import String

def led_client():
    led_number1 = 1
    led_number10 = 10
    led_number1_colour=2
    led_number10_colour=2

    random_colour = random.randint(0,8)

    led_number2_colour=random_colour
    led_number3_colour=random_colour
    led_number4_colour=random_colour
    led_number5_colour=random_colour
    led_number6_colour=random_colour
    led_number7_colour=random_colour
    led_number8_colour=random_colour
    led_number9_colour=random_colour

    var_client_request = [1, 2, 3] 

    list_1 =[1,2,7]
    list_2 =[2,led_number2_colour,7]
    list_3 =[3,led_number2_colour,7]
    list_4 =[4,led_number2_colour,7]
    list_5 =[5,led_number2_colour,7]
    list_6 =[6,led_number2_colour,7]
    list_7 =[7,led_number2_colour,7]
    list_8 =[8,led_number2_colour,7]
    list_9 =[9,led_number2_colour,7]
    list_10 =[10,2,7]

    var_client_request.extend(list_1)
    var_client_request.extend(list_2)
    var_client_request.extend(list_3)
    var_client_request.extend(list_4)
    var_client_request.extend(list_5)
    var_client_request.extend(list_6)
    var_client_request.extend(list_7)
    var_client_request.extend(list_8)
    var_client_request.extend(list_9)
    var_client_request.extend(list_10)

    var_client_request.append(58)
    var_client_request.append(13)
    var_client_request.append(10)

    print('message', var_client_request)

if __name__ == '__main__':
    rospy.init_node('set_led')

    while not rospy.is_shutdown():
        try:
            led_client()
        except Exception as e:
            print(e)

Solution

  • Tutorial

    • Create the service file titled as SetLed.srv for this example:

      int64[] input_var_list
      ---
      int64[] output_var_list
      

      This will enable the service to take in a list of integers and return a list of integers.

    • Create the server script for the SetLed service:

      #!/usr/bin/env python3
      
      """
      ROS Set Led Service Server
      """
      
      import rospy
      from your_ros_pkg.srv import SetLed, SetLedResponse
      
      
      def handle_input_var_list(req):
          """
          Handle the request from the client
          """
      
          print("Request received", req.input_var_list)
      
          list_1 = [1, 0, 7]
          list_2 = [2, 1, 7]
          list_3 = [3, 2, 7]
          list_4 = [4, 3, 7]
          list_5 = [5, 4, 7]
          list_6 = [6, 5, 7]
          list_7 = [7, 6, 7]
          list_8 = [8, 7, 7]
          list_9 = [9, 8, 7]
          list_10 = [10, 8, 7]
      
          var = list(req.input_var_list)
      
          var.extend(list_1)
          var.extend(list_2)
          var.extend(list_3)
          var.extend(list_4)
          var.extend(list_5)
          var.extend(list_6)
          var.extend(list_7)
          var.extend(list_8)
          var.extend(list_9)
          var.extend(list_10)
      
          print("Request processed", var)
      
          return SetLedResponse(var)
      
      
      def setup_led_server():
          """
          Function to create the server
          """
      
          rospy.init_node('set_led_server_node')
          rospy.Service('set_led_service', SetLed, handle_input_var_list)
      
          print("Ready to process client request.")
          rospy.spin()
      
      if __name__ == "__main__":
          setup_led_server()
      

      Make sure you replace your_ros_pkg with the name of your ROS package.

    • Create the client script for the SetLed service:

      #!/usr/bin/env python3
      
      """
      ROS Set Led Service Client
      """
      
      import random
      import rospy
      from your_ros_pkg.srv import SetLed
      
      
      def led_client(var_client_request):
          led_number1 = 1
          led_number10 = 10
          led_number1_colour=2
          led_number10_colour=2
      
          random_colour = random.randint(0,8)
      
          led_number2_colour=random_colour
          led_number3_colour=random_colour
          led_number4_colour=random_colour
          led_number5_colour=random_colour
          led_number6_colour=random_colour
          led_number7_colour=random_colour
          led_number8_colour=random_colour
          led_number9_colour=random_colour
      
          list_1 =[1,2,7]
          list_2 =[2,led_number2_colour,7]
          list_3 =[3,led_number2_colour,7]
          list_4 =[4,led_number2_colour,7]
          list_5 =[5,led_number2_colour,7]
          list_6 =[6,led_number2_colour,7]
          list_7 =[7,led_number2_colour,7]
          list_8 =[8,led_number2_colour,7]
          list_9 =[9,led_number2_colour,7]
          list_10 =[10,2,7]
      
          var_client_request.extend(list_1)
          var_client_request.extend(list_2)
          var_client_request.extend(list_3)
          var_client_request.extend(list_4)
          var_client_request.extend(list_5)
          var_client_request.extend(list_6)
          var_client_request.extend(list_7)
          var_client_request.extend(list_8)
          var_client_request.extend(list_9)
          var_client_request.extend(list_10)
      
          var_client_request.append(58)
          var_client_request.append(13)
          var_client_request.append(10)
      
          print('message', var_client_request)
      
      
      
      def request_list():
          """
          request processed list by the server
          """
      
          try:
              server_call = rospy.ServiceProxy('set_led_service', SetLed)
      
              input_var_list = [1, 2, 3]
      
              service_response = list(server_call(input_var_list).output_var_list)
              led_client(service_response)
      
          except rospy.ServiceException as error:
              print("Service call failed: %s" % error)
      
      
      if __name__ == "__main__":
          rospy.wait_for_service('set_led_service')
          request_list()
      

    References: