粒子滤波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内存优化的实战技巧分享

《Python内存优化的实战技巧分享》Python作为一门解释型语言,虽然在开发效率上有着显著优势,但在执行效率方面往往被诟病,然而,通过合理的内存优化策略,我们可以让Python程序的运行速度提升3... 目录前言python内存管理机制引用计数机制垃圾回收机制内存泄漏的常见原因1. 循环引用2. 全局变

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送

python 线程池顺序执行的方法实现

《python线程池顺序执行的方法实现》在Python中,线程池默认是并发执行任务的,但若需要实现任务的顺序执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录方案一:强制单线程(伪顺序执行)方案二:按提交顺序获取结果方案三:任务间依赖控制方案四:队列顺序消

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

Python WebSockets 库从基础到实战使用举例

《PythonWebSockets库从基础到实战使用举例》WebSocket是一种全双工、持久化的网络通信协议,适用于需要低延迟的应用,如实时聊天、股票行情推送、在线协作、多人游戏等,本文给大家介... 目录1. 引言2. 为什么使用 WebSocket?3. 安装 WebSockets 库4. 使用 We

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php