forked from MankaranSingh/comma-scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
150 lines (115 loc) · 4.28 KB
/
run.py
File metadata and controls
150 lines (115 loc) · 4.28 KB
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
import numpy as np
import pandas as pd
import cv2
import os
import matplotlib.pyplot as plt
from visual_odometry import PinholeCamera, VisualOdometry
from bicycle_model import Bicycle
from kalman_filter import KalmanFilter
import math
import cubic_spline_planner
def latlon_to_xy_grid(lat1, lon1, lat2, lon2):
dx = (lon1-lon2)*40075*math.cos((lat1+lat2)*math.pi/360)/360
dy = (lat1-lat2)*40075/360
return dx, dy
def rotate(x, y, rad):
x_rot = math.cos(rad)*x - math.sin(rad)*y
y_rot = math.sin(rad)*x + math.cos(rad)*y
return x_rot, y_rot
meta_data_path = './interpolated.csv'
base_path = "C:/Users/asus/Desktop/Self Driving/Comma Scratch/data"
skip_factor = 1
start = 8800
df = pd.read_csv(meta_data_path)
df = df[df['frame_id'] == 'center_camera'][::skip_factor][start:]
df['timestamp'] = df['timestamp']/1000000000
ins = pd.read_csv('imu.csv')
ins['timestamp'] = ins['timestamp']/1000000000
cam = PinholeCamera(640, 480, 2152.445406, 2166.161453, 268.010970, 302.594211)
vo = VisualOdometry(cam, meta_data_path)
model = Bicycle()
traj = np.zeros((600,600,3), dtype=np.uint8)
#rel_x, rel_y = df[['lat', 'long']].values[0]
prev_lat, prev_lon = df[['lat', 'long']].values[0]
xg, yg = 0, 0
R_y = 0.4
Q_y = 0.025
INIT_y = 0
INIT_VAR_y = 0.5**2
kalman_y = KalmanFilter(INIT_y, INIT_VAR_y, R_y**2, Q_y**2)
R_x = 0.6
Q_x = 0.02
INIT_x = 0
INIT_VAR_x = 0
kalman_x = KalmanFilter(INIT_x, INIT_VAR_x, R_x**2, Q_x**2)
bs = []
gs = []
N_STEPS = 300
counter = 0
trajectory = []
for img_id in range(len(df['filename'].values)):
img_path = os.path.join(base_path, df['filename'].values[img_id])
img = cv2.imread(img_path, 0)
v, delta, current_time = df[['speed', 'angle', 'timestamp']].values[img_id]
#acc_idx = abs(ins['timestamp'].values - current_time).argmin()
#acc_x = ins['ax'].values[acc_idx]
#acc_y = -ins['ay'].values[acc_idx]
#print(v, delta)
dy_b, dx_b = model.step(v, delta, current_time)
kalman_y.predict(dy_b)
kalman_x.predict(-dx_b)
# Visual Odometery update
vo.update(img, img_id)
cur_t = vo.cur_t
if(img_id > 2):
x, y, z = cur_t[0], cur_t[1], cur_t[2]
else:
x, y, z = 0., 0., 0.
vo.x, vo.y, vo.z = x, y, z
lat, lon = df[['lat', 'long']].values[img_id]
d_xg, d_yg = latlon_to_xy_grid(lat, lon, prev_lat, prev_lon)
xg += d_xg * 1000
yg += d_yg * 1000
if img_id < 2:
angle = np.arctan2(xg, yg)
if np.rad2deg(angle) < 0:
sign = -1
else:
sign = 1
continue
prev_lat = lat
prev_lon = lon
xg_rot, yg_rot = rotate(xg, yg, -(np.pi/2 - angle))
kalman_y.update(xg_rot)
kalman_x.update(yg_rot*sign)
trajectory.append([kalman_x.x, kalman_y.x, -model.yc, model.xc, yg_rot*sign, xg_rot, x, z, xg, yg])
draw_xg, draw_yg = int(xg_rot + 90), int(yg_rot*sign + 290)
draw_x, draw_y = int(x) + 290, int(z) + 90
draw_xb, draw_yb = -int(model.yc) + 290, int(model.xc) + 90
draw_kalman_x, draw_kalman_y = int(kalman_x.x) + 290, int(kalman_y.x) + 90
#print(kalman_y.P, kalman_x.P)
print(sign)
#cv2.circle(traj, (draw_x, draw_y), 1, (255, 0, 0), 1)
cv2.circle(traj, (draw_xb, draw_yb), 1, (0,0,255), 1)
cv2.circle(traj, (draw_yg, draw_xg), 1, (0,255, 0), 1)
cv2.circle(traj, (draw_kalman_x, draw_kalman_y), 1, (255, 255, 255), 1)
cv2.rectangle(traj, (10, 20), (600, 60), (0,0,0), -1)
text = "Coordinates: x=%2fm y=%2fm z=%2fm"%(x,y,z)
cv2.putText(traj, text, (20,40), cv2.FONT_HERSHEY_PLAIN, 1, (255,255,255), 1, 8)
cv2.imshow('Road facing camera', img)
cv2.imshow('Trajectory', traj)
cv2.waitKey(1)
print(kalman_y.x)
if counter > N_STEPS:
break
counter += 1
trajectory = np.array(trajectory)
cx, cy, cyaw, ck, s = cubic_spline_planner.calc_spline_course(list(trajectory[:, 0][::2]), list(trajectory[:, 1][::2]), ds=0.1)
plt.scatter(trajectory[:, 0], trajectory[:, 1], label='Filtered', s=0.5)
plt.scatter(trajectory[:, 2], trajectory[:, 3], label='Bicycle Model', s=0.5)
plt.scatter(trajectory[:, 4], trajectory[:, 5], label='GNSS', s=0.5)
plt.scatter(trajectory[:, 6], trajectory[:, 7], label='Visual Odometery', s=0.5)
plt.scatter(cx, cy, label='Spline', s=0.5)
plt.xticks([-3, -2, -1, 0, 1, 2, 3])
plt.legend(loc='best')
plt.show()