class Node:
    def __init__(self, x, y, cost, parent_index):
        self.x = x  # index of grid
        self.y = y
        self.cost = cost  # 计算 g(n) 的值
        self.parent_index = parent_index  # 父节点的索引

    def __str__(self):
        return str(self.x) + ',' + str(self.y) + ',' + str(self.cost) + ',' + str(self.parent_index)  # 返回的字符串


    # 经过 planning 函数处理,传入 sx, sy, gx, gy, 返回 pathx, pathy(最终的路径)
def planning(self, sx, sy, gx, gy):

        nstart = self.Node(self.calc_xyindex(sx, self.minx),  # ([(-5) - (-10)]/2 = 2.5 取整
                           self.calc_xyindex(sy, self.miny),  # 2 (2.5)
                           0.0,
                           -1)  # 父节点随机初始化为 1
        print(nstart)
        ngoal = self.Node(self.calc_xyindex(gx, self.minx),
                          self.calc_xyindex(gy, self.miny),
                          0.0,
                          -1)

        def calc_angle(self, n3, n4):

            l = n3.x - n4.x
            m = n3.y - n4.y
            angle = math.degrees(math.atan2(l, m))
            return angle

        def get_motion_model(self, ngoal):
            # 在 get_motion_model 中可以访问 ngoal
            current =  # 根据需要定义 current
            angle = self.calc_angle(ngoal, current)
            return angle

        # 调用 get_motion_model,并传递 ngoal
        angles = self.get_motion_model(ngoal)

要在 get_motion_model 方法中获取 planning 方法中的 ngoal,可以将 ngoal 作为参数传递给 get_motion_model 方法。然后,您可以在调用 get_motion_model 方法时将 ngoal 作为参数传递给它。

以下是示例代码:

class MyClass:
    def __init__(self):
        pass

    def planning(self, sx, sy, gx, gy):
        nstart = self.Node(self.calc_xyindex(sx, self.minx),
                           self.calc_xyindex(sy, self.miny),
                           0.0,
                           -1)
        ngoal = self.Node(self.calc_xyindex(gx, self.minx),
                          self.calc_xyindex(gy, self.miny),
                          0.0,
                          -1)

        # 调用 get_motion_model,并传递 ngoal
        angles = self.get_motion_model(ngoal)

    def calc_angle(self, n3, n4):
        l = n3.x - n4.x
        m = n3.y - n4.y
        angle = math.degrees(math.atan2(l, m))
        return angle

    def get_motion_model(self, ngoal):
        # 在 get_motion_model 中可以访问 ngoal
        current =  # 根据需要定义 current
        angle = self.calc_angle(ngoal, current)
        return angle

在上述示例中,将 get_motion_model 方法的定义修改为接受 ngoal 参数。然后,在 planning 方法中调用 get_motion_model 时,将 ngoal 作为参数传递给它。

通过这种方式,get_motion_model 方法就可以访问并使用 planning 方法中的 ngoal。根据具体的需求,您可以在调用 get_motion_model 方法时传递适当的参数。

Python Node 类和规划算法示例

原文地址: https://www.cveoy.top/t/topic/bWV9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录