导航与定位:室外导航技术_(7).室外导航算法与数据处理
室外导航算法与数据处理

在室外导航技术领域中,算法与数据处理被视为实现精确导航与定位的关键手段。本节将深入探讨多种主流的室外导航算法及其相关数据处理技术。具体而言,我们将详细阐述基于全球定位系统(GPS)的技术特点及其应用效果;结合图像处理的方法来提升导航精度;通过传感器融合提升导航系统的整体性能;并采用路径规划优化运动效率等技术手段。在分析这些核心技术时,我们不仅会探讨其理论基础与工作原理,还会结合实际案例来演示其应用过程以帮助理解
1. 基于GPS的导航算法
全球定位系统(GPS)是室外导航的主要应用之一。该系统通过接收卫星发射的信号并解析其中包含的位置数据来确定用户的地理位置信息。然而,在实际应用中可能会受到信号干扰、多路径效应等因素的影响而导致定位精度下降。因此,在保证定位准确性的同时需要结合一些优化算法进行辅助处理以提高整体性能。
1.1 GPS信号处理
GPS信号处理主要涉及三个关键环节:信号捕获、追踪以及解码。在进行信号捕获时,系统会扫描接收天线周围的电磁波频谱,并识别出目标卫星发射的独特代码序列。随后进行的追踪过程旨在精确测定各颗卫星发送的实时频率偏移及伪距编码特征。最后一步是解析接收的数据流以提取关于这些卫星运行状态及发射机工作参数的关键导航参数。
1.1.1 信号捕获
该过程通过使用相关器得以实现。借助这些设备能够测量接收信号与内部生成伪随机噪声(PRN)码的相关性,并据此识别卫星信号的存在及其频率与码相位。
import numpy as np
def capture_signal(received_signal, prn_code, frequency_bins, code_bins):
"""
信号捕获函数
:param received_signal: 接收到的信号
:param prn_code: 伪随机噪声码
:param frequency_bins: 频率搜索范围
:param code_bins: 码相位搜索范围
:return: 捕获结果,包括频率和码相位
"""
max_correlation = 0
best_frequency = 0
best_code_phase = 0
for frequency in frequency_bins:
for code_phase in code_bins:
# 生成本地PRN码
local_code = np.roll(prn_code, code_phase)
# 计算相关性
correlation = np.sum(received_signal * local_code)
if correlation > max_correlation:
max_correlation = correlation
best_frequency = frequency
best_code_phase = code_phase
return best_frequency, best_code_phase
# 示例数据
received_signal = np.random.randn(1000)
prn_code = np.random.randint(0, 2, 1000) * 2 - 1
frequency_bins = np.arange(-1000, 1000, 10)
code_bins = np.arange(0, 1000, 10)
# 捕获信号
best_frequency, best_code_phase = capture_signal(received_signal, prn_code, frequency_bins, code_bins)
print(f"最佳频率: {best_frequency}, 最佳码相位: {best_code_phase}")
1.2 信号跟踪
信号跟踪主要依靠锁相装置(PLL)和码捕获器(DLL)来进行。信号的载波相位由锁相环进行追踪,而伪随机噪声码相位则由 DLL 进行追踪。两个回路协同工作以确保对卫星信号持续追踪,并最终使定位精度得到显著提升。
1.2.1 锁相环(PLL)
锁相环通过检测输入信号与内部生成载波之间的相位偏差值来校正内部载波频率和相位参数以实现与输入信号相一致的目标
import numpy as np
class PLL:
def __init__(self, loop_bandwidth, noise_bandwidth):
self.loop_bandwidth = loop_bandwidth
self.noise_bandwidth = noise_bandwidth
self.phase_error = 0
self.frequency_error = 0
def update(self, received_signal, local_signal):
"""
更新锁相环状态
:param received_signal: 接收到的信号
:param local_signal: 本地生成的信号
:return: 调整后的本地信号
"""
# 计算相位误差
self.phase_error = np.angle(received_signal * np.conj(local_signal))
# 计算频率误差
self.frequency_error = self.phase_error * self.loop_bandwidth
# 调整本地信号的频率和相位
local_signal = local_signal * np.exp(1j * self.phase_error * self.noise_bandwidth)
return local_signal
# 示例数据
received_signal = np.exp(1j * np.linspace(0, 2 * np.pi, 1000))
local_signal = np.exp(1j * np.linspace(0, 2 * np.pi, 1000))
pll = PLL(0.1, 0.01)
# 跟踪信号
tracked_signal = pll.update(received_signal, local_signal)
print(f"跟踪后的信号: {tracked_signal[:10]}")
1.2.2 码环(DLL)
码环通过对比接收的信号源与生成器生成伪随机噪声码中的phase error of the code,并校正本地码相位以使该相位与接收的信号源保持一致。
import numpy as np
class DLL:
def __init__(self, loop_bandwidth, noise_bandwidth):
self.loop_bandwidth = loop_bandwidth
self.noise_bandwidth = noise_bandwidth
self.code_phase_error = 0
def update(self, received_signal, local_code):
"""
更新码环状态
:param received_signal: 接收到的信号
:param local_code: 本地生成的伪随机噪声码
:return: 调整后的本地伪随机噪声码
"""
# 计算码相位误差
self.code_phase_error = np.sum(received_signal * local_code) - np.sum(received_signal * np.roll(local_code, 1))
# 调整本地码相位
local_code = np.roll(local_code, int(self.code_phase_error * self.noise_bandwidth))
return local_code
# 示例数据
received_signal = np.random.randn(1000)
local_code = np.random.randint(0, 2, 1000) * 2 - 1
dll = DLL(0.1, 0.01)
# 跟踪信号
tracked_code = dll.update(received_signal, local_code)
print(f"跟踪后的码相位: {tracked_code[:10]}")
1.3 解码导航信息
导航系统的解码过程是基于收集到的信号中提取卫星星历数据与时间参数。这些参数可用于计算用户的精确位置。
import numpy as np
def decode_navigation_data(received_signal, tracked_code):
"""
解码导航信息
:param received_signal: 接收到的信号
:param tracked_code: 跟踪到的伪随机噪声码
:return: 导航信息
"""
# 从信号中提取导航信息
navigation_data = received_signal * tracked_code
# 去除噪声
navigation_data = np.fft.fft(navigation_data)
navigation_data = np.fft.ifft(navigation_data)
return navigation_data
# 示例数据
received_signal = np.random.randn(1000)
tracked_code = np.random.randint(0, 2, 1000) * 2 - 1
# 解码导航信息
navigation_data = decode_navigation_data(received_signal, tracked_code)
print(f"解码后的导航信息: {navigation_data[:10]}")
2. 图像处理技术
在定位与导航中的户外环境中,图像处理技术可用于识别路标和其他建筑等关键环境特征。常见的几种包括路径检测算法能够识别出道路标识符,并通过几何分析来确定位置信息;基于视觉匹配的方法能够准确辨别不同场景下的相似物体;以及拼接多幅图象以构建完整的地理信息模型。
2.1 特征检测
主要通过计算图像中的关键点来识别环境特征。在实际应用中,SIFT,SURF和ORB是最常用的几种算法。
2.1.1 SIFT特征检测
基于SIFT(尺度不变特征变换)的方法是针对尺度和旋转不变的特征检测而设计的一种算法。该方法利用SIFT(尺度不变特征变换)技术能够有效地识别图像中的关键点并生成相应的描述子。
import cv2
def detect_sift_features(image):
"""
使用SIFT检测图像特征
:param image: 输入图像
:return: 关键点和描述子
"""
# 初始化SIFT检测器
sift = cv2.SIFT_create()
# 检测关键点和提取描述子
keypoints, descriptors = sift.detectAndCompute(image, None)
return keypoints, descriptors
# 示例图像
image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)
keypoints, descriptors = detect_sift_features(image)
# 绘制关键点
image_with_keypoints = cv2.drawKeypoints(image, keypoints, None)
cv2.imshow('SIFT Features', image_with_keypoints)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.2 特征匹配
特征匹配旨在通过识别两幅图像中的对应关系来定位关键对应点的过程,在实际应用中通常会采用多种算法结合以提高匹配效率与准确性。其中较为常见的方法包括基于平面搜索的数据结构(FLANN)以及基于暴力搜索的快速比对器(BFMatcher)等技术方案。
2.2.1 FLANN特征匹配
该库(全称为Fast Library for Approximate Nearest Neighbors, FLANN)被用作一种高性能的特征匹配算法。通过该库可以有效地识别出两幅图像中的成对的关键点。
import cv2
def match_sift_features(descriptors1, descriptors2):
"""
使用FLANN匹配SIFT特征
:param descriptors1: 第一幅图像的描述子
:param descriptors2: 第二幅图像的描述子
:return: 匹配的关键点对
"""
# 初始化FLANN匹配器
flann = cv2.FlannBasedMatcher_create()
# 执行匹配
matches = flann.knnMatch(descriptors1, descriptors2, k=2)
# 筛选匹配点
good_matches = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good_matches.append(m)
return good_matches
# 示例图像
image1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)
keypoints1, descriptors1 = detect_sift_features(image1)
keypoints2, descriptors2 = detect_sift_features(image2)
# 匹配特征
good_matches = match_sift_features(descriptors1, descriptors2)
# 绘制匹配结果
match_image = cv2.drawMatches(image1, keypoints1, image2, keypoints2, good_matches, None)
cv2.imshow('Feature Matching', match_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.3 图像拼接
图像拼接主要通过融合多幅图像形成一个完整的全景图以提供更为丰富的环境信息。常用的图像拼接算法主要有RANSAC与Homography等方法。
2.3.1 RANSAC算法
该算法通过基于关键点对进行抽样一致性估计的方式,在大量噪声数据中准确地识别出一致的点对集合,并计算出相应的单应性矩阵(Homography),进而完成图像拼接。
import cv2
import numpy as np
def ransac_homography(keypoints1, keypoints2, good_matches, threshold=5.0):
"""
使用RANSAC算法计算单应性矩阵
:param keypoints1: 第一幅图像的关键点
:param keypoints2: 第二幅图像的关键点
:param good_matches: 匹配的关键点对
:param threshold: 阈值
:return: 单应性矩阵
"""
# 提取匹配点的坐标
src_pts = np.float32([keypoints1[m.queryIdx].pt for m in good_matches])
dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in good_matches])
# 使用RANSAC计算单应性矩阵
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, threshold)
return H, mask
# 示例图像
image1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)
keypoints1, descriptors1 = detect_sift_features(image1)
keypoints2, descriptors2 = detect_sift_features(image2)
good_matches = match_sift_features(descriptors1, descriptors2)
# 计算单应性矩阵
H, mask = ransac_homography(keypoints1, keypoints2, good_matches)
# 拼接图像
height1, width1 = image1.shape
height2, width2 = image2.shape
warp_matrix = np.array([[1, 0, width1], [0, 1, 0]])
warp_image = cv2.warpPerspective(image2, H, (width1 + width2, max(height1, height2)))
warp_image[0:height1, 0:width1] = image1
cv2.imshow('Panorama', warp_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 传感器融合技术
基于多源传感器数据融合的技术显著提升了户外导航的精确度与可靠性。其中涉及的主要设备包括GPS定位系统、惯性测量单元(IMU)、激光雷达(LiDAR)等技术手段。
3.1 GPS与IMU融合
GPS和IMU的数据被该卡尔曼滤波算法融合。该算法能够整合精确的位置数据与运动状态数据,并能提供更为精准的定位结果。
3.1.1 卡尔曼滤波器
卡尔曼滤波器是一种基于反馈机制的迭代优化算法,在状态估计领域具有重要地位。它旨在通过动态更新的方式实现对系统状态的最佳线性无偏估计,并且能够在噪声存在的情况下有效工作。在实际应用中,通过结合GPS和IMU等多源传感器数据的实时处理能力,在提升系统精度的同时实现了对复杂运动环境的有效适应
import numpy as np
import matplotlib.pyplot as plt
class KalmanFilter:
def __init__(self, Q, R, P, F, H):
self.Q = Q # 状态转移矩阵的噪声
self.R = R # 观测矩阵的噪声
self.P = P # 估计误差协方差矩阵
self.F = F # 状态转移矩阵
self.H = H # 观测矩阵
def predict(self, state):
"""
预测步骤
:param state: 当前状态
:return: 预测状态
"""
# 预测状态
predicted_state = self.F @ state
# 预测误差协方差
self.P = self.F @ self.P @ self.F.T + self.Q
return predicted_state
def update(self, predicted_state, measurement):
"""
更新步骤
:param predicted_state: 预测状态
:param measurement: 观测值
:return: 更新后的状态
"""
# 计算卡尔曼增益
K = self.P @ self.H.T @ np.linalg.inv(self.H @ self.P @ self.H.T + self.R)
# 更新状态
updated_state = predicted_state + K @ (measurement - self.H @ predicted_state)
# 更新误差协方差
self.P = (np.eye(len(self.P)) - K @ self.H) @ self.P
return updated_state
# 示例数据
gps_data = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) # GPS数据
imu_data = np.array([0.9, 2.1, 3.1, 3.9, 5.1]) # IMU数据
# 初始化卡尔曼滤波器
Q = np.array([[1, 0], [0, 1]]) # 状态转移噪声
R = np.array([[1, 0], [0, 1]]) # 观测噪声
P = np.array([[1, 0], [0, 1]]) # 估计误差协方差
F = np.array([[1, 1], [0, 1]]) # 状态转移矩阵
H = np.array([[1, 0]]) # 观测矩阵
kalman = KalmanFilter(Q, R, P, F, H)
# 融合数据
state = np.array([0, 0])
kalman_states = []
for gps, imu in zip(gps_data, imu_data):
# 预测步骤
predicted_state = kalman.predict(state)
# 更新步骤
measurement = np.array([gps, imu])
state = kalman.update(predicted_state, measurement)
kalman_states.append(state[0])
# 绘制结果
plt.plot(gps_data, label='GPS Data')
plt.plot(imu_data, label='IMU Data')
plt.plot(kalman_states, label='Fused Data')
plt.legend()
plt.show()
3.2 GPS与LiDAR融合
GPS和LiDAR的观测信息可通过粒子滤波器(Particle Filter)实现融合。粒子滤波器作为采用蒙特卡洛方法的一种重要技术,在处理非线性与非高斯特性方面具有显著优势,并特别适合应用于复杂动态系统的状态估计问题。
3.2.1 粒子滤波器
粒子滤波器通过模拟系统状态生成多个粒子,并基于观测数据对各粒子的权重进行调整,从而对系统的行为进行估计。
import numpy as np
import matplotlib.pyplot as plt
class ParticleFilter:
def __init__(self, num_particles, initial_state, std_dev):
self.num_particles = num_particles
self.particles = np.random.normal(initial_state, std_dev, (num_particles, 2))
self.weights = np.ones(num_particles) / num_particles
def predict(self, motion_model, motion_noise):
"""
预测步骤
:param motion_model: 运动模型
:param motion_noise: 运动噪声
"""
self.particles = motion_model(self.particles) + np.random.normal(0, motion_noise, self.particles.shape)
def update(self, measurement, measurement_model, measurement_noise):
"""
更新步骤
:param measurement: 观测值
:param measurement_model: 观测模型
:param measurement_noise: 观测噪声
"""
for i in range(self.num_particles):
error = measurement - measurement_model(self.particles[i])
self.weights[i] *= np.exp(-0.5 * (error / measurement_noise) ** 2)
self.weights /= np.sum(self.weights)
def resample(self):
"""
重新采样步骤
"""
indices = np.random.choice(np.arange(self.num_particles), self.num_particles, p=self.weights)
self.particles = self.particles[indices]
self.weights = np.ones(self.num_particles) / num_particles
def estimate(self):
"""
估计状态
:return: 估计的系统状态
"""
return np.sum(self.particles * self.weights[:, np.newaxis], axis=0)
# 示例数据
gps_data = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) # GPS数据
lidar_data = np.array([1.1, 2.1, 3.1, 4.1, 5.1]) # LiDAR数据
# 初始化粒子滤波器
num_particles = 1000
initial_state = np.array([0, 0])
std_dev = 1.0
particle_filter = ParticleFilter(num_particles, initial_state, std_dev)
# 融合数据
state = initial_state
particle_states = []
def motion_model(particles):
"""
运动模型
:param particles: 当前粒子
:return: 预测粒子
"""
return particles + 0.1
def measurement_model(particle):
"""
观测模型
:param particle: 当前粒子
:return: 观测值
"""
return particle[0]
for gps, lidar in zip(gps_data, lidar_data):
# 预测步骤
particle_filter.predict(motion_model, 0.1)
# 更新步骤
measurement = np.array([gps, lidar])
particle_filter.update(measurement, measurement_model, 0.1)
# 重新采样步骤
particle_filter.resample()
# 估计状态
state = particle_filter.estimate()
particle_states.append(state[0])
# 绘制结果
plt.plot(gps_data, label='GPS Data')
plt.plot(lidar_data, label='LiDAR Data')
plt.plot(particle_states, label='Fused Data')
plt.legend()
plt.show()
4. 路径规划算法
该类算法旨在通过环境中的分析来识别最佳路径方案,并最终实现导航目的。常见的这类方法包括 A* 算法(以其高效的最短路径搜索著称)、Dijkstra 算法(适用于无权重图的最短路径计算)以及 RRT 方法(具有随机化扩展性的动态规划技术)。
4.1 A*算法
A 算法是一种 heuristic 搜索算法,在估算从当前节点至目标节点所需代价后可寻找到最佳路线。A 算法特别适用于明确起始点和目标点的路径规划问题。
4.1.1 A*算法实现
import heapq
class Node:
def __init__(self, x, y, g, h, parent=None):
self.x = x
self.y = y
self.g = g # 从起点到当前节点的实际代价
self.h = h # 从当前节点到目标节点的估计代价
self.f = g + h # 总代价
self.parent = parent
def __lt__(self, other):
return self.f < other.f
def heuristic(a, b):
"""
启发式函数,计算两个节点之间的曼哈顿距离
:param a: 节点a
:param b: 节点b
:return: 曼哈顿距离
"""
return abs(a.x - b.x) + abs(a.y - b.y)
def a_star(start, goal, grid):
"""
A*算法实现
:param start: 起点
:param goal: 终点
:param grid: 地图网格
:return: 最优路径
"""
open_list = []
closed_list = set()
start_node = Node(start[0], start[1], 0, heuristic(start, goal))
heapq.heappush(open_list, start_node)
goal_node = Node(goal[0], goal[1], 0, 0)
while open_list:
current_node = heapq.heappop(open_list)
if (current_node.x, current_node.y) in closed_list:
continue
closed_list.add((current_node.x, current_node.y))
if (current_node.x, current_node.y) == (goal_node.x, goal_node.y):
path = []
while current_node.parent:
path.append((current_node.x, current_node.y))
current_node = current_node.parent
path.append((start_node.x, start_node.y))
return path[::-1]
for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
x, y = current_node.x + dx, current_node.y + dy
if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == 0 and (x, y) not in closed_list:
child_node = Node(x, y, current_node.g + 1, heuristic((x, y), goal), current_node)
heapq.heappush(open_list, child_node)
return None
# 示例地图
grid = [
[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0]
]
start = (0, 0)
goal = (4, 4)
# 执行A*算法
path = a_star(start, goal, grid)
print(f"最优路径: {path}")
# 绘制地图和路径
plt.imshow(grid, cmap='Greys', interpolation='nearest')
if path:
path_x, path_y = zip(*path)
plt.plot(path_y, path_x, 'r')
plt.scatter(start[1], start[0], c='b', marker='o')
plt.scatter(goal[1], goal[0], c='g', marker='x')
plt.show()
4.2 Dijkstra算法
Dijkstra算法基于经典理论构建了最短路径模型,在计算过程中能够自动识别并处理复杂的网络拓扑关系;该算法特别适用于无负权值的情况,在这种情况下能够保证计算出全局最优解
4.2.1 Dijkstra算法实现
import heapq
def dijkstra(start, goal, grid):
"""
Dijkstra算法实现
:param start: 起点
:param goal: 终点
:param grid: 地图网格
:return: 最短路径
"""
open_list = []
closed_list = set()
heapq.heappush(open_list, (0, start))
parents = {start: None}
costs = {start: 0}
while open_list:
current_cost, current_node = heapq.heappop(open_list)
if current_node in closed_list:
continue
closed_list.add(current_node)
if current_node == goal:
path = []
while current_node:
path.append(current_node)
current_node = parents[current_node]
return path[::-1]
for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
x, y = current_node[0] + dx, current_node[1] + dy
if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == 0 and (x, y) not in closed_list:
new_cost = current_cost + 1
if (x, y) not in costs or new_cost < costs[(x, y)]:
costs[(x, y)] = new_cost
parents[(x, y)] = current_node
heapq.heappush(open_list, (new_cost, (x, y)))
return None
# 示例地图
grid = [
[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0]
]
start = (0, 0)
goal = (4, 4)
# 执行Dijkstra算法
path = dijkstra(start, goal, grid)
print(f"最短路径: {path}")
# 绘制地图和路径
plt.imshow(grid, cmap='Greys', interpolation='nearest')
if path:
path_x, path_y = zip(*path)
plt.plot(path_y, path_x, 'r')
plt.scatter(start[1], start[0], c='b', marker='o')
plt.scatter(goal[1], goal[0], c='g', marker='x')
plt.show()
4.3 RRT(快速随机树)算法
RRT算法是一种在高维空间与复杂环境中都能应用的路径规划方案。该算法通过随机采样与树状结构扩展,在探索环境中寻找连接起始点至目标点的有效路径。
4.3.1 RRT算法实现
import random
import matplotlib.pyplot as plt
class RRT:
def __init__(self, start, goal, grid, step_size=1, max_iter=1000):
self.start = start
self.goal = goal
self.grid = grid
self.step_size = step_size
self.max_iter = max_iter
self.tree = {start: None}
def is_free(self, x, y):
"""
检查节点是否在自由空间内
:param x: 节点x坐标
:param y: 节点y坐标
:return: 布尔值
"""
return 0 <= x < len(self.grid) and 0 <= y < len(self.grid[0]) and self.grid[x][y] == 0
def get_nearest_node(self, random_node):
"""
找到离随机节点最近的树节点
:param random_node: 随机节点
:return: 最近的树节点
"""
nearest_node = None
min_distance = float('inf')
for node in self.tree:
distance = np.sqrt((node[0] - random_node[0]) ** 2 + (node[1] - random_node[1]) ** 2)
if distance < min_distance:
min_distance = distance
nearest_node = node
return nearest_node
def extend_tree(self, nearest_node, random_node):
"""
扩展树
:param nearest_node: 最近的树节点
:param random_node: 随机节点
:return: 新节点
"""
direction = np.array(random_node) - np.array(nearest_node)
direction = direction / np.linalg.norm(direction)
new_node = tuple(np.array(nearest_node) + self.step_size * direction)
if self.is_free(new_node[0], new_node[1]):
self.tree[new_node] = nearest_node
return new_node
return None
def find_path(self):
"""
查找路径
:return: 从起点到终点的路径
"""
for _ in range(self.max_iter):
if random.random() < 0.5:
random_node = (random.randint(0, len(self.grid) - 1), random.randint(0, len(self.grid[0]) - 1))
else:
random_node = self.goal
nearest_node = self.get_nearest_node(random_node)
new_node = self.extend_tree(nearest_node, random_node)
if new_node and np.linalg.norm(np.array(new_node) - np.array(self.goal)) < self.step_size:
self.tree[self.goal] = new_node
path = [self.goal]
while path[0] is not None:
path.insert(0, self.tree[path[0]])
return path
return None
# 示例地图
grid = [
[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0]
]
start = (0, 0)
goal = (4, 4)
# 执行RRT算法
rrt = RRT(start, goal, grid, step_size=0.5, max_iter=10000)
path = rrt.find_path()
print(f"找到的路径: {path}")
# 绘制地图和路径
plt.imshow(grid, cmap='Greys', interpolation='nearest')
if path:
path_x, path_y = zip(*path)
plt.plot(path_y, path_x, 'r')
plt.scatter(start[1], start[0], c='b', marker='o')
plt.scatter(goal[1], goal[0], c='g', marker='x')
plt.show()
