0
$\begingroup$

Rosanswers logo

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()

Originally posted by BV_Pradeep on ROS Answers with karma: 46 on 2019-09-01

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by pavel92 with karma: 1655 on 2019-09-02

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by gvdhoorn on 2019-09-02:
If I understand the OP correctly, what he wants to do is actually possible. See #q231492 fi and rospy.Subscriber(..).

Comment by BV_Pradeep on 2019-09-02:
Both the methods work but I am going with global variables.

Comment by pavel92 on 2019-09-02:
@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.

Comment by OzzieTheHead on 2021-08-20:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.