rqt ROS 中的线程 Python

Threads in rqt ROS Python

我正在使用 python 为 rqt 中的机器人设计一个 UI 插件。基本上,有一个按钮称为 'Goto Home' 按钮。单击此按钮后,我想移动机器人。请注意,每当我单击此按钮时,机器人都会移动,但 GUI 会暂时无响应,这从编写代码的方式来看是显而易见的。请参阅下面的代码片段:

import rospy
from robot_controller import RobotController

from qt_gui.plugin import Plugin
from python_qt_binding.QtGui import QWidget, QVBoxLayout, QPushButton
class MyPlugin(Plugin):
    def __init__(self, context):
        super(MyPlugin, self).__init__(context)

        # Give QObjects reasonable names
        self.setObjectName('MyPlugin')

        # Create QWidget
        self._widget = QWidget()
        self._widget.setObjectName('MyPluginUi')

        # Create push button and connect a function
        self._goto_home_button = QPushButton('Goto Home')
        self._goto_home_button.clicked.connect(self.goto_home)
        self._vertical_layout = QVBoxLayout()
        self._vertical_layout.addWidget(self._goto_home_button.)
        self._widget.setLayout(self._vertical_layout)
        context.add_widget(self._widget)

        # Create robot object to move robot from GUI
        self._robot = RobotController()

    def goto_home(self):
        self._robot.move_to_joint_angles(self._joint_angles)

我想在这里实现一个线程。更珍贵的是,如何在rqt中使用线程调用self._robot.move_to_joint_angles(self._joint_angles)。请注意,我在 Ubuntu 14.04 LTS PC 上的 ROS Indigo 中使用 Python 2.7。

我找到了解决方法。请看下面的代码片段:

import thread
thread.start_new_thread(self._robot.move_to_joint_angles, (self.home_pose,))

有什么更好的方法吗?

actions 更适合这个。

In some cases, however, if the service takes a long time to execute, the user might want the ability to cancel the request during execution or get periodic feedback about how the request is progressing. The actionlib package provides tools to create servers that execute long-running goals that can be preempted. It also provides a client interface in order to send requests to the server.