callback error in subscriber file
I'm writing a program, subscribing to the topic /ardrone/front/image_raw, but while running the python script its showing the following error -
UnboundLocalError: local variable 'callback' referenced before assignment
#!/usr/bin/env python
#import library ros
import rospy
import time
#import library for send command and receive data navigation from quadcopter
from geometry_msgs.msg import Twist #all these are msg type
from std_msgs.msg import String
from std_msgs.msg import Empty
from ardrone_autonomy.msg import Navdata
from sensor_msgs.msg import Range
from sensor_msgs.msg import Image
#import class status for determine status form quadcopter
#from drone_status import DroneStatus
COMMAND_PERIOD = 1000
class AutonomousFlight():
def callback(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
def __init__(self):
self.status = ""
rospy.init_node('forward', anonymous=False)
self.rate = rospy.Rate(10)
self.pubTakeoff = rospy.Publisher("ardrone/takeoff",Empty, queue_size=10)
self.pubLand = rospy.Publisher("ardrone/land",Empty, queue_size=10)
self.pubCommand = rospy.Publisher('cmd_vel',Twist, queue_size=10)
self.subHeight = rospy.Subscriber('sonar_height',Range, self.callback)
self.subCamera = rospy.Subscriber('ardrone/front/image_raw',Image, self.callback)
rospy.spin()
self.command = Twist()
self.state_change_time = rospy.Time.now()
rospy.on_shutdown(self.SendLand)
def SendTakeOff(self):
self.pubTakeoff.publish(Empty())
self.rate.sleep()
def SendLand(self):
self.pubLand.publish(Empty())
def SetCommand(self, linear_x, linear_y, linear_z, angular_x, angular_y, angular_z):
self.command.linear.x = linear_x
self.command.linear.y = linear_y
self.command.linear.z = linear_z
self.command.angular.x = angular_x
self.command.angular.y = angular_y
self.command.angular.z = angular_z
self.pubCommand.publish(self.command)
self.rate.sleep()
#def SetHeight(self):
#pass
def ToggleCam(self):
self.subCamera.subscribe(Empty())
self.rate.sleep()
if __name__ == '__main__':
try:
i = 0
uav = AutonomousFlight()
while not rospy.is_shutdown():
uav.ToggleCam()
uav.SendTakeOff()
if i <= 30 :
uav.SetCommand(0,0,1,0,0,0)
i+=1
except rospy.ROSInterruptException:
pass
Asked by shubham11 on 2018-04-13 00:49:01 UTC
Comments
If this is an exact copy paste, then your indentation is off.
Asked by gvdhoorn on 2018-04-13 03:54:10 UTC
Actually the indentation error arised earlier, now the indentation problem is solved but somehow its showing me this callback referencing error. The above program is not the exact copy of the actual program there may be some spacing error
Asked by shubham11 on 2018-04-13 04:41:52 UTC
Then please update your question text with the exact program. Otherwise we will only guess.
Referenced before assignment errors can be caused by wrong indentation, that's why I'm asking.
Asked by gvdhoorn on 2018-04-13 04:45:14 UTC
Btw: shouldn't it be
self.callback
instead of justcallback
in yourSubcriber(..)
initialisation lines?Asked by gvdhoorn on 2018-04-13 04:47:10 UTC
i tried using self.callback, still no success. it is giving the following error
TypeError: callback() takes exactly 1 argument (2 given)
Asked by shubham11 on 2018-04-13 07:56:18 UTC
the signature of the callback should be
def callback(self, data)
. It is a member of the class, thus you Need the self which is the first of the two arguments passed.You still have the indentation errors though. Please format the code yourself the next time by clicking the button with 010101.
Asked by mgruhler on 2018-04-13 08:24:49 UTC