Janoko Oke
Janoko Oke

Reputation: 1

how to get a value-object from another method in python

I can't really understand what I'm doing wrong.. I try to get a value-object from another method.. this is my code

#!/usr/bin/env python
 

class tracksendi():
    def __init__(self):
        rospy.on_shutdown(self.shutdown)

        rospy.Subscriber('robotis/servo_head_pan_joint',
                         Float64, self.posisi_ax12_pan)
        rospy.Subscriber('robotis/servo_head_tilt_joint',
                         Float64, self.posisi_ax12_tilt)
        rospy.Subscriber('robotis/servo_right_elbow_joint',
                         Float64, self.posisi_ax12_elbow)

        while not rospy.is_shutdown():
            self.operasikan_servo()
            rate.sleep()

    def posisi_ax12_pan(self,pan):
        self.posisi_pan_servo = pan.data   
        return

    def posisi_ax12_tilt(self,tilt):
        self.posisi_tilt_servo = tilt.data
        return     

    def posisi_ax12_elbow(self,elbow):
        self.posisi_elbow_data = elbow.data
        return

    def ambil_timestamp(self,waktu):
        self.data_time_joint_states = waktu.header.stamp
        return             

    def operasikan_servo(self):
    # Lengan Kanan
        try:

            vektor_n_rs = self.posisi_pan_servo - self.posisi_tilt_servo
            vektor_re_rs = self.posisi_tilt_servo - self.posisi_elbow_data

        except KeyError:
            pass

 
if __name__ == '__main__':
    try:
        tracksendi()
    except rospy.ROSInterruptException:
        pass

But, I get this error

vektor_n_rs = self.posisi_pan_servo - self.posisi_tilt_servo  

AttributeError: tracksendi instance has no attribute 'posisi_pan_servo'

How that problem solved ???

Note :

rospy.Subscriber('robotis/servo_head_pan_joint', Float64, self.posisi_ax12_pan)

rospy.Subscriber('robotis/servo_head_tilt_joint', Float64, self.posisi_ax12_tilt)

rospy.Subscriber('robotis/servo_right_elbow_joint', Float64, self.posisi_ax12_elbow)

rospy.Subscriber is a line command to insert Float64 data for self.posisi_ax12_pan method, self.posisi_ax12_tilt method and self.posisi_ax12_elbow.

Upvotes: 0

Views: 428

Answers (4)

ndpu
ndpu

Reputation: 22571

Obviously posisi_ax12_pan and posisi_ax12_tilt called later (after events that you subscribing are occured) than operasikan_servo, so, you should init this attributes - self.posisi_pan_servo and self.posisi_tilt_servo:

   def __init__(self):
        rospy.on_shutdown(self.shutdown)
        self.posisi_pan_servo = 0 # or any number you want
        self.posisi_tilt_servo = 0 # or any number you want
        #....

Upvotes: 1

acj
acj

Reputation: 134

Looks like you are not executing the method posisi_pan_servo, which initialize the attribute 'posisi_pan_servo'

You should execute it before, trying to get that attribute.

Maybe in the init method you should invoke the method. So try to change from:

rospy.Subscriber('robotis/servo_head_pan_joint', Float64, self.posisi_ax12_pan)

To:

rospy.Subscriber('robotis/servo_head_pan_joint', Float64, self.posisi_ax12_pan(pan))

Passing a the right parameters in that invoke.

But other thing is deep test the rospy.Subscriber method, to check if it is working as you expect

Upvotes: 0

mike
mike

Reputation: 1764

I guess that a call to posisi_ax12_* methods should be done before calling operasikan_servo in the constructor.

Upvotes: 0

E.Z.
E.Z.

Reputation: 6661

The error says self.posisi_pan_servo does not exist. You only seem to define this variable in posisi_ax12_pan(). That means the method posisi_ax12_pan() was not yet called when you tried to access that attribute within operasikan_servo().

Upvotes: 0

Related Questions