Using python classes to access information in other functions
In these functions
def callback(data):
#rospy.loginfo(data.position[1])
print(data.position[0])
def listener():
rospy.init_node('listener')
rospy.Subscriber('/rrbot/joint_states', JointState, callback)
rospy.spin()
def talker():
pub = rospy.Publisher('/rrbot/joint1_position_controller/command', Float64, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(1) # 10hz
while not rospy.is_shutdown():
#hello_str = "hello world %s" % rospy.get_time()
#force = random.choice([-1,1])
force = 0
rospy.loginfo(force)
pub.publish(force)
rate.sleep()
The talker function works fine as it publishes the position to the robot, and the callback and listener function work fine and reads the correct position over time every 10hz. But they only work separately in two different terminals but I need access to both the force that is being published and the position that is being subscribed to to run a machine learning algorythm so I made this class
class RobotControl():
def __init__(self):
rospy.init_node('robot_control_node', anonymous=True)
self.talker = rospy.Publisher('/rrbot/joint1_position_controller/command', Float64, queue_size=10)
self.rate = rospy.Rate(1)
self.sub = rospy.Subscriber('/rrbot/joint_states', JointState, self.callback)
self.force = random.choice([-1,1])
def give_force(self):
while not rospy.is_shutdown():
#hello_str = "hello world %s" % rospy.get_time()
#force = random.choice([-1,1])
#force = 0
#rospy.loginfo(self.force)
self.talker.publish(self.force)
self.rate.sleep()
def callback(self,data):
print(data.position[0])
print(self.force)
r = RobotControl()
r.callback()
I am having trouble with the force not changing between 1 and -1 it just sticks to either or. and I'm getting
TypeError: callback() missing 1 required positional argument: 'data'
here is an example of the output
-1
-3.0659357154670257
-1
-3.0659357154670257
-1
-3.0659357154670257
-1
-3.0659357154670257
-1
-3.0659357154670257
-1