【问题标题】:ROS how to use values from one callback function to anotherROS如何使用从一个回调函数到另一个回调函数的值
【发布时间】:2021-05-20 17:53:27
【问题描述】:

抱歉,英语不是我的母语。希望你能正确理解我的问题。

我正在使用带有 ROS Melodic 的 Ubuntu 18.04。我正在尝试从 mapCallbackodometryCallback 函数中获取数据,并在 SampleTree 函数中使用这些值。当我运行下面的代码时,我想返回 frontCones 数组,但我的输出是空数组。如何使SampleTree 函数获取在mapCallbackodometryCallback 函数中发布的值,并在循环中使用这些数据返回frontCones 数组?

这是我的代码:

# !/usr/bin/python

import rospy

from nav_msgs.msg import Odometry
from egn_messages.msg import Map
from tf.transformations import euler_from_quaternion

odometry = Odometry()
map = Map()


class Test:
    def __init__(self):
        rospy.init_node('test_node')

        rospy.Subscriber('/map1', Map, self.mapCallback)
        rospy.Subscriber('/odometry', Odometry, self.odometryCallback)
        self.map = []

        self.carPosX = 0.0
        self.carPosY = 0.0
        self.carPosYaw = 0.0

    def odometryCallback(self, odometry):
        orientation_q = odometry.pose.pose.orientation
        orientation_list = [orientation_q.x, orientation_q.y,
                            orientation_q.z, orientation_q.w]
        (roll, pitch, yaw) = euler_from_quaternion(orientation_list)
        self.carPosX = odometry.pose.pose.position.x
        self.carPosY = odometry.pose.pose.position.y
        self.carPosYaw = yaw

    def mapCallback(self, map):
        self.map = map

    def SampleTree(self):
        if not self.map:
            print
            'map is empty, return'
            return

        frontConesDist = 12
        frontCones = self.getFrontConeObstacles(frontConesDist)
        print(frontCones)

    def getFrontConeObstacles(self, frontDist):
        if not map:
            return []

        headingVector = [1.0, 0]

        behindDist = 0.5
        carPosBehindPoint = [self.carPosX - behindDist * headingVector[0], self.carPosY - behindDist * headingVector[1]]

        frontDistSq = frontDist ** 2

        frontConeList = []
        for cone in map.cones:
            if (headingVectorOrt[0] * (cone.y - carPosBehindPoint[1]) - headingVectorOrt[1] * (
                    cone.x - carPosBehindPoint[0])) < 0:
                if ((cone.x - self.carPosX) ** 2 + (cone.y - self.carPosY) ** 2) < frontDistSq:
                    frontConeList.append(cone)
        return frontConeList


if __name__ == "__main__":
    inst = Test()
    inst.SampleTree()
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")

【问题讨论】:

  • 不清楚您要做什么,因为您根本没有调用 SampleTree 方法。但总的来说。你可能想用自己的回调方法添加rospy.Timer,并检查你是否在那里收到了地图和里程计消息。
  • 只是一个粗略的想法:您可以尝试在回调中设置一个 bool 变量,以记录它们何时被调用并获得 vail 数据。然后仅在这些为真时才调用 SampleTree 并在之后重置。
  • 很抱歉我的问题不正确。事实是这些行只是一段代码,我用一个错误的例子来描述我的问题。我已经修复了我的代码和问题以使其更清晰。实际上 SampleTree 函数本身是在一个单独的 .py 文件中调用的。

标签: python ros subscriber rospy


【解决方案1】:

通过在回调函数中打印新的地图信息来确保 self.map 中的值实际上正在更新。

同时添加:

if __name__ == "__main__"():
    inst = Test()
    inst.SampleTree()
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")

rospy.spin() 将无限循环执行代码,除非被中断。

建议:

你可以给函数命名

def getFrontConeObstacles(self, frontDist):

而不是

def getFrontConeObstacles(self, map, frontDist):

因为您可以在课堂上轻松访问 self.map。

【讨论】:

  • 我再次稍微更改了这个问题中的代码,以消除一些错误并遵循您的建议,但 SampleTree 函数仍然不会为 getFrontConeObstacles 函数获取已发布的数据。 (输出:地图为空,返回)。我正在经营两个出版商。其中之一发送包含 float64 x 和 float64 y 作为 Map 的自定义数组。此外,我将 Odometry 消息作为第二个发布者发送,然后我使用上面提供的代码运行订阅者。我做错了什么?
  • 在终端中运行 $ rostopic echo /map1 时是否从地图中获取输出?
  • 是的,我看到所有发布到 /map1 和 /odometry 的消息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 2011-04-14
相关资源
最近更新 更多