Python--粒子滤波定位案例程序
生活随笔
收集整理的這篇文章主要介紹了
Python--粒子滤波定位案例程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、參考代碼
# ------------------------------------------------------------------------ # coding=utf-8 # ------------------------------------------------------------------------ # # Created by Martin J. Laubach on 2011-11-15 # # ------------------------------------------------------------------------import math import turtle import randomturtle.tracer(50000, delay=0) turtle.register_shape("dot", ((-3,-3), (-3,3), (3,3), (3,-3))) turtle.register_shape("tri", ((-3, -2), (0, 3), (3, -2), (0, 0))) turtle.speed(0) turtle.title("Poor robbie is lost")UPDATE_EVERY = 0 DRAW_EVERY = 2class Maze(object):def __init__(self, maze):self.maze = mazeself.width = len(maze[0])self.height = len(maze)turtle.setworldcoordinates(0, 0, self.width, self.height)self.blocks = []self.update_cnt = 0self.one_px = float(turtle.window_width()) / float(self.width) / 2self.beacons = []for y, line in enumerate(self.maze):for x, block in enumerate(line):if block:nb_y = self.height - y - 1self.blocks.append((x, nb_y))if block == 2:self.beacons.extend(((x, nb_y), (x+1, nb_y), (x, nb_y+1), (x+1, nb_y+1)))def draw(self):for x, y in self.blocks:turtle.up()turtle.setposition(x, y)turtle.down()turtle.setheading(90)turtle.begin_fill()for _ in range(0, 4):turtle.fd(1)turtle.right(90)turtle.end_fill()turtle.up()turtle.color("#00ffff")for x, y in self.beacons:turtle.setposition(x, y)turtle.dot()turtle.update()def weight_to_color(self, weight):return "#%02x00%02x" % (int(weight * 255), int((1 - weight) * 255))def is_in(self, x, y):if x < 0 or y < 0 or x > self.width or y > self.height:return Falsereturn Truedef is_free(self, x, y):if not self.is_in(x, y):return Falseyy = self.height - int(y) - 1xx = int(x)return self.maze[yy][xx] == 0def show_mean(self, x, y, confident=False):if confident:turtle.color("#00AA00")else:turtle.color("#cccccc")turtle.setposition(x, y)turtle.shape("circle")turtle.stamp()def show_particles(self, particles):self.update_cnt += 1if UPDATE_EVERY > 0 and self.update_cnt % UPDATE_EVERY != 1:returnturtle.clearstamps()turtle.shape('tri')draw_cnt = 0px = {}for p in particles:draw_cnt += 1if DRAW_EVERY == 0 or draw_cnt % DRAW_EVERY == 1:# Keep track of which positions already have something# drawn to speed up display renderingscaled_x = int(p.x * self.one_px)scaled_y = int(p.y * self.one_px)scaled_xy = scaled_x * 10000 + scaled_yif not scaled_xy in px:px[scaled_xy] = 1turtle.setposition(*p.xy)turtle.setheading(90 - p.h)turtle.color(self.weight_to_color(p.w))turtle.stamp()def show_robot(self, robot):turtle.color("green")turtle.shape('turtle')turtle.setposition(*robot.xy)turtle.setheading(90 - robot.h)turtle.stamp()turtle.update()def random_place(self):x = random.uniform(0, self.width)y = random.uniform(0, self.height)return x, ydef random_free_place(self):while True:x, y = self.random_place()if self.is_free(x, y):return x, ydef distance(self, x1, y1, x2, y2):return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)def distance_to_nearest_beacon(self, x, y):d = 99999for c_x, c_y in self.beacons:distance = self.distance(c_x, c_y, x, y)if distance < d:d = distanced_x, d_y = c_x, c_yreturn d2.https://blog.csdn.net/weixin_34279579/article/details/94562301
1.?https://www.jb51.net/article/201041.htm
二、安裝scipy
如果代碼運行過程報錯,顯示?
import scipy.stats 有錯誤,但是又安裝了scipy , 則可能是scipy的版本太低,換成1.6版本。(低版本的文件大小明顯小的很多,才十幾M,而1.6版本則32M多,所以可能是很多子文件未包括在內)安裝方法同下。
參考:https://blog.csdn.net/gaoyu18/article/details/105086277
?
三、第三方庫安裝? statsmodels
第一種方式:電腦中dos命令行模式下? 直接 pip install statsmodels即可成功完成, 如果安裝一半報錯結束,則進入第二種方法。
?
第二種方式:
pypi官網是很多第三方庫文件的匯總網站,很多庫可以在上面找到。 注意自己的Python版本,電腦操作系統版本等再下載。
網址:https://pypi.org/project/statsmodels/#files
?
?
下載完文件,放入自己電腦的Python安裝目錄下的?目錄中E:\Python\Python38\Lib\site-packages 參考自己電腦的安裝目錄。
?
cmd進入dos界面,進入到E:\Python\Python38\Lib\site-packages目錄執行命令pip install statsmodels-0.12.2-cp38-cp38-none-win_amd64.whl安裝成功后退出
?
總結
以上是生活随笔為你收集整理的Python--粒子滤波定位案例程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python-OpenCV-- 台式机外
- 下一篇: 机器人学习--路径规划--A*算法实现