def msg_pub(): rospy.init_node('message_publisher') pub = rospy.Publisher('complex',Complex,queue_size=10) rate = rospy.Rate(1) count = 0 while not rospy.is_shutdown(): msg = Complex() msg.real = random() msg.imaginary = random() rospy.loginfo("Complex number %d is %f + %fi", count, msg.real, msg.imaginary) pub.publish(msg) count +=1 rate.sleep()
def msg_pub(): rospy.init_node('message_publisher') pub = rospy.Publisher('complex',Complex,queue_size=10) rate = rospy.Rate(1) count = 0 while not rospy.is_shutdown(): msg = Complex() # Alternatively we can use keyword arguments to initialize the message class. E.g.: # 'msg = Complex(real=2.3, imaginary=4.6)' or 'msg = Complex(real=1.2)' or # even 'msg = Complex(2.3, 4.6)' msg.real = random() msg.imaginary = random() rospy.loginfo("Complex number %d is %f + %fi", count, msg.real, msg.imaginary) pub.publish(msg) count +=1 rate.sleep()
#!/usr/bin/env python import rospy from basics.msg import Complex from random import random rospy.init_node('topic_publisher') pub = rospy.Publisher('complex', Complex) rate = rospy.Rate(2) while not rospy.is_shutdown(): msg = Complex() msg.real = random() msg.imaginary = random() pub.publish(msg) rate.sleep()
#!/usr/bin/env python import rospy from basics.msg import Complex from random import random rospy.init_node('message_publisher') pub = rospy.Publisher('complex', Complex) # publisher, which calls Complex rate = rospy.Rate(2) while not rospy.is_shutdown(): msg = Complex() # calls message, Complex() msg.real = random() # when real, random msg.imaginary = random() # when imaginary, random pub.publish(msg) # publish rate.sleep()
def callback(msg): ret_msg = Complex() ret_msg.real = 2 * msg.real ret_msg.imaginary = 2 * msg.imaginary pub.publish(ret_msg)