Publisher and subscriber in one node (Python)
Hello, I new using ROS and I saw a lot of posts but I can't solve my problem.
I have a publisher (1) that sends a random number between 0 and 5 and the next node (2) needs to receive that value and multiply it by 10. After that, the same node (2) needs to send that information to another node. This is the first code
#!/usr/bin/env python
# encoding: utf-8
import rospy
import random
from std_msgs.msg import String
def nodo():
rospy.init_node('nodo_publisher')
pub = rospy.Publisher('example', String, queue_size=10)
rate = rospy.Rate(10)
while not rospy.is_shutdown():
ran = random.randint(0, 5)
mensaje = str(ran)
rospy.loginfo(mensaje)
pub.publish(mensaje)
rate.sleep()
if __name__ == '__main__':
try:
nodo()
except rospy.ROSInterruptException:
pass
And this is my attempt for the second code
#!/usr/bin/env python
# encoding: utf-8
import rospy
from std_msgs.msg import String
rospy.init_node("Mixer_Node")
pub = rospy.Publisher("echo", String)
def callback(mensaje):
mensaje1 = str(int(mensaje.data)*10
pub.publish(mensaje1)
# The message has 2 parts, a header and the data. We only want to use the actual data
sub=rospy.Subscriber("chatter", String, callback)