# Get Topic values in script

Hi i am doing an apriltag following project. I am currently trying to test how to get distance between the tag and the camera. I have subscribed to the AprilTagDetectionArray msg.

My main question is how do i get the distance between the tag and my camera. Should i use the position or the orientation values? I want the robot to detect and then approach the tag.

I tried getting the values from both position and orientation but i am getting the error below.

#!/usr/bin/env python

import rospy
from apriltag_ros.msg import AprilTagDetectionArray

w = 0.0

def tagDetect(msg):
global w
w = msg.pose.pose.orientation.w
w = abs(w)

def listener():
rospy.init_node("listener",anonymous=True)
rospy.Subscriber("/tag_detections",AprilTagDetectionArray,tagDetect)
rospy.spin()

if __name__ == '__main__':
listener()


but i am getting this error. how do i resolve this??

 [ERROR] [1593270010.489688]: bad callback: <function tagDetect at 0x7f53e4b62a28>
Traceback (most recent call last):
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "/home/zeus/catkin_ws/src/autonav/src/test.py", line 10, in tagDetect
w = msg.pose.pose.position[0].x
AttributeError: 'AprilTagDetectionArray' object has no attribute 'pose'


Here is the message just in case.

[apriltag_ros/AprilTagDetectionArray]:
uint32 seq
time stamp
string frame_id
apriltag_ros/AprilTagDetection[] detections
int32[] id
float64[] size
geometry_msgs/PoseWithCovarianceStamped pose
uint32 seq
time stamp
string frame_id
geometry_msgs/PoseWithCovariance pose
geometry_msgs/Pose pose
geometry_msgs/Point position
float64 x
float64 y
float64 z
geometry_msgs/Quaternion orientation
float64 x
float64 y
float64 z
float64 w
float64[36] covariance

• how do i resolve the error? – sudhanv apte Jun 28 at 6:06

I can't comment, so writing this as an answer. Did you try calling msg.detections.pose.pose.orientation.w?