No response from service, node won't close either
I am running into an issue with calling a service in the callback function of a listener. I couldn't find a way to call the service as the initialization of the client stood in the main, but moving that to the function as well gave a whole different series of errors, so i tried to implement a work-around
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "final/add_duplicates.h"
#include <string>
int message = 0;
std::string text;
void preparefortrouble(const std_msgs::String::ConstPtr& msg)
{
text = msg->data.c_str();
message = 1;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_duplicates_client");
ros::init(argc, argv, "print_twice");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("print_twice", 1, preparefortrouble);
ros::ServiceClient client = n.serviceClient<final::add_duplicates>("add_duplicates");
while (1){
while (message == 1)
{
final::add_duplicates srv;
srv.request.text = text.c_str();
srv.request.nrOfDuplicates = 2;
if (client.call(srv) == false)
{
ROS_ERROR("Orga het is stuk");
}
message = 0;
}
}
ros::spin();
return 0;
}
The server has been tested and works fine, the topic is called in the terminal. However, when i use this node, it cannot be close by crtl-c and sending a message to the topic results in nothing.
i try to call it with
$ rostopic pub /print_twice std_msgs/string “hello"
and my srv file is as following:
string text
int32 nrOfDuplicates
---
string multitext
thanks for the support.
I don't really understand what you write here, so I've ignored that for now.
What are you trying to do really?