ROS C++ on Qt subscriber is not working [closed]
I'm using ros melodic
I'm trying to make a ros gui in qt using widgets, and roscpp. I managed to make a publisher which publishes on event. but I am unable to subscribe to anything. The code compiles, and publishes well, but I want it to subscribe to the data publisher, but I am unable to receive anything. manually publishing using the terminal didn't do anything, so it is code related. Here is my code: I would appreciate any help.
main.cpp
#include "rosqt.h"
#include <QApplication>
int main(int argc, char *argv[])
{
ros::init(argc, argv, "RosGUI");
QApplication a(argc, argv);
RosQt w;
w.show();
return a.exec();
}
rosqt.cpp
#include "rosqt.h"
#include "ui_rosqt.h"
#include "iostream"
RosQt::RosQt(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::RosQt)
{ // the sub main loop, of the gui
ui->setupUi(this);
chatter_sub = node.subscribe("/chatter", 1, &RosQt::subFunction, this);
chatter_pub = node.advertise<std_msgs::String>("/chatter", 1);
}
RosQt::~RosQt()
{
delete ui;
}
QString RosQt::toQString(std::string const &s){
return QString::fromUtf8(s.c_str());
}
std::string RosQt::fromQString(QString const &s){
return std::string(s.toUtf8().constData());
}
void RosQt::subFunction(const std_msgs::String::ConstPtr &data){
QString dataQString = toQString(data->data);
sub_message = dataQString;
//ui->outputText->setPlainText(dataQString);
}
void RosQt::on_publishButton_clicked()
{
std_msgs::String msg;
QString input_Qstring = ui->inputText->toPlainText();
std::string input_string = fromQString(input_Qstring);
msg.data = input_string;
RosQt::chatter_pub.publish(msg);
ui->outputText->setPlainText("Published: " + sub_message);
}
rosqt.h
#ifndef ROSQT_H
#define ROSQT_H
#include <QMainWindow>
#include <QApplication>
#include <QMessageBox>
#include <QString>
#include <string>
#include <QTextStream>
#include <ros/ros.h>
#include <std_msgs/String.h>
namespace Ui {
class RosQt;
}
class RosQt : public QMainWindow
{
Q_OBJECT
public:
explicit RosQt(QWidget *parent = 0);
~RosQt();
void subFunction(const std_msgs::String::ConstPtr &data);
QString sub_message;
ros::NodeHandle node;
ros::Publisher chatter_pub;
ros::Subscriber chatter_sub;
private slots:
void on_publishButton_clicked();
private:
Ui::RosQt *ui;
QString toQString(std::string const &s);
std::string fromQString(QString const &s);
};
#endif // ROSQT_H
@miker2808 You may want to init ROS master and node in order to have proper communication. You can check a nice example here.
rosmaster is initiated manually via a termina, ros node is initiated in the main.cpp, and using rosnode list I can clearly see that the node is indeed initiated, plus being able to publish is a good sign for that. Additionally, the example you gave me is running on Qt4 so, unless I manually change the whole project, It wont give me much.
If you are working in Qt5 I will just post the examples from here, here and here that I used on your last post. On the other hand if you have the master running, you are sure the node is initiated you can debug your connections with commands like
roswtf
or see the node/topic graph by running therqt_graph
command and see if there are communication problems.Hello, I didn't forget your answer, I was just fighting with Qt and ROS.. But I finally came with good news, I managed to use qmake, and make a publisher + subscriber in ros.. finally.. I got to thank you, I used to those examples as a reference, as I couldn't compile them because of some catkin errors, but I managed to use ros without using catkin at all. if anyone would like, I uploaded the template to github, and anyone can adapt it to his project. The repository can be found HERE Thanks for the help Weasfas
@miker2808, great!, glad this helped you. Regards.
missing ros::spin() but im using ros::AsyncSpinner
@moji It does not matter how the node spins, either on a single thread or on a pool. You can spin on a pool of threads and then Emit Q_EMIT when you want to shut down the node with ros::shutdown signal and ros::waitForShutdown to join the spinners.