Why does this error comes?

Yes.

You have imported two differrent Empty objects, so the last one imported wins. You should import (and use) the second one with another name. E.g:

from std_msgs.msg import Empty as EmptyMsg

Other suggestions:

  • Create the service last, just before rospy.spin(), so that all the global variables used with the callback will already exist.
  • Remove the rate object - it’s useless here: Proper use of rospy.Rate or ros::Rate
1 Like