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

Add a new ROS publisher node to a QTcreator project which already contains a ROS subscriber node

asked 2015-06-29 10:17:05 -0500

Saeid gravatar image

updated 2015-06-30 07:28:39 -0500

Hi guys, I've used QT to create a package with GUI and a ROS node (subscriber), and every thing works as I expected. Now I need to add a new publisher node to my QT. Please help me to do this if you have any idea.

Thanks

my whole code is here Main.cpp

#include "ros/ros.h"

#include "std_msgs/String.h" #include "mainwindow.h" #include <qapplication> #include <boost thread.hpp=""> #include <qtextedit> #include <string.h> #include <iostream> #include <string> #include <qlistwidget> #include <qlistwidgetitem> #include <qmainwindow> #include "newwindow.h" #include <qlabel> #include <sstream> #include "gui_sub/Position.h"

using namespace std; MainWindow* mainWin;

void infoCallback(const std_msgs::String::ConstPtr& msg) { // ROS_INFO("The New module is: [%s]", msg->data.c_str()); std::string str = msg->data; //The Name of Raspberry Pi std::string str_name = str.substr (14,19); // List of Servo motors std::string str_servo_1 = str.substr (55,23); std::string str_servo_2 = str.substr (82,23); std::string str_servo_3 = str.substr (109,23); std::string str_servo_4 = str.substr (136,23); // List of Brushless motors std::string str_brushless_1 = str.substr (163,25); std::string str_brushless_2 = str.substr (192,25); std::string str_brushless_3 = str.substr (221,25); std::string str_brushless_4 = str.substr (250,25);

//Converting the STD Strings to QStrings
QString qstr = QString::fromStdString(str);
QString qstr_name = QString::fromStdString(str_name);

QString qstr_servo_1 = QString::fromStdString(str_servo_1);
QString qstr_servo_2 = QString::fromStdString(str_servo_2);
QString qstr_servo_3 = QString::fromStdString(str_servo_3);
QString qstr_servo_4 = QString::fromStdString(str_servo_4);

QString qstr_brushless_1 = QString::fromStdString(str_brushless_1);
QString qstr_brushless_2 = QString::fromStdString(str_brushless_2);
QString qstr_brushless_3 = QString::fromStdString(str_brushless_3);
QString qstr_brushless_4 = QString::fromStdString(str_brushless_4);

// Using the new strings to create our lists via signal and slot methods
mainWin->updatemethod_info(qstr);
mainWin->updatemethod_name(qstr_name);

mainWin->updatemethod_servo_1(qstr_servo_1);
mainWin->updatemethod_servo_2(qstr_servo_2);
mainWin->updatemethod_servo_3(qstr_servo_3);
mainWin->updatemethod_servo_4(qstr_servo_4);

mainWin->updatemethod_brush_1(qstr_brushless_1);
mainWin->updatemethod_brush_2(qstr_brushless_2);
mainWin->updatemethod_brush_3(qstr_brushless_3);
mainWin->updatemethod_brush_4(qstr_brushless_4);

} void callbackmethods() { ros::NodeHandle n; ros::Subscriber sub = n.subscribe("/info", 1000, infoCallback); ros::Rate rate(30); while (ros::ok()){ ros::spinOnce(); rate.sleep(); } }

int main(int argc, char **argv) { ros::init(argc, argv, "gui_sub");

QApplication app(argc, argv);
app.setOrganizationName("Trolltech");
app.setApplicationName("Application");

mainWin = new MainWindow();
mainWin->show();

//boost::thread thread_spin( boost::bind( ros::spin )); boost::thread thread_spin( boost::bind( callbackmethods ));

ros::init(argc, argv, "servoinput"); ros::NodeHandle nt; ros::Publisher position_pub;

position_pub=nt.advertise<gui_sub::position>("position", 1000);

std::string di; gui_sub::Position msg; std::cout << "I am here "; std::cin>>di; msg.position = atof(di.c_str()); position_pub.publish(msg);

return app.exec(); }

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-06-29 15:44:43 -0500

updated 2015-06-30 07:07:40 -0500

Create another publisher (class) inside the node like in the C++ tutorial for creating nodes? Should be something like this:

ros::Publisher pub; //When in class this is the member in class definition
pub = nh.advertise<pkg::TopicType>("awesome_topic", 1000 /*Queue*/); //This goes to Constructor 

//Maybe your main loop

ros::Rate rate(10);
while(ros:ok())
{
  pkg::TopicType msg; 
  /*add data to your msg here*/
  msg.field="some data";
  pub.publish(msg); //Publish it

  ros::spinOnce();
  rate.sleep(); //limit loop rate
}

See here: http://wiki.ros.org/ROS/Tutorials/Wri...

edit flag offensive delete link more

Comments

Many thanks for your interest to answer my question. I tried to do what you mentioned by adding this lines to my Main.cpp : (NEXT COOMENT)

Saeid gravatar image Saeid  ( 2015-06-30 03:55:34 -0500 )edit

ros::Publisher position_pub; ros::NodeHandle nt;

position_pub=nt.advertise<beginner_tutorials::position>("position", 1);

std::string di="1.5"; beginner_tutorials::Position msg; msg.position = atof(di.c_str()); position_pub.publish(msg);

Saeid gravatar image Saeid  ( 2015-06-30 03:55:56 -0500 )edit

But unfortunately it's not publishing .

Saeid gravatar image Saeid  ( 2015-06-30 03:56:59 -0500 )edit

Could you please paste the whole code? Maybe edit your answer and add it.

cyborg-x1 gravatar image cyborg-x1  ( 2015-06-30 05:09:43 -0500 )edit

Probably in qt you will have your main "ros" loop in a second thread.

cyborg-x1 gravatar image cyborg-x1  ( 2015-06-30 07:10:16 -0500 )edit

Ahh now I see you problem.

 position_pub=nt.advertise<gui_sub::position>("position", 1000);

must go into a function you call. I did not think of the main function of the Qt project, just put it somewhere shortly after where you set the values of the message.

cyborg-x1 gravatar image cyborg-x1  ( 2015-06-30 07:42:22 -0500 )edit

normally when using Qt you put the while(ros::ok()) loop inside a function of a seperate thread. You are executing ros::spin() as thread, it does take care of the incomming messages and calls the callback functions. publish(msg) should be able to be called anywhere.

cyborg-x1 gravatar image cyborg-x1  ( 2015-06-30 07:44:55 -0500 )edit

Thank you man, It works now, thanks a lot for your help man

Saeid gravatar image Saeid  ( 2015-06-30 09:37:21 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2015-06-29 10:17:05 -0500

Seen: 1,541 times

Last updated: Jun 30 '15