目的
为了在测试时通过rviz发送topic触发部分功能的测试效果
代码
头文件 test_plugin.h
#ifndef PROJECT_TEST_RVIZ_H
#define PROJECT_TEST_RVIZ_H
#include <ros/ros.h>
#include <rviz/panel.h>
#include <QHBoxLayout>
#include <QPushButton>
#include <QString>
namespace test_rviz_plugin{
class TestRvizPlugin :public rviz::Panel{
Q_OBJECT
public:
TestRvizPlugin(QWidget *parent = 0);
virtual void load(const rviz::Config &config);
virtual void save(rviz::Config config) const;
protected Q_SLOTS:
void map_callback();
void send_goal_callback();
void patrol_callback();
private:
ros::NodeHandle nh_;
QPushButton *map_button_;
QPushButton *send_goal_button_;
QPushButton *patrol_button_;
ros::Publisher test_pub_;
};
}
#endif //PROJECT_TEST_RVIZ_H
执行文件 test_plugin.cpp
#include "test_plugin.h"
#include <std_msgs/Int8.h>
namespace test_rviz_plugin{
TestRvizPlugin::TestRvizPlugin(QWidget *parent):Panel(parent)
{
//创建3个按钮
auto *button_layout = new QHBoxLayout;
map_button_ = new QPushButton(tr("mapping"),this);
button_layout->addWidget(map_button_);
send_goal_button_ = new QPushButton(tr("send target slot"),this);
button_layout->addWidget(send_goal_button_);
patrol_button_ = new QPushButton(tr("patrol"),this);
button_layout->addWidget(patrol_button_);
setLayout(button_layout);
//信号连接---点击信号发生,连接到槽函数test_callback()
connect(map_button_,SIGNAL(clicked()),this,SLOT(map_callback()));
connect(send_goal_button_,SIGNAL(clicked()),this,SLOT(send_goal_callback()));
connect(patrol_button_,SIGNAL(clicked()),this,SLOT(patrol_callback()));
test_pub_ = nh_.advertise<std_msgs::Int8>("test_msg",10);
}
//加载配置数据---必须要有的
void TestRvizPlugin::load(const rviz::Config &config){
Panel::load(config);
}
//将所有配置数据保存到给定的Config对象中。在这里,重要的是要对父类调用save(),以便保存类id和面板名称。---必须要有的
void TestRvizPlugin::save(rviz::Config config) const{
Panel::save(config);
}
void TestRvizPlugin::map_callback()
{
std_msgs::Int8 msg;
msg.data = 1;
ROS_INFO("Start to map!");
test_pub_.publish(msg);
}
void TestRvizPlugin::send_goal_callback()
{
std_msgs::Int8 msg;
msg.data = 2;
ROS_INFO("Send target slot to slam!");
test_pub_.publish(msg);
}
void TestRvizPlugin::patrol_callback()
{
std_msgs::Int8 msg;
msg.data = 3;
ROS_INFO("Start to patrol!");
test_pub_.publish(msg);
}
}
#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(test_rviz_plugin::TestRvizPlugin,rviz::Panel)
创建CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(rviz_control)
find_package(catkin REQUIRED COMPONENTS
roscpp
rviz
)
catkin_package()
include_directories(${catkin_INCLUDE_DIRS})
link_directories(${catkin_LIBRARY_DIRS})
set(CMAKE_AUTOMOC ON)
## This plugin includes Qt widgets, so we must include Qt.
## We'll use the version that rviz used so they are compatible.
if(rviz_QT_VERSION VERSION_LESS "5")
message(STATUS "Using Qt4 based on the rviz_QT_VERSION: ${rviz_QT_VERSION}")
find_package(Qt4 ${rviz_QT_VERSION} EXACT REQUIRED QtCore QtGui)
## pull in all required include dirs, define QT_LIBRARIES, etc.
include(${QT_USE_FILE})
else()
message(STATUS "Using Qt5 based on the rviz_QT_VERSION: ${rviz_QT_VERSION}")
find_package(Qt5 ${rviz_QT_VERSION} EXACT REQUIRED Core Widgets)
## make target_link_libraries(${QT_LIBRARIES}) pull in all required dependencies
set(QT_LIBRARIES Qt5::Widgets)
endif()
## I prefer the Qt signals and slots to avoid defining "emit", "slots",
## etc because they can conflict with boost signals, so define QT_NO_KEYWORDS here.
add_definitions(-DQT_NO_KEYWORDS)
add_library(test_plugin src/test_plugin.cpp src/test_plugin.h)
add_dependencies(test_plugin ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(test_plugin ${QT_LIBRARIES} ${catkin_LIBRARIES})
创建package.xml
<package>
<name>rviz_control</name>
<version>0.11.0</version>
<description>
Tutorials showing how to write plugins for RViz.
</description>
<maintainer email="Hope_166@163.com">Mu Zhui</maintainer>
<license>BSD</license>
<url>http://ros.org/wiki/rviz_plugin_tutorials</url>
<author>Dave Hershberger</author>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>qtbase5-dev</build_depend>
<build_depend>rviz</build_depend>
<run_depend>libqt5-core</run_depend>
<run_depend>libqt5-gui</run_depend>
<run_depend>libqt5-widgets</run_depend>
<run_depend>rviz</run_depend>
<export>
<rviz plugin="${prefix}/plugin_description.xml"/>
</export>
</package>
创建 plugin_description.xml
<library path="lib/libtest_plugin">
<class name="Control"
type="test_rviz_plugin::TestRvizPlugin"
base_class_type="rviz::Panel">
<description>
A panel widget is used to test rviz plugin.
</description>
</class>
</library>
编译
catkin_make
调用
打开rviz
rviz
菜单栏
)
选择add new panel->Control
c
效果