Ask Your Question
0

How to get the data from Subscribers message with python

asked 2019-05-21 04:54:18 -0500

A_YIng gravatar image

Hello, In my program, I try to get the Pose msg data which publish from other topic.

But when I print the variable which to read the Subscribers message, I will get the all values are 0.

Here is my code:

import sys
import rospy 
import geometry_msgs.msg 
from geometry_msgs.msg import Pose 

grasp_goal = Pose()

def callback(msg):
    global grasp_goal
    grasp_goal = msg.grasp_goal

# Create a ROS node.
rospy.init_node('move_to_grasp', anonymous=True)
rospy.loginfo('Start to grasp the object...')

# Subscribe to the ROS topic that contains the grasp data.
sub = rospy.Subscriber('/grasp_pose', Pose, callback)

rospy.loginfo("x:%f y:%f z:%f w:%f x:%f y:%f z:%f", grasp_goal.position.x, grasp_goal.position.y, grasp_goal.position.z, grasp_goal.orientation.w, grasp_goal.orientation.x, grasp_goal.orientation.y, grasp_goal.orientation.z)

This is echo /grasp_pose topic: image description

This is result, all value are zero: image description

Thanks for your help !

edit retag flag offensive close merge delete

Comments

1

Please don't use images to display text. Images are not searchable and the text cannot be copied and pasted. Please update your question with a copy and paste of the text instead of using images. See the support page

jayess gravatar image jayess  ( 2019-05-21 13:03:09 -0500 )edit

sorry for mistake, I will edit it.

A_YIng gravatar image A_YIng  ( 2019-05-21 22:06:19 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-05-21 06:52:05 -0500

Your program is not waiting for a message to be received before it is printing out the pose information from the global variable. In order what your program is doing.

  1. intializes global variables 2, setup node 3, register subscriber (not the same as waiting for a message) 4, print values of global variables 5, shutdown

If you try printing out the values of the global pose variable in a 1 second loop, then you will see the correct values from messages being printed after the first message has been received. It might be a good idea to put a print in your callback, to confirm messages are being received too.

Hope this helps.

edit flag offensive delete link more

Comments

Sorry, I'm still not sure what do you mean. Could you provide me some example code like this? It can help me know it clearly.

A_YIng gravatar image A_YIng  ( 2019-05-21 07:56:39 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-05-21 04:54:18 -0500

Seen: 1,923 times

Last updated: May 21 '19