0

我真的不明白我做错了什么..我尝试从另一种方法获取值对象..这是我的代码

#!/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

但是,我得到这个错误

vektor_n_rs = self.posisi_pan_servo - self.posisi_tilt_servo  

AttributeError: tracksendi instance has no attribute 'posisi_pan_servo'

这个问题是怎么解决的???

笔记 :

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 是为 self.posisi_ax12_pan 方法、self.posisi_ax12_tilt 方法和 self.posisi_ax12_elbow 插入 Float64 数据的行命令。

4

4 回答 4

1

显然posisi_ax12_panposisi_ax12_tilt 比operasikan_servo调用晚(在您订阅的事件发生之后),因此,您应该初始化此属性 - self.posisi_pan_servo 和 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
        #....
于 2013-04-26T07:25:13.377 回答
0

错误说不self.posisi_pan_servo存在。您似乎只在posisi_ax12_pan(). 这意味着posisi_ax12_pan()当您尝试在operasikan_servo().

于 2013-04-26T07:26:24.587 回答
0

我想在调用构造函数posisi_ax12_*之前应该调用方法。operasikan_servo

于 2013-04-26T07:27:41.917 回答
0

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

于 2013-04-26T07:28:18.760 回答