ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

class Arm:
    def __init__(self):
        self.sub = rospy.Subscriber('/rx150/joint_states', JointState, self.callback)
        self.waistp = None
        self.shoulderp = None
        self.elbowp = None
        self.waistv = None
        self.shoulderv = None
        self.elbowv = None

def callback(self, data):
    self.waistp = math.degrees(data.position[5])+180
    self.shoulderp = abs(math.degrees(data.position[4])-90)
    self.elbowp = abs(math.degrees(data.position[0])-90)
    self.waistv = data.velocity[5]
    self.shoulderv = data.velocity[4]
    self.elbowv = data.velocity[0]

def return_waistp(self):
        if self.waistp is not None:
            return self.waistp
        else:
            rospy.loginfo("No waistp loaded yet!")

You have to have these variables in your class fields somewhere, with initial values. I suggest you something like code below.

class Arm:
    def __init__(self):
        self.sub = rospy.Subscriber('/rx150/joint_states', JointState, self.callback)
        self.waistp = None
        self.shoulderp = None
        self.elbowp = None
        self.waistv = None
        self.shoulderv = None
        self.elbowv = None

def callback(self, data):
    self.waistp = math.degrees(data.position[5])+180
    self.shoulderp = abs(math.degrees(data.position[4])-90)
    self.elbowp = abs(math.degrees(data.position[0])-90)
    self.waistv = data.velocity[5]
    self.shoulderv = data.velocity[4]
    self.elbowv = data.velocity[0]

def return_waistp(self):
        if self.waistp is not None:
            return self.waistp
        else:
            rospy.loginfo("No waistp loaded yet!")

You have to have these variables in your class fields somewhere, with initial values. I suggest you something like code below.

class Arm:
    def __init__(self):
        self.sub = rospy.Subscriber('/rx150/joint_states', JointState, self.callback)
        self.waistp = None
        self.shoulderp = None
        self.elbowp = None
        self.waistv = None
        self.shoulderv = None
        self.elbowv = None

 def callback(self, data):
     self.waistp = math.degrees(data.position[5])+180
     self.shoulderp = abs(math.degrees(data.position[4])-90)
     self.elbowp = abs(math.degrees(data.position[0])-90)
     self.waistv = data.velocity[5]
     self.shoulderv = data.velocity[4]
     self.elbowv = data.velocity[0]

 def return_waistp(self):
         if self.waistp is not None:
             return self.waistp
         else:
             rospy.loginfo("No waistp loaded yet!")

You have to have these variables in your class fields somewhere, with initial values. values before the callback overwrites them with real values.. I suggest you something like code below.

class Arm:
    def __init__(self):
        self.sub = rospy.Subscriber('/rx150/joint_states', JointState, self.callback)
        self.waistp = None
        self.shoulderp = None
        self.elbowp = None
        self.waistv = None
        self.shoulderv = None
        self.elbowv = None

        self.is_everything_initialized = False

    def callback(self, data):
        if self.is_everything_initialized == False:
            self.is_everything_initialized = True
        self.waistp = math.degrees(data.position[5])+180
        self.shoulderp = abs(math.degrees(data.position[4])-90)
        self.elbowp = abs(math.degrees(data.position[0])-90)
        self.waistv = data.velocity[5]
        self.shoulderv = data.velocity[4]
        self.elbowv = data.velocity[0]

    def return_waistp(self):
            if self.waistp is not None:
self.is_everything_initialized:
                return self.waistp
            else:
                rospy.loginfo("No waistp loaded yet!")

Try to use it like this now. In general, you have to have a longer initialization phase in your code, try to add some waits here and here, because it looks like you try to get these values before any callback is processed.

You have to have these variables in your class fields somewhere, with initial values before the callback overwrites them with real values.. I suggest you something like code below.

class Arm:
    def __init__(self):
        self.sub = rospy.Subscriber('/rx150/joint_states', JointState, self.callback)
        self.waistp = None
        self.shoulderp = None
        self.elbowp = None
        self.waistv = None
        self.shoulderv = None
        self.elbowv = None

        self.is_everything_initialized = False

    def callback(self, data):
        if self.is_everything_initialized == False:
            self.is_everything_initialized = True
        self.waistp = math.degrees(data.position[5])+180
        self.shoulderp = abs(math.degrees(data.position[4])-90)
        self.elbowp = abs(math.degrees(data.position[0])-90)
        self.waistv = data.velocity[5]
        self.shoulderv = data.velocity[4]
        self.elbowv = data.velocity[0]

    def return_waistp(self):
            if self.is_everything_initialized:
                return self.waistp
            else:
                return -999

Try to use it like this now. In general, you have to have a longer initialization phase in your code, try to add some waits here and here, because it looks like you try to get these values before any callback is processed.