callback error in subscriber file

asked 2018-04-13 00:49:01 -0500

shubham11 gravatar image

updated 2018-04-13 08:23:41 -0500

mgruhler gravatar image

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
edit retag flag offensive close merge delete

Comments

2

If this is an exact copy paste, then your indentation is off.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-13 03:54:10 -0500 )edit

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

shubham11 gravatar image shubham11  ( 2018-04-13 04:41:52 -0500 )edit
1

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.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-13 04:45:14 -0500 )edit
1

Btw: shouldn't it be self.callback instead of just callback in your Subcriber(..) initialisation lines?

gvdhoorn gravatar image gvdhoorn  ( 2018-04-13 04:47:10 -0500 )edit

i tried using self.callback, still no success. it is giving the following error

TypeError: callback() takes exactly 1 argument (2 given)

shubham11 gravatar image shubham11  ( 2018-04-13 07:56:18 -0500 )edit
1

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.

mgruhler gravatar image mgruhler  ( 2018-04-13 08:24:49 -0500 )edit