Ask Your Question
0

subscriber in rqt plugin python

asked 2017-02-09 13:17:53 -0600

fonstein gravatar image

updated 2017-02-11 07:59:47 -0600

lucasw gravatar image

I'm writing a rqt plugin in python, but can't get the subscriber to work. I want the string from "controller/mode" to show in "lineControlMode". For some reason this doesn't happen. Hope some of you can help me.

import os
import rospkg
import rospy

from qt_gui.plugin import Plugin
from python_qt_binding import loadUi
from python_qt_binding.QtWidgets import QWidget, QGraphicsView
from std_msgs.msg import String

class MyPlugin(Plugin):

def __init__(self, context):
    #PLUGIN CODE
    super(MyPlugin, self).__init__(context)

    # Give QObjects reasonable names
    self.setObjectName('MyPlugin')
    rp = rospkg.RosPack()

    # Process standalone plugin command-line arguments
    from argparse import ArgumentParser
    parser = ArgumentParser()
    # Add argument(s) to the parser.
    parser.add_argument("-q", "--quiet", action="store_true",
                  dest="quiet",
                  help="Put plugin in silent mode")
    args, unknowns = parser.parse_known_args(context.argv())
    if not args.quiet:
        print 'arguments: ', args
        print 'unknowns: ', unknowns

    # Create QWidget
    self._widget = QWidget()
    ui_file = os.path.join(rp.get_path('rqt_vortex_control_mode'), 'resource', 'MyPlugin.ui')

    # Extend the widget with all attributes and children from UI file
    loadUi(ui_file, self._widget)

    # Give QObjects reasonable names
    self._widget.setObjectName('MyPluginUi')

    # Add widget to the user interface
    context.add_widget(self._widget)

    #MY CODE
    self._widget.lineControlMode.setReadOnly(True)
    self._widget.lineControlMode.setText("init")

    #Subscriber
    self.sub = rospy.Subscriber("/uranus_dp/controller/mode", String, self.callback)

def shutdown_plugin(self):
    self.sub.unregister()

def callback(self, mode):
    self._widget.lineControlMode.setText(mode)

Here is the link to my repository just in case: https://github.com/vortexntnu/rov-gui

edit retag flag offensive close merge delete

Comments

The plugin appears to work, but when you publish to /uranus_dp/controller/mode the lineControlMode widget doesn't show anything?

lucasw gravatar imagelucasw ( 2017-02-11 08:02:12 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-02-11 08:11:37 -0600

lucasw gravatar image

updated 2017-02-11 08:12:45 -0600

Does the "init" test message successfully show up?

If so then try this at the bottom of the init:

temp = String("test string")
self._widget.lineControlMode.setText(temp)

If that works re-enable the subscriber and put a rospy.loginfo(mode) in the callback and publish test messages to the topic, and see if the callback is getting triggered.

edit flag offensive delete link more

Comments

Thanks, that helped a lot. Got it to work now!

fonstein gravatar imagefonstein ( 2017-02-15 12:47:27 -0600 )edit

What was the issue? Did setText not like taking a String?

lucasw gravatar imagelucasw ( 2017-02-15 15:29:12 -0600 )edit

I had to cast the data to String. There was also some bugs on the Subscriber, but with your tips on debugging I got it to work.

fonstein gravatar imagefonstein ( 2017-02-15 16:16:04 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-02-09 13:17:53 -0600

Seen: 492 times

Last updated: Feb 11 '17