ros node that subscribes to topic and receives pose in python

To create a ROS node that subscribes to a topic and receives Pose messages, you can follow these steps:

  1. Create a python file with the following initial lines to declare it as an executable:

    main.py
    #!/usr/bin/env python
    
    22 chars
    2 lines
  2. Import the necessary ROS libraries and the Pose message type:

    main.py
    import rospy
    from geometry_msgs.msg import Pose
    
    48 chars
    3 lines
  3. Define a callback function that will be executed every time a new message is received by the subscribed topic:

    main.py
    def pose_callback(data):
        rospy.loginfo(rospy.get_caller_id() + "I heard %s", data)
    
    87 chars
    3 lines
  4. Initiate the ROS node and subscribe to the desired topic, registering the callback function as a handler:

    main.py
    rospy.init_node('pose_subscriber', anonymous=True)
    rospy.Subscriber("pose_topic", Pose, pose_callback)
    
    103 chars
    3 lines
  5. Call the rospy.spin() method to keep the node alive and waiting for new messages:

    main.py
    rospy.spin()
    
    13 chars
    2 lines

The full code snippet will look like this:

main.py
#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Pose

def pose_callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", data)

rospy.init_node('pose_subscriber', anonymous=True)
rospy.Subscriber("pose_topic", Pose, pose_callback)

rospy.spin()
277 chars
13 lines

Remember to replace pose_topic and Pose with the actual topic name and desired message type for your use case.

gistlibby LogSnag