No output received for subscriber program
I am running ROS melodic on my Ubuntu Bionic. I have coded a publisher and subcriber program. While the publisher program works correctly, when I run the subscriber program using rosrun
I dont receive any output
The publisher code -
//This program publishes randomly generated velocity messages for the turtlesim//
#include<ros/ros.h>
#include<geometry_msgs/Twist.h>
#include<stdlib.h> //For rand and RAND_MAX
int main(int argc, char **argv){
//Initialize ROS system and publish a node
ros::init(argc, argv, "publish_velocity");
ros::NodeHandle nh;
//Create publisher object
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000);
//Seed the random number generator
srand(time(0));
//Loop at 2Hz until the node is shut down
ros::Rate rate(2);
while(ros::ok()){
//Create and fill in the four fields, the other four fields, which are ignored by turtlesim, default to 0
geometry_msgs::Twist msg;
msg.linear.x = double(rand())/double(RAND_MAX);
msg.angular.z = 2*double(rand())/double(RAND_MAX) - 1;
//Publish the message
pub.publish(msg);
//Send a message to rosout with the details
ROS_INFO_STREAM("Sending random velocity command:"
<<" linear="<<msg.linear.x
<<" angular="<<msg.angular.z);
//Wait until its time for another iteration
rate.sleep();
}
}
and the corresponding subscriber code -
//This program subscribes to /turtle1/pose and displays it on the screen
#include<ros/ros.h>
#include<turtlesim/Pose.h>
#include<iomanip> //for std::setprecision and std::fixed
// A calback function that is executed each time a pose message is received
void poseMessageReceived(const turtlesim::Pose &msg){
ROS_INFO_STREAM(std::setprecision(2) << std::fixed
<< "position=("<< msg.x <<","<< msg.y<< ")"
<< " direction="<< msg.theta);
}
int main(int argc, char **argv){
//Initialize a ROS system and become a node
ros::init(argc, argv, "subscribe_to_pose");
ros::NodeHandle nh;
//Create a subscriber object
ros::Subscriber sub = nh.subscribe("turle1/pose", 1000, &poseMessageReceived);
//Let ROS take over
ros::spin();
}
Both of these have been saved under the same package. The CMakeLists.txt is-
cmake_minimum_required(VERSION 2.8.3)
project(first_package)
find_package(catkin REQUIRED COMPONENTS roscpp geometry_msgs turtlesim)
catkin_package()
include_directories(${catkin_INCLUDE_DIRS})
add_executable(hello hello.cpp)
add_executable(pubvel pubvel.cpp)
add_executable(subpose subpose.cpp)
target_link_libraries(hello ${catkin_LIBRARIES})
target_link_libraries(pubvel ${catkin_LIBRARIES})
target_link_libraries(subpose ${catkin_LIBRARIES})
What could be possible error here. It compiles perfectly when i use catkin_make
. Is the error in the code itself or in the CMakeLists.txt?
I'm sorry to be so strict, but I'm closing your question as it doesn't follow the Support Guidelines. Specifically the sections about not using screenshots to show consoles, log files and/or code (snippets).
Please edit your question and I'll re-open it.
Sorry for posting the screenshot. Will i be able to post just the code like i have done for the CMakeLists.txt? Even though the topics do not match, the by publishing random values to the command velocity, the pose of the turtle changes and hence I should get a output for the subscriber right?
yes. It's all text.
Ok I have done it. Can you please open the question and solve it?
What is it that you want to achieve? Do you want the bot to update its pose based on the given command velocities? Are you doing this on hardware or in simulation?
I am doing it on simulation. Based on the command velocities being sent, the turtle pose would change right? I want this pose to be given as output and displayed by the subscriber.