ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ROS Services in Python

asked 2022-04-23 08:44:46 -0600

Astronaut gravatar image

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 here the client node

#!/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)

Any help would appreciate Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-04-23 13:32:57 -0600

Mike Scheutzow gravatar image

The wiki has a tutorial for creating a ros-service server and client in python:

http://wiki.ros.org/ROS/Tutorials/Wri...

You must create custom Request and Response messages. In python, you must create rospy.Service() object on the server, and rospy.ServiceProxy() object on the client.

edit flag offensive delete link more

Comments

Yes I know that. Need to check which parameters will the the rospy.Service() and rospy.ServiceProxy() objects. What is your opinion regarding these parameters? And how will looks like the custom Request and Response messages for my case?

Astronaut gravatar image Astronaut  ( 2022-04-24 01:21:55 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2022-04-23 08:44:46 -0600

Seen: 235 times

Last updated: Apr 23 '22