How to use publisher message - apply some logic and publish it again under a different topic
Hello everyone,
I am a newbie to ROS, having the below query :
- i have a publisher who is publishing x and y values randomly under the topic sensorValue
- the x , y values are declared in my sensor.msg file
- I am trying to make a talker.py file which will be read the values x,y and publish the product of the same. below is my code for my talker.py 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():
rospy.init_node("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.spin()
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)
random.seed()
while not rospy.is_shutdown():
msg = sensor()
msg.Area = msg.x * msg.y
pub.publish(msg)
rate.sleep()
if __name__ == '__main__':
try:
talker()
listener()
except rospy.ROSInterruptException:
pass
add a comment