-
Notifications
You must be signed in to change notification settings - Fork 13
/
drive.py
186 lines (141 loc) · 4.87 KB
/
drive.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""Test model."""
import argparse
import base64
from datetime import datetime
import os
import shutil
import numpy as np
import socketio
import eventlet
import eventlet.wsgi
from PIL import Image
from flask import Flask
from io import BytesIO
import torch
from torch.autograd import Variable
import torchvision.transforms as transforms
from model import *
sio = socketio.Server()
app = Flask(__name__)
model = None
prev_image_array = None
transformations = transforms.Compose(
[transforms.Lambda(lambda x: (x / 127.5) - 1.0)])
class SimplePIController:
def __init__(self, Kp, Ki):
self.Kp = Kp
self.Ki = Ki
self.set_point = 0.
self.error = 0.
self.integral = 0.
def set_desired(self, desired):
self.set_point = desired
def update(self, measurement):
# proportional error
self.error = self.set_point - measurement
# integral error
self.integral += self.error
return self.Kp * self.error + self.Ki * self.integral
controller = SimplePIController(0.1, 0.002)
set_speed = 10
controller.set_desired(set_speed)
# MAX_SPEED = 15
# MIN_SPEED = 10
# speed_limit = MAX_SPEED
@sio.on('telemetry')
def telemetry(sid, data):
if data:
# The current steering angle of the car
steering_angle = float(data["steering_angle"])
# The current throttle of the car
throttle = float(data["throttle"])
# The current speed of the car
speed = float(data["speed"])
image = Image.open(BytesIO(base64.b64decode(data["image"])))
image_array = np.array(image.copy())
image_array = image_array[65:-25, :, :]
# transform RGB to BGR for cv2
image_array = image_array[:, :, ::-1]
image_array = transformations(image_array)
image_tensor = torch.Tensor(image_array)
image_tensor = image_tensor.view(1, 3, 70, 320)
image_tensor = Variable(image_tensor)
steering_angle = model(image_tensor).view(-1).data.numpy()[0]
# throttle = controller.update(float(speed))
# ----------------------- Improved by Siraj ----------------------- #
# global speed_limit
# if speed > speed_limit:
# speed_limit = MIN_SPEED
# else:
# speed_limit = MAX_SPEED
throttle = 1.2 - steering_angle ** 2 - (speed / set_speed) ** 2
# ----------------------- Improved by Siraj ----------------------- #
send_control(steering_angle, throttle)
print("Steering angle: {} | Throttle: {}".format(
steering_angle, throttle))
# save frame
if args.image_folder != '':
timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
image_filename = os.path.join(args.image_folder, timestamp)
image.save('{}.jpg'.format(image_filename))
else:
# NOTE: DON'T EDIT THIS.
sio.emit('manual', data={}, skip_sid=True)
@sio.on('connect')
def connect(sid, environ):
print("connect ", sid)
send_control(0, 0)
def send_control(steering_angle, throttle):
sio.emit(
"steer",
data={
'steering_angle': steering_angle.__str__(),
'throttle': throttle.__str__()
},
skip_sid=True)
if __name__ == '__main__':
"""Testing phase."""
parser = argparse.ArgumentParser(description='Remote Driving')
parser.add_argument(
'model',
type=str,
help='Path to model h5 file. Model should be on the same path.'
)
parser.add_argument(
'image_folder',
type=str,
nargs='?',
default='',
help='Path to image folder. This is where the images from the run will be saved.'
)
args = parser.parse_args()
# define model
# model = LeNet()
model = NetworkNvidia()
# check that model version is same as local PyTorch version
try:
checkpoint = torch.load(
args.model, map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint['state_dict'])
except KeyError:
checkpoint = torch.load(
args.model, map_location=lambda storage, loc: storage)
model = checkpoint['model']
except RuntimeError:
print("==> Please check using the same model as the checkpoint")
import sys
sys.exit()
if args.image_folder != '':
print("Creating image folder at {}".format(args.image_folder))
if not os.path.exists(args.image_folder):
os.makedirs(args.image_folder)
else:
shutil.rmtree(args.image_folder)
os.makedirs(args.image_folder)
print("RECORDING THIS RUN ...")
else:
print("NOT RECORDING THIS RUN ...")
# wrap Flask application with engineio's middleware
app = socketio.Middleware(sio, app)
# deploy as an eventlet WSGI server
eventlet.wsgi.server(eventlet.listen(('', 4567)), app)