ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The problem is in your void void QNode::run() function. The while loop in this function makes this a "blocking function" meaning that the processor never gets handed back to the GUI to update. The way I usually deal with this problem is by using a qTimer.

Basically you want to create a timer in your main_window tells ROS things to happen. In between timer calls the GUI will be able to update properly.

Add the following member to your main_window.hpp

#include <QTimer>
...
class MainWindow : public QMainWindow{
...

private slots:
    void spin_ros();
private:
    QTimer *rosTimer;
...
}

In your main_window.cpp

in your constructor add:

rosTimer = new QTimer(this);
connect(rosTimer, SIGNAL(timeout()), this, SLOT(spin_ros()));
rosTimer->start(10);  // set your spin rate here

implement the spin_ros() function we declared in the header. This will be the function that does the body of your while loop invoid QNode::run()function.

void MainWindow::spin_ros(){
    myQnode.runOnce();   // you will need to modify QNode::run so it only does one loop iteration when called!
}

Plausible function for QNode::runOnce():

void QNode::runOnce() {
        if(ros::ok() && rawpacketpolling())
        {
            pub_topicmsg_.publish(topicmessage);

            Q_EMIT logunitsmessages(topicmessage.msg1, topicmessage.msg2); // EMIT after messages is decoded.

            ros::spinOnce();
            loop_rate.sleep();
        }
}