trying to get joint data from subscriber class function
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
Please make sure your code is formatted correctly. Earlier I noticed incorrect indentation, which can lead to errors with Python,