Add a new ROS publisher node to a QTcreator project which already contains a ROS subscriber node
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(); }