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

rospy.loginfo is not printing when node is started with a launch file

asked 2021-02-09 08:20:57 -0500

mab0189 gravatar image

updated 2021-02-09 11:31:47 -0500

I did the very helpfull beginner tutorials and wanted to apply what I learned on a self written example. Therefore I wrote a package called "calculate" with a "calc_talker" node that calculates the fibonacci numbers and a "calc_listener" node that listens to them.

calc_talker:

#!/usr/bin/env python

import rospy
from calculate.msg import Calc

def talker():
    pub = rospy.Publisher("Calc_Chatter", Calc, queue_size=100)
    rospy.init_node("Calc_Talker", anonymous=True)
    rate = rospy.Rate(10)

    count   = 3
    number1 = 1
    number2 = 1

    while not rospy.is_shutdown():

        result = number1 + number2

        msg = Calc()
        msg.first_number = number1
        msg.second_number = number2
        msg.result = f"The {count} fibonacci number is {result}"

        number2 = number1
        number1 = result
        count = count + 1

        rospy.loginfo(msg)
        pub.publish(msg)
        rate.sleep()


if __name__ == "__main__":
    try:
        talker()
    except rospy.ROSInterruptException:
        pass
#EoF

calc_listener:

#!/usr/bin/env python

import rospy
from calculate.msg import Calc

def callback(data):
    msg = f"{data.result}. It was calculated from {data.first_number} and {data.second_number}"
    rospy.loginfo(msg)


def listener():
    rospy.init_node("Calc_Listener", anonymous=True)
    rospy.Subscriber("Calc_Chatter", Calc, callback)
      rospy.spin()


if __name__ == "__main__":
    listener()
#EoF

When i start the nodes with rosrun i get the following example output:

rosrun calculate calc_talker.py:

[INFO] [1612869970.095087]: first_number: 1 second_number 1 result: "The 3 fibonacci number is 2"

rosrun calculate calc_listener.py:

[INFO] [1612869970]: The 3 fibonacci number is 2. It was calculated from 1.0 and 1.0

I wrote a simple launch file to launch both nodes:

<launch>

    <group ns="talker">
        <node pkg="calculate" name="my_calc_talker" type="calc_talker.py"/>
    </group>

    <group ns="listener">
        <node pkg="calculate" name="my_calc_listener" type="calc_listener.py" output="screen"/>
    </group>

</launch>

My Problem: When i add output="screen" to the calc_talker i get the expected log output. However i does not work when i add it to the calc_listener and i can not find out why. I am very grateful for your help!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2021-02-09 16:37:31 -0500

robodan gravatar image

Hey mab0189!

The reason that you cannot see the log info is because your callback callc_listener.callback is never executed.

But why is that? Well in this case it is from the grouping of each node under a different namespace.

Your talker node is under the "talker" namespace meaning that (unless specified to be global) subscriptions are nested under that namespace. This means that your talker node is publishing to the topic /talker/Calc_Chatter and similarly your listener is listening on the topic /listener/Calc_Chatter. These two topics are clearly different and that is the root of your issue.

The solution is therefore to remove the (in this case) unneeded group tags that currently wrap your nodes in the launch file. try...

<launch>
    <node pkg="calculate" name="my_calc_talker" type="calc_talker.py" output="screen"/>
    <node pkg="calculate" name="my_calc_listener" type="calc_listener.py" output="screen"/>
</launch>

A few other notes.

  1. You can check my analysis of the topics by running rostopic list after launching your launch file.
  2. You are correct in that output="screen" is required for the logging to be visible when using the launch file.
  3. Namespacing is a tricky topic (I struggle with it still). you can read more on it here.
edit flag offensive delete link more

Comments

@robodan thank you very much for this comprehensive answer! As you suggested i checked your analysis with rostopic list and you are correct. I also used the rqt_graph tool to visualise it. I should have done it in the first place but since it worked with rosrun i thought the bug must lie elsewhere.

mab0189 gravatar image mab0189  ( 2021-02-10 09:41:52 -0500 )edit
0

answered 2021-02-09 16:04:08 -0500

naianis gravatar image

As a launch file can launchmultiple nodes at once, it doesn't display every output you put on the code. It has to do with the verbosity level. You can find more about it here: http://wiki.ros.org/Verbosity%20Levels

However, you can use rospy.logerr instead of rospy.loginfo and it may appear even when the node is started by a launch file. For example, inside the callback in calc_listener, use:

rospy.logerr(msg)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-02-09 06:05:55 -0500

Seen: 1,388 times

Last updated: Feb 09 '21