在rviz中添加插件


rviz
ROS官方的一款3D可视化工具,几乎我们需要用到的所有机器人相关数据都可以在rviz中展现,当然由于机器人系统的需求不同,很多时候rviz中已有的一些功能仍然无法满足我们的需求,这个时候rvizplugin机制就派上用场了。上一篇我们探索了插件的概念和基本实现,这一篇通过rviz中的插件实现,来进行巩固加深。

rviz作为一种可扩展化的视图工具,可以使用这种插件机制来扩展丰富的功能,进行二次开发,我们在rviz中常常使用的激光数据可视化显示、图像数据可视化显示,其实都是官方提供的插件。所以,我们完全可以在rviz的基础上,打造属于我们自己的机器人人机界面。

 

一、目标

参考官方例子编写rviz plugin,参考teleop

image

二、创建功能包

首先,我们来创建一个功能包,用来放置plugin的所有相关代码:

  1. $ catkin_create_pkg rviz_telop_commander roscpp rviz std_msgs

这个功能包的依赖于rviz,因为rviz是基于Qt开发的,所以省去了对Qt的依赖。

 

三、实现主代码

接下来,我们在功能包的src文件夹下开始做代码的实现,这个plugin相对简单,只需要一个cpp文件就可以完成,当然对应的还要有它的头文件。

 

3.1 头文件

  1. #ifndef TELEOP_PAD_H
  2. #define TELEOP_PAD_H
  3.  
  4. //所需要包含的头文件
  5. #include <ros/ros.h>
  6. #include <ros/console.h>
  7. #include <rviz/panel.h>   //plugin基类的头文件
  8.  
  9. class QLineEdit;
  10.  
  11. namespace rviz_telop_commander
  12. {
  13. // 所有的plugin都必须是rviz::Panel的子类
  14. class TeleopPanel: public rviz::Panel
  15. {
  16. // 后边需要用到Qt的信号和槽,都是QObject的子类,所以需要声明Q_OBJECT宏
  17. Q_OBJECT
  18. public:
  19.   // 构造函数,在类中会用到QWidget的实例来实现GUI界面,这里先初始化为0即可
  20.   TeleopPanel( QWidget* parent = 0 );
  21.  
  22.   // 重载rviz::Panel积累中的函数,用于保存、加载配置文件中的数据,在我们这个plugin
  23.   // 中,数据就是topic的名称
  24.   virtual void load( const rviz::Config& config );
  25.   virtual void save( rviz::Config config ) const;
  26.  
  27.   // 公共槽.
  28. public Q_SLOTS:
  29.   // 当用户输入topic的命名并按下回车后,回调用此槽来创建一个相应名称的topic publisher
  30.   void setTopic( const QString& topic );
  31.  
  32.   // 内部槽.
  33. protected Q_SLOTS:
  34.   void sendVel();                 // 发布当前的速度值
  35.   void update_Linear_Velocity();  // 根据用户的输入更新线速度值
  36.   void update_Angular_Velocity(); // 根据用户的输入更新角速度值
  37.   void updateTopic();             // 根据用户的输入更新topic name
  38.  
  39.   // 内部变量.
  40. protected:
  41.   // topic name输入框
  42.   QLineEdit* output_topic_editor_;
  43.   QString output_topic_;
  44.  
  45.   // 线速度值输入框
  46.   QLineEdit* output_topic_editor_1;
  47.   QString output_topic_1;
  48.  
  49.   // 角速度值输入框
  50.   QLineEdit* output_topic_editor_2;
  51.   QString output_topic_2;
  52.  
  53.   // ROS的publisher,用来发布速度topic
  54.   ros::Publisher velocity_publisher_;
  55.  
  56.   // The ROS node handle.
  57.   ros::NodeHandle nh_;
  58.  
  59.   // 当前保存的线速度和角速度值
  60.   float linear_velocity_;
  61.   float angular_velocity_;
  62. };
  63.  
  64. } // end namespace rviz_plugin_tutorials
  65.  
  66. #endif // TELEOP_PANEL_H

 

3.2 cpp文件

接下来是cpp代码文件 teleop_pad.cpp

  1. #include <stdio.h>
  2.  
  3. #include <QPainter>
  4. #include <QLineEdit>
  5. #include <QVBoxLayout>
  6. #include <QHBoxLayout>
  7. #include <QLabel>
  8. #include <QTimer>
  9.  
  10. #include <geometry_msgs/Twist.h>
  11. #include <QDebug>
  12.  
  13. #include "teleop_pad.h"
  14.  
  15. namespace rviz_telop_commander
  16. {
  17.  
  18. // 构造函数,初始化变量
  19. TeleopPanel::TeleopPanel( QWidget* parent )
  20.   : rviz::Panel( parent )
  21.   , linear_velocity_( 0 )
  22.   , angular_velocity_( 0 )
  23. {
  24.  
  25.   // 创建一个输入topic命名的窗口
  26.   QVBoxLayout* topic_layout = new QVBoxLayout;
  27.   topic_layout->addWidget( new QLabel( "Teleop Topic:" ));
  28.   output_topic_editor_ = new QLineEdit;
  29.   topic_layout->addWidget( output_topic_editor_ );
  30.  
  31.   // 创建一个输入线速度的窗口
  32.   topic_layout->addWidget( new QLabel( "Linear Velocity:" ));
  33.   output_topic_editor_1 = new QLineEdit;
  34.   topic_layout->addWidget( output_topic_editor_1 );
  35.  
  36.   // 创建一个输入角速度的窗口
  37.   topic_layout->addWidget( new QLabel( "Angular Velocity:" ));
  38.   output_topic_editor_2 = new QLineEdit;
  39.   topic_layout->addWidget( output_topic_editor_2 );
  40.  
  41.   QHBoxLayout* layout = new QHBoxLayout;
  42.   layout->addLayout( topic_layout );
  43.   setLayout( layout );
  44.  
  45.   // 创建一个定时器,用来定时发布消息
  46.   QTimer* output_timer = new QTimer( this );
  47.  
  48.   // 设置信号与槽的连接
  49.   connect( output_topic_editor_, SIGNAL( editingFinished() ), this, SLOT( updateTopic() ));             // 输入topic命名,回车后,调用updateTopic()
  50.   connect( output_topic_editor_1, SIGNAL( editingFinished() ), this, SLOT( update_Linear_Velocity() )); // 输入线速度值,回车后,调用update_Linear_Velocity()
  51.   connect( output_topic_editor_2, SIGNAL( editingFinished() ), this, SLOT( update_Angular_Velocity() ));// 输入角速度值,回车后,调用update_Angular_Velocity()
  52.  
  53.   // 设置定时器的回调函数,按周期调用sendVel()
  54.   connect( output_timer, SIGNAL( timeout() ), this, SLOT( sendVel() ));
  55.  
  56.   // 设置定时器的周期,100ms
  57.   output_timer->start( 100 );
  58. }
  59.  
  60. // 更新线速度值
  61. void TeleopPanel::update_Linear_Velocity()
  62. {
  63.     // 获取输入框内的数据
  64.     QString temp_string = output_topic_editor_1->text();
  65.  
  66.     // 将字符串转换成浮点数
  67.     float lin = temp_string.toFloat();
  68.  
  69.     // 保存当前的输入值
  70.     linear_velocity_ = lin;
  71. }
  72.  
  73. // 更新角速度值
  74. void TeleopPanel::update_Angular_Velocity()
  75. {
  76.     QString temp_string = output_topic_editor_2->text();
  77.     float ang = temp_string.toFloat() ;
  78.     angular_velocity_ = ang;
  79. }
  80.  
  81. // 更新topic命名
  82. void TeleopPanel::updateTopic()
  83. {
  84.   setTopic( output_topic_editor_->text() );
  85. }
  86.  
  87. // 设置topic命名
  88. void TeleopPanel::setTopic( const QString& new_topic )
  89. {
  90.   // 检查topic是否发生改变.
  91.   if( new_topic != output_topic_ )
  92.   {
  93.     output_topic_ = new_topic;
  94.  
  95.     // 如果命名为空,不发布任何信息
  96.     if( output_topic_ == "" )
  97.     {
  98.       velocity_publisher_.shutdown();
  99.     }
  100.     // 否则,初始化publisher
  101.     else
  102.     {
  103.       velocity_publisher_ = nh_.advertise<geometry_msgs::Twist>( output_topic_.toStdString(), 1 );
  104.     }
  105.  
  106.     Q_EMIT configChanged();
  107.   }
  108. }
  109.  
  110. // 发布消息
  111. void TeleopPanel::sendVel()
  112. {
  113.   if( ros::ok() && velocity_publisher_ )
  114.   {
  115.     geometry_msgs::Twist msg;
  116.     msg.linear.x = linear_velocity_;
  117.     msg.linear.y = 0;
  118.     msg.linear.z = 0;
  119.     msg.angular.x = 0;
  120.     msg.angular.y = 0;
  121.     msg.angular.z = angular_velocity_;
  122.     velocity_publisher_.publish( msg );
  123.   }
  124. }
  125.  
  126. // 重载父类的功能
  127. void TeleopPanel::save( rviz::Config config ) const
  128. {
  129.   rviz::Panel::save( config );
  130.   config.mapSetValue( "Topic", output_topic_ );
  131. }
  132.  
  133. // 重载父类的功能,加载配置数据
  134. void TeleopPanel::load( const rviz::Config& config )
  135. {
  136.   rviz::Panel::load( config );
  137.   QString topic;
  138.   if( config.mapGetString( "Topic", &topic ))
  139.   {
  140.     output_topic_editor_->setText( topic );
  141.     updateTopic();
  142.   }
  143. }
  144.  
  145. } // end namespace rviz_plugin_tutorials
  146.  
  147. // 声明此类是一个rviz的插件
  148. #include <pluginlib/class_list_macros.h>
  149. PLUGINLIB_EXPORT_CLASS(rviz_telop_commander::TeleopPanel,rviz::Panel )
  150. // END_TUTORIAL

代码文件就是这样,并没有很多代码,还是比较好理解的。

 

四、完成编译文件

为了编译成功,还需要完成一些编译文件的设置。

4.1 plugin的描述文件

在功能包的根目录下需要创建一个plugin的描述文件 plugin_description.xml

  1. <library path="lib/librviz_telop_commander">//lib/lib后面的是我们的namespase
  2. <!--这个是显示的插件类别与插件名-->
  3.   <class name="rviz_telop_commander/Teleop"    
  4.          type="rviz_telop_commander::TeleopPanel"
  5.          base_class_type="rviz::Panel">
  6.     <description>
  7.       A panel widget allowing simple diff-drive style robot base control.
  8.     </description>
  9.   </class>
  10. </library>

 

4.2  package.xml

然后在 package.xml文件里添加plugin_description.xml

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

 编译规则参考,与qt相关,可能会编译不过

4.3  CMakeLists.txt

当然, CMakeLists.txt文件也必须要加入相应的编译规则:

  1. ## This plugin includes Qt widgets, so we must include Qt like so:
  2. find_package(Qt4 COMPONENTS QtCore QtGui REQUIRED)
  3. include(${QT_USE_FILE})
  4.  
  5. ## I prefer the Qt signals and slots to avoid defining "emit", "slots",
  6. ## etc because they can conflict with boost signals, so define QT_NO_KEYWORDS here.
  7. add_definitions(-DQT_NO_KEYWORDS)
  8.  
  9. ## Here we specify which header files need to be run through "moc",
  10. ## Qt's meta-object compiler.
  11. qt4_wrap_cpp(MOC_FILES
  12.   src/teleop_pad.h
  13. )
  14.  
  15. ## Here we specify the list of source files, including the output of
  16. ## the previous command which is stored in ``${MOC_FILES}``.
  17. set(SOURCE_FILES
  18.   src/teleop_pad.cpp 
  19.   ${MOC_FILES}
  20. )
  21.  
  22. ## An rviz plugin is just a shared library, so here we declare the
  23. ## library to be called ``${PROJECT_NAME}`` (which is
  24. ## "rviz_plugin_tutorials", or whatever your version of this project
  25. ## is called) and specify the list of source files we collected above
  26. ## in ``${SOURCE_FILES}``.
  27. add_library(${PROJECT_NAME} ${SOURCE_FILES})
  28.  
  29. ## Link the library with whatever Qt libraries have been defined by
  30. ## the ``find_package(Qt4 ...)`` line above, and with whatever libraries
  31. ## catkin has included.
  32. ##
  33. ## Although this puts "rviz_plugin_tutorials" (or whatever you have
  34. ## called the project) as the name of the library, cmake knows it is a
  35. ## library and names the actual file something like
  36. ## "librviz_plugin_tutorials.so", or whatever is appropriate for your
  37. ## particular OS.
  38. target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES} ${catkin_LIBRARIES})

现在就可以开始编译了!

 

五、实现效果

编译成功后,我们来运行rviz,需要注意的是:一定要source devel文件夹下的setup脚本,来生效路径,否则会找不到插件

  1. rosrun rviz rviz

启动之后应该并没有什么不同,点击菜单栏中的“Panels”选项,选择“Add New Panel”,在打开的窗口中的最下方,就可以看到我们创建的plugin了,点中之后,可以在下方的"Description"中看到我们在plugin_description.xml文件中对plugin的描述。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值