ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

No output received for subscriber program

asked 2019-01-15 04:39:37 -0500

TristanNoctis gravatar image

updated 2019-01-15 07:14:38 -0500

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?

edit retag flag offensive close merge delete

Comments

1

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.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-15 04:48:02 -0500 )edit

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?

TristanNoctis gravatar image TristanNoctis  ( 2019-01-15 07:06:12 -0500 )edit
1

Will i be able to post just the code like i have done for the CMakeLists.txt?

yes. It's all text.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-15 07:07:53 -0500 )edit

Ok I have done it. Can you please open the question and solve it?

TristanNoctis gravatar image TristanNoctis  ( 2019-01-15 07:15:14 -0500 )edit

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?

curi_ROS gravatar image curi_ROS  ( 2019-01-15 09:35:40 -0500 )edit

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.

TristanNoctis gravatar image TristanNoctis  ( 2019-01-15 11:25:24 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-01-15 23:46:39 -0500

billy gravatar image

You've misspelled the topic in your subscriber. You're listening on a topic that doesn't exist.

edit flag offensive delete link more

Comments

where exactly is this error?

TristanNoctis gravatar image TristanNoctis  ( 2019-01-16 01:11:24 -0500 )edit

Not the response I was expecting, but here you go.

//Create a subscriber object ros::Subscriber sub = nh.subscribe("turle1/pose", 1000, &poseMessageReceived);

billy gravatar image billy  ( 2019-01-16 01:37:20 -0500 )edit

Oh sorry I am new to both programming and ROS. By the way, how could this have compiled even though made is mistake?

TristanNoctis gravatar image TristanNoctis  ( 2019-01-16 02:10:12 -0500 )edit

@TristanNoctis: topic names are not checked by the compiler, it's just a string.

It would also be interesting to think about how that could be checked at compile time: how would the compiler know which topics exist at runtime, when topics can be created at runtime?

gvdhoorn gravatar image gvdhoorn  ( 2019-01-16 03:22:34 -0500 )edit

I get it now! But still after making the changes, I dont see any output. Is there any other change i must do?

TristanNoctis gravatar image TristanNoctis  ( 2019-01-16 03:58:19 -0500 )edit

You can echo the topic you're listening to to make sure the publisher is actually publishing. That narrows down the issue by 50%. Also when needed I have nodes print stuff to screen so I can track how far through the processing it gets before things stop working.

billy gravatar image billy  ( 2019-01-16 11:06:58 -0500 )edit

@billy: can you elaborate? I am printing the pose.

TristanNoctis gravatar image TristanNoctis  ( 2019-01-18 10:12:16 -0500 )edit
1

Who is publishing the pose topic? It's not your publisher.

I have to ask. After you corrected the name of the topic, did you rerun catkin_make and restart roscore? Put a few very simple info_stream statements in your main to make sure the program is even starting.

billy gravatar image billy  ( 2019-01-18 11:59:39 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-15 04:39:37 -0500

Seen: 574 times

Last updated: Jan 15 '19