ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Passing Multiple Arguments to Subscriber Callback function in python

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 close merge delete

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

3

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

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

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

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

@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 image pavel92  ( 2019-09-02 03:05:50 -0500 )edit

This shouldn't be marked as "correct". I think you need to edit the first paragraph as it is irrelevant to what is being asked.

OzzieTheHead gravatar image OzzieTheHead  ( 2021-08-20 15:36:31 -0500 )edit

Question Tools

2 followers

Stats

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

Seen: 4,508 times

Last updated: Sep 02 '19