Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872# -*- coding: utf-8 -*-1"""2Created on Sat May 24 19:14:06 201434@author: rlabbe5"""6from __future__ import division, print_function7from KalmanFilter import KalmanFilter8import numpy as np9import matplotlib.pyplot as plt10import numpy.random as random11import math121314class DMESensor(object):15def __init__(self, pos_a, pos_b, noise_factor=1.0):16self.A = pos_a17self.B = pos_b18self.noise_factor = noise_factor1920def range_of (self, pos):21""" returns tuple containing noisy range data to A and B22given a position 'pos'23"""2425ra = math.sqrt((self.A[0] - pos[0])**2 + (self.A[1] - pos[1])**2)26rb = math.sqrt((self.B[0] - pos[0])**2 + (self.B[1] - pos[1])**2)2728return (ra + random.randn()*self.noise_factor,29rb + random.randn()*self.noise_factor)303132def dist(a,b):33return math.sqrt ((a[0]-b[0])**2 + (a[1]-b[1])**2)3435def H_of (pos, pos_A, pos_B):36from math import sin, cos, atan23738theta_a = atan2(pos_a[1]-pos[1], pos_a[0] - pos[0])39theta_b = atan2(pos_b[1]-pos[1], pos_b[0] - pos[0])4041return np.mat([[-cos(theta_a), 0, -sin(theta_a), 0],42[-cos(theta_b), 0, -sin(theta_b), 0]])4344# equivalently we can do this...45#dist_a = dist(pos, pos_A)46#dist_b = dist(pos, pos_B)4748#return np.mat([[(pos[0]-pos_A[0])/dist_a, 0, (pos[1]-pos_A[1])/dist_a,0],49# [(pos[0]-pos_B[0])/dist_b, 0, (pos[1]-pos_B[1])/dist_b,0]])5051525354pos_a = (100,-20)55pos_b = (-100, -20)5657f1 = KalmanFilter(dim=4)58dt = 1.0 # time step59'''60f1.F = np.mat ([[1, dt, 0, 0],61[0, 1, 0, 0],62[0, 0, 1, dt],63[0, 0, 0, 1]])6465'''66f1.F = np.mat ([[0, 1, 0, 0],67[0, 0, 0, 0],68[0, 0, 0, 1],69[0, 0, 0, 0]])7071f1.B = 0.7273f1.R = np.eye(2) * 1.74f1.Q = np.eye(4) * .17576f1.x = np.mat([1,0,1,0]).T77f1.P = np.eye(4) * 5.7879# initialize storage and other variables for the run80count = 3081xs, ys = [],[]82pxs, pys = [],[]8384d = DMESensor (pos_a, pos_b, noise_factor=1.)8586pos = [0,0]87for i in range(count):88pos = (i,i)89ra,rb = d.range_of(pos)90rx,ry = d.range_of((i+f1.x[0,0], i+f1.x[2,0]))9192print ('range =', ra,rb)9394z = np.mat([[ra-rx],[rb-ry]])95print('z =', z)9697f1.H = H_of (pos, pos_a, pos_b)98print('H =', f1.H)99100##f1.update (z)101102print (f1.x)103xs.append (f1.x[0,0]+i)104ys.append (f1.x[2,0]+i)105pxs.append (pos[0])106pys.append(pos[1])107f1.predict ()108print (f1.H * f1.x)109print (z)110print (f1.x)111f1.update(z)112print(f1.x)113114p1, = plt.plot (xs, ys, 'r--')115p2, = plt.plot (pxs, pys)116plt.legend([p1,p2], ['filter', 'ideal'], 2)117plt.show()118119120