Search code examples
pythonmultithreadingrosrqt

Threads in rqt ROS Python


I am designing a UI plugin for robot inside rqt using python. Basically, there is a button termed as 'Goto Home' button. I want to move the robot once this button is clicked. Please note that the robot moves whenever I click this button but GUI becomes unresponsive for a while, which is obvious by the way code is written. Please see the code snippet below:

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)

I want to implement a thread here. More preciously, how to call self._robot.move_to_joint_angles(self._joint_angles) using thread in rqt. Please note that I am using Python 2.7 in ROS Indigo on Ubuntu 14.04 LTS PC.


Solution

  • I found a workaround. Please see below code snippet:

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

    Is there any better way to do it?