Why rospy.loginfo instead of print

In the following code rospy.loginfo is used instead of print()

#! /usr/bin/env python

import rospy
from std_srvs.srv import Empty, EmptyResponse # you import the service message python classes generated from Empty.srv.
from geometry_msgs.msg import Twist

def my_callback(request):
    rospy.loginfo("The Service move_bb8_in_circle has been called")
    move_circle.linear.x = 0.2
    move_circle.angular.z = 0.2
    my_pub.publish(move_circle)
    rospy.loginfo("Finished service move_bb8_in_circle")
    return EmptyResponse() # the service Response class, in this case EmptyResponse

rospy.init_node('service_move_bb8_in_circle_server') 
my_service = rospy.Service('/move_bb8_in_circle', Empty , my_callback) # create the Service called move_bb8_in_circle with the defined callback
my_pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
move_circle = Twist()
rospy.loginfo("Service /move_bb8_in_circle Ready")
rospy.spin() # mantain the service open.

I tried looking up in google to understand difference between print() and rospy.loginfo, but I couldn’t get it.
Would you please tell me:

  1. What’s the difference between rospy.loginfo and print() ?
  2. Why are we using rospy.loginfo here, in the code above?
1 Like

Simply, rospy.loginfo() is an enhanced print that produces a more detailed output, useful for debugging. You will learn more in the Debugging unit. In the meantime, you can try using them side by side to see the very clear difference!

2 Likes

Thank you Mr Bayodesegun for all your responses.

2 Likes