-
Notifications
You must be signed in to change notification settings - Fork 2
/
partA.py
161 lines (122 loc) · 4.57 KB
/
partA.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
All files written in python 3
This file runs the required components for
part A. It provides the figures for questions
2 and 3, then prints the results to question
6 to the terminal
"""
from motion import mobile_robot
from measure import*
from params import*
from utils import landmark_data
import matplotlib.pyplot as plt
import numpy as np
def question_two(noise):
# control inputs
u = np.array([[0.5, 0.0, 0.5, 0.0, 0.5],
[0.0, -1/(2*np.pi), 0.0, 1/(2*np.pi), 0.0]])
# each command applied for 1 second
dt = 1
# initialize pose
pose = np.array([0, 0, 0])
# store trajectory
# columns are x, y, theta
traj = np.array([pose])
for cmd in range(u.shape[1]):
# update pose
pose = mobile_robot(u[:,cmd], pose, dt, noise)
traj = np.append(traj, [pose], axis=0)
#print(traj[:,0])
#print(traj[:,1])
plt.figure(dpi=110, facecolor='w')
plt.plot(traj[:,0], traj[:,1], 'red', linewidth=2)
plt.xlabel("Global X Positon")
plt.ylabel("Global Y Position")
plt.title("Motion of Robot Given Control Sequence")
#plt.legend("Motion Model")
plt.show()
def question_three(odom_file, ground_file, noise):
# controls
v = [] # velocity
w = [] # angular velocity
t = [] # time stamps
# ground truth position of robot
x_true = [] # global x position
y_true = [] # global y position
theta_true = [] # orientation
# open odometry file and store controls
file_odom = open(odom_file, "r")
for line in file_odom:
if not line.startswith("#"):
values = line.split()
v.append(float(values[1]))
w.append(float(values[2]))
t.append(float(values[0]))
file_odom.close()
# open ground truth file
file_ground = open(ground_file, "r")
for line in file_ground:
if not line.startswith("#"):
values = line.split()
x_true.append(float(values[1]))
y_true.append(float(values[2]))
theta_true.append(float(values[3]))
file_odom.close()
# initialize pose with ground truth
pose = [x_true[0], y_true[0], theta_true[0]]
# init time
curr_time = 0
# store trajectory
# lists are x, y, theta
traj = [[pose[0]], [pose[1]], [pose[2]]]
# write odometry data to file
#file = open(motion_model_odom, "w")
#file.write(str(pose[0]) + " " + str(pose[1]) + " " + str(pose[2]) + "\n")
# apply odometry controls to motion model
for cmd in range(len(v)):
# delta t
dt = t[cmd] - curr_time
curr_time = t[cmd]
# update pose
pose = mobile_robot([v[cmd], w[cmd]], pose, dt, noise)
traj[0].append(pose[0])
traj[1].append(pose[1])
traj[2].append(pose[2])
# write to file
#file.write(str(pose[0]) + " " + str(pose[1]) + " " + str(pose[2]) + "\n")
#file.close()
plt.figure(dpi=110, facecolor='w')
#plt.plot(traj_ground[0][0:5000], traj_ground[1][0:5000], 'red', linewidth=2)
#plt.plot(traj[0][0:5000], traj[1][0:5000], 'blue', linewidth=2)
plt.plot(x_true, y_true, 'red', linewidth=2)
plt.plot(traj[0], traj[1], 'black', linewidth=2)
plt.xlabel("Global X Positon")
plt.ylabel("Global Y Position")
plt.title("Odometry and Ground Truth Motion Model")
plt.legend(("Ground Truth","Dead Reckoning"))
plt.show()
def question_six(noise):
lm_dict = {}
landmark_data(lm_dict)
#print(lm_dict)
# landmark subjects
lm = [6, 13, 17]
# robot position assume theta = 0 rad
pose_rob = [[2, 3, 0], [0, 3, 0], [1, -2, 0]]
#pose_rob = [[1, 1, -3.14], [0, 3, 0], [1, -2, 0]]
# global position of landmark
meas_lm = []
print("-----------------------------")
print("Landmark Position Estimation")
print("-----------------------------")
for i in range(len(lm)):
# range and bearing measurement
meas_rb = sensor(lm_dict[lm[i]], pose_rob[i], noise)
# global position of landmark
meas_xy = landmark_position(meas_rb, pose_rob[i])
meas_lm.append(meas_xy)
print("The range and bearing of landmark ", lm[i], ": ", "range: ", meas_rb[0], "(m)", " bearing: ", meas_rb[1], " (rad)")
print("The global position of landmark ", lm[i], ": ", "x: ", meas_xy[0], " (m)", " y: ", meas_xy[1], " (m)")
print("Error in x: ", lm_dict[lm[i]][0] - meas_xy[0], " (m)", " Error in y: ", lm_dict[lm[i]][1] - meas_xy[1], " (m)")
print("----------------------------------------------------------------------------------------------")
#print(lm_dict[lm[i]][0])