Node service not publishing to topic
I have a problem with a simple node I wrote when it fails to publish any messages to a topic.
I'm using the following version of ROS:
SUMMARY
========
PARAMETERS
* /rosdistro: melodic
* /rosversion: 1.14.1
My subscriber node looks like this:
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <stdlib.h>
#include <iomanip>
void poseMessageReceived(const geometry_msgs::Twist& msg) {
ROS_INFO_STREAM("Received velocity cmd:" << std::setprecision(4) << std::fixed
<< " SUB linear==" << msg.linear.x
<< " SUB angular==" << msg.linear.z);
}
int main(int argc, char** argv) {
ros::init(argc, argv, "subscribe_to_twist");
ros::NodeHandle nh;
ROS_INFO_STREAM("Subscribed to: subpose_node/cmd_vel");
ros::Subscriber sub = nh.subscribe("subpose_node/cmd_vel", 1000, &poseMessageReceived);
ros::spin();
}
There is nothing magical in the subscriber code.
My publisher node is a service node and it looks like this -- please ignore Foo.h
srv for now:
#include <stdlib.h>
#include <iomanip>
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <svcsrv/Foo.h>
bool PubTwist(
svcsrv::Foo::Request &req,
svcsrv::Foo::Response &resp
) {
ROS_INFO_STREAM("Received message: " << "linearX==" << req.linear.x << " linearZ" << req.linear.z);
resp.linear.x = req.linear.x;
resp.linear.z = req.linear.z;
return true;
}
int main(int argc, char** argv) {
ros::init(argc, argv, "pub_twist");
ros::NodeHandle nh;
// register service with the master
ros::ServiceServer server = nh.advertiseService("pub_twist", &PubTwist);
ROS_INFO("Ready to receive and send messages!");
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("subpose_node/cmd_vel", 1000);
geometry_msgs::Twist msg;
msg.linear.x = 0.0;
msg.linear.z = 0.0;
pub.publish(msg);
ros::spin();
return 0;
}
The service client is also very simple:
#include <cstdlib>
#include <ros/ros.h>
#include <svcsrv/Foo.h>
int main(int argc, char **argv)
{
ros::init(argc, argv, "send_twist_message");
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<svcsrv::Foo>("pub_twist");
svcsrv::Foo::Request req;
svcsrv::Foo::Response resp;
req.linear.x = 3.0;
req.linear.z = 4.0;
if (client.call(req, resp))
{
ROS_INFO_STREAM("Received pub_twist response: " << "linearX==" << resp.linear.x << " linearZ==" << resp.linear.z);
}
else
{
ROS_ERROR("Failed to call service pub_twist");
return 1;
}
return 0;
}
When I run rosnode info
it displays that the node should be able to publish to subpose_node/cmd_vel
:
Connections:
* topic: /rosout
* to: /rosout
* direction: outbound
* transport: TCPROS
* topic: /subpose_node/cmd_vel
* to: /rostopic_15809_1534959147174
* direction: outbound
* transport: TCPROS
* topic: /subpose_node/cmd_vel
* to: /subscribe_to_twist
* direction: outbound
* transport: TCPROS
However subscriber NEVER receives any message. Any idea what I'm missing out here?
What's puzzling is that if I replace the service node code with the following code in works perfectly fine:
#include <stdlib.h>
#include <iomanip>
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <std_srvs/Empty.h>
bool toggle_pub = false;
bool togglePub(
std_srvs::Empty::Request &req,
std_srvs::Empty::Response &resp
) {
toggle_pub = !toggle_pub;
ROS_INFO_STREAM("Toggled send of a message:"
<< toggle_pub);
return true;
}
int main(int argc, char** argv) {
ros::init(argc, argv, "toggle_pubsvc");
ros::NodeHandle nh;
// register service with the master
ros::ServiceServer server = nh.advertiseService("toggle_pubsvc", &togglePub);
// publisher
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("subpose_node/cmd_vel", 1000);
ros::Rate rate(2);
while(ros ...
Sorry for being a beginner. Wow, this place can be really hostile. I'll try to avoid asking any more questions on this forum. As I said before thanks for your time and sorry for bothering anyone here.
My apologies if you found that comment too direct. You're completely welcome to ask questions.
I just wanted to highlight something I see a lot of people do: draw conclusions based on single / very few observations. It was not a critique on your question or your workflow.
@milosgajdos: I've restructured our comment-comment interaction a bit and summarised it into an actual answer.
I've left your expression of the sentiment that ROS Answers is a hostile place and my response as a comment under your question.
My apologies again if you feel that it was too direct.
Lets not generalise over all of ROS Answers based on a single interaction and give others a chance to prove you wrong.