【问题标题】:Threads in rqt ROS Pythonrqt ROS Python中的线程
【发布时间】:2018-04-24 09:56:51
【问题描述】:

我正在使用 python 为rqt 内部的机器人设计一个 UI 插件。基本上,有一个称为“转到主页”按钮的按钮。单击此按钮后,我想移动机器人。请注意,每当我单击此按钮时,机器人都会移动,但 GUI 会暂时无响应,这在编写代码的方式上很明显。请看下面的代码sn-p:

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。

【问题讨论】:

    标签: python multithreading ros rqt


    【解决方案1】:

    我找到了解决方法。请看下面的代码sn-p:

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

    有没有更好的办法?

    【讨论】:

      【解决方案2】:

      actions 更适合这个。

      但是,在某些情况下,如果服务需要很长时间才能执行,用户可能希望能够在执行期间取消请求或获得有关请求进展情况的定期反馈。 actionlib 包提供工具来创建执行可被抢占的长期运行目标的服务器。它还提供了一个客户端接口,以便向服务器发送请求。

      【讨论】:

        猜你喜欢
        • 2017-06-30
        • 1970-01-01
        • 2018-08-20
        • 1970-01-01
        • 2021-12-23
        • 2019-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多