ros rviz插件编程

官方给的例子都有些问题,所以在这里做个记录防止下次在掉坑里面去。

这里做一个很简单的panel插件:

一个 标签加一个编辑框控件

步骤如下:

1,创建一个目录,比如 rviz_plugin,在下面再创建一个src目录,进入到src,执行:

catkin_create_pkg my_control roscpp rviz std_msgs

2.修改生成的package.xml文件,export节添加如下内容:

<export>
      <rviz plugin="${prefix}/plugin_description.xml"/>
</export>

3.在同一层目录下添加plugin_description.xml文件,内容如下:

 <library path="lib/libmy_control">
      <class name="rviz_plugin_my/my"
             type="rviz_plugin_my::my_control"
             base_class_type="rviz::Panel">
        <description>
          A panel widget Control the Robot
        </description>
      </class>
  </library>

lib/libmy_control的libmy_control指出这个插件的动态库文件名;rviz_plugin_my是包的名字,反映到代码里面就是一个命名空间的名字,my是显示的名字,my_control是类名;所有的panel插件均是rviz::Panel的子类。

4.然后添加实现文件,my_control.h:

#ifndef CMD_CONTROL_H
#define CMD_CONTROL_H

#include <stdio.h>

//所需要包含的头文件
#include <ros/ros.h>
#include <ros/console.h>
#include <rviz/panel.h>   //plugin基类的头文件


#include <QPainter>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QPushButton>
#include <geometry_msgs/Twist.h>
#include <QDebug>
#include <QCheckBox>
#include <QSlider>

namespace rviz_plugin_my
{

class my_control:public rviz::Panel
{
    Q_OBJECT
public:
    // 构造函数,在类中会用到QWidget的实例来实现GUI界面,这里先初始化为0即可
    my_control(QWidget* parent=0);
    // 重载rviz::Panel积累中的函数,用于保存、加载配置文件中的数据,在我们这个plugin中,数据就是topic的名称
    virtual void load( const rviz::Config& config );
    virtual void save( rviz::Config config ) const;
    // 内部变量.
protected:

    // The ROS node handle.
    ros::NodeHandle nh_;

    

};

}
#endif // CMD_CONTROL_H

5.添加实现文件my_control.cpp


#include "my_control.h"
#include <stdio.h>

#include <QPainter>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTimer>

#include <geometry_msgs/Twist.h>
#include <QDebug>

namespace rviz_plugin_my
{
my_control::my_control(QWidget* parent)
    :rviz::Panel (parent)
{

 QHBoxLayout* topic_layout = new QHBoxLayout;
  topic_layout->addWidget( new QLabel( "Output Topic:" ));
 QLineEdit *output_topic_editor_ = new QLineEdit;
  topic_layout->addWidget( output_topic_editor_ );
   
 QVBoxLayout* layout = new QVBoxLayout;
 layout->addLayout( topic_layout );
 setLayout(layout);

}

// 重载父类的功能
void my_control::save( rviz::Config config ) const
{
  rviz::Panel::save( config );
 
}

 //重载父类的功能,加载配置数据
void my_control::load( const rviz::Config& config )
{
  rviz::Panel::load( config );

}



}
// 声明此类是一个rviz的插件
#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(rviz_plugin_my::my_control,rviz::Panel )
// END_TUTORIAL


6.cmakelist.txt 文件内容

cmake_minimum_required(VERSION 3.0.2)
project(my_control)


find_package(catkin REQUIRED COMPONENTS
  roscpp
  rviz
  std_msgs
)

find_package(Qt5 REQUIRED Core Widgets)
set(QT_LIBRARIES Qt5::Widgets)


find_package(rviz REQUIRED)
include_directories(${rviz_INCLUDE_DIRS})
link_directories(${rviz_LIBRARY_DIRS})



catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES my_control
#  CATKIN_DEPENDS roscpp rviz std_msgs
#  DEPENDS system_lib
)

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

add_definitions(-DQT_NO_KEYWORDS)

qt5_wrap_cpp(MOC_FILES
src/my_control.h
)

set(SOURCE_FILES
src/my_control.cpp 
${MOC_FILES}
)

 add_library(${PROJECT_NAME}  ${SOURCE_FILES})
 target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES} ${catkin_LIBRARIES} ${rviz_LIBRARIES})




7,然后回到rviz_plugin 目录执行编译命令:

catkin_make 

8. 运行:

source devel/setup.base
rviz 

这是可以看到rviz出现了,点击顶层菜单的Panel:

这里已经看到我们的插件了,点击ok 按钮,添加它:

可以看到插件已经出现在panel位置,大功告成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值