Ask Your Question
0

Create service server in rqt c++

asked 2014-10-25 07:36:42 -0500

Bastbeat gravatar image

Hello, I need to create a service in c++ using qt. I found an example to do this in python, but not in c++.

What I need to consider to do that?

edit retag flag offensive close merge delete

Comments

1

How is the question specific to rqt? Does the standard C++ tutorial not work ( http://wiki.ros.org/ROS/Tutorials/Wri...

Dirk Thomas gravatar imageDirk Thomas ( 2014-10-25 19:39:51 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-10-27 11:35:33 -0500

Wolf gravatar image

updated 2014-10-27 11:40:28 -0500

There are some differences, not mentioned in http://wiki.ros.org/ROS/Tutorials/Wri... ). The service must be a class member of your plugin class; however, services/publishers/subscribers as class members are not so special to rqt; it's a quite common practise...

A few notes, special to rqt:

  • advertise your service in initPlugin
  • use getNodeHandle() as NodeHandle if your service can run in a single thread (with your GUI); note that long running services may block your GUI
  • use getNodeHandleMT() as NodeHandle if your service may run asynchronously with your GUI. Thread-safety? Your Job!!!
  • call service.shutDown() in shutDownPlugin(). Otherwise your service can be called after your GUI Plugin was unloaded which might cause seg fault.

Your code may look like:

Header:

#include "beginner_tutorials/AddTwoInts.h"    

  // this should be know to you
class YourRqtPlugin

  : public rqt_gui_cpp::Plugin
{

  Q_OBJECT

public:
  // this should be know to you
  YourRqtPlugin();

  virtual void initPlugin(qt_gui_cpp::PluginContext& context);

  virtual void shutdownPlugin();

protected:

      // this is new for the service
     bool add(beginner_tutorials::AddTwoInts::Request  &req,
                    beginner_tutorials::AddTwoInts::Response &res);
      // and this is new for the service
    ros::ServiceServer service;

};

Source:

YourRqtPlugin::YourRqtPlugin()  : 
  rqt_gui_cpp::Plugin()
{
  setObjectName("Qt based service server"); // or whatever
}

void YourRqtPlugin::initPlugin(qt_gui_cpp::PluginContext& context)
{  
     <<.........................>>
         other GUI intialization stuff
     <<.........................>>
      // advertise your service in initPlugin
      // use getNodeHandle() to get a nodehandle reference
     service = getNodeHandle().advertiseService("add_two_ints", &YourRqtPlugin::add, this);
}

void YourRqtPlugin::shutdownPlugin()
{
    // shutdown service; otherwise seg faults possible if service is called after plugin was unloaded
    service.shutDown();
}

bool YourRqtPlugin::add(beginner_tutorials::AddTwoInts::Request  &req,
                    beginner_tutorials::AddTwoInts::Response &res)
{

     res.sum = req.a + req.b;
     ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
     ROS_INFO("sending back response: [%ld]", (long int)res.sum);
     return true;
}
edit flag offensive delete link more

Comments

I tried to add a Service to the Cartesian path planner Plugin. But I couldn't. The plugin didn't start. After many hours, I realized that I should define the service (service = getNodeHandle().advertiseService("add_two_ints", &YourRqtPlugin::add, this)) at the final of the init function.

Bastbeat gravatar imageBastbeat ( 2014-10-29 13:08:04 -0500 )edit

That was maybe because the plugin need more time to start. So, I used a sleep(10) before define the service and now all works fine.

Bastbeat gravatar imageBastbeat ( 2014-10-29 13:09:47 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-10-25 07:36:42 -0500

Seen: 495 times

Last updated: Oct 27 '14