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

trying to get joint data from subscriber class function

asked 2022-11-08 17:32:40 -0500

PGTKing gravatar image

updated 2022-11-10 15:39:04 -0500

Here is the code

class Arm:
    def __init__(self):
        self.sub = rospy.Subscriber('/rx150/joint_states', JointState, self.callback)

    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):
        return self.waistp

Here is the code I used to try to extract the data

arm = Arm()
print("joints states are", arm.return_waistp())

Below is the error I am getting

AttributeError: 'Arm' object has no attribute 'waistp'

here is new code

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
    rospy.spin()

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 joint_states(self):
    if self.is_everything_initialized:
        joint_states = [self.waistp, self.shoulderp, self.elbowp, self.waistv, self.shoulderv, self.elbowv]
        return joint_states
    else:
        joint_statesn = [0, 0, 0, 0, 0, 0]
        return joint_statesn

def simulate(episode, is_training): #global waistp, shoulderp, elbowp, waistv, shoulderv, elbowv

reward = 0
max_step = 20
is_done = False
episode_reward = 0
obs = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0], dtype='float16')
next_obs = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0], dtype='float16')
x_target = math.cos(math.radians(episode*6))*200
y_target = math.sin(math.radians(episode*6))*200
z_target = 250
waist_force = 0
shoulder_force = 0
elbow_force = 0
arm = Arm()
print("joints states are", arm.joint_states())
x = arm.joint_states()
waistp = x[0]
shoulderp = x[1]
elbowp = x[2]
waistv = x[3]
shoulderv = x[4]
elbowv = x[5]

joint states stay [0,0,0,0,0,0] though they are not updating

edit retag flag offensive close merge delete

Comments

Please make sure your code is formatted correctly. Earlier I noticed incorrect indentation, which can lead to errors with Python,

ravijoshi gravatar image ravijoshi  ( 2022-11-08 20:15:23 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-11-08 18:02:44 -0500

ljaniec gravatar image

updated 2022-11-09 03:05:01 -0500

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.

edit flag offensive delete link more

Comments

I am getting NoneType when I print("joints states are", arm.return_waistp()) Thank you for helping me by the way. but I am trying to get data from the joints

PGTKing gravatar image PGTKing  ( 2022-11-08 19:42:18 -0500 )edit

You have to have at least one JointState callback processed before return_waistp() call, try to add some waits or ROS spins before your print

ljaniec gravatar image ljaniec  ( 2022-11-09 03:03:31 -0500 )edit

not working :(

PGTKing gravatar image PGTKing  ( 2022-11-10 15:37:11 -0500 )edit

I checked your code in the new edit, where is your call of rospy.spin() etc.? It is needed for ROS to process callbacks with messages etc.

ljaniec gravatar image ljaniec  ( 2022-11-12 12:24:10 -0500 )edit

Did you see my latest question? It's working now but I'm getting an unreadable message

PGTKing gravatar image PGTKing  ( 2022-11-12 17:42:30 -0500 )edit

No, I didn't see it. Please share a link? Or is it in the edit here?

ljaniec gravatar image ljaniec  ( 2022-11-12 18:17:42 -0500 )edit

You have your rospy.spin() in the __init__ function - it doesn't seem to be correct. You want to have something similar to this in your script:

if __name__ == '__main__':
    rospy.init_node('arm')
    Arm()
    rospy.spin()
ljaniec gravatar image ljaniec  ( 2022-11-12 18:25:41 -0500 )edit

hey, solved issue appreciate you taking time to look at my problem.

PGTKing gravatar image PGTKing  ( 2022-11-14 02:26:54 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2022-11-08 17:32:40 -0500

Seen: 57 times

Last updated: Nov 10 '22