双向RRT*轨迹规划

这篇博客介绍了使用Python进行双向RRT*算法的轨迹规划,适用于智能机器人课设。通过修改参数可以显示动画,展示双向生成树的过程。代码以随机搜索为基础,实现了中等效果,且作者认为Python非常易学。
摘要由CSDN通过智能技术生成

智能机器人课设内容,所用语言为python

import copy
import math
import random
import time

import matplotlib.pyplot as plt
from scipy.spatial.transform import Rotation as Rot
import numpy as np
import matplotlib

matplotlib.use('TKAgg')

import matplotlib.pyplot as plt

from matplotlib.pyplot import plot,savefig
show_animation = False

class RRT:

    def __init__(self, obstacleList, randArea, expandDis=1.0, goalSampleRate=10, maxIter=200):
        self.start = None
        self.goal = None
        self.min_rand = randArea[0]
        self.max_rand = randArea[1]
        self.expand_dis = expandDis
        self.goal_sample_rate = goalSampleRate
        self.max_iter = maxIter
        self.obstacle_list = obstacleList
        self.node_list = None
        self.mode_list = None

    def rrt_star_planning(self, start, goal, animation=True):
        start_time = time.time()
        #初始化设置
        self.start = Node(start[0], start[1])
        self.goal = Node(goal[0], goal[1])
        self.node_list = [self.start]
        self.mode_list = [self.goal]
        path1 = None
        path2 = None
        lastPathLength = float('inf')  # 上一次路径
        lastPathLength1 = float('inf')  # 上一次路径
        lastPathLength2 = float('inf')  # 上一次路径

        for i in range(self.max_iter):
            rnd = self.sample()  # 随机取样
            nnr = self.sample()

            n_ind = self.get_nearest_list_index(self.node_list, rnd)  # 找到离当前点最近的节点
            m_ind = self.get_nearest_list_index(self.mode_list, nnr)  # 找到离当前点最近的节点

            nearestNode = self.node_list[n_ind]
            fairNode = self.mode_list[m_ind]  # 为终点也建立最近点

            # steer
            theta = math.atan2(rnd[1] - nearestNode.y, rnd[0] - nearestNode.x)
            beta = math.atan2(nnr[1] - fairNode.y, nnr[0] - fairNode.x)  # 计算随机取样的点和最近的点之间的关系
            newNode = self.get_new_node(theta, n_ind, nearestNode)
            oldNode = self.get_new_node(beta, m_ind, fairNode)  # 起点、终点同时拓展

            noCollision = self.check_segment_collision(newNode.x, newNode.y, nearestNode.x, nearestNode.y)
            nno = self.check_segment_collision(oldNode.x, oldNode.y, fairNode.x, fairNode.y)  # 判断从终点当前点到最近的点,是否能拓展

            if noCollision:
                nearInds = self.find_near_nodes(newNode)  # 从这里开始改
                newNode = self.choose_parent(newNode, nearInds)
                self.node_list.append(newNode)
                self.rewire(newNode, nearInds)
                for node in self.mode_list:
                    d = self.line_cost(node, newNode)
                    if d < 1.0:
                        if self.check_segment_collision(node.x, node.y,
                                                        newNode.x, newNode.y):
                            judgednode = node

                            lastIndex = len(self.node_list) - 1
                            FirstIndex = len(self.mode_list) - 1

                            tempPath1 = self.get_final_course(newNode, lastIndex)
                            tempPath2 = self.get_first_course(judgednode, FirstIndex)
                            tempPathLen1 = self.get_path_len(tempPath1)
                            tempPathLen2 = self.get_path_len(tempPath2)
                            tempPathLen = tempPathLen1 + tempPathLen2+ d

                            if lastPathLength > tempPathLen:
                                path1 = tempPath1
                                lastPathLength = tempPathLen

                                if animation:
                                    self.draw_graph(judgednode, newNode, path1, path2)
                                tempPathLen = tempPathLen1 + tempPathLen2 + d

                                print("current path length: {}, It costs {} s".format(tempPathLen,
                                                                                      time.time() - start_time))
                                if time.time() - start_time >20:
                                    break


            if nno:  # 从终点出发的生成树是否接近对面已存在的点
                nearImds = self.find_near_modes(oldNode)
                oldNode = self.hoose_parent(oldNode, nearImds)

                self.mode_list.append(oldNode)
                self.remire(oldNode, nearImds)

                for node in self.node_list:
                    d = self.line_cost(node, oldNode)
                    if d < 1.0:
                        if self.check_segment_collision(node.x, node.y,
                                                        oldNode.x, oldNode.y):
                            judgenode = node
                            lastIndex = len(self.node_list) - 1
                            FirstIndex = len(self.mode_list) - 1

                            tempPath1 = self.get_final_course(judgenode, lastIndex)
                            tempPath2 = self.get_first_course(oldNode, FirstIndex)
                            tempPathLen1 = self.get_path_len(tempPath1)
                            tempPathLen2 = self.get_path_len(tempPath2)
                            tempPathLen = tempPathLen1 + tempPathLen2+ d
                            plt.plot(oldNode.x, oldNode.y, '-k')

                            if lastPathLength > tempPathLen:
                                path2 = tempPath2
                                lastPathLength = tempPathLen
                                tempPathLen = tempPathLen1 + tempPathLen2 + d
                                if animation:
                                    self.draw_graph(judgenode, oldNode, path1, path2)
                                print("current path length: {}, It costs {} s".format(tempPathLen, time.time() - start_time))


        r
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值