Ich habe das Problem. Ich habe zwei Knoten, Knoten 1 und Knoten2, wobei Knoten1 eine Gleitkommazahl in Knoten2 sendet und Knoten2 eine Gleitkommazahl in Knoten1 sendet. Im Funktionsrückruf möchte ich die empfangene Information mit anderem Wert summieren und die Variable aktualisieren. Aber das Problem ist, dass ich es nicht schaffen, die Informationen gesendet werden, weil die Anschluss druckt in Ausgabe nur erste Update (für den Knoten 1 i 2.9 bekommen, und Knoten 2 2.0) Diese Code für node1 und Knoten2Aktualisieren Sie die globale Variable in Rospy
Node1
!/usr/bin/env python
import rospy
import time
from std_msgs.msg import Float64
global x1
global a
x1 = 1.5
def callback(msg):
#print 'Sto ricevendo informazioni da %s nel tempo %s' % (msg.data, time.ctime())
#print "%f"%msg.data
a = msg.data
info_nodo2 = a + 0.5
x1 = info_nodo2
print "%f"%x1
def nodo():
pub = rospy.Publisher('chatter1', Float64)
rospy.init_node('nodo1', anonymous=True)
rospy.loginfo("In attesa")
rospy.Subscriber('chatter2', Float64, callback)
rate = rospy.Rate(1) # 10hz
while not rospy.is_shutdown():
for i in range(1,51):
#rospy.loginfo(num)
pub.publish(x1)
rate.sleep()
rospy.spin()
if __name__ == '__main__':
try:
nodo()
except rospy.ROSInterruptException:
pass
Node2
import rospy
import time
from std_msgs.msg import Float64
global x2
global a
x2 = 2.4
def callback(msg):
#print 'Sto ricevendo informazioni da %s nel tempo %s' % (msg.data, time.ctime())
#print "%f"%msg.data
a = msg.data
info_nodo1 = a + 0.5
x2 = info_nodo1
print "%f"%x2
def nodo():
pub = rospy.Publisher('chatter2', Float64)
rospy.init_node('nodo2', anonymous=True)
rospy.loginfo("In attesa")
rospy.Subscriber('chatter1', Float64, callback)
rate = rospy.Rate(1) # 10hz
while not rospy.is_shutdown():
for i in range(1,51):
# num = "%s" % (x2)
#rospy.loginfo(num)
pub.publish(x2)
rate.sleep()
rospy.spin()
if __name__ == '__main__':
try:
nodo()
except rospy.ROSInterruptException:
pass