-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive_elektra.py
149 lines (95 loc) · 4.14 KB
/
drive_elektra.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
#can be later merged with drive.py(only diff is of drive_config file)
import traceback
import sys
sys.path.append('drive_interfaces')
sys.path.append('drive_interfaces/elektra_interface')
sys.path.append('drive_interfaces/carla_interface/carla_client')
sys.path.append('drive_interfaces/carla_interface/carla_client/protoc')
sys.path.append('test_interfaces')
sys.path.append('utils')
sys.path.append('dataset_manipulation')
sys.path.append('configuration')
sys.path.append('structures')
sys.path.append('evaluation')
import math
import argparse
from noiser_elektra import Noiser
import datetime
from screen_manager import ScreenManager
import numpy as np
import os
import time
import pygame
#from config import *
#from eConfig import *
from drawing_tools import *
from extra import *
pygame.init()
clock = pygame.time.Clock()
def frame2numpy(frame, frameSize):
return np.resize(np.fromstring(frame, dtype='uint8'), (frameSize[1], frameSize[0], 3))
# TODO: TURN this into A FACTORY CLASS
def get_instance(drive_config,experiment_name,drivers_name,memory_use,input_method):
from elektra_recorder import Recorder
if drive_config.type_of_driver == "Human":
from elektra_human import ElektraHuman
driver = ElektraHuman(drive_config,input_method)
else:
from elektra_machine import ElektraMachine
driver = ElektraMachine("0",experiment_name,drive_config,input_method,memory_use)
if drivers_name is not None:
folder_name = str(datetime.datetime.today().day) + '_Elektra_' + drive_config.type_of_driver + '_' + experiment_name
folder_name += '_' + str(get_latest_file_number(drive_config.path,folder_name))
recorder = Recorder(drive_config.path + folder_name +'/',88,200,image_cut= drive_config.image_cut)
else:
recorder = Recorder(drive_config.path,88,200,image_cut= drive_config.image_cut)
return driver,recorder
def drive_elektra(experiment_name,drive_config,input_method,name = None,memory_use=1.0, ):
driver,recorder = get_instance(drive_config,experiment_name,name,memory_use,input_method)
noiser = Noiser(drive_config.noise)
#print 'before starting'
driver.start()
first_time = True
if drive_config.show_screen:
screen_manager = ScreenManager()
screen_manager.start_screen(drive_config.resolution,drive_config.number_screens,drive_config.scale_factor)
driver.use_planner =False
old_speed = 0 #the speed we start the car with
direction = 2
iteration = 0
try:
while direction != -1:
capture_time = time.time()
images = driver.get_sensor_data()
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
recording = driver.get_recording() #just booleans, received from joystick
driver.get_reset() #just booleans, received from joystick
action,new_speed,human_intervention = driver.compute_action(images,old_speed) #rewards.speed
#action_noisy,drifting_time,will_drift = noiser.compute_noise(action[drive_config.middle_camera])
action_noisy,drifting_time,will_drift = noiser.compute_noise(action)
if recording:
print "RECORDING"
recorder.record(images,action.speed,action.steer,action_noisy.steer,human_intervention)
'''if drive_config.show_screen:
if drive_config.interface == "Carla":
for i in range(drive_config.number_screens-1):
screen_manager.plot_driving_interface( capture_time,np.copy(images[i]),\
action[i],action_noisy,driver.compute_direction((rewards.player_x,rewards.player_y,22),(rewards.ori_x,rewards.ori_y,rewards.ori_z)),recording and (drifting_time == 0.0 or will_drift),\
drifting_time,will_drift,rewards.speed,0,0,i) #
else:
print "Not supported interface"
pass'''
'''if drive_config.type_of_driver == "Machine" and drive_config.show_screen and drive_config.plot_vbp:
image_vbp =driver.compute_perception_activations(images[drive_config.middle_camera],rewards.speed)
screen_manager.plot_image(image_vbp,1)'''
iteration +=1
old_speed = new_speed
driver.act(action_noisy)
except:
traceback.print_exc()
finally:
driver.end()
#driver.write_performance_file(path,folder_name,iteration)
pygame.quit()