我正在使用机器人操作系统 (ROS) 并尝试制作一个服务器/客户端,服务器将在其中启动由客户端指定的 ROS 节点。为了执行“启动”,我根据此处的建议使用 roslaunch:http://wiki.ros.org/roslaunch/API%20Usage
我可以在一个窗口中运行 roscore,然后我可以运行可以正常启动的服务器。但是,一旦我尝试通过客户端发送我想要启动的节点名称,我就会收到以下错误:
“错误处理请求:信号仅在主线程中有效”
然后它指向我尚未追踪到的各个区域中的一堆文件。
我已经尝试在我为要启动的节点(在本例中为 turtlesim_node 和 turtle_teleop_key)单独制作的每个启动文件上使用简单的 roslaunch 调用,它们可以正常启动并通过运行 roslaunch [package] turtlesim_launch 来工作.launch 等
下面是我的服务器的代码:
#!/usr/bin/env python
#Filename: primary_server.py
import rospy
import roslaunch
from robot_man.srv import *
class StartUpServer(object):
def __init__(self):
self._nodes = []
def handle_startup(self, names):
self._temp = names.nodes.split(",") #This reades in the
# information from the srv file sent by the client and
# separates each node/package pair
#This loop separates the input from pairs of 2 corresponding
# to the package and node names the user inputs
for i in range(len(self._temp)):
self._nodes.append(self._temp[i].split())
#This loop will launch each node that the user has specified
for package, executable in self._nodes:
print('package is: {0}, executable is: {1}'.format(package, executable))
node = roslaunch.core.Node(package, executable)
launch = roslaunch.scriptapi.ROSLaunch()
launch.start()
process = launch.launch(node)
print('running node: %s'%executable)
return StartUpResponse(self._nodes) #I will change this later
if __name__ == '__main__':
rospy.init_node('startup_node')
server = StartUpServer()
rospy.Service('startup', StartUp, server.handle_startup)
print('The servers are up and running')
rospy.spin()
这是我的客户的代码:
#!/usr/bin/env python
#Filename: primary_client_startup.py
import rospy
from robot_man.srv import *
def main(nodes):
rospy.wait_for_service('startup')
proxy = rospy.ServiceProxy('startup', StartUp)
response = proxy(nodes)
return response
if __name__ == '__main__':
nodes = input('Please enter the node packages followed by \
node names separated by spaces. \n To activate multiple nodes \
separate entries with a comma > ')
main(nodes)
这是我的 srv 文件:
#This should be a string of space/comma separated values
string nodes
---
#This should return "success" if node/s start up successfully, else "fail"
string status
最后,这是我为启动 turtle 模拟器而制作的两个启动文件:
turtlesim_launch.launch
<launch>
<node name="turtlesim_node" pkg="turtlesim" type="turtlesim_node" />
</launch>
turtle_teleop_launch.launch
<launch>
<node name="turtle_teleop_key" pkg="turtlesim" type="turtle_teleop_key" />
</launch>
我做了一些谷歌搜索,没有发现 ROS 有类似的问题(虽然 Django 等有一些类似的错误,但它们不相关)。
感谢您的帮助!
附言值得注意的是,当错误发生时,我到了第34行(process = launch.launch(node))。
最佳答案
其实就是文档里提到的
您没有从 Python 主线程调用 init_node()。 Python 只允许从主线程注册信号。
关于python - ROS 错误。 "Error processing request: signal only works in main thread",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551641/