Update the global variable in Rospy

别来无恙 提交于 2019-12-06 16:31:06

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).

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:

Node1:

#!/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()

Node2:

#!/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

Hope this helps.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!