Create service server in rqt c++
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?
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org |
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?
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:
getNodeHandle()
as NodeHandle if your service can run in a single thread (with your GUI); note that long running services may block your GUIgetNodeHandleMT()
as NodeHandle if your service may run asynchronously with your GUI. Thread-safety? Your Job!!!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;
}
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.
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2014-10-25 07:36:42 -0500
Seen: 712 times
Last updated: Oct 27 '14
Using spinOnce inside Services
Getting rosservice info from c++ node
How can I listen to service-topics?
[ROS2] how does the server's queue work?
How to rclpy.spin() a subscriber in Qt/Kivy GUI in ROS2?
Can a single service client be used from multiple threads?
Calling a ros service from several nodes at the same time?
How is the question specific to rqt? Does the standard C++ tutorial not work ( http://wiki.ros.org/ROS/Tutorials/Wri...