Passing Multiple Arguments to Subscriber Callback function in python
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()