ROS+qt入门(一):发布器的编写

ROS+QT的使用

环境报错

/opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:83: error: Could not find a package configuration file provided by “qt_build” with any of the following names: qt_buildConfig.cmake qt_build-config.cmake Add the installation prefix of “qt_build” to CMAKE_PREFIX_PATH or set “qt_build_DIR” to a directory containing one of the above files. If “qt_build” provides a separate development package or SDK, be sure it has been installed.

把这个参数的路径改成ros的路径就可以解决了;

用ros+qt写一个发布器

用qt官方创建的包里面,Qnode类就是专门用来写ros节点的,我们先把他

while(ros::ok())

里面的东西注释掉 发布器在官方历程里面已经写好了 ,叫

chatter_publish

我们暂时用这个就好

// Add your ros communications here.
	chatter_publisher = n.advertise<std_msgs::String>("chatter", 1000);

具体操作如下:

1. 首先我们唤醒ros_master

roscore

2. 用创造的模板包 把右面的connect上面两个对号点开即可 3. 在ui里面添加一个按钮,暂命名test_1; 4. 把按钮和槽连接起来 回到main_windows.cpp,写一个连接函数,把按钮和将要用于发布器的函数写出来:

void move_left();

   bool ret =QObject::connect(ui.test_1,SIGNAL(clicked(bool)),this,SLOT(moveLeft()));
    if (ret)
    {
        qDebug()<< "ok";
    }
    else
    {
        qDebug() <<"23333";
    }

5. 在mian_window写槽函数 处理按按钮之后的信号,用于发布器的功能的实现,要现在main_window里面定义一个

public slot:
		...
		void moveLeft();

然后在源码里写这个槽

void MainWindow::moveLeft(){
	    qDebug()<< "happy new year";
	    ui.test_1->setText("happy_new_year");
	    qnode.move_left();
	}

注意:,mainwindow里面一定要包含QNode.hpp这个头文件 并且在这个在这个头文件里定义一个接口用的函数:

Public:
...
	void move_left();

因为ros::publish 那个都是private定义的,所以需要转到Qnode这个类里面去调用ros。

6. 在Qnode.cpp里面调用ros

void QNode::move_left()
	{
	        std_msgs::String msg;
	            std::stringstream ss;
	            ss << "hello world ";//<< count;
	            msg.data = ss.str();
	    chatter_publisher.publish(msg);
	}

然后你打开

rostopic echo chatter

就可以看见了

经验分享 程序员 微信小程序 职场和发展