我正在使用python为rqt内部的机器人设计一个UI插件。基本上,有一个称为"转到主页"按钮的按钮。点击此按钮后,我想移动机器人。请注意,每当我单击此按钮时,机器人都会移动,但 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
更适合这样做。
但是,在某些情况下,如果服务需要很长时间才能执行,则用户可能希望能够在执行期间取消请求或定期获得有关请求进度的反馈。actionlib 包提供了用于创建服务器的工具,这些服务器执行可被抢占的长时间运行的目标。它还提供了一个客户端接口,以便向服务器发送请求。