CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sat May 24 19:14:06 2014
4
5
@author: rlabbe
6
"""
7
from __future__ import division, print_function
8
from KalmanFilter import KalmanFilter
9
import numpy as np
10
import matplotlib.pyplot as plt
11
import numpy.random as random
12
import math
13
14
15
class DMESensor(object):
16
def __init__(self, pos_a, pos_b, noise_factor=1.0):
17
self.A = pos_a
18
self.B = pos_b
19
self.noise_factor = noise_factor
20
21
def range_of (self, pos):
22
""" returns tuple containing noisy range data to A and B
23
given a position 'pos'
24
"""
25
26
ra = math.sqrt((self.A[0] - pos[0])**2 + (self.A[1] - pos[1])**2)
27
rb = math.sqrt((self.B[0] - pos[0])**2 + (self.B[1] - pos[1])**2)
28
29
return (ra + random.randn()*self.noise_factor,
30
rb + random.randn()*self.noise_factor)
31
32
33
def dist(a,b):
34
return math.sqrt ((a[0]-b[0])**2 + (a[1]-b[1])**2)
35
36
def H_of (pos, pos_A, pos_B):
37
from math import sin, cos, atan2
38
39
theta_a = atan2(pos_a[1]-pos[1], pos_a[0] - pos[0])
40
theta_b = atan2(pos_b[1]-pos[1], pos_b[0] - pos[0])
41
42
return np.mat([[-cos(theta_a), 0, -sin(theta_a), 0],
43
[-cos(theta_b), 0, -sin(theta_b), 0]])
44
45
# equivalently we can do this...
46
#dist_a = dist(pos, pos_A)
47
#dist_b = dist(pos, pos_B)
48
49
#return np.mat([[(pos[0]-pos_A[0])/dist_a, 0, (pos[1]-pos_A[1])/dist_a,0],
50
# [(pos[0]-pos_B[0])/dist_b, 0, (pos[1]-pos_B[1])/dist_b,0]])
51
52
53
54
55
pos_a = (100,-20)
56
pos_b = (-100, -20)
57
58
f1 = KalmanFilter(dim=4)
59
dt = 1.0 # time step
60
'''
61
f1.F = np.mat ([[1, dt, 0, 0],
62
[0, 1, 0, 0],
63
[0, 0, 1, dt],
64
[0, 0, 0, 1]])
65
66
'''
67
f1.F = np.mat ([[0, 1, 0, 0],
68
[0, 0, 0, 0],
69
[0, 0, 0, 1],
70
[0, 0, 0, 0]])
71
72
f1.B = 0.
73
74
f1.R = np.eye(2) * 1.
75
f1.Q = np.eye(4) * .1
76
77
f1.x = np.mat([1,0,1,0]).T
78
f1.P = np.eye(4) * 5.
79
80
# initialize storage and other variables for the run
81
count = 30
82
xs, ys = [],[]
83
pxs, pys = [],[]
84
85
d = DMESensor (pos_a, pos_b, noise_factor=1.)
86
87
pos = [0,0]
88
for i in range(count):
89
pos = (i,i)
90
ra,rb = d.range_of(pos)
91
rx,ry = d.range_of((i+f1.x[0,0], i+f1.x[2,0]))
92
93
print ('range =', ra,rb)
94
95
z = np.mat([[ra-rx],[rb-ry]])
96
print('z =', z)
97
98
f1.H = H_of (pos, pos_a, pos_b)
99
print('H =', f1.H)
100
101
##f1.update (z)
102
103
print (f1.x)
104
xs.append (f1.x[0,0]+i)
105
ys.append (f1.x[2,0]+i)
106
pxs.append (pos[0])
107
pys.append(pos[1])
108
f1.predict ()
109
print (f1.H * f1.x)
110
print (z)
111
print (f1.x)
112
f1.update(z)
113
print(f1.x)
114
115
p1, = plt.plot (xs, ys, 'r--')
116
p2, = plt.plot (pxs, pys)
117
plt.legend([p1,p2], ['filter', 'ideal'], 2)
118
plt.show()
119
120