0
$\begingroup$

Rosanswers logo

Hi

I was running "hector_quadrotor_demo outdoor_flight_gazebo.launch". And I build a publisher "auto_fly", to publish message to control the quadrotor. The code is showed below:

#!/usr/bin/env python

import rospy from geometry_msgs.msg import Twist

def auto_fly(): pub= rospy.Publisher('/uav/uav2/cmd_vel',Twist,queue_size=10) rospy.init_node('auto_fly',anonymous=True) r=rospy.Rate(10) # 10Hz

while not rospy.is_shutdown(): pub.publish['x=0,y=1,z=0.2' 'x=0,y=0,z=0'] r.sleep(1.0)

if name== 'main': try: auto_fly() except rospy.ROSInterruptException: pass

When I run it, it shows,

TypeError: 'instancemethod' object has no attribute 'getitem'

What does this error mean and how to fix it?

Thanks a lot Glen


Originally posted by Glen on ROS Answers with karma: 40 on 2014-07-16

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

This error: TypeError: 'instancemethod' object has no attribute '__getitem__' means that you're trying to use square brackets ( getitem ) on something that doesn't support them; namely a method.

For the publish method call, you should be using parentheses, just like any other method call in python. You'll also need to revise the argument to your publish call; it should be a python message object:

msg = Twist() # create a new Twist message object
msg.linear.y = 1 # set the linear y component to 1
pub.publish(msg) # publish your message

Originally posted by ahendrix with karma: 47576 on 2014-07-17

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Glen on 2014-07-17:
It works now, thanks a lot!

$\endgroup$

Your Answer

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