-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
93 lines (72 loc) · 2.36 KB
/
run.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
from robotcar import robotcar
from kalman_filter import kalman_filter
from random import seed, uniform
import numpy as np
import matplotlib.pyplot as plt
# Create Random Physical Landmarks
num_landmarks = 20
landmark_range = 300
landmarksx = np.empty(num_landmarks)
landmarksy = np.empty(num_landmarks)
seed()
for i in range(num_landmarks):
landmarksx[i] = uniform(-landmark_range,landmark_range)
landmarksy[i] = uniform(-landmark_range,landmark_range)
# Initialize Car and Kalman Filter
robot = robotcar(2, 0.5, num_landmarks, ts=0.1)
kf = kalman_filter(robot)
# Time Period
time = 120 # sec
n = int(time/robot.ts)
# Initialize empty arrays for plotting
t = np.empty(n)
e = np.empty(n)
x = np.empty(n)
y = np.empty(n)
x_pred = np.empty(n)
y_pred = np.empty(n)
x_update = np.empty(n)
y_update = np.empty(n)
print "START \n\n\n"
for i in range(n):
# Create random movement of wheels
l_wheel = uniform(5, 15)
r_wheel = uniform(5, 15)
robot.move_wheels(l_wheel, r_wheel)
# Prediction Step
kf.predict()
# Add data to plot array
x_pred[i] = robot.position[0,0]
y_pred[i] = robot.position[1,0]
# Update Steps - Perform an update to the current prediction for all landmarks
for j in range(num_landmarks):
landmarkx = landmarksx[j]
landmarky = landmarksy[j]
kf.update(landmarkx, landmarky, j)
# Uncomment to print all position data
# print "Run " + str(i) + ", updated: \n" + str(robot.position[0:3, 0])
# print "Run " + str(i) + ", actual: \n" + str(robot.positionVector)
# Add data to plot arrays
x_update[i] = robot.position[0,0]
y_update[i] = robot.position[1,0]
x[i] = robot.positionVector[0]
y[i] = robot.positionVector[1]
t[i] = robot.time
# Plot map and error
e = np.sqrt(np.square(x_update - x) + np.square(y_update-y))
print "Average RMS Error of Position: " + str(np.mean(e))
plt.figure(1)
actual, = plt.plot(x, y)
predicted, = plt.plot(x_pred, y_pred)
updated, = plt.plot(x_update, y_update)
lms, = plt.plot(landmarksx, landmarksy, 'o', label="Landmarks")
plt.figlegend( (actual, predicted, updated, lms), ('Actual Position', 'Predicted Position','Updated Position', 'Landmarks'), 'lower right')
plt.title('Map of Actual, Predicted and Updated Position')
plt.xlabel('X Position')
plt.ylabel('Y Position')
plt.figure(2)
plt.plot(t,e)
plt.title('Root Mean Square Error Between Actual and Updated Position')
plt.xlabel('Time (Sec)')
plt.ylabel('Error')
plt.show()