粒子滤波PF在于机器人定位的应用(python实践)

2023-10-28 10:59

本文主要是介绍粒子滤波PF在于机器人定位的应用(python实践),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 引言:
    • 粒子滤波主要的四个步骤:
      • 初始化步骤
      • 预测步
      • 更新步
      • 重采样
    • 粒子滤波器VS卡尔曼滤波器
    • 粒子滤波的缺点
    • 概括
    • 实践:

引言:

粒子滤波是带有离散编号的粒子。每个粒子都是一个向量,其中包含 x 坐标、y 坐标和方向。粒子的生存取决于它们与传感器测量值的一致性。一致性是基于实际测量与预测测量之间的不匹配来衡量的,称为权重。权重是衡量粒子重要性的指标。权重越大,越重要。粒子的存活概率与其重量成正比。
在这里插入图片描述

粒子滤波主要的四个步骤:

  • 初始化步骤:在初始化步骤中,我们根据 GPS 输入估计我们的位置。
  • 预测步骤:在预测步骤中,我们为所有粒子添加控制输入(偏航率和速度)。
  • 更新步骤:在更新步骤中,我们使用地图地标位置和特征测量来更新我们的粒子权重。
  • 重采样步骤:在重采样期间,我们将重采样 M 次,绘制一个粒子 i,与它的权重成正比。在这一步使用重采样轮。

初始化步骤

初始化所有粒子。在这一步,我们必须决定要使用多少粒子。选择合适粒子数,太小容易出错;选择太多,计算成本很高。

预测步

为了预测车辆的位置,将使用以下公式通过基于速度和偏航率的更新来预测车辆在下一个时间步的位置。
在这里插入图片描述

更新步

下一步是根据地标的 LIDAR 和 RADAR 读数更新粒子权重。
更新步主要有三小步:

  • Transformation
  • Association
  • Update Weights

Transformation
代理的测量值需要从其定位坐标系转换为地图坐标系。
在这里插入图片描述
这里 Xm 和 Ym 是测量观测值的变换地图坐标。 Xp,Yp 是粒子,位置。 Xc, Yc 是观察点在代理的局部坐标系中的位置,theta 是通过齐次变换矩阵的旋转角度。
这个齐次变换矩阵,如下所示,执行旋转和平移。
在这里插入图片描述
矩阵乘法得到:
在这里插入图片描述
Association
关联是将地标测量与现实世界中的对象(例如地图地标)进行匹配的问题。由于我们对地标进行了多次测量,我们可以使用最近邻技术来找到正确的。
Update Weights
实际测量值与预测测量值的比值称为该粒子的重要性权重。随着预测的测量值接近实际测量值,权重越来越接近 1。因此,权重越大,粒子越有可能代表正确的车辆位置。

重采样

就涉及多少操作而言,随机抽取样本并不是最有效的重采样方式。为了使重采样更有效,我们使用了一种称为重采样轮的技术。
重采样是一种用于从旧粒子中随机抽取新粒子的技术,并按其重要性权重的比例进行替换。重新采样后,具有较高权重的粒子可能会保留下来,而所有其他粒子可能会消失。
为了进行重采样,使用了重采样轮技术。
在这里插入图片描述
因此,在重新采样后,我们的粒子将更接近汽车的实际位置,并且彼此之间的距离也更近,因此噪音会降低。

粒子滤波器VS卡尔曼滤波器

粒子滤波器可用于解决非高斯噪声问题,但通常比卡尔曼滤波器计算成本更高。这是因为粒子过滤器使用模拟方法而不是解析方程来解决估计任务。

粒子滤波的缺点

当样本量太小时,粒子可能会漂移,并且永远不会以粒子聚集在所需状态附近的方式收敛;
如果样本量太大,粒子就会相互塌陷,它看起来像一个大点,所有的粒子都太粘在一起了,这被巧妙地称为粒子坍缩;
机器人面临的环境越复杂,描述后验概率分布的样本就越多,算法也就越复杂;
此外,重采样阶段可能会导致样本有效性和多样性的丧失,从而导致样本枯竭。

概括

粒子过滤器的主要目的是根据来自实际汽车的测量值和来自虚拟汽车(粒子)的测量值生成每个粒子是实际汽车的后验概率,并且随着汽车继续移动,不相关的粒子将逐渐被过滤掉。
所以总的来说,粒子滤波的实现可以概括为3个步骤:
生成一组粒子;
测量每个粒子是实际汽车(机器人)的概率;
基于概率权重的重采样。

实践:

**注:**使用 pygame 可视化的基本机器人,pip install pygame

import pygame
import random
import time
from math import *
from copy import deepcopydisplay_width = 800
display_height = 800world_size = display_widthred = (200, 0, 0)
blue = (0, 0, 255)
green = (0, 155, 0)
yellow = (255, 255, 0)
white = (255, 255, 255)
black = (0, 0, 0)car_length = 60
car_width = 40car_img = pygame.image.load("car60_40.png")origin = (display_width/2, display_height/2)pygame.init()screen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Moving robot")
clock = pygame.time.Clock()screen.fill(white)class robot:def __init__(self):self.x = random.random()*world_sizeself.y = random.random()*world_sizeself.orientation = random.random() * 2.0 * piself.forward_noise = 0.0self.turn_noise	= 0.0self.sense_noise = 0.0 # or bearing_noisedef set(self, x, y, orientation):if x >= world_size or x < 0:raise ValueError("X coordinate out of bound")if y >= world_size or y < 0:raise ValueError("Y coordinate out of bound")if orientation >= 2*pi or orientation < 0:raise ValueError("Orientation must be in [0..2pi]")self.x = xself.y = yself.orientation = orientationdef set_noise(self, f_noise, t_noise, s_noise):self.forward_noise = f_noiseself.turn_noise = t_noiseself.sense_noise = s_noisedef move(self, turn, forward):if forward < 0:raise ValueError("Cant move backwards")self.orientation = self.orientation + turn + random.gauss(0.0, self.turn_noise)self.orientation %= 2*pidist = forward + random.gauss(0.0, self.forward_noise)self.x = self.x + dist*cos(self.orientation)self.y = self.y - dist*sin(self.orientation)self.x %= world_sizeself.y %= world_sizetemp = robot()temp.set_noise(0.001, 0.1, 0.1)temp.set(self.x, self.y, self.orientation)temp.set_noise(self.forward_noise, self.turn_noise, self.sense_noise)return tempdef sense(self, landmarks, add_noise=False):Z = []for i in range(len(landmarks)):bearing_angle = atan2(landmarks[i][0] - self.y, landmarks[i][1] - self.x) - self.orientationif add_noise:bearing_angle += random.gauss(0.0, self.bearing_noise)# avoid angles greater than 2pibearing_angle %= 2*piZ.append(bearing_angle)return Z # Return vector Z of 4 bearings.def measurement_prob(self, measurements, landmarks):# calculate the correct measurementpredicted_measurements = self.sense(landmarks) # compute errorserror = 1.0for i in range(len(measurements)):error_bearing = abs(measurements[i] - predicted_measurements[i])error_bearing = (error_bearing + pi) % (2.0 * pi) - pi # truncate# update Gaussianerror *= (exp(- (error_bearing ** 2) / (self.sense_noise ** 2) / 2.0) /  sqrt(2.0 * pi * (self.sense_noise ** 2)))return errordef Gaussian(self, mu, sigma, x):# calculates the probability of x for 1-dim Gaussian with mean mu and var. sigmareturn exp(- ((mu - x) ** 2) / (sigma ** 2) / 2.0) / sqrt(2.0 * pi * (sigma ** 2))def draw_robot(car):x = car.xy = car.yorientation = car.orientationimg = pygame.transform.rotate(car_img, orientation*180/pi)screen.blit(img, (x-30, y-20))# # in radians# print orientation# # in degrees# print orientation*180/pi# pygame.draw.circle(screen, blue, (int(x), int(y)), 5)# rect = pygame.draw.polygon(screen, blue, ((x-car_length/2,y-car_width/2),(x+car_length/2,y-car_width/2), \# 	(x + car_length/2, y + car_width/2), (x-car_length/2, y+car_width/2)))def draw_particles(particles):for i in range(len(particles)):x = particles[i].xy = particles[i].yorientation = particles[i].orientationpygame.draw.circle(screen, green, (int(x), int(y)), 5)landmarks_loc  = [[200, 200], [600, 600], [200, 600], [600, 200], [200, 300], [300, 200], [500, 200],\[200, 200], [200, 500], [300, 600],[500, 600], [600, 500], [600, 300], [400, 200],\[200, 400], [400, 600], [600, 400]]car = robot()
# car.set_noise(0.1, 0.1, 0.1)orientation = 0
#in radians
orientation = orientation*pi/180
car.set(origin[0], origin[1], orientation)exit = Falsedelta_orient = 0.0
delta_forward = 0.0particles = []
# create particles
for i in range(1000):particle = robot()# particle.orientation = orientationparticle.set_noise(0.001, 0.1, 0.1)particles.append(particle)while exit == False:screen.fill(white)pygame.draw.line(screen, green, (display_width/2, 0), (display_width/2, display_height), 1)pygame.draw.line(screen, black, (0, display_height/2), (display_width, display_height/2), 1)for i in range(len(landmarks_loc)):pygame.draw.circle(screen, blue, landmarks_loc[i], 20)draw_robot(car)draw_particles(particles)pygame.display.update()clock.tick(100)for event in pygame.event.get():if event.type == pygame.QUIT:exit = Trueif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT:delta_orient = 0.0175elif event.key == pygame.K_RIGHT:delta_orient = -0.0175elif event.key == pygame.K_UP:delta_forward = 2elif event.key == pygame.K_DOWN:delta_forward = -2elif event.type == pygame.KEYUP:if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or event.key == pygame.K_UP \or event.key == pygame.K_DOWN:delta_orient = 0.0delta_forward = 0.0car.move(delta_orient, delta_forward)particles2 = []for particle in particles:# print "before :",particle.x, particle.y, particle.orientation# particle.orientation = car.orientationparticles2.append(particle.move(delta_orient, delta_forward))# print "afer :",particle.x, particle.y, particle.orientationparticles = particles2measurements = car.sense(landmarks_loc)weights = []for i in range(1000):weights.append(particles[i].measurement_prob(measurements, landmarks_loc))# resamplingp = []index = int(random.random() * 1000)beta = 0.0mw = max(weights)for i in range(1000):beta += random.random() * 2.0 * mwwhile beta > weights[index]:beta -= weights[index]index = (index + 1) % 1000p.append(particles[index])particles = deepcopy(p)

这篇关于粒子滤波PF在于机器人定位的应用(python实践)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/292744

相关文章

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

golang内存对齐的项目实践

《golang内存对齐的项目实践》本文主要介绍了golang内存对齐的项目实践,内存对齐不仅有助于提高内存访问效率,还确保了与硬件接口的兼容性,是Go语言编程中不可忽视的重要优化手段,下面就来介绍一下... 目录一、结构体中的字段顺序与内存对齐二、内存对齐的原理与规则三、调整结构体字段顺序优化内存对齐四、内