python 中的 ROS 服务。服务器和客户端节点
ROS Services in python. Server and client node
您好,我不是为服务器和客户端创建 ROS python 代码。但是需要一些帮助来为它们实现 ROS 部分。服务器只需要将消息打印为以 3 个数字开头的列表,然后附加三个元素(三个整数)的列表,第一个是从 1 到 10,是筹码编号,第二个 0 到 8 是筹码大小第三个总是7。在客户端,筹码1和10的筹码大小应该是2,第三个是7。对于筹码2到9的筹码大小是0到8之间的随机数,第三个数字也总是7 .
所以我在 python 中为服务器和客户端创建了非 ROS 代码,只是需要一些帮助来将其实现为 ROS 服务。这里首先是服务器节点
!/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()
和客户
#!/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)
教程
为此示例创建标题为 SetLed.srv
的服务文件:
int64[] input_var_list
---
int64[] output_var_list
这将使服务能够接收整数列表和 return 整数列表。
为 SetLed 服务创建服务器脚本:
#!/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()
确保将 your_ros_pkg
替换为您的 ROS 包的名称。
为 SetLed 服务创建客户端脚本:
#!/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()
参考文献:
您好,我不是为服务器和客户端创建 ROS python 代码。但是需要一些帮助来为它们实现 ROS 部分。服务器只需要将消息打印为以 3 个数字开头的列表,然后附加三个元素(三个整数)的列表,第一个是从 1 到 10,是筹码编号,第二个 0 到 8 是筹码大小第三个总是7。在客户端,筹码1和10的筹码大小应该是2,第三个是7。对于筹码2到9的筹码大小是0到8之间的随机数,第三个数字也总是7 .
所以我在 python 中为服务器和客户端创建了非 ROS 代码,只是需要一些帮助来将其实现为 ROS 服务。这里首先是服务器节点
!/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()
和客户
#!/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)
教程
为此示例创建标题为
SetLed.srv
的服务文件:int64[] input_var_list --- int64[] output_var_list
这将使服务能够接收整数列表和 return 整数列表。
为 SetLed 服务创建服务器脚本:
#!/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()
确保将
your_ros_pkg
替换为您的 ROS 包的名称。为 SetLed 服务创建客户端脚本:
#!/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()