-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhelper_utils.py
137 lines (116 loc) · 3.34 KB
/
helper_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'''
Created on Mar 16, 2013
@author: Doug Szumski
Helper utilities for Kalman filter examples
'''
from collections import deque
import matplotlib.pyplot as plt
from pylab import rcParams
class MovingAverage:
"""
Calculates a moving average
"""
def __init__(self, size):
"""
Configure the averaging window
Args:
size: window size
"""
self.size = size
self.stack = deque([])
def update(self, value):
"""
Update the moving average
Args:
value: latest reading
"""
if (len(self.stack) < self.size):
self.stack.append(value)
else:
self.stack.append(value)
self.stack.popleft()
def getAvg(self):
"""
Returns the current moving average
"""
self.avg = 0.0
for value in self.stack:
self.avg += value
self.avg /= self.size
return self.avg
class Logger:
"""
Simple logger
"""
def __init__(self):
"""
Create a container for the logs
"""
self.logs = {}
def new_log(self, item):
"""
Add a new log
Args:
item: log name
"""
self.logs[item] = []
def get_log(self, item):
"""
Returns a log
Args:
item: name of log to return
"""
return self.logs[item]
def get_all_logs(self):
"""
Returns all logs
"""
return self.logs
def log(self, item, data):
"""
Log a value to a log
Args:
item: log name
data: value to log
"""
self.logs[item].append(data)
class KalmanPlotter:
"""
Plots logged data from Kalman Filter
"""
def __init__(self):
"""
Configure the plot
"""
# Setup a summary figure
self.fig = plt.figure()
self.ax1 = plt.subplot2grid((2, 1), (0, 0))
self.ax2 = plt.subplot2grid((2, 1), (1, 0))
# Set the legend to auto locate
rcParams['legend.loc'] = 'best'
def plot_kalman_data(self, log):
"""
Plot the system behaviour as a function of time
Args:
log: a dictionary containing the keys plotted below each
associated with a list of data
"""
# Plot the evolution of the system state
self.ax1.set_title("Kalman filter example")
self.ax1.set_xlabel("Time (s)")
self.ax1.set_ylabel("Position (m)")
self.ax1.plot(log.get_log('time'), log.get_log('measurement'),
'o', label='Measured', markersize=5)
self.ax1.plot(log.get_log('time'), log.get_log('estimate'),
'-', label='Estimated', markersize=5)
self.ax1.plot(log.get_log('time'), log.get_log('actual'),
'-', label='Actual', markersize=5)
self.ax1.plot(log.get_log('time'), log.get_log('moving average'),
'-', label='Averaged', markersize=5)
self.ax1.legend(prop={'size': 10})
# Plot the evolution of the state covariance
self.ax2.set_xlabel("Time (s)")
self.ax2.set_ylabel("State covariance")
self.ax2.plot(log.get_log('time'), log.get_log('covariance'),
'-', label='State covariance', markersize=5)
plt.show()