Ask Your Question
0

Passing Multiple Arguments to Subscriber Callback function in python [closed]

asked 2019-09-01 23:40:04 -0500

BV_Pradeep gravatar image

updated 2019-09-02 02:37:02 -0500

Hi All,

I am wrote the following code to track position of my robot in rviz using markers.

import rospy
from visualization_msgs.msg import Marker
from geometry_msgs.msg import PoseStamped,Point


def callback(data,marker,pub_point):
    add_point = Point()
    add_point.x = data.pose.position.x
    add_point.y = data.pose.position.y
    add_point.z = 0
    marker.points.append(add_point)
    # Publish the Marker
    pub_point.publish(marker)

if __name__ == '__main__':
    rospy.init_node('position_tracker')
    pub_point = rospy.Publisher('realpoints_marker', Marker, queue_size=1)
    rospy.loginfo('Generate the system')

    marker = Marker()
    marker.header.frame_id = "/map"
    marker.type = marker.POINTS
    marker.action = marker.ADD

    # marker scale
    marker.scale.x = 0.03
    marker.scale.y = 0.03
    marker.scale.z = 0.03

    # marker color
    marker.color.a = 1.0
    marker.color.r = 1.0
    marker.color.g = 0.0
    marker.color.b = 0.0

    # marker orientaiton
    marker.pose.orientation.x = 0.0
    marker.pose.orientation.y = 0.0
    marker.pose.orientation.z = 0.0
    marker.pose.orientation.w = 1.0

    # marker position
    marker.pose.position.x = 0.0
    marker.pose.position.y = 0.0
    marker.pose.position.z = 0.0

    # marker line points
    marker.points = []
    rospy.Subscriber("/pose", PoseStamped, callback,marker,pub_point)

I get a error saying that queue size needs to be Integer. It is because of how rospy class is defined http://docs.ros.org/melodic/api/rospy/html/rospy.topics.Subscriber-class.html

The fourth argument is queue size.

I need to pass marker and pub_point objects to callback function.

Solution:

import rospy
from visualization_msgs.msg import Marker
from geometry_msgs.msg import PoseStamped,Point

def callback(data):
    add_point = Point()
    add_point.x = data.pose.position.x
    add_point.y = data.pose.position.y
    add_point.z = 0
    rospy.loginfo('Publishing Marker Point')
    marker.points.append(add_point)
    # Publish the Marker
    pub_point.publish(marker)

marker = Marker()
marker.header.frame_id = "/my_frame"
marker.type = marker.POINTS
marker.action = marker.ADD

# marker scale
marker.scale.x = 0.03
marker.scale.y = 0.03
marker.scale.z = 0.03

# marker color
marker.color.a = 1.0
marker.color.r = 1.0
marker.color.g = 0.0
marker.color.b = 0.0

# marker orientaiton
marker.pose.orientation.x = 0.0
marker.pose.orientation.y = 0.0
marker.pose.orientation.z = 0.0
marker.pose.orientation.w = 1.0

# marker position
marker.pose.position.x = 0.0
marker.pose.position.y = 0.0
marker.pose.position.z = 0.0

# marker line points
marker.points = []
print "Marker created...."

rospy.loginfo('Publishing Marker Point')
rospy.init_node('position_tracker')
pub_point = rospy.Publisher('realpoints_marker', Marker, queue_size=1)
print "Publisher created...."
rospy.Subscriber("/simulated_waypoints", PoseStamped, callback)
print "Subcriber created...."
rospy.spin()
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by BV_Pradeep
close date 2019-09-16 02:56:44.218188

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-09-02 01:29:56 -0500

pavel92 gravatar image

updated 2019-09-02 03:08:32 -0500

You cant, you are subscribing to a topic and topics handle one message type only (in your case a PoseStamped msg and in your callback this is the only argument-data). You can make your publisher object and marker global variables and use them in the callback where you are processing the pose. To better grasp the idea of publishers/subscribers please go through the tutorials.

Edit:
Passing arguments to a subscriber callback is definitely possible as @gvdhoorn has pointed out in the comments below.

edit flag offensive delete link more

Comments

2

If I understand the OP correctly, what he wants to do is actually possible. See #q231492 fi and rospy.Subscriber(..).

gvdhoorn gravatar imagegvdhoorn ( 2019-09-02 02:13:20 -0500 )edit

Both the methods work but I am going with global variables.

BV_Pradeep gravatar imageBV_Pradeep ( 2019-09-02 02:35:57 -0500 )edit

@gvdhoorn You are absolutely right, I made an oversight while answering and just gave a simple fix for his current problem and ignored the optional callback_args. However the proper way would be to use the references you provided.

pavel92 gravatar imagepavel92 ( 2019-09-02 03:05:50 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-09-01 23:40:04 -0500

Seen: 41 times

Last updated: Sep 02