How to use publisher message - apply some logic and publish it again under a different topic

asked 2020-02-27 22:54:06 -0500

Hello everyone,

I am a newbie to ROS, having the below query :

  1. i have a publisher who is publishing x and y values randomly under the topic sensorValue
  2. the x , y values are declared in my sensor.msg file
  3. I am trying to make a file which will be read the values x,y and publish the product of the same. below is my code for my file:

!/usr/bin/env python

import rospy
from std_msgs.msg import Int16
from sensorProject.msg import sensor
# x and y are declared as int16 data type in sensor.msg present in my sensorProject folder.

def callback1(data):
    rospy.loginfo (" Sensor Value x = %s ", data.x)
    rospy.loginfo (" Sensor Value y = %s ", data.y)

def listener():
    #values of x and y are published under the topics sensorValue
    rospy.Subscriber("sensorValue", value.x,callback1)
    rospy.Subscriber("sensorValue", value.y,callback1)
    rospy.loginfo(" Test: start spinning!")
    rospy.loginfo("node has shutdown!")

def talker():
    pub = rospy.Publisher('Area', value, queue_size=10) # topic name is 'Area'
    rospy.init_node('talker', anonymous=True)   # Node name is talker
    rate = rospy.Rate(0.5)  

    while not rospy.is_shutdown():

        msg = sensor()
        msg.Area = msg.x * msg.y


if __name__ == '__main__':
    except rospy.ROSInterruptException:
edit retag flag offensive close merge delete