TypeError: __init__() missing 1 required positional argument: 'target'

4.6k views Asked by At

absolute noob when it comes to python and i am not sure how to solve this error that i am getting "TypeError: init() missing 1 required positional argument: 'target'"

I'm trying to make an extra subscriber called target so i can get the coordinates of the so called target so that the "catcher" can go to target's position. The error is on line 54 which is : x=TurtleCatch()

class TurtleCatch:
    
def __init__(self, target):
    rospy.init_node('turtlecatch_initialiser', anonymous=True)
    
    self.velocity_publisher = rospy.Publisher('/robot1/pose', Twist, queue_size=10)
    
    self.pose_subscriber = rospy.Subscriber('/robot1/pose', Pose,  self.update_pose)
    target.pose_subscriber = rospy.Subscriber('/mytarget/pose', Pose, target.update_pose)

    target.pose = Pose()
    target.rate = rospy.Rate(10)
    self.pose = Pose()
    self.rate = rospy.Rate(10)

def euclidean_distance(self, target):
    return sqrt(pow((target.pose.x - self.pose.x), 2) + pow((target.pose.y - self.pose.y), 2))

def linear_vel(self, target, constant=1.5):
    return constant * self.euclidean_distance(target.pose)
def steering_angle(self, target):
    return atan2(target.pose.y - self.pose.y, target.pose.x - self.pose.x)
def angular_vel(self, target, constant=6):
    return constant * (self.steering_angle(target.pose) - self.pose.theta)
def move2target(self, target):
    
    distance_tolerance = 0.01;
    vel_msg = Twist()
    while self.euclidean_distance(target.pose) >= float(distance_tolerance):
        #angular velocity in y axis
        vel_msg.linear.x = self.linear_vel(target.pose)
        vel_msg.linear.y = 0
        vel_msg.linear.z = 0
        # Angular velocity in the z-axis.
        vel_msg.angular.x = 0
        vel_msg.angular.y = 0
        vel_msg.angular.z = self.angular_vel(target.pose)
        self.velocity_publisher.publish(vel_msg)
        self.rate.sleep()

        vel_msg.linear.x=0
        vel_msg.angular.z=0
        self.velocity_publisher.publish(vel_msg)
        rospy.spin()
if __name__ == '__main__':
    try:
        x = TurtleCatch()
        x.move2target()
    except rospy.ROSInterruptException:
        pass
2

There are 2 answers

2
leleogere On

You need to pass a target to TurtleCatch like that: TurtleCatch(target).

Here you call it with no argument (TurtleCatch()) so the __init__ function complains about a missing parameter.

1
dnorhoj On

When you initialize an object of the class TurtleCatch, it wants the parameter target, which is defined by the following line:

def __init__(self, target):

But when you initialize the x variable:

x = TurtleCatch()

You do not provide this target parameter.

You should include a target in the following format:

x = TurtleCatch(<target>)

(without the <>)