如何从python中的另一个方法获取一个value-object

时间:2013-04-26 07:18:58

标签: python ros

我无法理解我做错了什么..我试图从另一个方法中获取一个值对象..这是我的代码

#!/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方法插入Float64数据,self.posisi_ax12_tilt方法和self.posisi_ax12_elbow。

4 个答案:

答案 0 :(得分:1)

显然 posisi_ax12_pan posisi_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
        #....

答案 1 :(得分:0)

错误说self.posisi_pan_servo不存在。您似乎只在posisi_ax12_pan()中定义了此变量。这意味着当您尝试在posisi_ax12_pan()中访问该属性时,尚未调用方法operasikan_servo()

答案 2 :(得分:0)

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

答案 3 :(得分:0)

看起来你没有执行方法posisi_pan_servo,它初始化属性'posisi_pan_servo'

您之前应该执行它,尝试获取该属性。

也许在 init 方法中,您应该调用该方法。所以试着改变:

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

要:

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

在该调用中传递正确的参数。

但是其他的事情是深入测试rospy.Subscriber方法,检查它是否按预期工作