在 Rospy 中更新全局变量 [英] Update the global variable in Rospy

查看:94
本文介绍了在 Rospy 中更新全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我有两个节点,节点 1 和节点 2,其中节点 1 在节点 2 中发送浮点数,节点 2 在节点 1 中发送浮点数.在函数回调中,我想将收到的信息与其他值相加并更新变量.但问题是我没有成功发送信息,因为终端只在输出中打印第一次更新(对于节点 1,我得到 2.9,节点 2 得到 2.0)node1和node2的这些代码

i have the problem. I have two node, node 1 and node2, where node1 send a float number in node2 and node2 send a float number in node1. In the function callback, I want sum the information received with other value and update the variable. But the problem is that i don't succeed the information sent, because the terminal prints in output only first update(for node 1 i get 2.9, and node 2 2.0) These code for node1 and node2

<代码>

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

推荐答案

问题是您将 x1x2 声明为 global 变量.在每次迭代时,它们的值分别重置为 1.52.4.因此,发布的值是那些值加上 0.5(即 2.02.9).

The problem is you are declaring x1 and x2 as global variables. At every iteration their values get reset to 1.5 and 2.4, respectively. Thus, the published values are those values plus the 0.5 (i.e 2.0 and 2.9).

如果我理解正确,您希望两个节点不断更新彼此的值(对于 x1x2).我把你的节点写成类,并用实例变量替换了全局变量:

If I understand correctly, you want the two nodes to continuously update each others values (for x1 and x2). I wrote your nodes as classes instead and replaced the global variables by instance variables:

节点 1:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import rospy
import std_msgs.msg


class Nodo(object):
    def __init__(self):
        # Params
        self.x1 = 1.5
        self.a = None

        # Node cycle rate (in Hz).
        self.loop_rate = rospy.Rate(10)

        # Publishers
        self.pub = rospy.Publisher("~chatter1", std_msgs.msg.Float64, queue_size=10)

        # Subscribers
        rospy.Subscriber("~chatter2", std_msgs.msg.Float64, self.callback)

    def callback(self, msg):
        self.a = msg.data
        self.x1 = self.a + 0.5
        rospy.loginfo("x1: {}".format(self.x1))

    def start(self):
        rospy.loginfo("In attesa")

        while not rospy.is_shutdown():
            for ii in xrange(1, 51):
                self.pub.publish(self.x1)
                self.loop_rate.sleep()

if __name__ == '__main__':
    rospy.init_node("nodo1", anonymous=True)
    my_node = Nodo()
    my_node.start()

节点 2:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import rospy
import std_msgs.msg


class Nodo(object):
    def __init__(self):
        # Params
        self.x2 = 2.4
        self.a = None

        # Node cycle rate (in Hz).
        self.loop_rate = rospy.Rate(10)

        # Publishers
        self.pub = rospy.Publisher("~chatter2", std_msgs.msg.Float64, queue_size=10)

        # Subscribers
        rospy.Subscriber("~chatter1", std_msgs.msg.Float64, self.callback)

    def callback(self, msg):
        self.a = msg.data
        self.x2 = self.a + 0.5
        rospy.loginfo("x2: {}".format(self.x2))

    def start(self):
        rospy.loginfo("In attesa")

        while not rospy.is_shutdown():
            for ii in xrange(1, 51):
                self.pub.publish(self.x2)
                self.loop_rate.sleep()

if __name__ == '__main__':
    rospy.init_node("nodo2", anonymous=True)
    my_node = Nodo()
    my_node.start()

运行这两个节点时,控制台输出如下:

When running these two nodes the console output is as follows:

process[nodo_1-1]: started with pid [7688]
process[nodo_2-2]: started with pid [7689]
[INFO] [WallTime: 1478865725.627418] In attesa
[INFO] [WallTime: 1478865725.627904] In attesa
[INFO] [WallTime: 1478865725.725064] x2: 2.0
[INFO] [WallTime: 1478865725.725512] x1: 2.5
[INFO] [WallTime: 1478865725.825050] x2: 3.0
[INFO] [WallTime: 1478865725.825448] x1: 3.5
[INFO] [WallTime: 1478865725.925056] x2: 4.0
[INFO] [WallTime: 1478865725.925608] x1: 4.5
[INFO] [WallTime: 1478865726.025061] x2: 5.0
[INFO] [WallTime: 1478865726.025617] x1: 5.5
[INFO] [WallTime: 1478865726.125045] x2: 6.0
[INFO] [WallTime: 1478865726.125605] x1: 6.5
[INFO] [WallTime: 1478865726.225033] x2: 7.0
[INFO] [WallTime: 1478865726.225586] x1: 7.5
[INFO] [WallTime: 1478865726.325013] x2: 8.0
[INFO] [WallTime: 1478865726.325606] x1: 8.5
[INFO] [WallTime: 1478865726.425041] x2: 9.0
[INFO] [WallTime: 1478865726.425608] x1: 9.5
[INFO] [WallTime: 1478865726.525057] x2: 10.0
[INFO] [WallTime: 1478865726.525545] x1: 10.5
[INFO] [WallTime: 1478865726.625054] x2: 11.0

希望这会有所帮助.

这篇关于在 Rospy 中更新全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆