Mathematical operation on subscriber ROS node

302 views Asked by At

I am receiving data from a topic, I subscribed to this topic in a separate node I want to do some mathematical operation on these data but I get an error this is the code

def __init__(self):

rospy.init_node('extreme_values', annonymous=True)
self.x= rospy.subscriber('x_values', float32, callback)
self.y= rospy.subscriber('y_values', float32, callback)
self.theta= rospy.subscriber('theta_values', float32, callback)

self.extreme = (self.x - self.y) + ((self.x * self.y) * cos(self.theta))
1

There are 1 answers

3
GPrathap On BEST ANSWER

What you have done was wrong, you could try something to similar to bellow,

import rospy 
from std_msgs.msg import Float32 

class Handler():
    def __init__(self):
        rospy.subscriber('x_values', Float32, self.callback_xvalues)
        rospy.subscriber('y_values', Float32, self.callback_yvalues)
        rospy.subscriber('theta_values', Float32, self.callback_zvalues)
         self.extreme_pub = rospy.publisher('extreme/results', Float32, queue_size = 10) 

        self.x = None
        self.y = None
        self.theta = None 

    def callback_xvalues(self, data):
        self.y = data
    
    def callback_yvalues(self, data):
        self.y = data

    def callback_zvalues(self, data):
        self.y = data
        if((self.x is not None) and (self.theta is not None)):
            self.extreme = (self.x - self.y) + ((self.x * self.y) * cos(self.theta))
            info = Float32()
            info.data = self.extreme
            self.extreme_pub.publish(info)


rospy.init_node('extreme_values', annonymous=True)

node_ = Handler()

rospy.subscriber does not return anything, however when messages come corresponding callback, i.e., callback_xvalues, triggered, so your logic should be implemented inside one of those call backs, i.e., callback_zvalues. Do not register a same callback for a different topic, it is not recommended. Let me know you get the idea