ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

how to add a simple button in RVIZ

asked 2021-02-02 11:28:00 -0500

mateusguilherme gravatar image

Hello

I would like to add a button on rviz (in plugin format) that when I click on it, it publishes a Boolean message ROS informing the robot to light a led for example. Does anyone know how to do this?

I have an example of a plugin I made to publish a pose message on the map, but I need to interact with the mouse on the map. In that case I would like the message to be published as soon as I click on the button for the "light up led" plugin. Would it be possible to make this change?

setDock.cpp:

#ifndef SETDOCK_HPP
#define SETDOCK_HPP

#ifndef Q_MOC_RUN  // See: https://bugreports.qt-project.org/browse/QTBUG-22829
#include <QObject>
#include <ros/ros.h>
#include "rviz/default_plugin/tools/pose_tool.h"
#endif


namespace rviz_plugin_setDock
{
    //class SetDock;


    class SetDock : public rviz::PoseTool
    {
      Q_OBJECT
    public:
      SetDock();
      //virtual ~SetDock();
      //virtual void onInitialize();

    protected:
      virtual void onPoseSet(double x, double y, double theta);

    public Q_SLOTS:
      void updateTopic();

    protected:
      ros::NodeHandle nh_;
      ros::Publisher pub_;

    };
}  // namespace rviz_plugin_setDock

#endif  // SETDOCK_HPP

setDock.cpp:

#include <tf/transform_listener.h>
#include <geometry_msgs/PoseWithCovarianceStamped.h>
#include "rviz/display_context.h"
#include "setDock/setDock.hpp"


namespace rviz_plugin_setDock
{
    SetDock::SetDock()
    {
      updateTopic();
    }

    void SetDock::updateTopic()
    {
      pub_ = nh_.advertise<geometry_msgs::PoseWithCovarianceStamped>("dock_position", 1 );
    }

    void SetDock::onPoseSet(double x, double y, double theta)
    {
          std::string fixed_frame = context_->getFixedFrame().toStdString();
          geometry_msgs::PoseWithCovarianceStamped pose;
          pose.header.frame_id = fixed_frame;
          pose.header.stamp = ros::Time::now();
          pose.pose.pose.position.x = x;
          pose.pose.pose.position.y = y;

          tf::Quaternion quat;
          quat.setRPY(0.0, 0.0, theta);
          tf::quaternionTFToMsg(quat,
                        pose.pose.pose.orientation);
          pub_.publish(pose);

    }

}  // namespace rviz_plugin_setDock

#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(rviz_plugin_setDock::SetDock, rviz::Tool)

Thank you

edit retag flag offensive close merge delete

Comments

4

You could take a look at InstitutMaupertuis/simple_rviz_plugin.

gvdhoorn gravatar image gvdhoorn  ( 2021-02-02 12:40:57 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-02-03 18:39:11 -0500

mateusguilherme gravatar image

I used this example (example) and ended up creating a panel for what I needed. Gvdhoorn's comment is also valid.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-02-02 11:28:00 -0500

Seen: 865 times

Last updated: Feb 03 '21